Index: src/nongnu/cashews/rdf/Graph.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/Graph.java,v retrieving revision 1.4 diff -u -3 -p -u -r1.4 Graph.java --- src/nongnu/cashews/rdf/Graph.java 28 Mar 2005 19:29:21 -0000 1.4 +++ src/nongnu/cashews/rdf/Graph.java 4 Apr 2005 00:35:33 -0000 @@ -22,6 +22,7 @@ package nongnu.cashews.rdf; import java.io.Serializable; +import java.util.HashSet; import java.util.Set; /** @@ -55,6 +56,14 @@ public class Graph private Set graph; /** + * Constructs a new empty graph. + */ + public Graph() + { + graph = new HashSet(); + } + + /** * Returns true if the specified object is either of type Graph, * or a sub-type, and contains an equivalent set of triples. If the specified * object is null, false is returned. @@ -88,13 +97,39 @@ public class Graph } /** - * Returns the graph of RDF triples. + * Returns a clone of the graph of RDF triples. * - * @return a graph of RDF triples. + * @return a clone of the RDF triple graph. */ public Set getGraph() { - return graph; + Set clonedSet = new HashSet(); + for (Triple triple : graph) + clonedSet.add(triple.clone()); + return clonedSet; + } + + /** + * Adds a triple to the graph. + * + * @param triple the triple to add. + */ + public void addTriple(Triple triple) + { + graph.add(triple); } + /** + * Returns a textual representation of the graph. + * + * @return a textual representation. + */ + public String toString() + { + return getClass().getName() + + "[graph = " + + graph + + "]"; + } + } Index: src/nongnu/cashews/rdf/Literal.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/Literal.java,v retrieving revision 1.3 diff -u -3 -p -u -r1.3 Literal.java --- src/nongnu/cashews/rdf/Literal.java 3 Apr 2005 17:15:39 -0000 1.3 +++ src/nongnu/cashews/rdf/Literal.java 4 Apr 2005 00:35:33 -0000 @@ -85,4 +85,34 @@ public class Literal "]"; } + /** + * Returns a deep copy of this literal. + * + * @return a clone of the literal. + */ + public Literal clone() + { + try + { + Object clonedObject = super.clone(); + Literal clone = (Literal) clonedObject; + clone.setType(type.clone()); + return clone; + } + catch (CloneNotSupportedException e) + { + throw new IllegalStateException("Unexpected exception: " + e, e); + } + } + + /** + * Sets the type used by the literal to the one specified. + * + * @param type the new type to use. + */ + public void setType(Type type) + { + this.type = type; + } + } Index: src/nongnu/cashews/rdf/Predicate.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/Predicate.java,v retrieving revision 1.2 diff -u -3 -p -u -r1.2 Predicate.java --- src/nongnu/cashews/rdf/Predicate.java 28 Mar 2005 19:29:21 -0000 1.2 +++ src/nongnu/cashews/rdf/Predicate.java 4 Apr 2005 00:35:33 -0000 @@ -30,6 +30,14 @@ package nongnu.cashews.rdf; * @see RDFURI */ public interface Predicate + extends Cloneable { + /** + * Returns a deep copy of the predicate. + * + * @return a deep copy of the predicate. + */ + Predicate clone(); + } Index: src/nongnu/cashews/rdf/RDFObject.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/RDFObject.java,v retrieving revision 1.2 diff -u -3 -p -u -r1.2 RDFObject.java --- src/nongnu/cashews/rdf/RDFObject.java 28 Mar 2005 19:29:21 -0000 1.2 +++ src/nongnu/cashews/rdf/RDFObject.java 4 Apr 2005 00:35:33 -0000 @@ -33,7 +33,14 @@ package nongnu.cashews.rdf; * @see Literal */ public interface RDFObject - extends Node + extends Node, Cloneable { + /** + * Returns a deep copy of the object. + * + * @return a deep copy of the object. + */ + RDFObject clone(); + } Index: src/nongnu/cashews/rdf/RDFURI.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/RDFURI.java,v retrieving revision 1.3 diff -u -3 -p -u -r1.3 RDFURI.java --- src/nongnu/cashews/rdf/RDFURI.java 3 Apr 2005 17:15:39 -0000 1.3 +++ src/nongnu/cashews/rdf/RDFURI.java 4 Apr 2005 00:35:33 -0000 @@ -51,7 +51,7 @@ public class RDFURI */ public RDFURI(URI uri) { - this.uri = uri; + setURI(uri); } /** @@ -84,4 +84,34 @@ public class RDFURI } } + /** + * Returns a deep copy of this RDF URI. + * + * @return a clone of the RDF URI. + */ + public RDFURI clone() + { + try + { + Object clonedObject = super.clone(); + RDFURI clone = (RDFURI) clonedObject; + clone.setURI(getURI()); + return clone; + } + catch (CloneNotSupportedException e) + { + throw new IllegalStateException("Unexpected exception: " + e, e); + } + } + + /** + * Sets the URI used by this RDF URI to that specified. + * + * @param uri the new URI to use. + */ + public void setURI(URI uri) + { + this.uri = uri; + } + } Index: src/nongnu/cashews/rdf/Subject.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/Subject.java,v retrieving revision 1.2 diff -u -3 -p -u -r1.2 Subject.java --- src/nongnu/cashews/rdf/Subject.java 28 Mar 2005 19:29:21 -0000 1.2 +++ src/nongnu/cashews/rdf/Subject.java 4 Apr 2005 00:35:33 -0000 @@ -34,7 +34,14 @@ package nongnu.cashews.rdf; * @see Blank */ public interface Subject - extends Node + extends Node, Cloneable { + /** + * Returns a deep copy of the subject. + * + * @return a deep copy of the subject. + */ + Subject clone(); + } Index: src/nongnu/cashews/rdf/Triple.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/Triple.java,v retrieving revision 1.3 diff -u -3 -p -u -r1.3 Triple.java --- src/nongnu/cashews/rdf/Triple.java 3 Apr 2005 17:15:39 -0000 1.3 +++ src/nongnu/cashews/rdf/Triple.java 4 Apr 2005 00:35:33 -0000 @@ -45,7 +45,7 @@ import java.io.Serializable; * @see Predicate */ public class Triple - implements Serializable + implements Serializable, Cloneable { /** @@ -109,4 +109,121 @@ public class Triple "]"; } + /** + * Returns a deep copy of this triple. + * + * @return a clone of the triple. + */ + public Triple clone() + { + try + { + Object clonedObject = super.clone(); + Triple clone = (Triple) clonedObject; + clone.setSubject(subject.clone()); + clone.setPredicate(predicate.clone()); + clone.setObject(object.clone()); + return clone; + } + catch (CloneNotSupportedException e) + { + throw new IllegalStateException("Unexpected exception: " + e, e); + } + } + + /** + * Sets the subject of this triple to that specified. + * + * @param subject the new subject for this triple. + */ + public void setSubject(Subject subject) + { + this.subject = subject; + } + + /** + * Sets the predicate of this triple to that specified. + * + * @param predicate the new predicate for this triple. + */ + public void setPredicate(Predicate predicate) + { + this.predicate = predicate; + } + + /** + * Sets the object of this triple to that specified. + * + * @param object the new object for this triple. + */ + public void setObject(RDFObject object) + { + this.object = object; + } + + /** + * Returns true if the specified triple is equal to this one. + * + * @param object the object to compare. + */ + public boolean equals(Object obj) + { + if (obj == null) + return false; + if (obj == this) + return true; + if (obj.getClass() == getClass()) + { + Triple triple = (Triple) obj; + return subject.equals(triple.getSubject()) + && predicate.equals(triple.getPredicate()) + && object.equals(triple.getObject()); + } + return false; + } + + /** + * Returns the hashcode of this triple. This is calculated using + * the hash codes of the subject, predicate and object. + * + * @return the hashcode for the triple. + */ + public int hashCode() + { + return super.hashCode() + + 13 * subject.hashCode() + + 17 * predicate.hashCode() + + 19 * object.hashCode(); + } + + /** + * Returns a clone of the triple's subject. + * + * @return a clone of the subject. + */ + public Subject getSubject() + { + return subject.clone(); + } + + /** + * Returns a clone of the triple's predicate. + * + * @return a clone of the predicate. + */ + public Predicate getPredicate() + { + return predicate.clone(); + } + + /** + * Returns a clone of the triple's object. + * + * @return a clone of the object. + */ + public RDFObject getObject() + { + return object.clone(); + } + } Index: src/nongnu/cashews/rdf/Type.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/Type.java,v retrieving revision 1.1 diff -u -3 -p -u -r1.1 Type.java --- src/nongnu/cashews/rdf/Type.java 28 Mar 2005 19:29:21 -0000 1.1 +++ src/nongnu/cashews/rdf/Type.java 4 Apr 2005 00:35:33 -0000 @@ -43,4 +43,11 @@ public interface Type */ Object getValue(String lexical); + /** + * Returns a deep copy of the type. + * + * @return a deep copy of the type. + */ + Type clone(); + } Index: src/nongnu/cashews/rdf/XMLParser.java =================================================================== RCS file: /cvsroot/cashew-s-editor/cashews/src/nongnu/cashews/rdf/XMLParser.java,v retrieving revision 1.3 diff -u -3 -p -u -r1.3 XMLParser.java --- src/nongnu/cashews/rdf/XMLParser.java 3 Apr 2005 17:15:39 -0000 1.3 +++ src/nongnu/cashews/rdf/XMLParser.java 4 Apr 2005 00:35:33 -0000 @@ -65,6 +65,13 @@ public class XMLParser private XMLReader reader; /** + * The resulting graph. + * + * @serial the graph of RDF triples. + */ + private Graph graph; + + /** * Constructs a new XML-based RDF parser, using the specified handler * for messages. * @@ -83,39 +90,46 @@ public class XMLParser * Parse an RDF document from the specified system identifier (URI). * * @param systemId the URI of the RDF document. + * @return the graph generated by the parsing process. * @throws SAXException if an error occurs in parsing. * @throws IOException if an error occurs in the underlying input. */ - public void parse(String systemId) + public Graph parse(String systemId) throws IOException, SAXException { + graph = new Graph(); reader.parse(systemId); + return graph; } /** * Parse an RDF document from the specified input source. * * @param source the source of the RDF document. + * @return the graph generated by the parsing process. * @throws SAXException if an error occurs in parsing. * @throws IOException if an error occurs in the underlying input. */ - public void parse(InputSource source) + public Graph parse(InputSource source) throws IOException, SAXException { + graph = new Graph(); reader.parse(source); + return graph; } /** * Parse an RDF document from the specified file. * * @param file the file containing the RDF document. + * @return the graph generated by the parsing process. * @throws SAXException if an error occurs in parsing. * @throws IOException if an error occurs in the underlying input. */ - public void parse(File file) + public Graph parse(File file) throws IOException, SAXException { - parse(new InputSource(new FileInputStream(file))); + return parse(new InputSource(new FileInputStream(file))); } /** @@ -136,7 +150,7 @@ public class XMLParser handler.setLevel(Level.FINE); XMLParser parser = new XMLParser(handler); for (int a = 0; a < args.length; ++a) - parser.parse(new File(args[a])); + System.out.println(parser.parse(new File(args[a]))); } /** @@ -247,20 +261,21 @@ public class XMLParser { if (localName.equals("RDF")) { + /* rdf:RDF */ inRDF = true; logger.finer("Start of RDF block"); } else if (localName.equals("Description")) { + /* rdf:Description */ logger.finer("Start of RDF description block"); inRDF = true; if (!inSubject) { + /* Check for RDF URI subject */ String value = attributes.getValue(RDF_NAMESPACE, "about"); if (value != null) - { subject = parseRDFURI(value); - } inSubject = true; logger.fine("Created subject: " + subject); } @@ -273,12 +288,20 @@ public class XMLParser { if (inSubject) { + /* Predicate element */ predicateURI = uri + localName; logger.finer("Start of predicate: " + predicateURI); predicate = parseRDFURI(predicateURI); logger.fine("Created predicate: " + predicate); inSubject = false; inPredicate = true; + /* Check for RDF URI object */ + String value = attributes.getValue(RDF_NAMESPACE, "resource"); + if (value != null) + { + object = parseRDFURI(value); + logger.fine("Created object: " + object); + } } } } @@ -350,8 +373,6 @@ public class XMLParser else if (localName.equals("Description")) { logger.finer("End of description block"); - triple = new Triple(subject, predicate, object); - logger.fine("Created triple: " + triple); inSubject = false; } } @@ -360,6 +381,9 @@ public class XMLParser inPredicate = false; inSubject = true; predicateURI = null; + triple = new Triple(subject, predicate, object); + logger.fine("Created triple: " + triple); + graph.addTriple(triple); logger.finer("End of predicate block"); } }