2006/unicorn/src/org/w3c/unicorn/util ListFiles.java,NONE,1.1.2.1 ClientHttpRequest.java,NONE,1.1.2.1 LocaleFactory.java,NONE,1.1.2.1 LocalizedString.java,NONE,1.1.2.1 TemplateHelper.java,NONE,1.1.2.1 Unmarshaller.java,NONE,1.1.2.1 Property.java,1.1.2.3,1.1.2.4

Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/util
In directory hutz:/tmp/cvs-serv2609/src/org/w3c/unicorn/util

Modified Files:
      Tag: dev2
	Property.java 
Added Files:
      Tag: dev2
	ListFiles.java ClientHttpRequest.java LocaleFactory.java 
	LocalizedString.java TemplateHelper.java Unmarshaller.java 
Log Message:
all initialization actions in Init.java
+ compatibility windows/linux

--- NEW FILE: LocalizedString.java ---
// $Id: LocalizedString.java,v 1.1.2.1 2009/08/11 16:05:35 tgambet Exp $
// Author: Jean-Guilhem Rouel
// (c) COPYRIGHT MIT, ERCIM and Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.util;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * LocalizedString<br />
 * Created: May 30, 2006 11:55:23 AM<br />
 * 
 * @author Jean-Guilhem ROUEL & Damien LEROY
 */
public class LocalizedString {

	private static final Log logger = LogFactory.getLog(LocalizedString.class);

	public static String DEFAULT_LANGUAGE = "en";

	private Map<String, String> mapOfString = null;

	/**
	 * Default constructor for LocalizedString.
	 * 
	 */
	public LocalizedString() {
		LocalizedString.logger.trace("Constructor");
		this.mapOfString = new LinkedHashMap<String, String>();
	}

	/**
	 * Constructs a LocalizedString with an initial string paired with a
	 * language.
	 * 
	 * @param s
	 *            The string to be localized.
	 * @param lang
	 *            The corresponding language.
	 */
	public LocalizedString(String s, String lang) {
		this();
		mapOfString.put(lang, s);
	}

	/**
	 * Looks for the existence of a specified sLocale string in the map.
	 * 
	 * @param sLocale
	 *            The string to look for.
	 * @return True if the sLocale string is in the map, else false.
	 */
	public boolean hasLocale(final String sLocale) {
		return null != this.mapOfString.get(sLocale);
	}

	/**
	 * Adds a message and its corresponding localization to the mapOfString
	 * attribute.
	 * 
	 * @param sLocale
	 *            The localization.
	 * @param sMessage
	 *            The message to be written.
	 * @return The previous value associated with specified key, or null if
	 *         there was no mapping for key.
	 */
	public String addLocalization(final String sLocale, final String sMessage) {
		LocalizedString.logger.trace("addLocalization");
		if (LocalizedString.logger.isDebugEnabled()) {
			LocalizedString.logger.debug("Locale : " + sLocale + ".");
			LocalizedString.logger.debug("Message : " + sMessage + ".");
		}
		return this.mapOfString.put(sLocale, sMessage);
	}

	/**
	 * Finds and returns the message corresponding to the specified localization
	 * in the mapOfString.
	 * 
	 * @param sLocale
	 *            The localization wanted.
	 * @return The message corresponding to the localization or if there's none,
	 *         the one corresponding to the default language.
	 */
	public String getLocalization(final String sLocale) {
		final String sMessage = this.mapOfString.get(sLocale);
		if (null != sMessage) {
			return sMessage;
		}
		return this.mapOfString.get(LocalizedString.DEFAULT_LANGUAGE);
	}

	/**
	 * Returns the keys available in the mapOfString.
	 * 
	 * @return A set with all the keys.
	 */
	public Set<String> getSetOfLocale() {
		return this.mapOfString.keySet();
	}

	/**
	 * Returns the message in in DEFAULT_LANGUAGE (en) or in the first language
	 * in the list.
	 */
	@Override
	public String toString() {
		String res = this.mapOfString.get(LocalizedString.DEFAULT_LANGUAGE);
		if (res == null) {
			for (String s : this.mapOfString.values()) {
				return s;
			}
		}
		if (res == null) {
			return "";
		}
		return res;
	}

}

--- NEW FILE: TemplateHelper.java ---
// $Id: TemplateHelper.java,v 1.1.2.1 2009/08/11 16:05:35 tgambet Exp $
// Author: Thomas GAMBET.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2009.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;

/**
 * TemplateHelper provides functionalities to merge properties object, to load
 * properties objects in a velocity context, to get an internationalized
 * template, and to write internationalized templates to a file.
 * 
 * @author Thomas GAMBET
 */
public class TemplateHelper {
	public static final Log logger = LogFactory.getLog("org.w3c.unicorn.util");

	private static VelocityContext context = new VelocityContext();

	private static VelocityEngine engine = new VelocityEngine();

	public static void init() {
		//Properties aProperties = new Properties();
		
		Properties aProperties = Property.getProps("velocity.properties");
		
		try {
			//aProperties.load(Property.getPropertyFileURL("velocity.properties")
			//		.openStream());

			aProperties.put(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
					Property.get("PATH_TO_TEMPLATES"));

			engine.init(aProperties);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("finally")
	public static Properties getPropsFromFile(File propFile) {
		Properties props = new Properties();
		try {
			props.load(propFile.toURI().toURL().openStream());
		} catch (IOException e) {
			logger.error("Unable to load properties file : " + e.getMessage(),
					e);
			e.printStackTrace();
		} finally {
			return props;
		}
	}

	public static Properties getMergeProps(Properties defaultProps,
			Properties sourceProps) {
		Properties propMerge = new Properties();

		Set<Object> keys = defaultProps.keySet();
		Iterator<Object> itr = keys.iterator();
		String key;

		while (itr.hasNext()) {
			key = itr.next().toString();
			if (sourceProps.containsKey(key)) {
				propMerge.put(key, sourceProps.get(key));
			} else {
				propMerge.put(key, defaultProps.get(key));
			}
		}

		keys = sourceProps.keySet();
		itr = keys.iterator();
		while (itr.hasNext()) {
			key = itr.next().toString();
			if (!defaultProps.containsKey(key)) {
				propMerge.put(key, sourceProps.get(key));
			}
		}

		return propMerge;
	}

	public static Properties getMergePropsFromFiles(File defaultPropFile,
			File sourcePropFile) {
		Properties defaultProps = new Properties();
		Properties sourceProps = new Properties();
		try {
			defaultProps.load(defaultPropFile.toURI().toURL().openStream());
		} catch (IOException e) {
			logger.error("Unable to load default language properties : "
					+ e.getMessage(), e);
			e.printStackTrace();
			return null;
		}

		try {
			sourceProps.load(sourcePropFile.toURI().toURL().openStream());
		} catch (IOException e) {
			logger.error("Unable to find desired language properties : "
					+ e.getMessage(), e);
			e.printStackTrace();
			return defaultProps;
		}

		return getMergeProps(defaultProps, sourceProps);

		/*
		 * Properties propMerge = new Properties();
		 * 
		 * Set<Object> keys = defaultProps.keySet(); Iterator<Object> itr =
		 * keys.iterator(); String key;
		 * 
		 * while (itr.hasNext()) { key = itr.next().toString(); if
		 * (sourceProps.containsKey(key)) propMerge.put(key,
		 * sourceProps.get(key)); else propMerge.put(key,
		 * defaultProps.get(key)); }
		 * 
		 * return propMerge;
		 */
	}

	public static void loadInVelocityContext(Properties props,
			VelocityContext context) {
		Set<Object> keys = props.keySet();
		Iterator<Object> itr = keys.iterator();
		String key;
		while (itr.hasNext()) {
			key = itr.next().toString();
			context.put(key, props.get(key));
		}
	}

	public static Template getInternationalizedTemplate(String templateName,
			String langCode, VelocityContext context) {

		if (langCode != null) {
			context.put("lang", langCode);

			// Error templates have the same language properties file that their
			// coresponding non-error template
			String langFileName = templateName;
			if (templateName.length() > 6
					&& templateName.substring(templateName.length() - 6,
							templateName.length()).equals(".error")) {
				langFileName = templateName.substring(0,
						templateName.length() - 6);
			}

			// Language file for this template
			File langFile = new File(Property.get("PATH_TO_LANGUAGE_FILES")
					+ langFileName + "." + langCode + ".properties");

			// Default language file
			File defaultLangFile = new File(Property
					.get("PATH_TO_LANGUAGE_FILES")
					+ langFileName
					+ "."
					+ Property.get("DEFAULT_LANGUAGE")
					+ ".properties");

			// Merge the properties or use default language
			Properties mergedProps = new Properties();

			if (langFile.exists()) {
				mergedProps = getMergePropsFromFiles(defaultLangFile, langFile);
			} else {
				try {
					mergedProps.load(defaultLangFile.toURI().toURL()
							.openStream());
				} catch (IOException e1) {
					logger.error("IOException : " + e1.getMessage(), e1);
					e1.printStackTrace();
				}
			}

			File generalLangFile = new File(Property
					.get("PATH_TO_LANGUAGE_FILES")
					+ "general." + langCode + ".properties");

			if (!generalLangFile.exists()) {
				generalLangFile = new File(Property
						.get("PATH_TO_LANGUAGE_FILES")
						+ "general."
						+ Property.get("DEFAULT_LANGUAGE")
						+ ".properties");
			}

			mergedProps = getMergeProps(mergedProps,
					getPropsFromFile(generalLangFile));

			// Load in velocity context
			TemplateHelper.loadInVelocityContext(mergedProps, context);
		}

		// Return the template
		try {
			Template resultTemplate = engine.getTemplate(templateName + ".vm",
					"UTF-8");
			return resultTemplate;
		} catch (Exception e) {
			logger.error("Error : " + e.getMessage(), e);
			e.printStackTrace();
			return null;
		}
	}

	public static String generateFileFromTemplate(String templateName,
			String langCode, String destination, String fileExtension,
			VelocityContext context) {

		String destFileName;
		String tempFileName;

		String[] split = templateName.split("/");
		if (split.length > 0) {
			tempFileName = split[split.length - 1];
		} else {
			tempFileName = templateName;
		}

		if (langCode != null) {
			destFileName = tempFileName + "." + langCode + "." + fileExtension;
		} else {
			destFileName = tempFileName + "." + fileExtension;
		}

		if ((new File(destination + destFileName)).exists()) {
			return destination + destFileName;
		}

		if (langCode != null) {
			File langFile = new File(Property.get("PATH_TO_LANGUAGE_FILES")
					+ templateName + "." + langCode + ".properties");

			if (!langFile.exists()
					&& !langCode.equals(Property.get("DEFAULT_LANGUAGE"))) {
				return generateFileFromTemplate(templateName, Property
						.get("DEFAULT_LANGUAGE"), destination, fileExtension,
						context);
			}
		}

		Template template = getInternationalizedTemplate(templateName,
				langCode, context);

		try {
			OutputStreamWriter fileWriter = new OutputStreamWriter(
					new FileOutputStream(destination + destFileName), "UTF-8");
			template.merge(context, fileWriter);
			fileWriter.close();
		} catch (Exception e) {
			logger.error("Error : " + e.getMessage(), e);
			e.printStackTrace();
		}

		return destination + destFileName;
	}

	public static String generateFileFromTemplate(String templateName,
			String langCode, String destination, String fileExtension) {
		return generateFileFromTemplate(templateName, langCode, destination,
				fileExtension, context);
	}
}

--- NEW FILE: Unmarshaller.java ---
// $Id: Unmarshaller.java,v 1.1.2.1 2009/08/11 16:05:35 tgambet Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.util;

import java.io.IOException;
import java.net.URL;

/**
 * Interface for all unmarshaller class in package unicorn.
 * 
 * @author Damien LEROY
 */
public interface Unmarshaller {

	public void addURL(final URL aURL) throws IOException;

	public void unmarshal() throws Exception;

}

--- NEW FILE: LocaleFactory.java ---
// $Id: LocaleFactory.java,v 1.1.2.1 2009/08/11 16:05:35 tgambet Exp $
// Author: Jean-Guilhem Rouel
// (c) COPYRIGHT MIT, ERCIM and Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.util;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Locale;

/**
 * LocaleFactory<br />
 * Created: May 30, 2006 12:08:37 PM<br />
 * 
 * @author Jean-Guilhem ROUEL
 */
public class LocaleFactory {

	private static final LinkedHashMap<String, Locale> mapOfLocale = new LinkedHashMap<String, Locale>();

	/**
	 * Finds a Locale object among the mapOfLocale entries, given its name.
	 * 
	 * @param sLocale
	 *            The name of the Locale.
	 * @return The corresponding Locale object.
	 */
	public static Locale getLocale(final String sLocale) {
		return LocaleFactory.mapOfLocale.get(sLocale);
	}

	/**
	 * Returns the values available in the mapOfLocale.
	 * 
	 * @return The collection of values.
	 */
	public static Collection<Locale> values() {
		return LocaleFactory.mapOfLocale.values();
	}

	static {
		final Locale[] tLocale = Locale.getAvailableLocales();
		for (final Locale aLocale : tLocale) {
			final String sLanguage = aLocale.getLanguage();
			final String sCountry = aLocale.getCountry();
			LocaleFactory.mapOfLocale.put(sLanguage + "-" + sCountry, aLocale);
		}
	}

}

--- NEW FILE: ClientHttpRequest.java ---
package org.w3c.unicorn.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * Title: Client HTTP Request class
 * </p>
 * <p>
 * Description: this class helps to send POST HTTP requests with various form
 * data, including files. Cookies can be added to be included in the request.
 * </p>
 * 
 * @author Vlad Patryshev
 * @version 1.0
 */
public class ClientHttpRequest {

	private static final Log logger = LogFactory.getLog("org.w3c.unicorn.util");

	private static Random aRandom = new Random();

	private HttpURLConnection aURLConnection;

	private OutputStream aOutputStream = null;

	private Map<String, String> mapOfCookie = new HashMap<String, String>();

	private String sBoundary = "---------------------------"
			+ ClientHttpRequest.randomString()
			+ ClientHttpRequest.randomString()
			+ ClientHttpRequest.randomString();

	/**
	 * Connects to the output stream of the URLConnection.
	 * 
	 * @throws IOException
	 */
	private void connect() throws IOException {
		// ClientHttpRequest.logger.trace("connect");
		if (null == this.aOutputStream) {
			this.aOutputStream = this.aURLConnection.getOutputStream();
		}
	}

	/**
	 * Writes a single character on the output stream.
	 * 
	 * @param c
	 *            The character to write.
	 * @throws IOException
	 */
	private void write(final char c) throws IOException {
		this.connect();
		ClientHttpRequest.logger.debug(c);
		this.aOutputStream.write(c);
	}

	/**
	 * Writes a character string on the output stream.
	 * 
	 * @param s
	 *            The string to write.
	 * @throws IOException
	 */
	protected void write(final String s) throws IOException {
		this.connect();
		ClientHttpRequest.logger.debug(s);
		this.aOutputStream.write(s.getBytes());
	}

	/**
	 * Writes a new line on the output stream (carriage return).
	 * 
	 * @throws IOException
	 */
	protected void newline() throws IOException {
		this.connect();
		this.write("\r\n");
	}

	/**
	 * Writes a string and a new line on the output stream.
	 * 
	 * @param s
	 *            The string to write before the new line.
	 * @throws IOException
	 */
	protected void writeln(final String s) throws IOException {
		this.connect();
		this.write(s);
		this.newline();
	}

	/**
	 * Computes a random string.
	 * 
	 * @return A string containing a random long which radix is 36.
	 */
	protected static String randomString() {
		return Long.toString(ClientHttpRequest.aRandom.nextLong(), 36);
	}

	/**
	 * Writes the sBoundary on the output, composed of three random strings.
	 * 
	 * @throws IOException
	 */
	private void boundary() throws IOException {
		this.write("--");
		this.write(this.sBoundary);
	}

	/**
	 * Creates a new multipart POST HTTP request on a freshly opened
	 * URLConnection
	 * 
	 * @param aURLConnection
	 *            an already open URL connection
	 * @throws IOException
	 */
	public ClientHttpRequest(final URLConnection aURLConnection)
			throws IOException {
		ClientHttpRequest.logger.trace("Constructor(URLConnection)");
		this.aURLConnection = (HttpURLConnection) aURLConnection;
		this.aURLConnection.setRequestMethod("POST");
		this.aURLConnection.setDoOutput(true);
		this.aURLConnection.setRequestProperty("Content-Type",
				"multipart/form-data; boundary=" + this.sBoundary);
	}

	/**
	 * Creates a new multipart POST HTTP request for a specified URL
	 * 
	 * @param aURL
	 *            the URL to send request to
	 * @throws IOException
	 */
	public ClientHttpRequest(final URL aURL) throws IOException {
		this(aURL.openConnection());
		ClientHttpRequest.logger.trace("Constructor(URL)");
		if (ClientHttpRequest.logger.isDebugEnabled()) {
			ClientHttpRequest.logger.debug("URL : " + aURL + ".");
		}
	}

	/**
	 * Creates a new multipart POST HTTP request for a specified URL string
	 * 
	 * @param sURL
	 *            the string representation of the URL to send request to
	 * @throws IOException
	 */
	public ClientHttpRequest(final String sURL) throws IOException {
		this(new URL(sURL));
		ClientHttpRequest.logger.trace("Constructor(String)");
		if (ClientHttpRequest.logger.isDebugEnabled()) {
			ClientHttpRequest.logger.debug("String URL : " + sURL + ".");
		}
	}

	/**
	 * adds a cookie to the request
	 * 
	 * @param sName
	 *            cookie name
	 * @param sValue
	 *            cookie value
	 * @throws IOException
	 */
	public void setCookie(final String sName, final String sValue)
			throws IOException {
		this.mapOfCookie.put(sName, sValue);
	}

	/**
	 * Adds cookies to the request.
	 * 
	 * @param cookies
	 *            the cookie "name-to-value" map
	 * @throws IOException
	 */
	public void setCookies(final Map<String, String> mapOfCookie)
			throws IOException {
		if (null == mapOfCookie) {
			return;
		}
		this.mapOfCookie.putAll(mapOfCookie);
	}

	/**
	 * Adds cookies to the request.
	 * 
	 * @param tStringCookie
	 *            Array of cookie names and values (cookies[2*i] is a name,
	 *            cookies[2*i + 1] is a value).
	 * @throws IOException
	 */
	public void setCookies(final String[] tStringCookie) throws IOException {
		if (tStringCookie == null) {
			return;
		}
		for (int i = 0; i < tStringCookie.length - 1; i += 2) {
			this.setCookie(tStringCookie[i], tStringCookie[i + 1]);
		}
	}

	/**
	 * Sets a new language.
	 * 
	 * @param sLang
	 *            The new language chosen.
	 */
	public void setLang(final String sLang) {
		ClientHttpRequest.logger.debug("setLang(" + sLang + ")");
		this.aURLConnection.setRequestProperty("Accept-Language", sLang);
	}

	/**
	 * Writes a name in the appropriate format on the output.
	 * 
	 * @param sName
	 *            The name to write.
	 * @throws IOException
	 */
	private void writeName(final String sName) throws IOException {
		this.newline();
		this.write("Content-Disposition: form-data; name=\"");
		this.write(sName);
		this.write('"');
	}

	/**
	 * Adds a string parameter to the request.
	 * 
	 * @param sName
	 *            Parameter name.
	 * @param sValue
	 *            Parameter value.
	 * @throws IOException
	 */
	public void setParameter(final String sName, final String sValue)
			throws IOException {
		ClientHttpRequest.logger.trace("setParameter(String, String)");
		if (ClientHttpRequest.logger.isDebugEnabled()) {
			ClientHttpRequest.logger.debug("Name : " + sName + ".");
			ClientHttpRequest.logger.debug("Value : " + sValue + ".");
		}
		this.boundary();
		this.writeName(sName);
		this.newline();
		this.newline();
		this.writeln(sValue);
	}

	/**
	 * 
	 * @param aInputStream
	 * @param aOutputStream
	 * @throws IOException
	 */
	private static void pipe(final InputStream aInputStream,
			final OutputStream aOutputStream) throws IOException {
		byte[] tByte = new byte[500000];
		int iNbRead;
		int iTotal = 0;
		synchronized (aInputStream) {
			while ((iNbRead = aInputStream.read(tByte, 0, tByte.length)) >= 0) {
				aOutputStream.write(tByte, 0, iNbRead);
				iTotal += iNbRead;
			}
		}
		aOutputStream.flush();
		tByte = null;
	}

	/**
	 * Adds a file parameter to the request.
	 * 
	 * @param sName
	 *            Parameter name.
	 * @param sFileName
	 *            The name of the file.
	 * @param aInputStream
	 *            input stream to read the contents of the file from
	 * @throws IOException
	 */
	public void setParameter(final String sName, final String sFileName,
			final InputStream aInputStream) throws IOException {
		ClientHttpRequest.logger
				.trace("setParameter(String, String, InputStream)");
		if (ClientHttpRequest.logger.isDebugEnabled()) {
			ClientHttpRequest.logger.debug("Name : " + sName + ".");
			ClientHttpRequest.logger.debug("File name : " + sFileName + ".");
			ClientHttpRequest.logger.debug("InputStream : " + aInputStream
					+ ".");
		}
		this.boundary();
		this.writeName(sName);
		this.write("; filename=\"");
		this.write(sFileName);
		this.write('"');
		this.newline();
		this.write("Content-Type: ");
		String sType = URLConnection.guessContentTypeFromName(sFileName);
		if (sType == null) {
			sType = "application/octet-stream";
		}
		this.writeln(sType);
		this.newline();
		ClientHttpRequest.pipe(aInputStream, this.aOutputStream);
		this.newline();
	}

	/**
	 * adds a file parameter to the request
	 * 
	 * @param sName
	 *            parameter name
	 * @param aFile
	 *            the file to upload
	 * @throws IOException
	 */
	public void setParameter(final String sName, final File aFile)
			throws IOException {
		this.setParameter(sName, aFile.getPath(), new FileInputStream(aFile));
	}

	/**
	 * adds a parameter to the request; if the parameter is a File, the file is
	 * uploaded, otherwise the string value of the parameter is passed in the
	 * request
	 * 
	 * @param sName
	 *            parameter name
	 * @param object
	 *            parameter value, a File or anything else that can be
	 *            stringified
	 * @throws IOException
	 */
	public void setParameter(final String sName, final Object aObject)
			throws IOException {
		if (aObject instanceof File) {
			this.setParameter(sName, (File) aObject);
		} else {
			this.setParameter(sName, aObject.toString());
		}
	}

	/**
	 * adds parameters to the request
	 * 
	 * @param mapOfParameter
	 *            "name-to-value" map of parameters; if a value is a file, the
	 *            file is uploaded, otherwise it is stringified and sent in the
	 *            request
	 * @throws IOException
	 */
	public void setParameters(final Map mapOfParameter) throws IOException {
		if (mapOfParameter == null) {
			return;
		}
		for (final Iterator aIterator = mapOfParameter.entrySet().iterator(); aIterator
				.hasNext();) {
			final Map.Entry entry = (Map.Entry) aIterator.next();
			this.setParameter(entry.getKey().toString(), entry.getValue());
		}
	}

	/**
	 * adds parameters to the request
	 * 
	 * @param tObjectParameter
	 *            array of parameter names and values (parameters[2*i] is a
	 *            name, parameters[2*i + 1] is a value); if a value is a file,
	 *            the file is uploaded, otherwise it is stringified and sent in
	 *            the request
	 * @throws IOException
	 */
	public void setParameters(final Object[] tObjectParameter)
			throws IOException {
		if (tObjectParameter == null) {
			return;
		}
		for (int i = 0; i < tObjectParameter.length - 1; i += 2) {
			this.setParameter(tObjectParameter[i].toString(),
					tObjectParameter[i + 1]);
		}
	}

	/**
	 * posts the requests to the server, with all the cookies and parameters
	 * that were added
	 * 
	 * @return input stream with the server response
	 * @throws IOException
	 */
	public InputStream post() throws IOException {
		this.boundary();
		this.writeln("--");
		this.aOutputStream.close();

		return this.aURLConnection.getInputStream();
	}

	/**
	 * posts the requests to the server, with all the cookies and parameters
	 * that were added before (if any), and with parameters that are passed in
	 * the argument
	 * 
	 * @param mapOfParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameters
	 */
	public InputStream post(final Map mapOfParameter) throws IOException {
		this.setParameters(mapOfParameter);
		return this.post();
	}

	/**
	 * posts the requests to the server, with all the cookies and parameters
	 * that were added before (if any), and with parameters that are passed in
	 * the argument
	 * 
	 * @param tObjectParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameters
	 */
	public InputStream post(final Object[] tObjectParameter) throws IOException {
		this.setParameters(tObjectParameter);
		return this.post();
	}

	/**
	 * posts the requests to the server, with all the cookies and parameters
	 * that were added before (if any), and with cookies and parameters that are
	 * passed in the arguments
	 * 
	 * @param mapOfCookie
	 *            request cookies
	 * @param mapOfParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameters
	 * @see setCookies
	 */
	public InputStream post(final Map<String, String> mapOfCookie,
			final Map mapOfParameter) throws IOException {
		this.setCookies(mapOfCookie);
		this.setParameters(mapOfParameter);
		return this.post();
	}

	/**
	 * posts the requests to the server, with all the cookies and parameters
	 * that were added before (if any), and with cookies and parameters that are
	 * passed in the arguments
	 * 
	 * @param tStringCookie
	 *            request cookies
	 * @param tObjectParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameters
	 * @see setCookies
	 */
	public InputStream post(final String[] tStringCookie,
			final Object[] tObjectParameter) throws IOException {
		this.setCookies(tStringCookie);
		this.setParameters(tObjectParameter);
		return this.post();
	}

	/**
	 * post the POST request to the server, with the specified parameter
	 * 
	 * @param sName
	 *            parameter name
	 * @param oValue
	 *            parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public InputStream post(final String sName, final Object oValue)
			throws IOException {
		this.setParameter(sName, oValue);
		return this.post();
	}

	/**
	 * post the POST request to the server, with the specified parameters
	 * 
	 * @param sName1
	 *            first parameter name
	 * @param oValue1
	 *            first parameter value
	 * @param sName2
	 *            second parameter name
	 * @param oValue2
	 *            second parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public InputStream post(final String sName1, final Object oValue1,
			final String sName2, final Object oValue2) throws IOException {
		this.setParameter(sName1, oValue1);
		return this.post(sName2, oValue2);
	}

	/**
	 * post the POST request to the server, with the specified parameters
	 * 
	 * @param sName1
	 *            first parameter name
	 * @param oValue1
	 *            first parameter value
	 * @param sName2
	 *            second parameter name
	 * @param oValue2
	 *            second parameter value
	 * @param sName3
	 *            third parameter name
	 * @param oValue3
	 *            third parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public InputStream post(final String sName1, final Object oValue1,
			final String sName2, final Object oValue2, final String sName3,
			final Object oValue3) throws IOException {
		this.setParameter(sName1, oValue1);
		return this.post(sName2, oValue2, sName3, oValue3);
	}

	/**
	 * post the POST request to the server, with the specified parameters
	 * 
	 * @param sName1
	 *            first parameter name
	 * @param oValue1
	 *            first parameter value
	 * @param sName2
	 *            second parameter name
	 * @param oValue2
	 *            second parameter value
	 * @param sName3
	 *            third parameter name
	 * @param oValue3
	 *            third parameter value
	 * @param sName4
	 *            fourth parameter name
	 * @param oValue4
	 *            fourth parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public InputStream post(final String sName1, final Object oValue1,
			final String sName2, final Object oValue2, final String sName3,
			final Object oValue3, final String sName4, final Object oValue4)
			throws IOException {
		this.setParameter(sName1, oValue1);
		return this.post(sName2, oValue2, sName3, oValue3, sName4, oValue4);
	}

	/**
	 * posts a new request to specified URL, with parameters that are passed in
	 * the argument
	 * 
	 * @param mapOfParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameters
	 */
	public static InputStream post(final URL aURL, final Map mapOfParameter)
			throws IOException {
		return new ClientHttpRequest(aURL).post(mapOfParameter);
	}

	/**
	 * posts a new request to specified URL, with parameters that are passed in
	 * the argument
	 * 
	 * @param tObjectParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameters
	 */
	public static InputStream post(final URL aURL,
			final Object[] tObjectParameter) throws IOException {
		return new ClientHttpRequest(aURL).post(tObjectParameter);
	}

	/**
	 * posts a new request to specified URL, with cookies and parameters that
	 * are passed in the argument
	 * 
	 * @param mapOfCookie
	 *            request cookies
	 * @param mapOfParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setCookies
	 * @see setParameters
	 */
	public static InputStream post(final URL aURL,
			final Map<String, String> mapOfCookie, final Map mapOfParameter)
			throws IOException {
		return new ClientHttpRequest(aURL).post(mapOfCookie, mapOfParameter);
	}

	/**
	 * posts a new request to specified URL, with cookies and parameters that
	 * are passed in the argument
	 * 
	 * @param tStringCookie
	 *            request cookies
	 * @param tObjectParameter
	 *            request parameters
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setCookies
	 * @see setParameters
	 */
	public static InputStream post(final URL aURL,
			final String[] tStringCookie, final Object[] tObjectParameter)
			throws IOException {
		return new ClientHttpRequest(aURL)
				.post(tStringCookie, tObjectParameter);
	}

	/**
	 * post the POST request specified URL, with the specified parameter
	 * 
	 * @param name
	 *            parameter name
	 * @param value
	 *            parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public static InputStream post(final URL aURL, final String sName,
			final Object oValue) throws IOException {
		return new ClientHttpRequest(aURL).post(sName, oValue);
	}

	/**
	 * post the POST request to specified URL, with the specified parameters
	 * 
	 * @param sName1
	 *            first parameter name
	 * @param oValue1
	 *            first parameter value
	 * @param sName2
	 *            second parameter name
	 * @param oValue2
	 *            second parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public static InputStream post(final URL aURL, final String sName1,
			final Object oValue1, final String sName2, final Object oValue2)
			throws IOException {
		return new ClientHttpRequest(aURL).post(sName1, oValue1, sName2,
				oValue2);
	}

	/**
	 * post the POST request to specified URL, with the specified parameters
	 * 
	 * @param sName1
	 *            first parameter name
	 * @param oValue1
	 *            first parameter value
	 * @param sName2
	 *            second parameter name
	 * @param oValue2
	 *            second parameter value
	 * @param sName3
	 *            third parameter name
	 * @param oValue3
	 *            third parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public static InputStream post(final URL aURL, final String sName1,
			final Object oValue1, final String sName2, final Object oValue2,
			final String sName3, final Object oValue3) throws IOException {
		return new ClientHttpRequest(aURL).post(sName1, oValue1, sName2,
				oValue2, sName3, oValue3);
	}

	/**
	 * post the POST request to specified URL, with the specified parameters
	 * 
	 * @param name1
	 *            first parameter name
	 * @param value1
	 *            first parameter value
	 * @param name2
	 *            second parameter name
	 * @param value2
	 *            second parameter value
	 * @param name3
	 *            third parameter name
	 * @param value3
	 *            third parameter value
	 * @param sName4
	 *            fourth parameter name
	 * @param oValue4
	 *            fourth parameter value
	 * @return input stream with the server response
	 * @throws IOException
	 * @see setParameter
	 */
	public static InputStream post(final URL aURL, final String sName1,
			final Object oValue1, final String sName2, final Object oValue2,
			final String sName3, final Object oValue3, final String sName4,
			final Object oValue4) throws IOException {
		return new ClientHttpRequest(aURL).post(sName1, oValue1, sName2,
				oValue2, sName3, oValue3, sName4, oValue4);
	}
}

Index: Property.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/util/Attic/Property.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -d -r1.1.2.3 -r1.1.2.4
--- Property.java	6 Aug 2009 12:46:56 -0000	1.1.2.3
+++ Property.java	11 Aug 2009 16:05:35 -0000	1.1.2.4
@@ -1,24 +1,33 @@
 package org.w3c.unicorn.util;
 
+import java.util.Hashtable;
 import java.util.Properties;
 
 public class Property {
 	
-	private static Properties unicornProperties;
+	private static Hashtable<String, Properties> unicornPropertiesFiles = new Hashtable<String, Properties>();
 	
 	public static String get(String key) {
-		return unicornProperties.getProperty(key);
+		return unicornPropertiesFiles.get("unicorn.properties").getProperty(key);
 	}
 	
 	public static String get(String key1, String key2) {
-		return unicornProperties.getProperty(key1) + unicornProperties.getProperty(key2);
+		return unicornPropertiesFiles.get("unicorn.properties").getProperty(key1) 
+			 + unicornPropertiesFiles.get("unicorn.properties").getProperty(key2);
 	}
 
-	public static void setUnicornProperties(Properties unicornProperties) {
-		Property.unicornProperties = unicornProperties;
+	public static Properties getProps(String fileName) {
+		return unicornPropertiesFiles.get(fileName);
+	}
+	
+	public static Hashtable<String, Properties> getUnicornPropertiesFiles() {
+		return unicornPropertiesFiles;
 	}
 
-	public static Properties getUnicornProperties() {
-		return unicornProperties;
+	public static void setUnicornPropertiesFiles(
+			Hashtable<String, Properties> unicornPropertiesFiles) {
+		Property.unicornPropertiesFiles = unicornPropertiesFiles;
 	}
+
+	
 }

--- NEW FILE: ListFiles.java ---
// $Id: ListFiles.java,v 1.1.2.1 2009/08/11 16:05:35 tgambet Exp $
// Author: Jean-Guilhem Rouel
// (c) COPYRIGHT MIT, ERCIM and Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * ListFiles<br />
 * Created: Jun 26, 2006 2:17:42 PM<br />
 * This class provides static methods to list files into a directory.
 * 
 * @author Jean-Guilhem ROUEL
 */
public class ListFiles {

	private static final Log logger = LogFactory.getLog("org.w3c.unicorn.util");

	/**
	 * List all files matching a pattern in a directory
	 * 
	 * @param sDirectory
	 *            the directory to list
	 * @param sFilterPattern
	 *            only filenames matching this pattern will be returned
	 * @return an array of files matching the pattern
	 * @throws FileNotFoundException
	 */
	public static File[] listFiles(final String sDirectory,
			final String sFilterPattern) throws FileNotFoundException {
		ListFiles.logger.trace("listFiles(String, String)");
		if (ListFiles.logger.isDebugEnabled()) {
			ListFiles.logger.debug("Directory : " + sDirectory + ".");
			ListFiles.logger.debug("Filter pattern : " + sFilterPattern + ".");
		}

		final File aDirectory = new File(sDirectory);
		final Pattern aPattern = Pattern.compile(sFilterPattern);
		final FilenameFilter aFilenameFilter = new FilenameFilter() {
			public boolean accept(File aDirectory, String sName) {
				File aFile = new File(aDirectory.getPath() + sName);
				if (aFile.isDirectory()) {
					return false;
				}
				Matcher matcher = aPattern.matcher(sName);
				return matcher.find();
			}
		};

		final File[] tFile = aDirectory.listFiles(aFilenameFilter);
		if (null == tFile) {
			throw new FileNotFoundException("File in " + sDirectory
					+ " matching pattern " + sFilterPattern + " not found.");
		}
		return tFile;
	}

	/**
	 * List all filesin a directory
	 * 
	 * @param sDirectory
	 *            the directory to list
	 * @return an array of files
	 * @throws FileNotFoundException
	 */
	public static File[] listFiles(final String sDirectory)
			throws FileNotFoundException {
		ListFiles.logger.trace("listFiles(String)");
		if (ListFiles.logger.isDebugEnabled()) {
			ListFiles.logger.debug("Directory : " + sDirectory + ".");
		}

		final File aDirectory = new File(sDirectory);
		final FileFilter aFileFilter = new FileFilter() {
			public boolean accept(File aFile) {
				return !aFile.isDirectory();
			}
		};

		final File[] tFile = aDirectory.listFiles(aFileFilter);
		if (null == tFile) {
			throw new FileNotFoundException("File in " + sDirectory
					+ " not found.");
		}
		return tFile;
	}

	/**
	 * For testing purpose
	 * 
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException {
		File[] files = listFiles("/home/jean");
		for (final File file : files) {
			System.out.println(file.getAbsolutePath());
		}
		System.out.println("----------------------------------");
		files = listFiles("/home/jean", ".+\\..+");
		for (final File file : files) {
			System.out.println(file.getAbsolutePath());
		}
	}

}

Received on Tuesday, 11 August 2009 16:08:54 UTC