Index: src/nongnu/cashews/language/process/AtomicProcess.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/language/process/AtomicProcess.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 AtomicProcess.java --- src/nongnu/cashews/language/process/AtomicProcess.java 4 May 2005 07:31:55 -0000 1.1 +++ src/nongnu/cashews/language/process/AtomicProcess.java 4 May 2005 22:03:45 -0000 @@ -21,6 +21,9 @@ package nongnu.cashews.language.process; +import java.net.URI; +import java.net.URISyntaxException; + import nongnu.cashews.language.grounding.Grounding; /** @@ -43,5 +46,29 @@ public class AtomicProcess */ private Grounding grounding; + /** + * Constructs a new AtomicProcess with the specified + * name. + * + * @param name the name for this atomic process. + * @throws URISyntaxException if the supplied name is not a valid URI. + */ + public AtomicProcess(String name) + throws URISyntaxException + { + super(name); + } + + /** + * Constructs a new AtomicProcess with the specified + * name. + * + * @param name the name for this atomic process. + */ + public AtomicProcess(URI name) + { + super(name); + } + } Index: src/nongnu/cashews/language/process/CProcess.java =================================================================== RCS file: src/nongnu/cashews/language/process/CProcess.java diff -N src/nongnu/cashews/language/process/CProcess.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/nongnu/cashews/language/process/CProcess.java 4 May 2005 22:03:45 -0000 @@ -0,0 +1,33 @@ +/* CProcess.java -- Marks a class as a possible construct for composites. + Copyright (C) 2005 The University of Sheffield. + + This file is part of the CASheW-s editor. + + The CASheW-s editor is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + The CASheW-s editor is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with The CASheW-s editor; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. +*/ + +package nongnu.cashews.language.process; + +/** + * Marks this class as a possible control construct for + * CompositeProcesses. + * + * @author Andrew John Hughes (address@hidden) + * @see CompositeProcess + */ +public interface CProcess +{ +} Index: src/nongnu/cashews/language/process/CompositeProcess.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/language/process/CompositeProcess.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 CompositeProcess.java --- src/nongnu/cashews/language/process/CompositeProcess.java 4 May 2005 07:31:55 -0000 1.1 +++ src/nongnu/cashews/language/process/CompositeProcess.java 4 May 2005 22:03:45 -0000 @@ -21,8 +21,12 @@ package nongnu.cashews.language.process; +import java.net.URI; +import java.net.URISyntaxException; import java.util.List; +import nongnu.cashews.xml.Xmlizable; + /** *

* Represents the amalgamation of one or more Performances @@ -40,11 +44,19 @@ import java.util.List; * * @author Andrew John Hughes (address@hidden) */ -public abstract class CompositeProcess +public class CompositeProcess extends Process + implements Xmlizable { /** + * The control structure which defines how this process operates. + * + * @serial the control structure. + */ + private CProcess controlStructure; + + /** * The consumers for this composite process. * * @serial the consumers. @@ -58,5 +70,57 @@ public abstract class CompositeProcess */ private List producers; + /** + * Constructs a new CompositeProcess with the specified + * name. + * + * @param name the name for this composite process. + * @throws URISyntaxException if the supplied name is not a valid URI. + */ + public CompositeProcess(String name) + throws URISyntaxException + { + super(name); + } + + /** + * Constructs a new CompositeProcess with the specified + * name. + * + * @param name the name for this composite process. + */ + public CompositeProcess(URI name) + { + super(name); + } + + /** + * Sets the control structure used by this composite process. + * + * @param structure the structure to use. + */ + public void setControlStructure(CProcess structure) + { + controlStructure = structure; + } + + /** + * Returns a String representation of this process. + * + * @return a textual representation. + */ + public String toString() + { + String superString = super.toString(); + return superString.substring(0, superString.length() - 1) + + ", controlStructure=" + + controlStructure + + ", consumers=" + + consumers + + ", producers=" + + producers + + "]"; + } + } Index: src/nongnu/cashews/language/process/IfThenElse.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/language/process/IfThenElse.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 IfThenElse.java --- src/nongnu/cashews/language/process/IfThenElse.java 4 May 2005 07:31:55 -0000 1.1 +++ src/nongnu/cashews/language/process/IfThenElse.java 4 May 2005 22:03:45 -0000 @@ -32,7 +32,7 @@ import nongnu.cashews.language.expressio * @see nongnu.cashews.language.expression.Expression */ public class IfThenElse - extends CompositeProcess + implements CProcess { /** Index: src/nongnu/cashews/language/process/Loop.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/language/process/Loop.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 Loop.java --- src/nongnu/cashews/language/process/Loop.java 4 May 2005 07:31:55 -0000 1.1 +++ src/nongnu/cashews/language/process/Loop.java 4 May 2005 22:03:45 -0000 @@ -31,7 +31,7 @@ import nongnu.cashews.language.expressio * @see CompositeProcess */ public class Loop - extends CompositeProcess + implements CProcess { /** Index: src/nongnu/cashews/language/process/MultiPerform.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/language/process/MultiPerform.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 MultiPerform.java --- src/nongnu/cashews/language/process/MultiPerform.java 4 May 2005 07:31:55 -0000 1.1 +++ src/nongnu/cashews/language/process/MultiPerform.java 4 May 2005 22:03:45 -0000 @@ -23,6 +23,8 @@ package nongnu.cashews.language.process; import java.util.List; +import nongnu.cashews.xml.Xmlizable; + /** * A generic superclass for composite processes which handle an arbitrary * length list of Performances, interleaved with @@ -33,7 +35,7 @@ import java.util.List; * @see Connection */ public abstract class MultiPerform - extends CompositeProcess + implements CProcess, Xmlizable { /** Index: src/nongnu/cashews/language/process/Process.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/language/process/Process.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 Process.java --- src/nongnu/cashews/language/process/Process.java 4 May 2005 07:31:55 -0000 1.1 +++ src/nongnu/cashews/language/process/Process.java 4 May 2005 22:03:45 -0000 @@ -22,6 +22,7 @@ package nongnu.cashews.language.process; import java.net.URI; +import java.net.URISyntaxException; /** * Takes a set of inputs, does some processing and returns a set of @@ -49,5 +50,62 @@ public abstract class Process */ private URI name; + /** + * Constructs a new Process with the specified name. + * + * @param name the name of the process. + * @throws URISyntaxException if the supplied name is not a valid URI. + */ + public Process(String name) + throws URISyntaxException + { + setName(name); + } + + /** + * Constructs a new Process with the specified name. + * + * @param name the name of the process. + */ + public Process(URI name) + { + setName(name); + } + + /** + * Sets the name of this process to that specified. + * + * @param name the name of the process. + * @throws URISyntaxException if the supplied name is not a valid URI. + */ + public void setName(String name) + throws URISyntaxException + { + setName(new URI(name)); + } + + /** + * Sets the name of this process to that specified. + * + * @param name the name of the process. + */ + public void setName(URI name) + { + this.name = name; + } + + /** + * Returns a String representation of this process. + * + * @return a textual representation. + */ + public String toString() + { + return getClass().getName() + + "[name=" + + name + + "]"; + } + } Index: src/nongnu/cashews/language/process/ValueCollector.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/language/process/ValueCollector.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 ValueCollector.java --- src/nongnu/cashews/language/process/ValueCollector.java 4 May 2005 07:31:55 -0000 1.1 +++ src/nongnu/cashews/language/process/ValueCollector.java 4 May 2005 22:03:45 -0000 @@ -47,7 +47,7 @@ public class ValueCollector * * @serial the collector's width. */ - private int collectorWidth; + private int width; /** * The function which provides the input. Index: src/nongnu/cashews/xml/Serializer.java =================================================================== RCS file: src/nongnu/cashews/xml/Serializer.java diff -N src/nongnu/cashews/xml/Serializer.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/nongnu/cashews/xml/Serializer.java 4 May 2005 22:03:45 -0000 @@ -0,0 +1,212 @@ +/* Serializer.java -- Recursively serializes an object as XML. + Copyright (C) 2005 The University of Sheffield. + + This file is part of the CASheW-s editor. + + The CASheW-s editor is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + The CASheW-s editor is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with The CASheW-s editor; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. +*/ + +package nongnu.cashews.xml; + +import java.lang.reflect.Field; + +import java.net.URISyntaxException; + +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import nongnu.cashews.language.process.CompositeProcess; +import nongnu.cashews.language.process.Sequence; +import nongnu.cashews.xml.schema.TypeMapper; +import nongnu.cashews.xml.schema.XsdType; + +import org.w3c.dom.Document; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSSerializer; + +/** + * Generically serializes an object to an XML tree. + * + * @author Andrew John Hughes (address@hidden) + */ +public class Serializer +{ + + /** + * Implementation of the DOM for use within this class. + */ + private static DOMImplementation domImpl; + + /** + * Initialize the DOM implementation (one time operation only). + * + * @throws InstantiationException if the implementation class couldn't + * be instantiated. + * @throws IllegalAccessException if the implementation class can't be + * accessed. + * @throws ClassNotFoundException if the implementation class can't be + * found. + */ + private static void initializeImpl() + throws InstantiationException, IllegalAccessException, + ClassNotFoundException + { + if (domImpl == null) + { + DOMImplementationRegistry registry = + DOMImplementationRegistry.newInstance(); + domImpl = registry.getDOMImplementation("LS 3.0"); + } + } + + /** + * Serializes the specified object into an XML tree. If the supplied + * document argument is not null, the resulting tree + * is appended it. Otherwise, a new document is created. + * + * @param object the object to serialize. + * @param root the root node to append to, or null + * if a new root node should be created. + * @param document the document to use for creation. + * @return the serialized object in XML form. + * @throws IllegalAccessException if a field can't be accessed. + */ + public static Node serialize(Xmlizable object, Node root, + Document document) + throws IllegalAccessException + { + List fields = new LinkedList(); + Class clazz = object.getClass(); + Element objRoot = createElement(document, clazz.getSimpleName()); + while (clazz != null) + { + fields.addAll(0, Arrays.asList(clazz.getDeclaredFields())); + clazz = clazz.getSuperclass(); + } + for (Field field: fields) + { + Object value = field.get(object); + if (value == null) + continue; + if (value instanceof Collection) + { + Collection collection = (Collection) value; + for (Object obj : collection) + { + if (obj instanceof Xmlizable) + serialize((Xmlizable) value, objRoot, document); + } + } + else if (value instanceof Xmlizable) + serialize((Xmlizable) value, objRoot, document); + else + { + Element element = createElement(document, field.getName()); + serializeValue(document, element, value); + objRoot.appendChild(element); + } + } + if (root != null) + { + root.appendChild(objRoot); + return root; + } + return objRoot; + } + + /** + * Converts an XML document to a String. + * + * @param document the document to convert. + */ + public static String convertDocumentToString(Document document) + { + DOMImplementationLS loadAndSave = (DOMImplementationLS) domImpl; + LSSerializer serializer = loadAndSave.createLSSerializer(); + return serializer.writeToString(document); + } + + /** + * Serializes a reflected class field and its contents into an XML element. + * An XML schema datatype is searched for first, in order to provide + * the most appropriate translation. If this fails, the + * toString() content is used. + * + * @param document the document instance for creating further XML nodes. + * @param element the element to serialize to. + * @param value the value to serialize. + * @return the serialized element. + */ + private static void serializeValue(Document document, Element element, + Object value) + { + TypeMapper mapper = new TypeMapper(); + Class clazz = value.getClass(); + XsdType schemaType = mapper.map(clazz); + if (schemaType == null) + element.appendChild(document.createTextNode(value.toString())); + else + element.appendChild(schemaType.translateValue(document, value)); + } + + /** + * Creates an element with the appropriate naming schema. + * + * @param document the document for creating elements. + * @param name the proposed document name. + * @return the created element. + */ + private static Element createElement(Document document, String name) + { + char firstChar = name.charAt(0); + if (Character.isUpperCase(firstChar)) + name = Character.toLowerCase(firstChar) + + name.substring(1, name.length()); + return document.createElement(name); + } + + /** + * A simple test harness to ensure that objects can be successfully + * converted to XML. + * + * @param args the command-line arguments. + * @throws InstantiationException if a class couldn't be + * instantiated. + * @throws IllegalAccessException if a class can't be accessed. + * @throws ClassNotFoundException if a class can't be found. + * @throws URISyntaxException if one of the names is not a valid URI. + */ + public static void main(String[] args) + throws InstantiationException, IllegalAccessException, + ClassNotFoundException, URISyntaxException + { + CompositeProcess process = new CompositeProcess("MyProcess"); + Sequence sequence = new Sequence(); + process.setControlStructure(sequence); + initializeImpl(); + Document document = domImpl.createDocument(null,null,null); + document.appendChild(Serializer.serialize(process, null, document)); + System.out.println(convertDocumentToString(document)); + } + +} + Index: src/nongnu/cashews/xml/Xmlizable.java =================================================================== RCS file: src/nongnu/cashews/xml/Xmlizable.java diff -N src/nongnu/cashews/xml/Xmlizable.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/nongnu/cashews/xml/Xmlizable.java 4 May 2005 22:03:45 -0000 @@ -0,0 +1,34 @@ +/* CProcess.java -- Marks a class as a possible construct for composites. + Copyright (C) 2005 The University of Sheffield. + + This file is part of the CASheW-s editor. + + The CASheW-s editor is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + The CASheW-s editor is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with The CASheW-s editor; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. +*/ + +package nongnu.cashews.xml; + +/** + * Specifies that a class can be converted to an XML tree. The methods + * specified by this class allow additional information to be specified + * to the serializer. + * + * @author Andrew John Hughes (address@hidden) + * @see Serializer + */ +public interface Xmlizable +{ +} Index: src/nongnu/cashews/xml/schema/TypeMapper.java =================================================================== RCS file: src/nongnu/cashews/xml/schema/TypeMapper.java diff -N src/nongnu/cashews/xml/schema/TypeMapper.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/nongnu/cashews/xml/schema/TypeMapper.java 4 May 2005 22:03:45 -0000 @@ -0,0 +1,65 @@ +/* TypeMapper.java -- Maps Java types to XML schema types. + Copyright (C) 2005 The University of Sheffield. + + This file is part of the CASheW-s editor. + + The CASheW-s editor is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + The CASheW-s editor is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with The CASheW-s editor; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. +*/ + +package nongnu.cashews.xml.schema; + +import java.lang.reflect.Type; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import nongnu.cashews.xml.schema.datatypes.AnyUri; + +/** + * Maps a Java data type to an XML schema data type. + * + * @author Andrew John Hughes (address@hidden) + */ +public class TypeMapper +{ + + /** + * Map for the built-in XML schema datatypes. + */ + private static Map> builtInTypes; + + /** + * Initializer for the static map. + */ + static + { + builtInTypes = new HashMap>(); + builtInTypes.put(URI.class, new AnyUri()); + } + + /** + * Maps a Java Type to its corresponding + * XSD type. + * + * @param type the Java type to map, or null + * if no mapping exists. + */ + public XsdType map(Type type) + { + return builtInTypes.get(type); + } + +} Index: src/nongnu/cashews/xml/schema/XsdType.java =================================================================== RCS file: src/nongnu/cashews/xml/schema/XsdType.java diff -N src/nongnu/cashews/xml/schema/XsdType.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/nongnu/cashews/xml/schema/XsdType.java 4 May 2005 22:03:45 -0000 @@ -0,0 +1,45 @@ +/* XsdType.java -- Representation of an XML schema datatype. + Copyright (C) 2005 The University of Sheffield. + + This file is part of the CASheW-s editor. + + The CASheW-s editor is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + The CASheW-s editor is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with The CASheW-s editor; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. +*/ + +package nongnu.cashews.xml.schema; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +/** + * Specifies that a class represents an XML schema data type. + * + * @author Andrew John Hughes (address@hidden) + */ +public interface XsdType +{ + + /** + * Translates a value represented in a Java data type + * to the appropriate value in this XML schema data type. + * + * @param document the XML document to use to create the XML tree. + * @param value the value to translate. + * @return an XML tree node representing the type. + */ + Node translateValue(Document document, T value); + +} Index: src/nongnu/cashews/xml/schema/datatypes/AnyUri.java =================================================================== RCS file: src/nongnu/cashews/xml/schema/datatypes/AnyUri.java diff -N src/nongnu/cashews/xml/schema/datatypes/AnyUri.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/nongnu/cashews/xml/schema/datatypes/AnyUri.java 4 May 2005 22:03:45 -0000 @@ -0,0 +1,52 @@ +/* AnyUri.java -- Represents the XML schema type, xsd:anyURI. + Copyright (C) 2005 The University of Sheffield. + + This file is part of the CASheW-s editor. + + The CASheW-s editor is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + The CASheW-s editor is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with The CASheW-s editor; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. +*/ + +package nongnu.cashews.xml.schema.datatypes; + +import java.net.URI; + +import nongnu.cashews.xml.schema.XsdType; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +/** + * Represents the XML schema datatype, xsd:anyURI. + * + * @author Andrew John Hughes (address@hidden) + */ +public class AnyUri + implements XsdType +{ + + /** + * Translates the supplied Java URI into a value + * of the XML schema datatype, xsd:anyURI. + * @param document the XML document to use to create the XML tree. + * @param value the URI value to translate. + * @return an XML tree node representing the type. + */ + public Node translateValue(Document document, URI value) + { + return document.createTextNode(value.toString()); + } + +}