32 |
import java.util.logging.Level; |
import java.util.logging.Level; |
33 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
34 |
|
|
35 |
|
import nongnu.cashews.xml.XmlBaseHandler; |
36 |
|
|
37 |
import org.xml.sax.Attributes; |
import org.xml.sax.Attributes; |
38 |
import org.xml.sax.InputSource; |
import org.xml.sax.InputSource; |
39 |
import org.xml.sax.SAXException; |
import org.xml.sax.SAXException; |
40 |
import org.xml.sax.XMLReader; |
import org.xml.sax.XMLReader; |
|
import org.xml.sax.helpers.DefaultHandler; |
|
41 |
import org.xml.sax.helpers.XMLReaderFactory; |
import org.xml.sax.helpers.XMLReaderFactory; |
42 |
|
|
43 |
/** |
/** |
71 |
* @serial the graph of RDF triples. |
* @serial the graph of RDF triples. |
72 |
*/ |
*/ |
73 |
private Graph graph; |
private Graph graph; |
74 |
|
|
75 |
|
/** |
76 |
|
* The handler for the XML. |
77 |
|
* |
78 |
|
* @serial the XML handler. |
79 |
|
*/ |
80 |
|
private RDFHandler rdfHandler; |
81 |
|
|
82 |
/** |
/** |
83 |
* Constructs a new XML-based RDF parser, using the specified handler |
* Constructs a new XML-based RDF parser, using the specified handler |
84 |
* for messages. |
* for messages. |
90 |
public XMLParser(Handler handler) |
public XMLParser(Handler handler) |
91 |
throws SAXException |
throws SAXException |
92 |
{ |
{ |
93 |
reader = XMLReaderFactory.createXMLReader(); |
reader = XMLReaderFactory.createXMLReader(); |
94 |
reader.setContentHandler(new RDFHandler(handler)); |
rdfHandler = new RDFHandler(handler); |
95 |
|
reader.setContentHandler(rdfHandler); |
96 |
} |
} |
97 |
|
|
98 |
/** |
/** |
106 |
public Graph parse(String systemId) |
public Graph parse(String systemId) |
107 |
throws IOException, SAXException |
throws IOException, SAXException |
108 |
{ |
{ |
109 |
graph = new Graph(); |
return parse(new InputSource(systemId)); |
|
reader.parse(systemId); |
|
|
return graph; |
|
110 |
} |
} |
111 |
|
|
112 |
/** |
/** |
121 |
throws IOException, SAXException |
throws IOException, SAXException |
122 |
{ |
{ |
123 |
graph = new Graph(); |
graph = new Graph(); |
124 |
|
rdfHandler.setBaseURI(source.getSystemId()); |
125 |
reader.parse(source); |
reader.parse(source); |
126 |
return graph; |
return graph; |
127 |
} |
} |
168 |
* @author Andrew John Hughes (gnu_andrew@member.fsf.org) |
* @author Andrew John Hughes (gnu_andrew@member.fsf.org) |
169 |
*/ |
*/ |
170 |
private class RDFHandler |
private class RDFHandler |
171 |
extends DefaultHandler |
extends XmlBaseHandler |
172 |
{ |
{ |
173 |
|
|
174 |
/** |
/** |
175 |
* A <code>Logger</code> instance to log events generated by the |
* A <code>Logger</code> instance to log events generated by the |
176 |
* parsing process. |
* parsing process. |
177 |
*/ |
*/ |
178 |
private Logger logger; |
private Logger rdfLogger; |
179 |
|
|
180 |
/** |
/** |
181 |
* Flag to indicate whether we are inside an RDF document |
* Flag to indicate whether we are inside an RDF document |
238 |
*/ |
*/ |
239 |
public RDFHandler(Handler handler) |
public RDFHandler(Handler handler) |
240 |
{ |
{ |
241 |
logger = Logger.getLogger("nongnu.cashews.rdf.XMLParser"); |
super(handler); |
242 |
logger.addHandler(handler); |
rdfLogger = Logger.getLogger("nongnu.cashews.rdf.XMLParser"); |
243 |
logger.setLevel(handler.getLevel()); |
rdfLogger.addHandler(handler); |
244 |
|
rdfLogger.setLevel(handler.getLevel()); |
245 |
} |
} |
246 |
|
|
247 |
/** |
/** |
250 |
*/ |
*/ |
251 |
public void startDocument() |
public void startDocument() |
252 |
{ |
{ |
253 |
|
super.startDocument(); |
254 |
inSubject = false; |
inSubject = false; |
255 |
inRDF = false; |
inRDF = false; |
256 |
inPredicate = false; |
inPredicate = false; |
273 |
String qName, Attributes attributes) |
String qName, Attributes attributes) |
274 |
throws SAXException |
throws SAXException |
275 |
{ |
{ |
276 |
|
super.startElement(uri, localName, qName, attributes); |
277 |
if (uri.equals(RDF_NAMESPACE)) |
if (uri.equals(RDF_NAMESPACE)) |
278 |
{ |
{ |
279 |
if (localName.equals("RDF")) |
if (localName.equals("RDF")) |
280 |
{ |
{ |
281 |
/* rdf:RDF */ |
/* rdf:RDF */ |
282 |
inRDF = true; |
inRDF = true; |
283 |
logger.finer("Start of RDF block"); |
rdfLogger.finer("Start of RDF block"); |
284 |
} |
} |
285 |
else if (localName.equals("Description")) |
else if (localName.equals("Description")) |
286 |
{ |
{ |
287 |
/* rdf:Description */ |
/* rdf:Description */ |
288 |
logger.finer("Start of RDF description block"); |
rdfLogger.finer("Start of RDF description block"); |
289 |
inRDF = true; |
inRDF = true; |
290 |
if (!inSubject) |
if (!inSubject) |
291 |
{ |
{ |
300 |
if (value != null) |
if (value != null) |
301 |
subject = new Blank(value); |
subject = new Blank(value); |
302 |
else |
else |
303 |
logger.warning("No subject found in RDF description."); |
rdfLogger.warning("No subject found in RDF " + |
304 |
|
"description."); |
305 |
} |
} |
306 |
inSubject = true; |
inSubject = true; |
307 |
logger.fine("Created subject: " + subject); |
rdfLogger.fine("Created subject: " + subject); |
308 |
} |
} |
309 |
else |
else |
310 |
logger.warning("Invalid use of RDF namespace: " + uri + |
rdfLogger.warning("Invalid use of RDF namespace: " + uri + |
311 |
localName); |
localName); |
312 |
} |
} |
313 |
} |
} |
319 |
{ |
{ |
320 |
/* Predicate element */ |
/* Predicate element */ |
321 |
predicateURI = uri + localName; |
predicateURI = uri + localName; |
322 |
logger.finer("Start of predicate: " + predicateURI); |
rdfLogger.finer("Start of predicate: " + predicateURI); |
323 |
predicate = parseRDFURI(predicateURI); |
predicate = parseRDFURI(predicateURI); |
324 |
logger.fine("Created predicate: " + predicate); |
rdfLogger.fine("Created predicate: " + predicate); |
325 |
inSubject = false; |
inSubject = false; |
326 |
inPredicate = true; |
inPredicate = true; |
327 |
/* Check for RDF URI object */ |
/* Check for RDF URI object */ |
330 |
if (value != null) |
if (value != null) |
331 |
{ |
{ |
332 |
object = parseRDFURI(value); |
object = parseRDFURI(value); |
333 |
logger.fine("Created object: " + object); |
rdfLogger.fine("Created object: " + object); |
334 |
} |
} |
335 |
else |
else |
336 |
{ |
{ |
339 |
if (value != null) |
if (value != null) |
340 |
{ |
{ |
341 |
object = new Blank(value); |
object = new Blank(value); |
342 |
logger.fine("Created object: " + object); |
rdfLogger.fine("Created object: " + object); |
343 |
} |
} |
344 |
} |
} |
345 |
/* Check for a type */ |
/* Check for a type */ |
383 |
public void characters(char[] ch, int start, int length) |
public void characters(char[] ch, int start, int length) |
384 |
throws SAXException |
throws SAXException |
385 |
{ |
{ |
386 |
|
super.characters(ch, start, length); |
387 |
String value = new String(ch, start, length).trim(); |
String value = new String(ch, start, length).trim(); |
388 |
if (value.length() == 0) |
if (value.length() == 0) |
389 |
return; |
return; |
390 |
logger.finer("Characters: " + value); |
rdfLogger.finer("Characters: " + value); |
391 |
if (inPredicate) |
if (inPredicate) |
392 |
{ |
{ |
393 |
if (type == null) |
if (type == null) |
394 |
object = new Literal(value); |
object = new Literal(value); |
395 |
else |
else |
396 |
object = new Literal(value, type); |
object = new Literal(value, type); |
397 |
logger.fine("Created object: " + object); |
rdfLogger.fine("Created object: " + object); |
398 |
} |
} |
399 |
} |
} |
400 |
|
|
412 |
String qName) |
String qName) |
413 |
throws SAXException |
throws SAXException |
414 |
{ |
{ |
415 |
|
super.endElement(uri, localName, qName); |
416 |
if (uri.equals(RDF_NAMESPACE)) |
if (uri.equals(RDF_NAMESPACE)) |
417 |
{ |
{ |
418 |
if (localName.equals("RDF")) |
if (localName.equals("RDF")) |
419 |
{ |
{ |
420 |
inRDF = false; |
inRDF = false; |
421 |
logger.finer("End of RDF block"); |
rdfLogger.finer("End of RDF block"); |
422 |
} |
} |
423 |
else if (localName.equals("Description")) |
else if (localName.equals("Description")) |
424 |
{ |
{ |
425 |
logger.finer("End of description block"); |
rdfLogger.finer("End of description block"); |
426 |
inSubject = false; |
inSubject = false; |
427 |
} |
} |
428 |
} |
} |
433 |
predicateURI = null; |
predicateURI = null; |
434 |
type = null; |
type = null; |
435 |
triple = new Triple(subject, predicate, object); |
triple = new Triple(subject, predicate, object); |
436 |
logger.fine("Created triple: " + triple); |
rdfLogger.fine("Created triple: " + triple); |
437 |
graph.addTriple(triple); |
graph.addTriple(triple); |
438 |
logger.finer("End of predicate block"); |
rdfLogger.finer("End of predicate block"); |
439 |
} |
} |
440 |
} |
} |
441 |
|
|