- From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
- Date: Tue, 11 Aug 2009 16:05:42 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/tasklist In directory hutz:/tmp/cvs-serv2609/src/org/w3c/unicorn/tasklist Added Files: Tag: dev2 TasksListUnmarshaller.java .cvsignore TaskListUnmarshallerBeans.java RDFUnmarshallerJena.java Task.java RDFUnmarshaller.java Log Message: all initialization actions in Init.java + compatibility windows/linux --- NEW FILE: .cvsignore --- Test.java --- NEW FILE: RDFUnmarshallerJena.java --- // $Id: RDFUnmarshallerJena.java,v 1.1.2.1 2009/08/11 16:05:40 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.tasklist; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.unicorn.contract.Observer; import org.w3c.unicorn.tasklist.parameters.Mapping; 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("org.w3c.unicorn.tasklist"); 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_HASMAPPING = null; private static Property PROPERTY_HASPARAMETER = null; private static Property PROPERTY_HASVALUE = null; private static Property PROPERTY_LONGNAME = null; private static Property PROPERTY_OBSERVER = 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; public static void init() throws FileNotFoundException { FileReader fr = new FileReader(org.w3c.unicorn.util.Property .get("TASKLIST_RDF_MODEL")); RDFUnmarshallerJena.MODEL.read(fr, null); //RDFUnmarshallerJena.MODEL.read(org.w3c.unicorn.util.Property // .get("TASKLIST_RDF_MODEL"), null); // define resource use to find information into the RDF graph RDFUnmarshallerJena.RESOURCE_TASK = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "Task"); // define property use to find information into the RDF graph RDFUnmarshallerJena.PROPERTY_DESCRIPTION = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "description"); RDFUnmarshallerJena.PROPERTY_HASMAPPING = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "hasMapping"); RDFUnmarshallerJena.PROPERTY_HASPARAMETER = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "hasParameter"); RDFUnmarshallerJena.PROPERTY_HASVALUE = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "hasValue"); RDFUnmarshallerJena.PROPERTY_LONGNAME = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "longName"); RDFUnmarshallerJena.PROPERTY_OBSERVER = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "observer"); RDFUnmarshallerJena.PROPERTY_PARAMETER = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "parameter"); RDFUnmarshallerJena.PROPERTY_REFERENCE = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "reference"); RDFUnmarshallerJena.PROPERTY_TYPE = RDFUnmarshallerJena.MODEL .getProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); RDFUnmarshallerJena.PROPERTY_VALUE = RDFUnmarshallerJena.MODEL .getProperty(RDFUnmarshallerJena.UCN_NAMESPACE + "value"); } private Map<String, Observer> mapOfObserver = null; private Map<String, Task> 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"); if (RDFUnmarshallerJena.logger.isDebugEnabled()) { 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 mapping to the given value. * * @param value * The value to map. * @param aMapping * The mapping for the value. */ private void addMapping(final Value aValue, final Resource aMapping) { RDFUnmarshallerJena.logger.trace("addMapping"); if (RDFUnmarshallerJena.logger.isDebugEnabled()) { RDFUnmarshallerJena.logger.debug("Value : " + aValue.getName() + "."); RDFUnmarshallerJena.logger.debug("Mapping : " + aMapping.getLocalName() + "."); } // TODO verifier ce brol final String sObserver = aMapping.getProperty( RDFUnmarshallerJena.PROPERTY_OBSERVER).getLiteral().getString(); final String sParameter = aMapping.getProperty( RDFUnmarshallerJena.PROPERTY_PARAMETER).getLiteral() .getString(); final String sValue = aMapping.getProperty( RDFUnmarshallerJena.PROPERTY_VALUE).getLiteral().getString(); final Mapping oMapping = new Mapping(); oMapping.setObserver(this.mapOfObserver.get(sObserver)); oMapping.setParam(sParameter); oMapping.setValue(sValue); } /** * 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_VALUE).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 // TODO completer l'ajout de Value // find and add mapping of the Value for (final StmtIterator siMapping = aValue .listProperties(RDFUnmarshallerJena.PROPERTY_HASMAPPING); siMapping .hasNext();) { final Resource aMapping = (Resource) siMapping.nextStatement() .getObject(); this.addMapping(oValue, aMapping); } // find and add mapping 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 RDFUnmarshallerJena.logger.error("No task with reference : " + aReference.getLiteral().getString() + "."); throw new Exception("No task with reference : " + aReference.getLiteral().getString() + "."); } 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 observers. */ /* * public Map<String, Observer> getMapOfObserver () { return * this.mapOfObserver; } */ /** * @param mapOfTask * The observers to set. */ public void setMapOfObserver(final Map<String, Observer> mapOfObserver) { this.mapOfObserver = mapOfObserver; } /** * @return Returns the tasks. */ public Map<String, Task> getMapOfTask() { return this.mapOfTask; } /** * @param mapOfTask * The tasks to set. */ public void setMapOfTask(final Map<String, Task> mapOfTask) { this.mapOfTask = mapOfTask; } } --- 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.ThenType; 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.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("org.w3c.unicorn.tasklist"); private int NodeID = 0; /** * The tasklist corresponding to the xml file */ private Map<String, Task> 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.mapOfObserver = mapOfObserver; } private void addTask(final TaskType aTask) throws ParameterException { TaskListUnmarshallerBeans.logger.trace("addTask"); if (aTask == null) { TaskListUnmarshallerBeans.logger.warn("Task : null"); return; } if (TaskListUnmarshallerBeans.logger.isDebugEnabled()) { 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)); // parameters final ParametersType aParameters = aTask.getParameters(); if (aParameters != null) { final ParameterType[] listOfParameter; listOfParameter = aParameters.getParameterArray(); for (final ParameterType aParameterBeans : listOfParameter) { 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 .getValueArray()) { // 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 .getMappedArray()) { 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 */ public TLTNode ExpandTree(TaskType myTask) { TaskListUnmarshallerBeans.logger .trace("Creation of the tree based on the Task " + myTask.getId()); TLTNode root = new TLTNode(); root.setID(NodeID++); for (ExecType exec : myTask.getRoutine().getExecArray()) { final Observer obs = Framework.mapOfObserver.get(exec.getValue()); root.addExec(new TLTExec(exec.getId(), obs, exec.getValue(), exec .getType(), exec.getParam())); } for (IfType iflist : myTask.getRoutine().getIfArray()) { root.addIf(fillIfs(myTask, iflist)); } return root; } /** * Recursion over the Then tag to fill the tree * * @param myThen * the ThenType node for the recursion * @return the node created */ private TLTNode FillNode(TaskType myTask, ThenType myThen) { TaskListUnmarshallerBeans.logger.trace("Creation of a then branch "); TLTNode node = new TLTNode(); node.setID(NodeID++); for (ExecType exec : myThen.getExecArray()) { final Observer obs = Framework.mapOfObserver.get(exec.getValue()); node.addExec(new TLTExec(exec.getId(), obs, exec.getValue(), exec .getType(), exec.getParam())); } for (IfType iflist : myThen.getIfArray()) { node.addIf(fillIfs(myTask, iflist)); } return node; } /** * Created the Ifnode in the tree * * @param ifs * the IfType node template to create the Ifnode * @return the node created */ private TLTIf fillIfs(TaskType myTask, IfType ifs) { 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().getCondArray()) { 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(FillNode(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(FillNode(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 Map<String, org.w3c.unicorn.tasklist.Task> 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() throws Exception { TaskListUnmarshallerBeans.logger.trace("unmarshal tasklist"); // creates the tasklist without computing references for (final TaskType aTask : this.aTaskList.getTasklist().getTaskArray()) { if (this.mapOfTask.containsKey(aTask.getId())) { TaskListUnmarshallerBeans.logger.warn("Task with id " + aTask.getId() + " already defined."); } else { this.addTask(aTask); } } // 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.1.2.1 2009/08/11 16:05:40 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.tasklist; import java.util.Map; import org.w3c.unicorn.contract.Observer; import org.w3c.unicorn.util.Unmarshaller; /** * @author Damien LEROY * */ public interface RDFUnmarshaller extends Unmarshaller { public abstract Map<String, Task> getMapOfTask(); public abstract void setMapOfObserver( final Map<String, Observer> mapOfObserver); public abstract void setMapOfTask(final Map<String, Task> mapOfTask); } --- NEW FILE: Task.java --- // $Id: Task.java,v 1.1.2.1 2009/08/11 16:05:40 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.tasklist; import java.util.ArrayList; 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; } /** * 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("ID:").append(sID); aStringBuffer.append(sVariableSeparator); aStringBuffer.append(sVariableSeparator); aStringBuffer.append("parameters:").append(this.mapOfTaskParameter); aStringBuffer.append(sVariableSeparator); aStringBuffer.append("references:").append(this.listOfReference); return aStringBuffer.toString(); } } --- NEW FILE: TasksListUnmarshaller.java --- // $Id: TasksListUnmarshaller.java,v 1.1.2.1 2009/08/11 16:05:39 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.tasklist; import java.util.Map; 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 Map<String, org.w3c.unicorn.tasklist.Task> getMapOfTask(); }
Received on Tuesday, 11 August 2009 16:08:57 UTC