/** * PlexRDF: An RDF API that goes beyond what's specified in RDF 1.0. * Modified a bit by SBP * Ported to Java by Sean B. Palmer */ // import urllib, string // package rdfapi; import java.io.*; import java.net.*; import java.util.*; // __version__ = '$Revision$' // __cvsid__ = '$Id$' // __author__ = 'Aaron Swartz ' // class Namespace: // """A class so namespaced URIs can be abbreviated (like dc.subject).""" // def __init__(self, prefix): self.prefix = prefix // def __getattr__(self,name): return self.prefix + name class Namespace { String prefix; Namespace(String p) { prefix=p; } String term(String t) { // term: terrible hack because I don't have "getattr" // e.g. dc.term("subject"); // Then again, getattr is a bit of a hack, too return prefix + t; } } // class Node: // def __init__(self, value, uni=None): // self.nodeList = {} // if uni: self.universal = uni // if type(value) is type(u''): value = literalToURI(value) // if value and type(value) != type(''): // raise "Must be a u/literal", value // if value: self.uri = value class Node { Hashtable nodeList=new Hashtable(); URL uri; int universal; Node() { } // @@ pass? - a bNode Node(String u) { uri = Utils.literalToURI(u); // @@ damn function; returns an exception if (!(nodeList.containsKey(u))) { nodeList.put(u, u); } } Node(String u, int uni) { uri = Utils.literalToURI(u); if (uni == 1) { universal = 1; } else { universal = 0; } if (!(nodeList.containsKey(u))) { nodeList.put(u, u); } } Node(URL u) { uri = u; if (!(nodeList.containsKey(u))) { nodeList.put(u, u); } } Node(URL u, int uni) { uri = u; if (uni == 1) { universal = 1; } else { universal = 0; } if (!(nodeList.containsKey(u))) { nodeList.put(u, u); } } } class Utils { // def literalToURI(value): // """Converts a literal into a data: URI.""" // return 'data:,' + str(urllib.quote(value)) public static URL literalToURI(String value) { /* URL uri=new URL("data:,"+URLEncoder.encode(value)); return uri; */ return new URL(); // Gives an exception } // def URIToLiteral(uri): // """Converts a data: URI into a literal.""" // if uri[0:6] != "data:,": // BadURI = "Only deals with URIs of the form: data:,somedata" // raise BadURI // return urllib.unquote(uri[6:]) public static String URIToLiteral(URL uri) { /* String data="data:,"; String prefix=uri.toString().substring(0,6); if (prefix.equals(data)) { throw new IOException("Only deals with URIs of the form: data:,somedata"); } // String uToReturn=uri.toString; return URLDecoder.decode(uri.toString().substring(6)); // http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#substring(int) */ return "blargh"; } } // class Triple(Node): // """An RDF triple, the basic unit of association.""" // def __init__(self, store, s, p, o): // # Terms must be nodes // self.subject, self.predicate, self.object = s, p, o class Triple extends Node { Node subject; Node predicate; Node object; // def __eq__(self, other): // if not isinstance(other, Triple): return self is other // if (self.subject == other.subject // and self.predicate == other.predicate // and self.object == other.object): return 1 // else: return 0 public int eq(Triple other) { // if (!isinstance(other, Triple)) { return this == other; } if ( (subject == other.subject) && (predicate == other.predicate) && (object == other.object)) { return 1; } else { return 0; } } } // class Store: // """A generic RDF store.""" // // def __init__(self): // self.tripleList = [] # triples in this store class Store { Triple[] tripleList; // def triple(self, s, p, o): // result = Triple(self, s, p, o) // self.tripleList.append(result) // return result public Triple triple(Node s, Node p, Node o) { Triple result; result=Triple(s, p, o); tripleList.append(result); return result; } // def query(self, s, p, o): // if s: s = Node(s) // if p: p = Node(p) // if o: o = Node(o) // // results = [] // for t in self.tripleList: // if ((not s or t.subject is s) // and (not p or t.predicate is p) // and (not o or t.object is o)): results.append(t) // return results }