W3C home > Mailing lists > Public > www-validator-cvs@w3.org > August 2009

2006/unicorn/src/org/w3c/unicorn/tasklist TaskListUnmarshallerBeans.java,1.1,1.2 RDFUnmarshallerJena.java,1.1,1.2 Tasklist.java,1.1,1.2 TasksListUnmarshaller.java,1.1,1.2 Task.java,1.1,1.2 RDFUnmarshaller.java,1.1,1.2

From: Jean-Guilhem Rouel via cvs-syncmail <cvsmail@w3.org>
Date: Fri, 28 Aug 2009 12:39:56 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1Mh0kG-0005tk-90@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/tasklist
In directory hutz:/tmp/cvs-serv22368/src/org/w3c/unicorn/tasklist

Added Files:
	TaskListUnmarshallerBeans.java RDFUnmarshallerJena.java 
	Tasklist.java TasksListUnmarshaller.java Task.java 
	RDFUnmarshaller.java 
Log Message:
Merging dev2 in HEAD

--- NEW FILE: RDFUnmarshallerJena.java ---
// $Id: RDFUnmarshallerJena.java,v 1.2 2009/08/28 12:39:53 jean-gui 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.tasklist;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.unicorn.Framework;
import org.w3c.unicorn.tasklist.parameters.Parameter;
import org.w3c.unicorn.tasklist.parameters.Value;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

/**
 * @author Damien LEROY
 * 
 */
public class RDFUnmarshallerJena implements RDFUnmarshaller {

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

	private static final Model MODEL = ModelFactory.createDefaultModel();

	private static final String UCN_NAMESPACE = "http://www.w3.org/unicorn#";

	private static Resource RESOURCE_TASK = null;

	private static Property PROPERTY_DESCRIPTION = null;

	private static Property PROPERTY_HASPARAMETER = null;

	private static Property PROPERTY_HASVALUE = null;

	private static Property PROPERTY_LONGNAME = null;

	private static Property PROPERTY_PARAMETER = null;

	private static Property PROPERTY_REFERENCE = null;

	private static Property PROPERTY_TYPE = null;

	private static Property PROPERTY_VALUE = null;
	
	private static Property PROPERTY_DEFAULT = null;

	private Tasklist mapOfTask = null;

	private Model aModel = null;

	/**
	 * Default constructor.
	 * 
	 */
	public RDFUnmarshallerJena() {
		RDFUnmarshallerJena.logger.trace("Constructor");
		this.aModel = ModelFactory.createDefaultModel();
	}

	/**
	 * Adds a Model with the given URL to this aModel.
	 * 
	 * @param aURL
	 *            The URL to add.
	 */
	public void addURL(final URL aURL) throws IOException {
		RDFUnmarshallerJena.logger.trace("addURL");
		RDFUnmarshallerJena.logger.debug("URL : " + aURL + ".");
		final Model aModel = ModelFactory.createDefaultModel();
		aModel.read(aURL.openStream(), null);
		this.aModel.add(aModel);
	}

	/**
	 * Adds a name (with its language) to the given task.
	 * 
	 * @param aTask
	 *            The task to name.
	 * @param aLiteral
	 *            The name of the task.
	 */
	private void addLongName(final Task aTask, final Literal aLiteral) {
		RDFUnmarshallerJena.logger.debug("LongName lang:"
				+ aLiteral.getLanguage() + " value:" + aLiteral.getString()
				+ ".");
		aTask.addLongName(aLiteral.getLanguage(), aLiteral.getString());
	}

	/**
	 * Adds a description (with its language) to the given task.
	 * 
	 * @param aTask
	 *            The task to describe.
	 * @param aLiteral
	 *            The description of the task.
	 */
	private void addDescription(final Task aTask, final Literal aLiteral) {
		RDFUnmarshallerJena.logger.debug("Description lang:"
				+ aLiteral.getLanguage() + " value:" + aLiteral.getString()
				+ ".");
		aTask.addDescription(aLiteral.getLanguage(), aLiteral.getString());
	}

	/**
	 * Adds a name (with its language) to the given parameter.
	 * 
	 * @param aTask
	 *            The parameter to name.
	 * @param aLiteral
	 *            The name of the parameter.
	 */
	private void addLongName(final Parameter aParameter, final Literal aLiteral) {
		RDFUnmarshallerJena.logger.debug("Parameter long name lang:"
				+ aLiteral.getLanguage() + " value:" + aLiteral.getString()
				+ ".");
		aParameter.addLongName(aLiteral.getLanguage(), aLiteral.getString());
	}

	/**
	 * Adds a name (with its language) to the given value.
	 * 
	 * @param aTask
	 *            The value to name.
	 * @param aLiteral
	 *            The name of the value.
	 */
	private void addLongName(final Value aValue, final Literal aLiteral) {
		RDFUnmarshallerJena.logger.debug("Value long name lang:"
				+ aLiteral.getLanguage() + " value:" + aLiteral.getString()
				+ ".");
		aValue.addLongName(aLiteral.getLanguage(), aLiteral.getString());
	}

	/**
	 * Adds a value to the given parameter.
	 * 
	 * @param aParameter
	 *            The parameter to consider.
	 * @param aValue
	 *            The value to add.
	 */
	private void addValue(final Parameter aParameter, final Resource aValue) {
		final String sValue = aValue.getProperty(
				RDFUnmarshallerJena.PROPERTY_REFERENCE).getLiteral().getString();
		final Value oValue = aParameter.getValue(sValue);
		if (null == oValue) {
			RDFUnmarshallerJena.logger.warn("Value " + sValue
					+ " not found in parameter " + aParameter.getName() + ".");
			return;
		}
		RDFUnmarshallerJena.logger.debug("Parameter value  : " + sValue + ".");
		// find and add longName of the Value
		for (final StmtIterator siLongName = aValue
				.listProperties(RDFUnmarshallerJena.PROPERTY_LONGNAME); siLongName
				.hasNext();) {
			final Literal lLongName = siLongName.nextStatement().getLiteral();
			this.addLongName(oValue, lLongName);
		} // find and add longName of the Value
	}

	/**
	 * Adds a parameter to the given task.
	 * 
	 * @param aTask
	 *            The task to consider.
	 * @param aParameter
	 *            The parameter to add.
	 */
	private void addParameter(final Task aTask, final Resource aParameter) {
		final String sParameterReference = aParameter.getProperty(
				RDFUnmarshallerJena.PROPERTY_REFERENCE).getLiteral()
				.getString();
		final Parameter oParameter = aTask.getMapOfParameter().get(
				sParameterReference);
		if (null == oParameter) {
			RDFUnmarshallerJena.logger.warn("Parameter " + sParameterReference
					+ " not found in task " + aTask.getID() + ".");
			return;
		}
		RDFUnmarshallerJena.logger.debug("Parameter : " + sParameterReference
				+ ".");
		// find and add longName of the Parameter
		for (StmtIterator siLongName = aParameter
				.listProperties(RDFUnmarshallerJena.PROPERTY_LONGNAME); siLongName
				.hasNext();) {
			final Literal lLongName = siLongName.nextStatement().getLiteral();
			this.addLongName(oParameter, lLongName);
		} // find and add longName of the Parameter
		// find and add value of the Parameter
		for (final StmtIterator siValue = this.aModel.listStatements(
				aParameter, RDFUnmarshallerJena.PROPERTY_HASVALUE,
				(RDFNode) null); siValue.hasNext();) {
			final Resource aValue = (Resource) siValue.nextStatement()
					.getObject();
			if (null == aValue) {
				RDFUnmarshallerJena.logger.error("Resource value == null.");
				continue;
			}
			this.addValue(oParameter, aValue);
		} // find and add value of the Parameter
	}

	/**
	 * Adds a task to this object.
	 * 
	 * @param aTask
	 *            The task to add.
	 * @throws Exception
	 */
	private void addTask(final Resource aTask) throws Exception {
		final Statement aReference = aTask
				.getProperty(RDFUnmarshallerJena.PROPERTY_REFERENCE);
		final Task oTask = this.mapOfTask.get(aReference.getLiteral()
				.getString());
		if (oTask == null) {
			// TODO creer et initialiser une Task si les informations peuvent
			// TODO etre mise uniquement dans le fichier rdf
			Framework.logger.warn("> Found unused metadata for: " + aReference.getLiteral().getString());
			return;
		} else {
			Framework.logger.debug("> Found metadata for: " + aReference.getLiteral().getString());
			Statement isDefault = aTask.getProperty(RDFUnmarshallerJena.PROPERTY_DEFAULT);
			if (isDefault != null && isDefault.getLiteral().toString().equals("true")) {
				this.mapOfTask.setDefaultTaskId(aReference.getLiteral().getString());
				Framework.logger.debug(">> This is the default task.");
			}
		}
		RDFUnmarshallerJena.logger.debug("Reference : "
				+ aReference.getObject().toString() + ".");
		// find and add longName of the task
		for (final StmtIterator siLongName = aTask
				.listProperties(RDFUnmarshallerJena.PROPERTY_LONGNAME); siLongName
				.hasNext();) {
			final Literal lLongName = siLongName.nextStatement().getLiteral();
			this.addLongName(oTask, lLongName);
		} // find and add longName of the task
		// find and add description of the task
		for (final StmtIterator siDescription = aTask
				.listProperties(RDFUnmarshallerJena.PROPERTY_DESCRIPTION); siDescription
				.hasNext();) {
			final Literal lDescription = siDescription.nextStatement()
					.getLiteral();
			this.addDescription(oTask, lDescription);
		} // find and add description of the task
		// find and add Parameter of the task
		for (final StmtIterator siParameter = this.aModel.listStatements(aTask,
				RDFUnmarshallerJena.PROPERTY_HASPARAMETER, (RDFNode) null); siParameter
				.hasNext();) {
			final Resource aParameter = (Resource) siParameter.nextStatement()
					.getObject();
			if (null == aParameter) {
				RDFUnmarshallerJena.logger.error("Resource parameter == null.");
				continue;
			}
			this.addParameter(oTask, aParameter);
		} // find and add Parameter of the task
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.util.Unmarshaller#unmarshal(java.net.URL)
	 */
	public void unmarshal() throws Exception {
		RDFUnmarshallerJena.logger.trace("unmarshal");

		// find and add task
		for (final StmtIterator siTask = this.aModel.listStatements(null,
				RDFUnmarshallerJena.PROPERTY_TYPE,
				RDFUnmarshallerJena.RESOURCE_TASK); siTask.hasNext();) {
			final Resource aTask = siTask.nextStatement().getSubject();
			this.addTask(aTask);
		} // find and add task
		RDFUnmarshallerJena.logger.trace("End.");
	}

	/**
	 * @return Returns the tasks.
	 */
	public Tasklist getMapOfTask() {
		return this.mapOfTask;
	}

	/**
	 * @param mapOfTask
	 *            The tasks to set.
	 */
	public void setMapOfTask(final Tasklist mapOfTask) {
		this.mapOfTask = mapOfTask;
	}

	public static Resource getRESOURCE_TASK() {
		return RESOURCE_TASK;
	}

	public static void setRESOURCE_TASK(Resource rESOURCETASK) {
		RESOURCE_TASK = rESOURCETASK;
	}

	public static Property getPROPERTY_DESCRIPTION() {
		return PROPERTY_DESCRIPTION;
	}

	public static void setPROPERTY_DESCRIPTION(Property pROPERTYDESCRIPTION) {
		PROPERTY_DESCRIPTION = pROPERTYDESCRIPTION;
	}

	public static Property getPROPERTY_HASPARAMETER() {
		return PROPERTY_HASPARAMETER;
	}

	public static void setPROPERTY_HASPARAMETER(Property pROPERTYHASPARAMETER) {
		PROPERTY_HASPARAMETER = pROPERTYHASPARAMETER;
	}

	public static Property getPROPERTY_HASVALUE() {
		return PROPERTY_HASVALUE;
	}

	public static void setPROPERTY_HASVALUE(Property pROPERTYHASVALUE) {
		PROPERTY_HASVALUE = pROPERTYHASVALUE;
	}

	public static Property getPROPERTY_LONGNAME() {
		return PROPERTY_LONGNAME;
	}

	public static void setPROPERTY_LONGNAME(Property pROPERTYLONGNAME) {
		PROPERTY_LONGNAME = pROPERTYLONGNAME;
	}

	public static Property getPROPERTY_PARAMETER() {
		return PROPERTY_PARAMETER;
	}

	public static void setPROPERTY_PARAMETER(Property pROPERTYPARAMETER) {
		PROPERTY_PARAMETER = pROPERTYPARAMETER;
	}

	public static Property getPROPERTY_REFERENCE() {
		return PROPERTY_REFERENCE;
	}

	public static void setPROPERTY_REFERENCE(Property pROPERTYREFERENCE) {
		PROPERTY_REFERENCE = pROPERTYREFERENCE;
	}

	public static Property getPROPERTY_TYPE() {
		return PROPERTY_TYPE;
	}

	public static void setPROPERTY_TYPE(Property pROPERTYTYPE) {
		PROPERTY_TYPE = pROPERTYTYPE;
	}

	public static Property getPROPERTY_VALUE() {
		return PROPERTY_VALUE;
	}

	public static void setPROPERTY_VALUE(Property pROPERTYVALUE) {
		PROPERTY_VALUE = pROPERTYVALUE;
	}

	public static Model getModel() {
		return MODEL;
	}

	public static String getUcnNamespace() {
		return UCN_NAMESPACE;
	}

	public static Property getPROPERTY_DEFAULT() {
		return PROPERTY_DEFAULT;
	}

	public static void setPROPERTY_DEFAULT(Property pROPERTYDEFAULT) {
		PROPERTY_DEFAULT = pROPERTYDEFAULT;
	}

	
	
}

--- NEW FILE: TaskListUnmarshallerBeans.java ---
package org.w3c.unicorn.tasklist;

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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlbeans.XmlException;
import org.w3.unicorn.tasklist.CondType;
import org.w3.unicorn.tasklist.ExecType;
import org.w3.unicorn.tasklist.IfType;
import org.w3.unicorn.tasklist.MappedType;
import org.w3.unicorn.tasklist.ParameterType;
import org.w3.unicorn.tasklist.ParametersType;
import org.w3.unicorn.tasklist.TInputMethod;
import org.w3.unicorn.tasklist.TParamType;
import org.w3.unicorn.tasklist.TUi;
import org.w3.unicorn.tasklist.TaskType;
import org.w3.unicorn.tasklist.TasklistDocument;
import org.w3.unicorn.tasklist.RoutineType;
import org.w3.unicorn.tasklist.ValueType;
import org.w3c.unicorn.Framework;
import org.w3c.unicorn.contract.CallMethod;
import org.w3c.unicorn.contract.CallParameter;
import org.w3c.unicorn.contract.EnumInputMethod;
import org.w3c.unicorn.contract.InputMethod;
import org.w3c.unicorn.contract.Observer;
import org.w3c.unicorn.exceptions.ParameterException;
import org.w3c.unicorn.exceptions.UnknownObserverException;
import org.w3c.unicorn.tasklist.parameters.Mapping;
import org.w3c.unicorn.tasklist.parameters.Parameter;
import org.w3c.unicorn.tasklist.parameters.ParameterFactory;
import org.w3c.unicorn.tasklist.parameters.Value;
import org.w3c.unicorn.tasklisttree.TLTCond;
import org.w3c.unicorn.tasklisttree.TLTExec;
import org.w3c.unicorn.tasklisttree.TLTIf;
import org.w3c.unicorn.tasklisttree.TLTNode;
import org.w3c.unicorn.util.LocalizedString;

/**
 * Unmarshals the tasklist thanks to the XMLBeans tools.
 * 
 * @author Florent Batard, Jonathan Barouh
 * 
 */
public class TaskListUnmarshallerBeans implements TasksListUnmarshaller {

	private TasklistDocument aTaskList;

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

	private int NodeID = 0;

	/**
	 * The tasklist corresponding to the xml file
	 */
	//private Map<String, Task> mapOfTask;
	private Tasklist mapOfTask;
	
	/**
	 * The observers' list used to check some constraints on the tasks
	 */
	private Map<String, Observer> mapOfObserver;

	public TaskListUnmarshallerBeans() {
	}

	public TaskListUnmarshallerBeans(final Map<String, Observer> mapOfObserver) {
		TaskListUnmarshallerBeans.logger.trace("Constructor");
		//this.mapOfTask = new LinkedHashMap<String, org.w3c.unicorn.tasklist.Task>();
		this.mapOfTask = new Tasklist();
		this.mapOfObserver = mapOfObserver;
	}

	private void addTask(final TaskType aTask) throws ParameterException, UnknownObserverException {
		TaskListUnmarshallerBeans.logger.trace("addTask");

		if (aTask == null) {
			TaskListUnmarshallerBeans.logger.warn("Task : null");
			return;
		}

		TaskListUnmarshallerBeans.logger.trace("Add task : "
				+ aTask.getId());

		final Task aTaskCurrent = new Task();
		// Create the execution level tree
		aTaskCurrent.setID(aTask.getId());
		aTaskCurrent.setTree(this.expandTree(aTask, aTask.getRoutine()));

		// parameters
		final ParametersType aParameters = aTask.getParameters();
		if (aParameters != null) {

			for (final ParameterType aParameterBeans : aParameters.getParameterList()) {

				final TUi.Enum aTUi = aParameterBeans.getUi();
				final String sObserver = aParameterBeans.getObserver();
				final String sName = aParameterBeans.getName();

				// default values as string
				final String sDefaultValues = aParameterBeans.getDefault();

				final TParamType.Enum aParamType = aParameterBeans.getType();

				Parameter aParameter = null;

				if (sObserver != null && !"".equals(sObserver)) {
					aParameter = getParameterFromObserver(sName, sObserver,
							aTUi, sDefaultValues, aParamType);
				} else {

					// Values
					final Map<String, Value> mapOfValue = new LinkedHashMap<String, Value>();
					for (final ValueType aValue : aParameterBeans.getValueList()) {

						// name of the value
						String sValueName = aValue.getName();
						if (sValueName == null) {
							sValueName = "";
						}

						// Mappings of the value
						final Map<String, List<Mapping>> mapOfMapping = new LinkedHashMap<String, List<Mapping>>();
						for (final MappedType aMappedBeans : aValue.getMappedList()) {
							final Mapping aMapping = this
									.createMapping(aMappedBeans);
							if (aMapping != null) {
								final String sObs = aMapping.getObserver()
										.getID();
								List<Mapping> listOfMapping = mapOfMapping
										.get(sObs);
								if (null == listOfMapping) {
									listOfMapping = new ArrayList<Mapping>();
									mapOfMapping.put(sObs, listOfMapping);
								}
								listOfMapping.add(aMapping);
							}
						}
						mapOfValue.put(sValueName, new Value(mapOfMapping,
								sValueName));
					}

					aParameter = this.createParameter(aParamType, sName, aTUi,
							sDefaultValues, mapOfValue);
				}

				if (aParameter != null) {
					aTaskCurrent.addParameter(aParameter);
				}
			}
		}

		this.mapOfTask.put(aTaskCurrent.getID(), aTaskCurrent);

	}

	/**
	 * Creates a usable mapping from a JAXB-generated one.
	 * 
	 * @param aMappedJAXB
	 *            the JAXB-generated mapping
	 * @return the created mapping
	 */
	private Mapping createMapping(final MappedType aMapped) {
		TaskListUnmarshallerBeans.logger.trace("createMapping");

		// The mapped observer
		final String sMappingObserver = aMapped.getObserver();
		final Observer aObserverMapped = this.mapOfObserver
				.get(sMappingObserver);

		if (aObserverMapped == null) {
			TaskListUnmarshallerBeans.logger.error("The observer "
					+ sMappingObserver
					+ " does not seem to exist... Skipping mapping.");
			return null;
		}

		// the mapped parameter
		final String sMappingParam = aMapped.getParam();
		// the value mapped
		String sMappingValue = aMapped.getValue();
		if (sMappingValue == null) {
			sMappingValue = "";
		}

		// TODO check if is useful to add input method in mapping
		final List<EnumInputMethod> listOfEnumInputMethod = new ArrayList<EnumInputMethod>();

		// The list of mapped input methods

		final List<TInputMethod.Enum> listOfTInputMethodBeans = new ArrayList<TInputMethod.Enum>();
		for (Object methodString : aMapped.getInputmethod()) {
			listOfTInputMethodBeans.add(TInputMethod.Enum
					.forString(methodString.toString()));
		}
		// by default a parameter is mapped to all input methods
		if (listOfTInputMethodBeans.size() == 0) {
			listOfTInputMethodBeans.add(TInputMethod.DIRECT);
			listOfTInputMethodBeans.add(TInputMethod.FILE);
			listOfTInputMethodBeans.add(TInputMethod.URI);
		}

		/*
		 * For each input method, we check that the mapped observer: - can
		 * handle this input method - has a parameter with the corresponding
		 * name for this input method - can handle this value for this parameter
		 */

		for (final TInputMethod.Enum aTInputMethod : listOfTInputMethodBeans) {
			final EnumInputMethod aEnumInputMethod;
			aEnumInputMethod = TaskListUnmarshallerBeans
					.getEnumInputMethod(aTInputMethod);
			// the observer can handle this input method
			if (aObserverMapped.getInputMethod(aEnumInputMethod) == null) {
				TaskListUnmarshallerBeans.logger.warn(sMappingObserver
						+ " does not support " + aEnumInputMethod.value()
						+ " input method.");
				continue;
			}
			final CallParameter aCallParameterMapped;
			aCallParameterMapped = aObserverMapped.getInputMethod(
					aEnumInputMethod).getCallParameterByName(sMappingParam);
			// the parameter exists
			if (aCallParameterMapped == null) {
				TaskListUnmarshallerBeans.logger.error(sMappingObserver
						+ " does not have " + "a parameter named "
						+ sMappingParam + ".");
				continue;
			}
			// the value exists
			if (!aCallParameterMapped.contains(sMappingValue)) {
				TaskListUnmarshallerBeans.logger.error("Parameter "
						+ sMappingParam + " does not accept " + sMappingValue
						+ " as a value.");
				continue;
			}
			listOfEnumInputMethod.add(aEnumInputMethod);
		}

		if (listOfEnumInputMethod.size() == 0) {
			return null;
		}

		return new Mapping(aObserverMapped, sMappingParam, sMappingValue/*
																		 * ,
																		 * listOfEnumInputMethod
																		 */);
	}

	private Parameter getParameterFromObserver(final String sParamName,
			final String sObserverName, final TUi.Enum aTUi,
			final String sDefaultValues, final TParamType.Enum aTParamType)
			throws ParameterException {
		TaskListUnmarshallerBeans.logger.trace("getParameterFromObserver");
		if (TaskListUnmarshallerBeans.logger.isDebugEnabled()) {
			TaskListUnmarshallerBeans.logger.debug("Parameter name : "
					+ sParamName + ".");
			TaskListUnmarshallerBeans.logger.debug("Observer name : "
					+ sObserverName + ".");
			TaskListUnmarshallerBeans.logger.debug("TUi : " + aTUi + ".");
			TaskListUnmarshallerBeans.logger.debug("Default values : "
					+ sDefaultValues + ".");
			TaskListUnmarshallerBeans.logger.debug("TParamType : "
					+ aTParamType + ".");
		}

		final Observer aObserver = this.mapOfObserver.get(sObserverName);

		// does the requested observer exist?
		if (aObserver == null) {
			TaskListUnmarshallerBeans.logger.warn("The parameter " + sParamName
					+ "refers to a " + "non-existing observer: "
					+ sObserverName + ".");
			return null;
		}

		// the name of the parameter
		// TODO Change when the RDF is done
		final LocalizedString aLocalizedString = new LocalizedString();
		aLocalizedString.addLocalization("en", sParamName);

		final Map<String, Value> mapOfValue = new LinkedHashMap<String, Value>();

		// iterate over the observer's methods
		final Map<EnumInputMethod, InputMethod> mapOfInputMethod = aObserver
				.getMapOfInputMethod();
		for (final EnumInputMethod aEnumInputMethod : mapOfInputMethod.keySet()) {
			final InputMethod aInputMethod = mapOfInputMethod
					.get(aEnumInputMethod);

			if (aInputMethod.getCallParameter().getName().equals(sParamName)) {
				// the referenced parameter is an input one for the current
				// method, so it must be ignored
				TaskListUnmarshallerBeans.logger
						.info("The referenced parameter is an input one for the current method, so it must be ignored.");
				continue;
			}

			final CallMethod aCallMethod = aInputMethod.getCallMethod();
			final CallParameter aCallParameter = aCallMethod
					.getCallParameterByName(sParamName);
			// A parameter with this name exists for this method
			if (aCallParameter != null) {
				for (final String sValue : aCallParameter
						.getListOfPossibleValue()) {
					final Value aValueCurrent = mapOfValue.get(sValue);
					if (aValueCurrent != null) {
						// the newly created parameter already contains a
						// similar value

						// we know that the created parameter contains exactly
						// ONE mapping for this value
						// aValueCurrent.getMappings().get(sObserverName).get(0).addInputMethod(aEnumInputMethod);
					} else {
						// the newly created parameter does not contain a
						// similar value
						final LocalizedString aInternationalizedMessageValueName;
						aInternationalizedMessageValueName = new LocalizedString();
						// TODO Add localized names in RDF contract
						aInternationalizedMessageValueName.addLocalization(
								"en", sValue);

						final List<EnumInputMethod> mapOfNewInputMethod = new ArrayList<EnumInputMethod>();
						mapOfNewInputMethod.add(aEnumInputMethod);
						final Mapping aMapping = new Mapping(aObserver,
								sParamName, sValue);

						final List<Mapping> listOfMapping = new ArrayList<Mapping>();
						listOfMapping.add(aMapping);

						final Map<String, List<Mapping>> mapOfListOfMapping;
						mapOfListOfMapping = new LinkedHashMap<String, List<Mapping>>();
						mapOfListOfMapping.put(sObserverName, listOfMapping);

						final Value aValueToAdd = new Value(
								aInternationalizedMessageValueName,
								mapOfListOfMapping, sValue);
						mapOfValue.put(sValue, aValueToAdd);
					}
				}
			}
		}
		return this.createParameter(aTParamType, sParamName, aTUi,
				sDefaultValues, mapOfValue);
	}

	/**
	 * Create the tree from a TaskType element
	 * 
	 * @param myTask
	 *            the task to transform into a tree
	 * @throws UnknownObserverException 
	 */
	public TLTNode expandTree(TaskType myTask, RoutineType subroutine) throws UnknownObserverException {
		TaskListUnmarshallerBeans.logger
				.trace("Creation of the tree based on the Task "
						+ myTask.getId());
		TLTNode root = new TLTNode();
		root.setID(NodeID++);
		for (ExecType exec : subroutine.getExecList()) {
			final Observer obs = Framework.mapOfObserver.get(exec.getValue());
			if (obs == null)
				throw new UnknownObserverException("The observer " + exec.getValue() + " does not exist. Exec ID: " + exec.getId());
			root.addExec(new TLTExec(exec.getId(), obs, exec.getValue(), exec
					.getType(), exec.getParam()));
		}
		for (IfType iflist : subroutine.getIfList()) {
			root.addIf(fillIfs(myTask, iflist));
		}
		return root;
	}
	
	/**
	 * Created the Ifnode in the tree
	 * 
	 * @param ifs
	 *            the IfType node template to create the Ifnode
	 * @return the node created
	 * @throws UnknownObserverException 
	 */
	private TLTIf fillIfs(TaskType myTask, IfType ifs) throws UnknownObserverException {
		TaskListUnmarshallerBeans.logger.trace("Creation of an If ");
		// Create the if node
		TLTIf ifnode = new TLTIf();
		// Cares about the conditions
		String[] conds = ifs.getTest().split(",");

		for (String cond : conds) {
			TLTCond myCond = new TLTCond();

			for (CondType condlist : myTask.getConds().getCondList()) {
				if (condlist.getId().equals(cond)) {
					TaskListUnmarshallerBeans.logger
							.trace("Creation of a condition " + cond);
					myCond.setId(condlist.getId());
					final Observer obs = Framework.mapOfObserver.get(condlist
							.getObserver());
					myCond.setObserver(obs);
					myCond
							.setResult(condlist.getResult().equals("passed") ? true
									: false);
					myCond.setType(condlist.getType());
					myCond.setValue(condlist.getValue());

					ifnode.addCond(myCond);
					break;
				}
			}

		}

		// Add recursively the inner ifs in the then part
		if (ifs.getThen() != null) {
			TaskListUnmarshallerBeans.logger
					.trace("Call recursion for the Then ");
			ifnode.setIfOk(expandTree(myTask, ifs.getThen()));
			// Add recursively the inner if in the else part
			if (ifs.getElse() != null) {
				TaskListUnmarshallerBeans.logger
						.trace("Call recursion for the else");
				ifnode.setIfNotOk(expandTree(myTask, ifs.getElse()));
			}
		}
		return ifnode;
	}

	private org.w3c.unicorn.tasklist.parameters.Parameter createParameter(
			final TParamType.Enum aTParamType, final String sName,
			final TUi.Enum aTUi, final String sDefaultValues,
			final Map<String, Value> mapOfValue) throws ParameterException {
		TaskListUnmarshallerBeans.logger.trace("createParameter");
		if (TaskListUnmarshallerBeans.logger.isDebugEnabled()) {
			TaskListUnmarshallerBeans.logger.debug("TParamType : "
					+ aTParamType + ".");
			TaskListUnmarshallerBeans.logger.debug("Name : " + sName + ".");
			TaskListUnmarshallerBeans.logger.debug("TUi : " + aTUi + ".");
			TaskListUnmarshallerBeans.logger.debug("Default values : "
					+ sDefaultValues + ".");
			TaskListUnmarshallerBeans.logger.debug("Map of value : "
					+ mapOfValue + ".");
		}

		final org.w3c.unicorn.tasklist.parameters.Parameter aParameter = ParameterFactory
				.getParameter(aTParamType);
		if (null == aParameter) {
			return null;
		}
		aParameter.setName(sName);
		aParameter.setUiLevel(aTUi);
		aParameter.setMapOfValue(mapOfValue);
		aParameter.setDefaultValues(sDefaultValues);
		return aParameter;
	}

	/**
	 * Wraps a TInputMethod instance on an EnumInputMethod
	 * 
	 * @param aTInputMethod
	 * @return
	 */
	private static EnumInputMethod getEnumInputMethod(
			final TInputMethod.Enum aTInputMethod) {
		switch (aTInputMethod.intValue()) {
		case TInputMethod.INT_DIRECT:
			return EnumInputMethod.DIRECT;
		case TInputMethod.INT_FILE:
			return EnumInputMethod.UPLOAD;
		case TInputMethod.INT_URI:
			return EnumInputMethod.URI;
		default:
			return EnumInputMethod.URI;
		}
	}

	public Tasklist getMapOfTask() {
		TaskListUnmarshallerBeans.logger.trace("getMapOfTask");
		return this.mapOfTask;
	}

	public void addURL(URL aURL) throws IOException {
		TaskListUnmarshallerBeans.logger.trace("addURL");
		if (TaskListUnmarshallerBeans.logger.isDebugEnabled()) {
			TaskListUnmarshallerBeans.logger.debug("URL : " + aURL + ".");
		}

		try {
			this.aTaskList = TasklistDocument.Factory.parse(aURL.openStream());
		} catch (XmlException e) {
			TaskListUnmarshallerBeans.logger.error(
					"Parsing error in TasklistUnmarshaller", e);
			e.printStackTrace();
		}
	}

	public void unmarshal() {
		TaskListUnmarshallerBeans.logger.trace("unmarshal tasklist");
		// creates the tasklist without computing references
		for (final TaskType aTask : this.aTaskList.getTasklist().getTaskList()) {
			if (this.mapOfTask.containsKey(aTask.getId())) {
				TaskListUnmarshallerBeans.logger.warn("Task with id "
						+ aTask.getId() + " already defined.");
			} else {
				try {
					Framework.logger.debug("> Found task: " + aTask.getId());
					this.addTask(aTask);
					Framework.logger.debug("Successfully loaded task: " + aTask.getId());
				} catch (ParameterException e) {
					Framework.logger.error("Task \"" + aTask.getId() + "\" threw a ParameterException. This task is ignored.", e);
				} catch (UnknownObserverException e) {
					Framework.logger.error("Task \"" + aTask.getId() + "\" references an unknown observer. This task is ignored.", e);
				}
			}
		}

		// computes and replaces references by their corresponding observations
		// and parameters
		for (final org.w3c.unicorn.tasklist.Task aTask : this.mapOfTask
				.values()) {
			TaskListUnmarshallerBeans.logger.debug("Expand task : "
					+ aTask.getID() + ".");
			aTask.setTree(aTask.expandNode(mapOfTask, aTask.getTree()));
		}

	}

}

--- NEW FILE: RDFUnmarshaller.java ---
// $Id: RDFUnmarshaller.java,v 1.2 2009/08/28 12:39:54 jean-gui 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.tasklist;

import org.w3c.unicorn.util.Unmarshaller;

/**
 * @author Damien LEROY
 * 
 */
public interface RDFUnmarshaller extends Unmarshaller {

	public abstract Tasklist getMapOfTask();

	public abstract void setMapOfTask(final Tasklist mapOfTask);

}

--- NEW FILE: Tasklist.java ---
package org.w3c.unicorn.tasklist;

import java.util.LinkedHashMap;

import org.w3c.unicorn.Framework;

public class Tasklist extends LinkedHashMap<String, Task> {

	private static final long serialVersionUID = -868579272922431097L;

	private String defaultTaskId;

	public String getDefaultTaskId() {
		if (defaultTaskId == null) {
			String id = Framework.mapOfTask.values().toArray(new Task[0])[0].getID();
			Framework.logger.error("There is no default task defined in task rdf files. Returning first found id: \"" + id + "\" . Please add ucn:default=\"true\" to the default task.");
			this.defaultTaskId = id;
			return Framework.mapOfTask.values().toArray(new Task[0])[0].getID();
		}			
		else
			return defaultTaskId;
	}

	public void setDefaultTaskId(String defaultTaskId) {
		this.defaultTaskId = defaultTaskId;
	}

	@Override
	public java.lang.String toString() {
		String s = "";
		for (String key : this.keySet()) {
			s += get(key) + "\n";
		}
		return s;
	}

}

--- NEW FILE: Task.java ---
// $Id: Task.java,v 1.2 2009/08/28 12:39:54 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.tasklist;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.activation.MimeType;

import org.w3c.unicorn.contract.Observer;
import org.w3c.unicorn.tasklist.parameters.Parameter;
import org.w3c.unicorn.tasklisttree.TLTCond;
import org.w3c.unicorn.tasklisttree.TLTExec;
import org.w3c.unicorn.tasklisttree.TLTIf;
import org.w3c.unicorn.tasklisttree.TLTNode;
import org.w3c.unicorn.util.LocalizedString;

/**
 * Task<br />
 * Created: May 29, 2006 5:53:12 PM<br />
 */
public class Task {

	/**
	 * Id of the task
	 */
	private String sID;

	/**
	 * Longnames of the task
	 */
	private LocalizedString aLocalizedStringLongName;

	/**
	 * Descriptions of the task
	 */
	private LocalizedString aLocalizedStringDescription;

	/**
	 * Parameters of the task
	 */
	private Map<String, Parameter> mapOfTaskParameter;

	/**
	 * References to other tasks
	 */
	private List<String> listOfReference;

	/**
	 * Root of the execution level tree
	 */
	private TLTNode root;

	/**
	 * Creates a new Task.
	 */
	public Task() {
		this.sID = "";
		this.aLocalizedStringLongName = new LocalizedString();
		this.aLocalizedStringDescription = new LocalizedString();
		this.mapOfTaskParameter = new LinkedHashMap<String, Parameter>();
		this.listOfReference = new ArrayList<String>();
	}

	/**
	 * Allows to display the tree of execution level
	 * 
	 * @param root
	 *            the node to display
	 */
	public void displayTree(TLTNode root) {
		for (TLTExec exec : root.getExecutionList()) {
			System.out.println(exec);
		}
		for (TLTIf ifs : root.getIfList()) {
			displayTree(ifs.getIfOk());
			for (TLTCond conds : ifs.getCondArray()) {
				System.out.println(conds);
			}
			displayTree(ifs.getIfNotOk());
		}
	}

	/**
	 * Get the root of the execution level tree
	 * 
	 * @return the root of the tree
	 */
	public TLTNode getTree() {
		return this.root;
	}

	/**
	 * Set the root of the execution level tree
	 */
	public void setTree(TLTNode root) {
		this.root = root;
	}

	/**
	 * Creates a new Task.
	 * 
	 * @param aLocalizedStringDescription
	 * @param sID
	 * @param aLocalizedStringLongName
	 * @param mapOfParameter
	 * @param mapOfObservation
	 */
	public Task(final String sID,
			final LocalizedString aLocalizedStringDescription,
			final LocalizedString aLocalizedStringLongName,
			final Map<String, Parameter> mapOfParameter) {
		super();
		this.aLocalizedStringDescription = aLocalizedStringDescription;
		this.sID = sID;
		this.aLocalizedStringLongName = aLocalizedStringLongName;
		this.mapOfTaskParameter = mapOfParameter;
		this.listOfReference = new ArrayList<String>();
	}

	/**
	 * Returns the internationalized description of this task
	 * 
	 * @return Returns the description.
	 */
	public LocalizedString getDescription() {
		return this.aLocalizedStringDescription;
	}

	/**
	 * Returns a localized description of this task
	 * 
	 * @param sLang
	 *            requested locale (e.g. en, fr, zn-ch, ...)
	 * @return the localized description corresponding to the locale
	 */
	public String getDescription(final String sLang) {
		final String sDesc = this.aLocalizedStringDescription
				.getLocalization(sLang);
		if (sDesc == null) {
			return "";
		}
		return sDesc;
	}

	/**
	 * Sets the internationalized description of this task
	 * 
	 * @param aLocalizedString
	 *            The description to set.
	 */
	public void setDescription(final LocalizedString aLocalizedString) {
		this.aLocalizedStringDescription = aLocalizedString;
	}

	/**
	 * Adds a localized description to this task
	 * 
	 * @param sLang
	 *            the locale of the description
	 * @param sDesc
	 *            the localized description of this task
	 */
	public void addDescription(final String sLang, final String sDesc) {
		this.aLocalizedStringDescription.addLocalization(sLang, sDesc);
	}

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

	/**
	 * Sets the id of this task
	 * 
	 * @param sID
	 *            The id to set.
	 */
	public void setID(final String sID) {
		this.sID = sID;
	}

	/**
	 * Returns the itnernationalized long name of this task
	 * 
	 * @return Returns the longname.
	 */
	public LocalizedString getLongName() {
		return this.aLocalizedStringLongName;
	}

	/**
	 * Returns a localized long name of this task
	 * 
	 * @param sLang
	 *            requested locale (e.g. en, fr, zn-ch, ...)
	 * @return the localized long name corresponding to the locale
	 */
	public String getLongName(final String sLang) {
		final String sName = this.aLocalizedStringLongName
				.getLocalization(sLang);
		if (sName == null) {
			return "";
		}
		return sName;
	}

	/**
	 * Sets the internationalized long name of this task
	 * 
	 * @param aLocalizedString
	 *            The longname to set.
	 */
	public void setLongName(final LocalizedString aLocalizedString) {
		this.aLocalizedStringLongName = aLocalizedString;
	}

	/**
	 * Adds a localized long name to this task
	 * 
	 * @param sLang
	 *            the locale of the long name
	 * @param sLongName
	 *            the localized long name of this task
	 */
	public void addLongName(final String sLang, final String sLongName) {
		this.aLocalizedStringLongName.addLocalization(sLang, sLongName);
	}

	/**
	 * Returns the parameters list of this task
	 * 
	 * @return Returns the parameters.
	 */
	public Map<String, Parameter> getMapOfParameter() {
		return this.mapOfTaskParameter;
	}
	
	public Map<String, Parameter> getMapOfParameter(String uiLevel) {
		//Map<String, Parameter> map = new LinkedHashMap<String, Parameter>();
		Hashtable <String, Parameter> map = new Hashtable <String, Parameter>();
		for (String key : mapOfTaskParameter.keySet()) {
			if (mapOfTaskParameter.get(key).getUiLevel().toString().equals(uiLevel)) {
				map.put(key, mapOfTaskParameter.get(key));
			}
		}
		return map;
	}

	/**
	 * Sets the parameters list of this task
	 * 
	 * @param mapOfParameter
	 *            The parameters to set.
	 */
	public void setMapOfParameter(final Map<String, Parameter> mapOfParameter) {
		this.mapOfTaskParameter = mapOfParameter;
	}

	/**
	 * Adds a parameter to this task
	 * 
	 * @param aParameter
	 */
	public void addParameter(final Parameter aParameter) {
		this.mapOfTaskParameter.put(aParameter.getName(), aParameter);
	}

	/**
	 * Returns a list of tasknames referenced bye this task
	 * 
	 * @return Returns the references.
	 */
	public List<String> getListOfReference() {
		return this.listOfReference;
	}

	public List<Observer> getAllObservers() {
		if (this.getTree() != null) {
			return this.getTree().getAllObservers();
		}
		return new ArrayList<Observer>();
	}

	// MimeType's equals() doesn't work as expected
	// so it's easier to store the String representation
	// of mime types :-/
	public List<String> getSupportedMimeTypes() {
		List<String> res = new ArrayList<String>();
		List<Observer> observers = getAllObservers();
		for (Observer o : observers) {
			List<MimeType> mimes = o.getSupportedMimeTypes();
			for (MimeType m : mimes) {
				if (!res.contains(m.toString())) {
					res.add(m.toString());
				}
			}
		}
		return res;
	}

	public void mergeSubtask(final Map<String, Task> mapOfTask, Task subtask) {
		for (TLTExec exec : subtask.getTree().getExecutionList()) {
			if (exec.getType().equals("observation")) {
				this.root.addExec(exec);
			} else if (exec.getType().equals("subtask")) {
				Task newTask = mapOfTask.get(exec.getValue());
				newTask.expandNode(mapOfTask, newTask.getTree());
				mergeSubtask(mapOfTask, newTask);
			}

		}
		for (TLTIf tltIf : subtask.getTree().getIfList()) {
			this.root.addIf(tltIf);
		}
	}

	/**
	 * 
	 */
	public TLTNode expandNode(final Map<String, Task> mapOfTask, TLTNode aRoot) {
		aRoot.bExpandingOrExpanded = true;

		TLTNode finalRoot = new TLTNode();

		for (TLTExec exec : aRoot.getExecutionList()) {
			if (exec.getType().equals("subtask")) {
				finalRoot = mergeNode(mapOfTask, finalRoot, mapOfTask.get(
						exec.getValue()).getTree());
			} else if (exec.getType().equals("observation")) {
				finalRoot.addExec(exec);
			}
		}

		for (TLTIf tltIf : aRoot.getIfList()) {
			tltIf = expandIf(mapOfTask, tltIf);
			finalRoot.addIf(tltIf);
		}

		return finalRoot;
	}

	public TLTNode mergeNode(final Map<String, Task> mapOfTask,
			TLTNode firstNode, TLTNode secondNode) {
		TLTNode finalNode = firstNode;
		for (TLTExec exec : secondNode.getExecutionList()) {
			if (exec.getType().equals("observation")) {
				finalNode.addExec(exec);
			} else if (exec.getType().equals("subtask")) {
				TLTNode newNode = mapOfTask.get(exec.getValue()).getTree();
				if (!mapOfTask.get(exec.getValue()).getTree().bExpandingOrExpanded) {
					newNode = expandNode(mapOfTask, mapOfTask.get(
							exec.getValue()).getTree());
				}
				finalNode = mergeNode(mapOfTask, finalNode, newNode);
			}
		}
		for (TLTIf tltIf : secondNode.getIfList()) {
			tltIf = expandIf(mapOfTask, tltIf);
			finalNode.addIf(tltIf);
		}
		return finalNode;
	}

	public TLTIf expandIf(final Map<String, Task> mapOfTask, TLTIf tltIf) {
		if (!tltIf.getIfOk().bExpandingOrExpanded) {
			TLTNode tltIfOk = expandNode(mapOfTask, tltIf.getIfOk());
			tltIf.setIfOk(tltIfOk);
		}
		if (!tltIf.getIfNotOk().bExpandingOrExpanded) {
			TLTNode tltIfNotOk = expandNode(mapOfTask, tltIf.getIfNotOk());
			tltIf.setIfNotOk(tltIfNotOk);
		}
		return tltIf;
	}

	/**
	 * Adds a reference to another task
	 * 
	 * @param sReference
	 *            the referenced task
	 */
	public void addReference(final String sReference) {
		this.listOfReference.add(sReference);
	}

	@Override
	public String toString() {
		final int iStringBufferSize = 5000;
		final String sVariableSeparator = "\n";
		final StringBuffer aStringBuffer = new StringBuffer(iStringBufferSize);
		
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append(sID);
		aStringBuffer.append(sVariableSeparator);
		aStringBuffer.append("\tparameters:\n"); //.append(this.mapOfTaskParameter);
		for (String key : mapOfTaskParameter.keySet()) {
			Parameter param = mapOfTaskParameter.get(key);
			aStringBuffer.append("\t\t" + key + " (" + param.getUiLevel() + "-" + param.getType() + ") => \n");
			for (String key1: param.getMapOfValue().keySet()) {
				aStringBuffer.append("\t\t\t" + key1 + " => " + mapOfTaskParameter.get(key).getMapOfValue().get(key1) + "\n");
			}
		}
		aStringBuffer.append("\treferences:").append(this.listOfReference);

		return aStringBuffer.toString();
	}

}

--- NEW FILE: TasksListUnmarshaller.java ---
// $Id: TasksListUnmarshaller.java,v 1.2 2009/08/28 12:39:54 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.tasklist;

import org.w3c.unicorn.util.Unmarshaller;

/**
 * TasksListUnmarshaller<br />
 * Created: May 30, 2006 5:54:45 PM<br />
 * Interface used to retreive a list of tasks
 * 
 * @author Jean-Guilhem ROUEL
 */
public interface TasksListUnmarshaller extends Unmarshaller {

	/**
	 * Returns the map of tasks.
	 * 
	 * @return Returns the tasks.
	 */
	public abstract Tasklist getMapOfTask();

}
Received on Friday, 28 August 2009 12:40:07 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:17:38 UTC