2006/unicorn/src/org/w3c/unicorn/contract EnumInputMethod.java,1.1,1.2 CallParameter.java,1.1,1.2 WADLUnmarshaller.java,1.1,1.2 Observer.java,1.1,1.2 WADLUnmarshallerXPath.java,1.1,1.2 InputMethod.java,1.1,1.2 CallMethod.java,1.1,1.2

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

Added Files:
	EnumInputMethod.java CallParameter.java WADLUnmarshaller.java 
	Observer.java WADLUnmarshallerXPath.java InputMethod.java 
	CallMethod.java 
Log Message:
Merging dev2 in HEAD

--- NEW FILE: EnumInputMethod.java ---
// $Id: EnumInputMethod.java,v 1.2 2009/08/28 12:40:09 jean-gui Exp $
// Author: Damien LEROY
// (c) COPYRIGHT MIT, ERCIM and Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.contract;

/**
 * Enumeration for the input methods
 * 
 * @author Damien LEROY
 */
public enum EnumInputMethod {

	URI("uri"), UPLOAD("upload"), DIRECT("direct");

	private final String sValue;

	private EnumInputMethod(final String sValue) {
		this.sValue = sValue;
	}

	public final String value() {
		return this.sValue;
	}

	public static EnumInputMethod fromValue(final String sValue) {
		for (final EnumInputMethod aEnumInputMethod : EnumInputMethod.values()) {
			if (aEnumInputMethod.sValue.equals(sValue)) {
				return aEnumInputMethod;
			}
		}
		throw new IllegalArgumentException("Unknow input method : "
				+ sValue.toString() + ".");
	}

}

--- NEW FILE: Observer.java ---
// $Id: Observer.java,v 1.2 2009/08/28 12:40:09 jean-gui 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.contract;

import java.util.List;
import java.util.Map;

import javax.activation.MimeType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.unicorn.exceptions.UnknownParserException;
import org.w3c.unicorn.util.LocalizedString;
import org.w3c.unicorn.util.Property;

/**
 * Observer<br />
 * Created: May 22, 2006 2:56:07 PM<br />
 * 
 * @author Jean-Guilhem Rouel
 */
public class Observer {

	/**
	 * Use to log any information during use of this class.
	 */
	private static final Log logger = LogFactory.getLog(Observer.class);

	/**
	 * ID of the observer
	 */
	private String sID;

	/**
	 * Name of the observer
	 */
	private LocalizedString aLocalizedStringName = null;

	/**
	 * Description of the observer
	 */
	private LocalizedString aLocalizedStringDescription = null;

	/**
	 * An help for the location of the observer
	 */
	private LocalizedString aLocalizedStringHelpLocation = null;

	/**
	 * A string provider for the observer
	 */
	private LocalizedString aLocalizedStringProvider = null;

	/**
	 * The name of the language for the observer
	 */
	private String sParamLangName = null;

	/**
	 * The response type of the observer
	 */
	private String responseType = null;

	/**
	 * List of method who can be used to call this observer.
	 */
	private List<CallMethod> listOfCallMethod;

	/**
	 * Map of input method handle by this observer.
	 */
	private Map<EnumInputMethod, InputMethod> mapOfInputMethod;

	private List<MimeType> supportedMimeTypes;

	/**
	 * Creates the observer
	 * 
	 */
	public Observer() {
		Observer.logger.trace("Constructor");
	}

	public String getID() {
		return this.sID;
	}

	public String getName(final String sLang) {
		return this.aLocalizedStringName.getLocalization(sLang);
	}

	public String getDescription(final String sLang) {
		return this.aLocalizedStringDescription.getLocalization(sLang);
	}

	public String getHelpLocation(final String sLang) {
		return this.aLocalizedStringHelpLocation.getLocalization(sLang);
	}

	public String getProvider(final String sLang) {
		return this.aLocalizedStringProvider.getLocalization(sLang);
	}

	public String getParamLangName() {
		return this.sParamLangName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.contract.Observer#getMethods()
	 */
	public List<CallMethod> getListOfCallMethod() {
		return this.listOfCallMethod;
	}

	public void setID(final String sID) {
		this.sID = sID;
	}

	public void setName(final LocalizedString aLocalizedString) {
		this.aLocalizedStringName = aLocalizedString;
	}

	public void setDescription(final LocalizedString aLocalizedString) {
		this.aLocalizedStringDescription = aLocalizedString;
	}

	public void setHelpLocation(final LocalizedString aLocalizedString) {
		this.aLocalizedStringHelpLocation = aLocalizedString;
	}

	public void setProvider(final LocalizedString aLocalizedString) {
		this.aLocalizedStringProvider = aLocalizedString;
	}

	public void setParamLangName(final String sParamLangName) {
		this.sParamLangName = sParamLangName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.contract.Observer#setMethods(java.util.ArrayList)
	 */
	public void setListOfCallMethod(final List<CallMethod> listOfCallMethod) {
		this.listOfCallMethod = listOfCallMethod;
	}

	/**
	 * Prints the object
	 */
	@Override
	public String toString() {
		final int sbSize = 1000;
		final String sVariableSeparator = "\n";
		final StringBuffer aStringBuffer = new StringBuffer(sbSize);

		aStringBuffer.append("ID:").append(this.sID);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("Name:").append(this.aLocalizedStringName);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("Description:").append(
				this.aLocalizedStringDescription);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("HelpLocation:").append(
				this.aLocalizedStringHelpLocation);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("Provider:").append(this.aLocalizedStringProvider);
		aStringBuffer.append(sVariableSeparator);
		// aStringBuffer.append("Description:[").append(this.aObserverDescription).append("]");
		// aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("Methods:[").append(this.listOfCallMethod).append(
				"]");
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("InputMethods:[").append(this.mapOfInputMethod)
				.append("]");

		return aStringBuffer.toString();
	}

	public final Map<EnumInputMethod, InputMethod> getMapOfInputMethod() {
		return this.mapOfInputMethod;
	}

	public void setMapOfInputMethod(
			final Map<EnumInputMethod, InputMethod> mapOfInputMethod) {
		this.mapOfInputMethod = mapOfInputMethod;
	}

	public void addInputMethod(final EnumInputMethod aEnumInputMethod,
			final InputMethod aInputMethod) {
		this.mapOfInputMethod.put(aEnumInputMethod, aInputMethod);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.contract.Observer#getInputMethod(org.w3c.unicorn.contract.methods.EnumInputMethod)
	 */
	public InputMethod getInputMethod(final EnumInputMethod aEnumInputMethod) {
		return this.mapOfInputMethod.get(aEnumInputMethod);
	}

	public InputMethod getBestInputMethod(final EnumInputMethod preferred) {
		// First try to get what caller would like
		InputMethod im = getInputMethod(preferred);
		if (im != null) {
			return im;
		}

		// If not possible, grab another one
		for (final EnumInputMethod aEIM : EnumInputMethod.values()) {
			im = getInputMethod(aEIM);
			if (im != null) {
				return im;
			}
		}

		// we should not arrive here (that would mean an observer doesn't
		// have any input method
		return null;
	}

	public CallMethod getCallMethod(final EnumInputMethod aEnumInputMethod) {
		return this.getInputMethod(aEnumInputMethod).getCallMethod();
	}

	public void addMimeType(final MimeType aMimeType) {
		this.supportedMimeTypes.add(aMimeType);
	}

	/**
	 * Returns <code>true</code> if the mime-type is supported by this
	 * observer
	 * 
	 * @param aMimeType
	 *            mime-type to check
	 * @return whether or not the mime-type is handled
	 */
	public boolean canHandleMimeType(final MimeType aMimeType) {
		// return this.supportedMimeTypes.contains(aMimeType);
		// equals and thus contains doesn't work :(
		for (final MimeType mt : this.supportedMimeTypes) {
			if (mt.match(aMimeType)) {
				return true;
			}
		}
		return false;
	}

	public void setSupportedMimeTypes(List<MimeType> mimeTypes) {
		this.supportedMimeTypes = mimeTypes;
	}

	public List<MimeType> getSupportedMimeTypes() {
		return this.supportedMimeTypes;
	}

	public String getResponseType() {
		return responseType;
	}

	public void setResponseType(String responseType) throws UnknownParserException {
		if (!Property.getProps("responseParsers.properties").containsKey(responseType))
			throw new UnknownParserException("Unknown parser: " + responseType + ". Check observer contract or responseParsers.properties.");
			
		this.responseType = responseType;
	}

}

--- NEW FILE: CallParameter.java ---
// $Id: CallParameter.java,v 1.2 2009/08/28 12:40:09 jean-gui 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.contract;

import java.util.ArrayList;
import java.util.List;

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

/**
 * CallParameter<br />
 * Created: May 22, 2006 3:26:19 PM<br />
 */
public class CallParameter {

	/**
	 * Object for complex logging purpose
	 */
	private static final Log logger = LogFactory
			.getLog(CallParameter.class);

	// Attributes of the parameter
	/**
	 * Name of the parameter
	 */
	private String sName = null;

	/**
	 * Style of the parameter
	 */
	private String sStyle = null;

	/**
	 * ID of the parameter
	 */
	private String sID = null;

	/**
	 * Type of the parameter
	 */
	private String sType = null;

	/**
	 * Default value for the parameter
	 */
	private String sDefaultValue;

	/**
	 * Defines whether or not the parameter is mandatory
	 */
	private boolean bRequired; // Is this parameter mandatory

	/**
	 * Defines whether or not the parameter can be repeated
	 */
	private boolean bRepeating; // Can this parameter be repeated

	/**
	 * Indicates if the parameter can be manually set or not
	 */
	private String sFixed = null; // Indicates if the parameter can be

	// manually set or not

	/**
	 * Defines the path of the parameter
	 */
	private String sPath = null;

	/**
	 * Possible values for this parameter
	 */
	private List<String> listOfPossibleValue = null;

	/**
	 * Constructor for a parameter
	 * 
	 * @param sName
	 *            Name of the parameter
	 * @param listOfPossibleValue
	 *            all the possibles values
	 * @param sFixed
	 *            Indicates if the parameter can be manually set or not
	 */
	public CallParameter(final String sName, final List<String> listOfValue,
			final String sFixed, final boolean bRequired,
			final boolean bRepeating) {
		super();

		CallParameter.logger
				.trace("Constructor(String, List<String>, String, boolean, boolean)");
		if (CallParameter.logger.isDebugEnabled()) {
			CallParameter.logger.debug("Name : " + sName + ".");
			CallParameter.logger.debug("List of value : " + listOfValue + ".");
			CallParameter.logger.debug("Fixed : " + sFixed + ".");
			CallParameter.logger.debug("Required : " + bRequired + ".");
			CallParameter.logger.debug("Repeating : " + bRepeating + ".");
		}

		this.sName = sName;
		this.listOfPossibleValue = listOfValue;
		this.sFixed = sFixed;
		this.bRequired = bRequired;
		this.bRepeating = bRepeating;
	}

	/**
	 * Various constructor with only the name and default value
	 * 
	 * @param sName
	 *            name of the parameter
	 */
	public CallParameter(final String sName) {
		this(sName, new ArrayList<String>(), null, false, false);
		CallParameter.logger.trace("Constructor(String)");
	}

	/**
	 * Various constructor with empty name
	 */
	public CallParameter() {
		this("", new ArrayList<String>(), null, false, false);
		CallParameter.logger.trace("Constructor()");
	}

	// ===================
	// ===== GETTERS =====
	// ===================
	public String getName() {
		return this.sName;
	}

	public String getStyle() {
		return this.sStyle;
	}

	public String getID() {
		return this.sID;
	}

	public String getType() {
		return this.sType;
	}

	public String getDefaultValue() {
		return this.sDefaultValue;
	}

	public boolean isRequired() {
		return this.bRequired;
	}

	public boolean isRepeating() {
		return this.bRepeating;
	}

	public boolean isFixed() {
		return this.sFixed != null;
	}

	public String getFixed() {
		return this.sFixed;
	}

	public String getPath() {
		return this.sPath;
	}

	public List<String> getListOfPossibleValue() {
		return listOfPossibleValue;
	}

	// ==================
	// ===== SETTERS ====
	// ==================
	public void setName(final String sName) {
		this.sName = sName;
	}

	public void setStyle(String style) {
		this.sStyle = style;
	}

	public void setID(String sid) {
		this.sID = sid;
	}

	public void setType(String type) {
		this.sType = type;
	}

	public void setDefaultValue(String defaultValue) {
		this.sDefaultValue = defaultValue;
	}

	public void setRequired(final boolean bRequired) {
		this.bRequired = bRequired;
	}

	public void setRepeating(final boolean bRepeating) {
		this.bRepeating = bRepeating;
	}

	public void setFixed(final String sFixed) {
		this.sFixed = sFixed;
	}

	public void setPath(final String sPath) {
		this.sPath = sPath;
	}

	public void setPossibleValues(final List<String> listOfValue) {
		this.listOfPossibleValue = listOfValue;
	}

	public void addValue(final String sValue) {
		if (this.listOfPossibleValue == null) {
			this.listOfPossibleValue = new ArrayList<String>();
		}
		this.listOfPossibleValue.add(sValue);
	}

	public Object getValue(final int iPosition) {
		return this.listOfPossibleValue.get(iPosition);
	}

	public boolean contains(final String sValue) {
		return this.listOfPossibleValue.contains(sValue)
				|| (this.listOfPossibleValue.size() == 1 && this.listOfPossibleValue
						.contains(""));
	}

	/**
	 * Prints the object
	 */
	@Override
	public String toString() {
		final int iSize = 1000;
		final String sVariableSeparator = "\n";
		final StringBuffer aStringBuffer = new StringBuffer(iSize);

		aStringBuffer.append("[begin CallParameter]\n");
		aStringBuffer.append("name:=").append(sName);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("id:=").append(sID);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("possibleValues:=").append(listOfPossibleValue);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("fixed:=").append(sFixed);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("required:=").append(bRequired);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("repeating:=").append(bRepeating);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("style:=").append(sStyle);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("path:=").append(sPath);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("style:=").append(sStyle);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("default:=").append(sDefaultValue);
		aStringBuffer.append("[end CallParameter]\n");

		return aStringBuffer.toString();
	}

}

--- NEW FILE: CallMethod.java ---
// $Id: CallMethod.java,v 1.2 2009/08/28 12:40:09 jean-gui 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.contract;

import java.net.URL;
import java.util.Map;

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

/**
 * CallMethod Created: May 23, 2006 11:46:50 AM
 */
public class CallMethod {

	/**
	 * Object for complex logging purpose
	 */
	private static final Log logger = LogFactory
			.getLog(CallMethod.class);

	/**
	 * URL for the call
	 */
	private URL aURL;

	// GET or POST
	/**
	 * The url to call
	 */
	private String sName;

	/**
	 * The name to call
	 */
	private String sID;

	/**
	 * the ID of the call
	 */
	private boolean bPost;

	/**
	 * whether the call is sent or not
	 */
	private Map<String, CallParameter> mapOfCallParameter;

	/**
	 * Set the parameter for the object call
	 * 
	 * @param aURL
	 *            the url to call
	 * @param bPost
	 *            whether or not the call is post
	 * @param sName
	 *            the name to call
	 * @param sID
	 *            the id of the call
	 * @param mapOfCallParameter
	 *            the parameter of the call
	 */
	public CallMethod(final URL aURL, final boolean bPost, final String sName,
			final String sID,
			final Map<String, CallParameter> mapOfCallParameter) {
		CallMethod.logger.trace("Constructor");
		if (CallMethod.logger.isDebugEnabled()) {
			CallMethod.logger.debug("URL : " + aURL + ".");
			CallMethod.logger.debug("Post : " + bPost + ".");
			CallMethod.logger.debug("Name : " + sName + ".");
			CallMethod.logger.debug("ID : " + sID + ".");
			CallMethod.logger.debug("Map of call parameter : "
					+ mapOfCallParameter + ".");
		}

		this.aURL = aURL;
		this.mapOfCallParameter = mapOfCallParameter;
		this.bPost = bPost;
		this.sName = sName;
		this.sID = sID;
	}

	/**
	 * Returns all the parameters of the call
	 * 
	 * @return Returns the parameters.
	 */
	public Map<String, CallParameter> getMapOfCallParameter() {
		return this.mapOfCallParameter;
	}

	/**
	 * Add a parameter to the call
	 * 
	 * @param aCallParameter
	 *            a parameter to add
	 */
	public void addParameter(final CallParameter aCallParameter) {
		this.mapOfCallParameter.put(aCallParameter.getName(), aCallParameter);
	}

	/**
	 * Gets the parameter by its name
	 * 
	 * @param sName
	 *            name of the parameter to get
	 * @return the Call parameter researched
	 */
	public CallParameter getCallParameterByName(final String sName) {
		return this.mapOfCallParameter.get(sName);
	}

	/**
	 * Returns if the call is post
	 * 
	 * @return Returns the post.
	 */
	public boolean isPost() {
		return this.bPost;
	}

	/**
	 * Returns the URI
	 * 
	 * @return Returns the uri.
	 */
	public URL getURL() {
		return this.aURL;
	}

	/**
	 * Returns the ID
	 * 
	 * @return Returns the id.
	 */
	public String getID() {
		return this.sID;
	}

	/**
	 * Get the name of the call
	 * 
	 * @return the name of the call
	 */
	public String getName() {
		return this.sName;
	}

	/**
	 * Print the object
	 */
	@Override
	public String toString() {
		final int iSize = 1000;
		final String sVariableSeparator = "\n";
		final StringBuffer aStringBuffer = new StringBuffer(iSize);

		aStringBuffer.append("uri=").append(this.aURL);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("id=").append(this.sID);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("post=").append(this.bPost);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("parameters=\n");
		aStringBuffer.append(this.mapOfCallParameter)
				.append(sVariableSeparator);

		return aStringBuffer.toString();
	}

}

--- NEW FILE: WADLUnmarshallerXPath.java ---
// $Id: WADLUnmarshallerXPath.java,v 1.2 2009/08/28 12:40:09 jean-gui 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.contract;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.unicorn.util.LocalizedString;
import org.xml.sax.SAXException;

import com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl;

/**
 * WADLUnmarshallerXPath<br />
 * Created: May 22, 2006 6:01:14 PM<br />
 * 
 * @author Jean-Guilhem ROUEL
 */
public class WADLUnmarshallerXPath implements WADLUnmarshaller {

	/**
	 * Object for complex logging purpose
	 */
	private static final Log logger = LogFactory.getLog(WADLUnmarshaller.class);

	/**
	 * Namespace for the context
	 */
	private static NamespaceContext aNamespaceContext;

	/**
	 * List of the different call method
	 */
	private List<CallMethod> listOfCallMethod = new ArrayList<CallMethod>();

	/**
	 * Factory to build Document
	 */
	private DocumentBuilderFactory aDocumentBuilderFactory;

	/**
	 * Document builder
	 */
	private DocumentBuilder aDocumentBuilder;

	/**
	 * Document XML
	 */
	private Document aDocument;

	/**
	 * Path for the document
	 */
	private XPath aXPath;

	/**
	 * Description of a observer to complete with information from a WADL file.
	 */
	private String sID = new String();

	private LocalizedString aLocalizedStringName = new LocalizedString();

	private LocalizedString aLocalizedStringDescription = new LocalizedString();

	private LocalizedString aLocalizedStringHelpLocation = new LocalizedString();

	private LocalizedString aLocalizedStringProvider = new LocalizedString();

	private List<MimeType> listOfMimeType = new ArrayList<MimeType>();

	private String responseType;

	/**
	 * name of parameter lang if observer has one
	 */
	private String nameOfLangParameter = null;

	/**
	 * Map of different input method handle by the observer.
	 */
	private Map<EnumInputMethod, InputMethod> mapOfInputMethod = new LinkedHashMap<EnumInputMethod, InputMethod>();

	/**
	 * Create the object to Unmarshall WADL with XPATH
	 * 
	 * @throws ParserConfigurationException
	 */
	public WADLUnmarshallerXPath() throws ParserConfigurationException {
		WADLUnmarshallerXPath.logger.trace("Constructor");

		this.aDocumentBuilderFactory = DocumentBuilderFactory.newInstance();

		this.aDocumentBuilder = this.aDocumentBuilderFactory
				.newDocumentBuilder();
		this.aXPath = new XPathFactoryImpl().newXPath();
		this.aXPath
				.setNamespaceContext(WADLUnmarshallerXPath.getNamespaceContext());
	}

	/**
	 * Get the document from an URL
	 * 
	 * @throws SAXException
	 */
	public void addURL(final URL aURL) throws IOException {

		WADLUnmarshallerXPath.logger.trace("addURL");
		WADLUnmarshallerXPath.logger.trace("URL : " + aURL + ".");

		try {
			this.aDocument = this.aDocumentBuilder.parse(aURL.openStream());
		} catch (SAXException e) {
			WADLUnmarshallerXPath.logger.error(
					"Parsing error with SAX in WADLUnmarshaller", e);
			e.printStackTrace();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.contract.WADLUnmarshaller#unmarshal(java.net.URL)
	 */
	public void unmarshal() throws XPathExpressionException,
			ParserConfigurationException, SAXException, IOException,
			MimeTypeParseException {
		WADLUnmarshallerXPath.logger.trace("unmarshal");
		this.parseDocsHeader();
		this.parseMethods();
	}

	/**
	 * Parse the header of the WADL file
	 * 
	 * @throws XPathExpressionException
	 *             error in the XPATH browsing of the document
	 * @throws MimeTypeParseException
	 *             error in the mime-type of the document
	 */
	private void parseDocsHeader() throws XPathExpressionException,
			MimeTypeParseException {
		final Node aNodeResource = this.aDocument.getElementsByTagName(
				"resources").item(0);
		XPathExpression aXPathExpression = this.aXPath
				.compile("//resource/doc");
		NodeList aNodeListResult = (NodeList) aXPathExpression.evaluate(
				aNodeResource, XPathConstants.NODESET);
		for (int i = 0; i < aNodeListResult.getLength(); i++) {
			Node nodeDoc = aNodeListResult.item(i);

			String vText = nodeDoc.getTextContent();

			// browse the attributes of a document
			String vTitle = null;
			String vLang = null;
			NamedNodeMap nnm = nodeDoc.getAttributes();
			for (int j = 0; j < nnm.getLength(); j++) {
				String attrName = nnm.item(j).getNodeName();
				String attrValue = nnm.item(j).getNodeValue();
				if ("title".equals(attrName)) {
					vTitle = attrValue;
				} else if ("xml:lang".equals(attrName)) {
					vLang = attrValue;
				}
			}

			if ("name".equals(vTitle)) {
				aLocalizedStringName.addLocalization(vLang, vText);
			} else if ("description".equals(vTitle)) {
				aLocalizedStringDescription.addLocalization(vLang, vText);
			} else if ("help".equals(vTitle)) {
				aLocalizedStringHelpLocation.addLocalization(vLang, vText);
			} else if ("provider".equals(vTitle)) {
				aLocalizedStringProvider.addLocalization(vLang, vText);
			} else if ("paramLang".equals(vTitle)) {
				nameOfLangParameter = vText;
			} else if ("mimetype".equals(vTitle)) {
				listOfMimeType.add(new MimeType(vText));
			} else if ("reference".equals(vTitle)) {
				sID = vText;
			} else if ("responseType".equals(vTitle)) {
				responseType = vText;
			}
		}
	}

	/**
	 * Parse all the methods of the document
	 * 
	 * @throws ParserConfigurationException
	 *             Error in the configuration of the parser
	 * @throws SAXException
	 *             error in the XML DOM
	 * @throws IOException
	 *             error in the input/output
	 * @throws XPathExpressionException
	 *             error in the evaluation of the XPATH query
	 */
	private void parseMethods() throws ParserConfigurationException,
			SAXException, IOException, XPathExpressionException {
		WADLUnmarshallerXPath.logger.trace("parseMethods");

		// base uri
		final Node aNodeResource = this.aDocument.getElementsByTagName(
				"resources").item(0);
		final String sBaseURI = aNodeResource.getAttributes().getNamedItem(
				"base").getNodeValue();

		final NodeList aNodeListMethod = this.aDocument
				.getElementsByTagName("method");

		for (int i = 0; i < aNodeListMethod.getLength(); i++) {
			final Node aNodeMethod = aNodeListMethod.item(i);

			final Map<String, CallParameter> mapOfCallParameter;
			mapOfCallParameter = new LinkedHashMap<String, CallParameter>();

			// URI of the resource (will be appended to the base URI)
			final String sResourceURI;
			sResourceURI = aNodeMethod.getParentNode().getAttributes()
					.getNamedItem("path").getNodeValue();

			// Type : GET/POST and id of the method
			final NamedNodeMap aNamedNodeMapAttribute = aNodeMethod
					.getAttributes();
			final String sName = aNamedNodeMapAttribute.getNamedItem("name")
					.getNodeValue();
			final boolean bPost = "POST".equals(sName.trim());
			final String sMethodID = aNamedNodeMapAttribute.getNamedItem("id")
					.getNodeValue().trim();

			// Query variables
			XPathExpression aXPathExpression = this.aXPath
					.compile("//method[@id='" + sMethodID + "']//param");
			NodeList aNodeListResult = (NodeList) aXPathExpression.evaluate(
					aNodeMethod, XPathConstants.NODESET);

			// iterate over param list
			for (int j = 0; j < aNodeListResult.getLength(); j++) {
				final NamedNodeMap aNamedNodeMap = aNodeListResult.item(j)
						.getAttributes();
				final CallParameter aCallParameter = new CallParameter();

				// iterate over attributes
				for (int k = 0; k < aNamedNodeMap.getLength(); k++) {
					final Node aNodeCurrentAttribute = aNamedNodeMap.item(k);

					final String sAttributeName = aNodeCurrentAttribute
							.getNodeName();
					final String sAttributeValue = aNodeCurrentAttribute
							.getNodeValue();

					if ("name".equals(sAttributeName)) {
						aCallParameter.setName(sAttributeValue);
					} else if ("required".equals(sAttributeName)) {
						aCallParameter.setRequired("true"
								.equals(sAttributeValue));
					} else if ("repeating".equals(sAttributeName)) {
						aCallParameter.setRepeating("true"
								.equals(sAttributeValue));
					} else if ("fixed".equals(sAttributeName)) {
						aCallParameter.setFixed(sAttributeValue);
					} else if ("style".equals(sAttributeName)) {
						aCallParameter.setStyle(sAttributeValue);
					} else if ("id".equals(sAttributeName)) {
						aCallParameter.setID(sAttributeValue);
					} else if ("path".equals(sAttributeName)) {
						aCallParameter.setPath(sAttributeValue);
					} else if ("default".equals(sAttributeName)) {
						aCallParameter.setDefaultValue(sAttributeValue);
					}
				} // iterate over attributes

				// read option type

				XPathExpression aOptionXPathExpression = this.aXPath
						.compile("//method[@id='" + sMethodID
								+ "']//request//param[@name='"
								+ aCallParameter.getName() + "']//option");
				NodeList aOptionNodeListResult = (NodeList) aOptionXPathExpression
						.evaluate(aNodeMethod, XPathConstants.NODESET);

				for (int k = 0; k < aOptionNodeListResult.getLength(); k++) {
					aCallParameter.addValue(aOptionNodeListResult.item(k)
							.getAttributes().item(0).getNodeValue());
				}

				mapOfCallParameter.put(new String(aCallParameter.getName()),
						aCallParameter);

			} // iterate over query_variable list

			final CallMethod aCallMethod = new CallMethod(new URL(sBaseURI
					+ sResourceURI), bPost, sName, sMethodID,
					mapOfCallParameter);
			this.listOfCallMethod.add(aCallMethod);

			// fill mapOfInputMethod

			NodeList listChildMethod = aNodeMethod.getChildNodes();
			String sInputMethod = null;
			String sInputParamName = null;
			for (int j = 0; j < listChildMethod.getLength(); j++) {
				Node childMethod = listChildMethod.item(j);
				if ("doc".equals(childMethod.getNodeName())) {
					String firstAttrName = childMethod.getAttributes().item(0)
							.getNodeName();
					if ("title".equals(firstAttrName)) {
						String firstAttrValue = childMethod.getAttributes()
								.item(0).getNodeValue();
						if ("inputMethod".equals(firstAttrValue)) {
							sInputMethod = childMethod.getTextContent();
						} else if ("inputParamName".equals(firstAttrValue)) {
							sInputParamName = childMethod.getTextContent();
						}
					}
				}
			}

			InputMethod aInputMethod = new InputMethod(sInputMethod);
			aInputMethod.setCallMethod(aCallMethod);
			aInputMethod.setCallParameter(aCallMethod
					.getCallParameterByName(sInputParamName));
			if ("URI".equals(sInputMethod)) {
				this.mapOfInputMethod.put(EnumInputMethod.URI, aInputMethod);
			} else if ("UPLOAD".equals(sInputMethod)) {
				this.mapOfInputMethod.put(EnumInputMethod.UPLOAD, aInputMethod);
			} else if ("DIRECT".equals(sInputMethod)) {
				this.mapOfInputMethod.put(EnumInputMethod.DIRECT, aInputMethod);
			}

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.contract.WADLUnmarshaller#getMethods()
	 */
	public List<CallMethod> getListOfCallMethod() {
		return this.listOfCallMethod;
	}

	public LocalizedString getDescription() {
		return this.aLocalizedStringDescription;
	}

	public LocalizedString getHelpLocation() {
		return this.aLocalizedStringHelpLocation;
	}

	public String getID() {
		return this.sID;
	}

	public Map<EnumInputMethod, InputMethod> getMapOfInputMethod() {
		return this.mapOfInputMethod;
	}

	public LocalizedString getName() {
		return this.aLocalizedStringName;
	}

	public String getNameOfLangParameter() {
		return this.nameOfLangParameter;
	}

	public LocalizedString getProvider() {
		return this.aLocalizedStringProvider;
	}

	public String getResponseType() {
		return responseType;
	}

	public List<MimeType> getSupportedMimeTypes() {
		return this.listOfMimeType;
	}

	/**
	 * Main method of the class only for testing purpose
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(final String[] args) throws Exception {
		final WADLUnmarshaller t = new WADLUnmarshallerXPath();
		t.addURL(new URL("http://localhost/css.wadl"));
		t.unmarshal();
		System.out.println(t.getID());

		System.out.println(t.getMapOfInputMethod());
		System.out.println("***************************************");
		for (InputMethod im : t.getMapOfInputMethod().values()) {
			System.out.println(im.getCallParameter());
			System.out.println("---------------------------------");
		}
	}

	public static void setNamespaceContext(NamespaceContext aNamespaceContext) {
		WADLUnmarshallerXPath.aNamespaceContext = aNamespaceContext;
	}

	public static NamespaceContext getNamespaceContext() {
		return aNamespaceContext;
	}

}

--- NEW FILE: InputMethod.java ---
// $Id: InputMethod.java,v 1.2 2009/08/28 12:40:09 jean-gui Exp $
// Author: Damien LEROY
// (c) COPYRIGHT MIT, ERCIM and Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.contract;

/**
 * @author Damien LEROY
 * 
 */
public class InputMethod {

	/**
	 * Method of the call
	 */
	private CallMethod aCallMethod = null;

	/**
	 * Parameter of the call
	 */
	private CallParameter aCallParameter = null;

	private EnumInputMethod method;

	public InputMethod(EnumInputMethod method) {
		this.method = method;
	}

	public InputMethod(String method) {
		if ("URI".equals(method)) {
			this.method = EnumInputMethod.URI;
		} else if ("UPLOAD".equals(method)) {
			this.method = EnumInputMethod.UPLOAD;
		} else if ("DIRECT".equals(method)) {
			this.method = EnumInputMethod.DIRECT;
		}
	}

	public EnumInputMethod getMethod() {
		return this.method;
	}

	/**
	 * Get the current calling method
	 * 
	 * @return current calling method
	 */
	public CallMethod getCallMethod() {
		return this.aCallMethod;
	}

	/**
	 * Set the method of the call
	 * 
	 * @param aCallMethod
	 *            new calling method to add
	 */
	public void setCallMethod(final CallMethod aCallMethod) {
		this.aCallMethod = aCallMethod;
	}

	/**
	 * Get the current call parameter
	 * 
	 * @return the current call parameter
	 */
	public CallParameter getCallParameter() {
		return this.aCallParameter;
	}

	/**
	 * Set the current Call parameter
	 * 
	 * @param aCallParameter
	 *            call parameter to set
	 */
	public void setCallParameter(final CallParameter aCallParameter) {
		this.aCallParameter = aCallParameter;
	}

	/**
	 * Get a parameter by its name
	 * 
	 * @param sCallParameterName
	 *            name of the parameter to look for
	 * @return the parameter researched
	 */
	public CallParameter getCallParameterByName(final String sCallParameterName) {
		return aCallMethod.getCallParameterByName(sCallParameterName);
	}

	/**
	 * Prints the object
	 */
	@Override
	public final String toString() {
		final int iStringBufferSize = 1000;
		final String sVariableSeparator = "\n";
		final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize);
		aStringBuffer.append("/BeginInputMethod/");
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("CallMethod:{").append(this.aCallMethod).append(
				"}");
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("CallParameter:{").append(this.aCallParameter)
				.append("}");
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("/EndInputMethod/");

		return aStringBuffer.toString();
	}

}

--- NEW FILE: WADLUnmarshaller.java ---
// $Id: WADLUnmarshaller.java,v 1.2 2009/08/28 12:40:09 jean-gui 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.contract;

import java.util.List;
import java.util.Map;

import javax.activation.MimeType;

import org.w3c.unicorn.util.LocalizedString;
import org.w3c.unicorn.util.Unmarshaller;

/**
 * WADLUnmarshallerXPath<br />
 * Created: May 24, 2006 12:34:38 PM<br />
 */
public interface WADLUnmarshaller extends Unmarshaller {

	/**
	 * @return Returns a list of methods.
	 */
	public abstract List<CallMethod> getListOfCallMethod();

	Map<EnumInputMethod, InputMethod> getMapOfInputMethod();

	public String getID();

	public LocalizedString getName();

	public LocalizedString getDescription();

	public LocalizedString getHelpLocation();

	public LocalizedString getProvider();

	public String getNameOfLangParameter();

	public String getResponseType();

	public List<MimeType> getSupportedMimeTypes();

}

Received on Friday, 28 August 2009 12:40:22 UTC