001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.resultio.sparqlxml;
007:
008: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BOOLEAN_FALSE;
009: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BOOLEAN_TAG;
010: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BOOLEAN_TRUE;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.util.Map;
015:
016: import org.xml.sax.SAXException;
017:
018: import info.aduna.xml.SimpleSAXAdapter;
019: import info.aduna.xml.SimpleSAXParser;
020: import info.aduna.xml.XMLReaderFactory;
021:
022: import org.openrdf.query.resultio.BooleanQueryResultFormat;
023: import org.openrdf.query.resultio.BooleanQueryResultParser;
024: import org.openrdf.query.resultio.QueryResultParseException;
025:
026: /**
027: * Parser for reading boolean query results formatted as SPARQL Results
028: * Documents. See <a href="http://www.w3.org/TR/rdf-sparql-XMLres/">SPARQL Query
029: * Results XML Format</a> for the definition of this format. The parser assumes
030: * that the XML is wellformed.
031: */
032: public class SPARQLBooleanXMLParser implements BooleanQueryResultParser {
033:
034: /*-------------*
035: * Construtors *
036: *-------------*/
037:
038: /**
039: * Creates a new parser for the SPARQL Query Results XML Format.
040: */
041: public SPARQLBooleanXMLParser() {
042: super ();
043: }
044:
045: /*---------*
046: * Methods *
047: *---------*/
048:
049: public BooleanQueryResultFormat getBooleanQueryResultFormat() {
050: return BooleanQueryResultFormat.SPARQL;
051: }
052:
053: public boolean parse(InputStream in) throws IOException,
054: QueryResultParseException {
055: try {
056: SPARQLBooleanParser valueParser = new SPARQLBooleanParser();
057:
058: SimpleSAXParser simpleSAXParser = new SimpleSAXParser(
059: XMLReaderFactory.createXMLReader());
060: simpleSAXParser.setListener(valueParser);
061: simpleSAXParser.parse(in);
062:
063: return valueParser.getValue();
064: } catch (SAXException e) {
065: Exception wrappedExc = e.getException();
066:
067: if (wrappedExc instanceof QueryResultParseException) {
068: throw (QueryResultParseException) wrappedExc;
069: } else {
070: throw new QueryResultParseException(wrappedExc);
071: }
072: }
073: }
074:
075: /*---------------------------------*
076: * Inner class SPARQLBooleanParser *
077: *---------------------------------*/
078:
079: private static class SPARQLBooleanParser extends SimpleSAXAdapter {
080:
081: /*-----------*
082: * Variables *
083: *-----------*/
084:
085: private Boolean value;
086:
087: /*---------*
088: * Methods *
089: *---------*/
090:
091: @Override
092: public void startTag(String tagName, Map<String, String> atts,
093: String text) throws SAXException {
094: if (BOOLEAN_TAG.equals(tagName)) {
095: if (BOOLEAN_TRUE.equals(text)) {
096: value = true;
097: } else if (BOOLEAN_FALSE.equals(text)) {
098: value = false;
099: } else {
100: throw new SAXException("Illegal value for element "
101: + BOOLEAN_TAG + ": " + text);
102: }
103: }
104: }
105:
106: @Override
107: public void endDocument() throws SAXException {
108: if (value == null) {
109: throw new SAXException("Malformed document, "
110: + BOOLEAN_TAG + " element not found");
111: }
112: }
113:
114: public boolean getValue() {
115: return value != null && value;
116: }
117: }
118: }
|