You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2013/12/13 11:40:07 UTC
svn commit: r1550681 - in /jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena:
query/ResultSetFactory.java sparql/resultset/XMLInputSAX.java
sparql/resultset/XMLInputStAX.java
Author: andy
Date: Fri Dec 13 10:40:06 2013
New Revision: 1550681
URL: http://svn.apache.org/r1550681
Log:
Misc tidying and reformatting while passing.
Modified:
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/ResultSetFactory.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputSAX.java
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputStAX.java
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/ResultSetFactory.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/ResultSetFactory.java?rev=1550681&r1=1550680&r2=1550681&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/ResultSetFactory.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/ResultSetFactory.java Fri Dec 13 10:40:06 2013
@@ -18,22 +18,23 @@
package com.hp.hpl.jena.query;
-import java.io.InputStream;
-import java.util.List;
+import java.io.InputStream ;
+import java.util.List ;
-import org.apache.jena.atlas.logging.Log;
+import org.apache.jena.atlas.io.IO ;
+import org.apache.jena.atlas.logging.Log ;
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.shared.NotFoundException;
-import com.hp.hpl.jena.sparql.engine.QueryIterator;
-import com.hp.hpl.jena.sparql.engine.ResultSetStream;
-import com.hp.hpl.jena.sparql.graph.GraphFactory;
-import com.hp.hpl.jena.sparql.resultset.*;
-import com.hp.hpl.jena.sparql.sse.Item;
-import com.hp.hpl.jena.sparql.sse.SSE;
-import com.hp.hpl.jena.sparql.sse.builders.BuilderTable;
-import com.hp.hpl.jena.util.FileManager;
+import com.hp.hpl.jena.rdf.model.Model ;
+import com.hp.hpl.jena.rdf.model.ModelFactory ;
+import com.hp.hpl.jena.shared.NotFoundException ;
+import com.hp.hpl.jena.sparql.engine.QueryIterator ;
+import com.hp.hpl.jena.sparql.engine.ResultSetStream ;
+import com.hp.hpl.jena.sparql.graph.GraphFactory ;
+import com.hp.hpl.jena.sparql.resultset.* ;
+import com.hp.hpl.jena.sparql.sse.Item ;
+import com.hp.hpl.jena.sparql.sse.SSE ;
+import com.hp.hpl.jena.sparql.sse.builders.BuilderTable ;
+import com.hp.hpl.jena.util.FileManager ;
/** ResultSetFactory - make result sets from places other than a query. */
@@ -263,32 +264,35 @@ public class ResultSetFactory {
}
if (format.equals(ResultsFormat.FMT_RS_XML) || format.equals(ResultsFormat.FMT_RS_JSON)
- || format.equals(ResultsFormat.FMT_RS_TSV) || format.equals(ResultsFormat.FMT_RS_CSV)) {
+ || format.equals(ResultsFormat.FMT_RS_TSV) || format.equals(ResultsFormat.FMT_RS_CSV)) {
InputStream in = null;
try {
- in = FileManager.get().open(filenameOrURI);
- if (in == null)
- throw new NotFoundException(filenameOrURI);
- } catch (NotFoundException ex) {
- throw new NotFoundException("File not found: " + filenameOrURI);
- }
-
- SPARQLResult x = null;
-
- if (format.equals(ResultsFormat.FMT_RS_JSON))
- return JSONInput.make(in, GraphFactory.makeDefaultModel());
- else if (format.equals(ResultsFormat.FMT_RS_XML))
- return XMLInput.make(in, GraphFactory.makeDefaultModel());
- else if (format.equals(ResultsFormat.FMT_RS_TSV)) {
- ResultSet rs = TSVInput.fromTSV(in);
- return new SPARQLResult(rs);
- } else if (format.equals(ResultsFormat.FMT_RS_CSV)) {
- ResultSet rs = CSVInput.fromCSV(in);
- return new SPARQLResult(rs);
- } else if (format.equals(ResultsFormat.FMT_RS_BIO)) {
- ResultSet rs = BIOInput.fromBIO(in);
- return new SPARQLResult(rs);
-
+ try {
+ in = FileManager.get().open(filenameOrURI);
+ if (in == null)
+ throw new NotFoundException(filenameOrURI);
+ } catch (NotFoundException ex) {
+ throw new NotFoundException("File not found: " + filenameOrURI);
+ }
+
+ SPARQLResult x = null;
+
+ if (format.equals(ResultsFormat.FMT_RS_JSON))
+ return JSONInput.make(in, GraphFactory.makeDefaultModel());
+ else if (format.equals(ResultsFormat.FMT_RS_XML))
+ return XMLInput.make(in, GraphFactory.makeDefaultModel());
+ else if (format.equals(ResultsFormat.FMT_RS_TSV)) {
+ ResultSet rs = TSVInput.fromTSV(in);
+ return new SPARQLResult(rs);
+ } else if (format.equals(ResultsFormat.FMT_RS_CSV)) {
+ ResultSet rs = CSVInput.fromCSV(in);
+ return new SPARQLResult(rs);
+ } else if (format.equals(ResultsFormat.FMT_RS_BIO)) {
+ ResultSet rs = BIOInput.fromBIO(in);
+ return new SPARQLResult(rs);
+ }
+ } finally {
+ IO.close(in) ;
}
}
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputSAX.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputSAX.java?rev=1550681&r1=1550680&r2=1550681&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputSAX.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputSAX.java Fri Dec 13 10:40:06 2013
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.resultset;
+package com.hp.hpl.jena.sparql.resultset ;
import java.io.IOException ;
import java.io.InputStream ;
@@ -24,12 +24,7 @@ import java.util.ArrayList ;
import java.util.List ;
import org.apache.jena.atlas.logging.Log ;
-import org.xml.sax.Attributes ;
-import org.xml.sax.ContentHandler ;
-import org.xml.sax.InputSource ;
-import org.xml.sax.Locator ;
-import org.xml.sax.SAXException ;
-import org.xml.sax.XMLReader ;
+import org.xml.sax.* ;
import org.xml.sax.helpers.XMLReaderFactory ;
import com.hp.hpl.jena.datatypes.RDFDatatype ;
@@ -50,375 +45,328 @@ import com.hp.hpl.jena.vocabulary.RDF ;
/** Code that reads an XML Result Set and builds the ARQ structure for the same. */
-
-class XMLInputSAX extends SPARQLResult
-{
+class XMLInputSAX extends SPARQLResult {
// See also XMLInputStAX, which is preferred.
- // SAX is not a streaming API - the SAX handler is called as fast as the
+ // SAX is not a streaming API - the SAX handler is called as fast as the
// parser wants to call it, so the parser is calling for all the XML
- // and we have to build an in-memory structure (or the client application would
- // need to be inside the code path of the SAX handler).
-
- public XMLInputSAX(InputStream in, Model model)
- {
+ // and we have to build an in-memory structure (or the client application
+ // would need to be inside the code path of the SAX handler).
+
+ public XMLInputSAX(InputStream in, Model model) {
worker(new InputSource(in), model) ;
}
- public XMLInputSAX(String str, Model model)
- {
+ public XMLInputSAX(String str, Model model) {
worker(new InputSource(str), model) ;
}
- private void worker(InputSource in, Model model)
- {
+ private void worker(InputSource in, Model model) {
if ( model == null )
model = GraphFactory.makeJenaDefaultModel() ;
try {
XMLReader xr = XMLReaderFactory.createXMLReader() ;
xr.setFeature("http://xml.org/sax/features/namespace-prefixes", true) ;
- //ResultSetXMLHandler1 handler = new ResultSetXMLHandler1() ;
+ // ResultSetXMLHandler1 handler = new ResultSetXMLHandler1() ;
ResultSetXMLHandler2 handler = new ResultSetXMLHandler2() ;
xr.setContentHandler(handler) ;
xr.parse(in) ;
- if ( handler.isBooleanResult )
- {
+ if ( handler.isBooleanResult ) {
// Set superclass member
set(handler.askResult) ;
return ;
}
-
ResultSetStream rss = new ResultSetStream(handler.variables, model,
new QueryIterPlainWrapper(handler.results.iterator())) ;
// Set superclass member
set(rss) ;
+ } catch (SAXException ex) {
+ throw new ResultSetException("Problems parsing file (SAXException)", ex) ;
+ } catch (IOException ex) {
+ throw new ResultSetException("Problems parsing file (IOException)", ex) ;
}
- catch (SAXException ex)
- { throw new ResultSetException("Problems parsing file (SAXException)", ex) ; }
- catch (IOException ex)
- { throw new ResultSetException("Problems parsing file (IOException)", ex) ; }
}
- static class ResultSetXMLHandler2 implements ContentHandler
- {
- static final String namespace = XMLResults.dfNamespace ;
- static final String variableElt = XMLResults.dfVariable ;
- static final String resultElt = XMLResults.dfSolution ;
+ static class ResultSetXMLHandler2 implements ContentHandler {
+ static final String namespace = XMLResults.dfNamespace ;
+ static final String variableElt = XMLResults.dfVariable ;
+ static final String resultElt = XMLResults.dfSolution ;
// Boolean
- boolean isBooleanResult = false ;
- boolean askResult = false ;
-
- int rowCount = 0 ;
- LabelToNodeMap bNodes = LabelToNodeMap.createBNodeMap() ;
-
- boolean accumulate = false ;
- StringBuffer buff = new StringBuffer() ;
- List<String> variables = new ArrayList<String>() ;
-
- List<Binding> results = new ArrayList<Binding>() ;
+ boolean isBooleanResult = false ;
+ boolean askResult = false ;
+
+ int rowCount = 0 ;
+ LabelToNodeMap bNodes = LabelToNodeMap.createBNodeMap() ;
+
+ boolean accumulate = false ;
+ StringBuffer buff = new StringBuffer() ;
+ List<String> variables = new ArrayList<String>() ;
+
+ List<Binding> results = new ArrayList<Binding>() ;
// The current solution
- BindingMap binding = null ;
-
+ BindingMap binding = null ;
+
// Note on terminology:
// A "Binding" in ARQ is a set of name/value pairs
// In the XML format is it one pair.
-
+
// Current value
- String varName ;
- String datatype = null ;
- String langTag = null ;
-
- String rdfPrefix = "rdf" ;
-
+ String varName ;
+ String datatype = null ;
+ String langTag = null ;
+
+ String rdfPrefix = "rdf" ;
+
ResultSetXMLHandler2() {}
-
+
@Override
public void setDocumentLocator(Locator locator) {}
-
+
@Override
public void startDocument() throws SAXException {}
-
+
@Override
public void endDocument() throws SAXException {}
-
+
@Override
- public void startPrefixMapping(String prefix, String uri) throws SAXException
- {
+ public void startPrefixMapping(String prefix, String uri) throws SAXException {
if ( uri.equals(RDF.getURI()) )
rdfPrefix = prefix ;
}
-
+
@Override
public void endPrefixMapping(String prefix) throws SAXException {}
-
+
@Override
- public void startElement(String ns, String localName, String qName, Attributes attrs) throws SAXException
- {
- if ( ! ns.equals(namespace) )
- {
+ public void startElement(String ns, String localName, String qName, Attributes attrs) throws SAXException {
+ if ( !ns.equals(namespace) ) {
// Wrong namespace
return ;
}
-
-
- // ---- Header
-
- if ( localName.equals(XMLResults.dfVariable) )
- {
- if ( attrs.getValue(XMLResults.dfAttrVarName) != null )
- {
+
+ // ---- Header
+
+ if ( localName.equals(XMLResults.dfVariable) ) {
+ if ( attrs.getValue(XMLResults.dfAttrVarName) != null ) {
String name = attrs.getValue(XMLResults.dfAttrVarName) ;
variables.add(name) ;
}
- return ;
+ return ;
}
-
- // ---- Results
-
+
+ // ---- Results
+
if ( localName.equals(XMLResults.dfResults) )
return ;
// Boolean
- if ( localName.equals(XMLResults.dfBoolean) )
- {
+ if ( localName.equals(XMLResults.dfBoolean) ) {
isBooleanResult = true ;
// Wait for the content
}
-
- // ---- One solution
-
- if ( localName.equals(XMLResults.dfSolution) )
- {
- binding = BindingFactory.create() ;
+ // ---- One solution
+
+ if ( localName.equals(XMLResults.dfSolution) ) {
+ binding = BindingFactory.create() ;
return ;
}
-
+
// One variable
-
- if ( localName.equals(XMLResults.dfBinding) )
- {
+
+ if ( localName.equals(XMLResults.dfBinding) ) {
varName = attrs.getValue(XMLResults.dfAttrVarName) ;
return ;
}
-
+
// One value
-
- if ( localName.equals(XMLResults.dfURI) )
- {
+
+ if ( localName.equals(XMLResults.dfURI) ) {
startElementURI(ns, localName, qName, attrs) ;
return ;
}
-
- if ( localName.equals(XMLResults.dfLiteral) )
- {
+
+ if ( localName.equals(XMLResults.dfLiteral) ) {
startElementLiteral(ns, localName, qName, attrs) ;
return ;
}
-
- if ( localName.equals(XMLResults.dfBNode) )
- {
+
+ if ( localName.equals(XMLResults.dfBNode) ) {
startElementBNode(ns, localName, qName, attrs) ;
return ;
}
-
+
if ( localName.equals(XMLResults.dfUnbound) )
return ;
-
+
}
-
+
@Override
- public void endElement(String ns, String localName, String qName) throws SAXException
- {
- if ( ! ns.equals(namespace) )
- {
+ public void endElement(String ns, String localName, String qName) throws SAXException {
+ if ( !ns.equals(namespace) ) {
// Wrong namespace
return ;
}
-
- // ---- Results
-
+
+ // ---- Results
+
if ( localName.equals(XMLResults.dfResults) )
return ;
-
- if ( localName.equals(XMLResults.dfBoolean) )
- {
+
+ if ( localName.equals(XMLResults.dfBoolean) ) {
endElementBoolean() ;
return ;
}
-
- // ---- One solution
-
- if ( localName.equals(XMLResults.dfSolution) )
- {
+
+ // ---- One solution
+
+ if ( localName.equals(XMLResults.dfSolution) ) {
varName = null ;
- datatype= null ;
+ datatype = null ;
langTag = null ;
results.add(binding) ;
binding = null ;
return ;
}
-
+
// ---- One variable
-
- if ( localName.equals(XMLResults.dfBinding) )
- {
+
+ if ( localName.equals(XMLResults.dfBinding) ) {
varName = null ;
return ;
}
// ---- One value.
-
- if ( localName.equals(XMLResults.dfURI) )
- {
+
+ if ( localName.equals(XMLResults.dfURI) ) {
endElementURI(ns, localName, qName) ;
return ;
}
-
- if ( localName.equals(XMLResults.dfLiteral) )
- {
+
+ if ( localName.equals(XMLResults.dfLiteral) ) {
endElementLiteral(ns, localName, qName) ;
return ;
}
-
- if ( localName.equals(XMLResults.dfBNode) )
- {
+
+ if ( localName.equals(XMLResults.dfBNode) ) {
endElementBNode(ns, localName, qName) ;
return ;
}
-
+
if ( localName.equals(XMLResults.dfUnbound) )
return ;
-
-
+
}
-
- private boolean checkVarName(String cxtMsg)
- {
+
+ private boolean checkVarName(String cxtMsg) {
if ( cxtMsg == null )
cxtMsg = "" ;
-
- if ( varName == null )
- {
- Log.warn(this, "No variable name in scope: "+cxtMsg) ;
+
+ if ( varName == null ) {
+ Log.warn(this, "No variable name in scope: " + cxtMsg) ;
return false ;
}
- if ( !variables.contains(varName) )
- {
- Log.warn(this, "Variable name '"+varName+"'not declared: "+cxtMsg) ;
+ if ( !variables.contains(varName) ) {
+ Log.warn(this, "Variable name '" + varName + "'not declared: " + cxtMsg) ;
return false ;
}
return true ;
-
+
}
-
- private void startElementURI(String ns, String localName, String name, Attributes attrs)
- {
+
+ private void startElementURI(String ns, String localName, String name, Attributes attrs) {
startAccumulate() ;
}
-
- private void endElementURI(String ns, String localName, String name)
- {
+
+ private void endElementURI(String ns, String localName, String name) {
endAccumulate() ;
String uri = buff.toString() ;
Node n = NodeFactory.createURI(uri) ;
- if ( checkVarName("URI: "+uri) )
+ if ( checkVarName("URI: " + uri) )
addBinding(binding, Var.alloc(varName), n) ;
}
-
- private void startElementLiteral(String ns, String localName, String name, Attributes attrs)
- {
+
+ private void startElementLiteral(String ns, String localName, String name, Attributes attrs) {
if ( attrs.getValue("datatype") != null )
datatype = attrs.getValue("datatype") ;
-
+
if ( attrs.getValue("xml:lang") != null )
langTag = attrs.getValue("xml:lang") ;
-
+
startAccumulate() ;
}
-
- private void endElementLiteral(String ns, String localName, String name)
- {
+
+ private void endElementLiteral(String ns, String localName, String name) {
endAccumulate() ;
String lexicalForm = buff.toString() ;
-
+
RDFDatatype dType = null ;
if ( datatype != null )
- dType = TypeMapper.getInstance().getSafeTypeByName(datatype);
-
- Node n = NodeFactory.createLiteral(lexicalForm.toString(), langTag, dType) ;
- if ( checkVarName("Literal: "+FmtUtils.stringForNode(n)) )
+ dType = TypeMapper.getInstance().getSafeTypeByName(datatype) ;
+
+ Node n = NodeFactory.createLiteral(lexicalForm.toString(), langTag, dType) ;
+ if ( checkVarName("Literal: " + FmtUtils.stringForNode(n)) )
addBinding(binding, Var.alloc(varName), n) ;
-
- // Finished value - clear intermediates (the wonders of event based processing)
+
+ // Finished value - clear intermediates (the wonders of event based
+ // processing)
this.datatype = null ;
this.langTag = null ;
this.varName = null ;
return ;
}
-
- private void endElementBoolean()
- {
+
+ private void endElementBoolean() {
endAccumulate() ;
String result = buff.toString() ;
- if ( result.equals("true") )
- {
+ if ( result.equals("true") ) {
this.askResult = true ;
return ;
}
- if ( result.equalsIgnoreCase("false") )
- {
+ if ( result.equalsIgnoreCase("false") ) {
askResult = false ;
return ;
}
- throw new ResultSetException("Unknown boolean value: "+result) ;
+ throw new ResultSetException("Unknown boolean value: " + result) ;
}
-
- private void startElementBNode(String ns, String localName, String name, Attributes attrs)
- {
+
+ private void startElementBNode(String ns, String localName, String name, Attributes attrs) {
startAccumulate() ;
}
-
- private void endElementBNode(String ns, String localName, String name)
- {
+
+ private void endElementBNode(String ns, String localName, String name) {
endAccumulate() ;
String bnodeId = buff.toString() ;
Node node = bNodes.asNode(bnodeId) ;
- if ( checkVarName("BNode: "+bnodeId) )
+ if ( checkVarName("BNode: " + bnodeId) )
addBinding(binding, Var.alloc(varName), node) ;
}
-
-
- private void startAccumulate()
- {
+
+ private void startAccumulate() {
buff.setLength(0) ;
accumulate = true ;
}
-
- private void endAccumulate()
- {
+
+ private void endAccumulate() {
accumulate = false ;
}
-
-
+
@Override
- public void characters(char[] chars, int start, int finish) throws SAXException
- {
- if ( accumulate )
- {
+ public void characters(char[] chars, int start, int finish) throws SAXException {
+ if ( accumulate ) {
if ( buff == null )
buff = new StringBuffer() ;
buff.append(chars, start, finish) ;
}
}
-
+
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
-
+
@Override
public void processingInstruction(String target, String data) throws SAXException {}
-
+
@Override
public void skippedEntity(String name) throws SAXException {}
}
Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputStAX.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputStAX.java?rev=1550681&r1=1550680&r2=1550681&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputStAX.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/XMLInputStAX.java Fri Dec 13 10:40:06 2013
@@ -16,7 +16,8 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.sparql.resultset;
+package com.hp.hpl.jena.sparql.resultset ;
+
import java.io.InputStream ;
import java.io.Reader ;
import java.io.StringReader ;
@@ -52,491 +53,452 @@ import com.hp.hpl.jena.sparql.engine.bin
import com.hp.hpl.jena.sparql.graph.GraphFactory ;
import com.hp.hpl.jena.sparql.util.LabelToNodeMap ;
-/** Code that reads an XML Results format and builds the ARQ structure for the same.
- * Can read result set and boolean result forms.
- * This is a streaming implementation. */
-
-
-class XMLInputStAX extends SPARQLResult
-{
- private static final String XML_NS = ARQConstants.XML_NS ;
-
- public static ResultSet fromXML(InputStream in)
- {
- return fromXML( in, null) ;
- }
-
- public static ResultSet fromXML(InputStream in, Model model)
- {
- XMLInputStAX x = new XMLInputStAX(in, model) ;
+/**
+ * Code that reads an XML Results format and builds the ARQ structure for the
+ * same. Can read result set and boolean result forms. This is a streaming
+ * implementation.
+ */
+
+class XMLInputStAX extends SPARQLResult {
+ private static final String XML_NS = ARQConstants.XML_NS ;
+
+ public static ResultSet fromXML(InputStream in) {
+ return fromXML(in, null) ;
+ }
+
+ public static ResultSet fromXML(InputStream in, Model model) {
+ XMLInputStAX x = new XMLInputStAX(in, model) ;
if ( !x.isResultSet() )
throw new ResultSetException("Not a result set") ;
return x.getResultSet() ;
}
- public static ResultSet fromXML(String str)
- {
+ public static ResultSet fromXML(String str) {
return fromXML(str, null) ;
-
+
}
-
- public static ResultSet fromXML(String str, Model model)
- {
- XMLInputStAX x = new XMLInputStAX(str, model) ;
+
+ public static ResultSet fromXML(String str, Model model) {
+ XMLInputStAX x = new XMLInputStAX(str, model) ;
if ( !x.isResultSet() )
throw new ResultSetException("Not a result set") ;
return x.getResultSet() ;
}
- public static boolean booleanFromXML(InputStream in)
- {
- XMLInputStAX x = new XMLInputStAX(in) ;
+ public static boolean booleanFromXML(InputStream in) {
+ XMLInputStAX x = new XMLInputStAX(in) ;
return x.getBooleanResult() ;
}
- public static boolean booleanFromXML(String str)
- {
- XMLInputStAX x = new XMLInputStAX(str) ;
+ public static boolean booleanFromXML(String str) {
+ XMLInputStAX x = new XMLInputStAX(str) ;
return x.getBooleanResult() ;
}
-
- public XMLInputStAX(InputStream in)
- { this(in, null) ; }
- public XMLInputStAX(InputStream in, Model model)
- {
+ public XMLInputStAX(InputStream in) {
+ this(in, null) ;
+ }
+
+ public XMLInputStAX(InputStream in, Model model) {
XMLInputFactory xf = XMLInputFactory.newInstance() ;
- try
- {
+ try {
XMLStreamReader xReader = xf.createXMLStreamReader(in) ;
worker(xReader, model) ;
- } catch (XMLStreamException e)
- {
+ } catch (XMLStreamException e) {
throw new ResultSetException("Can't initialize StAX parsing engine", e) ;
- }
- catch (Exception ex)
- {
+ } catch (Exception ex) {
throw new ResultSetException("Failed when initializing the StAX parsing engine", ex) ;
}
}
- public XMLInputStAX(String str)
- { this(str, null) ; }
-
- public XMLInputStAX(String str, Model model)
- {
+ public XMLInputStAX(String str) {
+ this(str, null) ;
+ }
+
+ public XMLInputStAX(String str, Model model) {
XMLInputFactory xf = XMLInputFactory.newInstance() ;
- try
- {
+ try {
Reader r = new StringReader(str) ;
XMLStreamReader xReader = xf.createXMLStreamReader(r) ;
worker(xReader, model) ;
- } catch (XMLStreamException e)
- {
+ } catch (XMLStreamException e) {
throw new ResultSetException("Can't initialize StAX parsing engine", e) ;
- }
- catch (Exception ex)
- {
+ } catch (Exception ex) {
throw new ResultSetException("Failed when initializing the StAX parsing engine", ex) ;
}
}
- private void worker(XMLStreamReader xReader, Model model)
- {
+ private void worker(XMLStreamReader xReader, Model model) {
if ( model == null )
model = GraphFactory.makeJenaDefaultModel() ;
-
+
ResultSetStAX rss = new ResultSetStAX(xReader, model) ;
if ( rss.isResultSet )
set(rss) ;
else
set(rss.askResult) ;
}
-
- //private XMLInputStAX()
-
+
+ // private XMLInputStAX()
+
// -------- Result Set
-
- class ResultSetStAX implements ResultSet, Closeable
- {
+ static class ResultSetStAX implements ResultSet, Closeable {
// ResultSet variables
- QuerySolution current = null ;
- XMLStreamReader parser = null ;
- List<String> variables = new ArrayList<String>() ;
- Binding binding = null ; // Current binding
- //RefBoolean inputGraphLabels = new RefBoolean(ARQ.inputGraphBNodeLabels, false) ;
- boolean inputGraphLabels = ARQ.isTrue(ARQ.inputGraphBNodeLabels) ;
-
- LabelToNodeMap bNodes = LabelToNodeMap.createBNodeMap() ;
-
+ QuerySolution current = null ;
+ XMLStreamReader parser = null ;
+ List<String> variables = new ArrayList<String>() ;
+ Binding binding = null ; // Current
+ // binding
+ // RefBoolean inputGraphLabels = new
+ // RefBoolean(ARQ.inputGraphBNodeLabels, false) ;
+ boolean inputGraphLabels = ARQ.isTrue(ARQ.inputGraphBNodeLabels) ;
+
+ LabelToNodeMap bNodes = LabelToNodeMap.createBNodeMap() ;
+
// Type
- boolean isResultSet = false ;
+ boolean isResultSet = false ;
// Result set
- boolean ordered = false ;
- boolean distinct = false ;
- boolean finished = false ;
- Model model = null ;
- int row = 0 ;
-
+ boolean ordered = false ;
+ boolean distinct = false ;
+ boolean finished = false ;
+ Model model = null ;
+ int row = 0 ;
+
// boolean
- boolean askResult = false ;
+ boolean askResult = false ;
- ResultSetStAX(XMLStreamReader reader, Model model)
- {
+ ResultSetStAX(XMLStreamReader reader, Model model) {
parser = reader ;
this.model = model ;
init() ;
}
-
- private void init()
- {
- try {
- // Because all the tags are different, we could use one big switch statement!
- skipTo(XMLResults.dfHead) ;
- processHead() ;
- skipTo(new String[]{XMLResults.dfResults, XMLResults.dfBoolean}, new String[]{XMLResults.dfResults}) ;
- // Next should be a <result>, <boolean> element or </results>
-
- // Need to decide what sort of thing we are reading.
-
- String tag = parser.getLocalName() ;
- if ( tag.equals(XMLResults.dfResults) )
- {
- isResultSet = true ;
- processResults() ;
- }
- if ( tag.equals(XMLResults.dfBoolean) )
- {
- isResultSet = false ;
- processBoolean() ;
- }
-
- } catch (XMLStreamException ex)
- {
- Log.warn(this ,"XMLStreamException: "+ex.getMessage(), ex) ;
- }
- }
-
- @Override
- public boolean hasNext()
- {
- if ( ! isResultSet )
- throw new ResultSetException("Not an XML result set") ;
-
- if ( finished )
- return false ;
-
- try {
- if ( binding == null )
- binding = getOneSolution() ;
- } catch (XMLStreamException ex)
- {
- staxError("XMLStreamException: "+ex.getMessage(), ex) ;
- }
- boolean b = (binding != null) ;
- //parser.close() ; // Some way to close the input stream.
- if (!b)
- close();
- return b ;
- }
-
- @Override
- public QuerySolution next()
- {
- return nextSolution() ;
- }
-
- @Override
- public Binding nextBinding()
- {
- if ( finished )
- throw new NoSuchElementException("End of XML Results") ;
- if ( ! hasNext() )
- throw new NoSuchElementException("End of XML Results") ;
- Binding r = binding ;
- row++ ;
- binding = null ;
- return r ;
- }
-
- @Override
- public QuerySolution nextSolution()
- {
- Binding r = nextBinding() ;
- ResultBinding currentEnv = new ResultBinding(model, r) ;
- return currentEnv ;
- }
-
- @Override
- public int getRowNumber()
- {
- return row ;
- }
-
- @Override
- public List<String> getResultVars()
- {
- return variables ;
- }
-
- public boolean isOrdered() { return ordered ; }
-
- public boolean isDistinct() { return distinct ; }
-
- // No model - it was from a stream
- @Override
- public Model getResourceModel() { return null ; }
-
- @Override
- public void remove()
- {
- throw new UnsupportedOperationException(XMLInputStAX.class.getName()) ;
- }
-
- @Override
- public void close()
- { finished = true ; }
-
- // -------- Boolean stuff
-
- private void processBoolean() throws XMLStreamException
- {
- // At start of <boolean>
- String s = parser.getElementText() ;
- if ( s.equalsIgnoreCase("true") )
- {
- askResult = true ;
- return ;
+
+ private void init() {
+ try {
+ // Because all the tags are different, we could use one big
+ // switch statement!
+ skipTo(XMLResults.dfHead) ;
+ processHead() ;
+ skipTo(new String[]{XMLResults.dfResults, XMLResults.dfBoolean}, new String[]{XMLResults.dfResults}) ;
+ // Next should be a <result>, <boolean> element or </results>
+
+ // Need to decide what sort of thing we are reading.
+
+ String tag = parser.getLocalName() ;
+ if ( tag.equals(XMLResults.dfResults) ) {
+ isResultSet = true ;
+ processResults() ;
+ }
+ if ( tag.equals(XMLResults.dfBoolean) ) {
+ isResultSet = false ;
+ processBoolean() ;
+ }
+
+ } catch (XMLStreamException ex) {
+ Log.warn(this, "XMLStreamException: " + ex.getMessage(), ex) ;
+ }
}
- if ( s.equalsIgnoreCase("false") )
- {
- askResult = false ;
- return ;
+
+ @Override
+ public boolean hasNext() {
+ if ( !isResultSet )
+ throw new ResultSetException("Not an XML result set") ;
+
+ if ( finished )
+ return false ;
+
+ try {
+ if ( binding == null )
+ binding = getOneSolution() ;
+ } catch (XMLStreamException ex) {
+ staxError("XMLStreamException: " + ex.getMessage(), ex) ;
+ }
+ boolean b = (binding != null) ;
+ if ( !b )
+ close() ;
+ return b ;
+ }
+
+ @Override
+ public QuerySolution next() {
+ return nextSolution() ;
+ }
+
+ @Override
+ public Binding nextBinding() {
+ if ( finished )
+ throw new NoSuchElementException("End of XML Results") ;
+ if ( !hasNext() )
+ throw new NoSuchElementException("End of XML Results") ;
+ Binding r = binding ;
+ row++ ;
+ binding = null ;
+ return r ;
+ }
+
+ @Override
+ public QuerySolution nextSolution() {
+ Binding r = nextBinding() ;
+ ResultBinding currentEnv = new ResultBinding(model, r) ;
+ return currentEnv ;
+ }
+
+ @Override
+ public int getRowNumber() {
+ return row ;
+ }
+
+ @Override
+ public List<String> getResultVars() {
+ return variables ;
+ }
+
+ public boolean isOrdered() {
+ return ordered ;
+ }
+
+ public boolean isDistinct() {
+ return distinct ;
+ }
+
+ // No model - it was from a stream
+ @Override
+ public Model getResourceModel() {
+ return null ;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException(XMLInputStAX.class.getName()) ;
+ }
+
+ @Override
+ public void close() {
+ finished = true ;
+ try {
+ parser.close() ;
+ } catch (XMLStreamException ex) {}
+ }
+
+ // -------- Boolean stuff
+
+ private void processBoolean() throws XMLStreamException {
+ try {
+ // At start of <boolean>
+ String s = parser.getElementText() ;
+ if ( s.equalsIgnoreCase("true") ) {
+ askResult = true ;
+ return ;
+ }
+ if ( s.equalsIgnoreCase("false") ) {
+ askResult = false ;
+ return ;
+ }
+ throw new ResultSetException("Unknown boolean value: " + s) ;
+ } finally {
+ close() ;
+ }
+ }
+
+ // --------
+
+ private void skipTo(String tag1) throws XMLStreamException {
+ skipTo(new String[]{tag1}, null) ;
}
- throw new ResultSetException("Unknown boolean value: "+s) ;
- }
-
- // --------
- private void skipTo(String tag1) throws XMLStreamException
- { skipTo(new String[]{tag1}, null) ; }
-
- private void skipTo(String[] startElementNames, String[] stopElementNames) throws XMLStreamException
- {
- boolean found = false ;
- loop:
- while(parser.hasNext())
- {
- int event = parser.next();
- switch (event)
- {
- case XMLStreamConstants.END_DOCUMENT:
+ private void skipTo(String[] startElementNames, String[] stopElementNames) throws XMLStreamException {
+ boolean found = false ;
+ loop : while (parser.hasNext()) {
+ int event = parser.next() ;
+ switch (event) {
+ case XMLStreamConstants.END_DOCUMENT :
break loop ;
- case XMLStreamConstants.END_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT :
if ( stopElementNames == null )
break ;
-
+
String endTag = parser.getLocalName() ;
if ( endTag != null && containsName(stopElementNames, endTag) )
return ;
break ;
- case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.START_ELEMENT :
if ( startElementNames == null )
break ;
QName qname = parser.getName() ;
- if ( ! qname.getNamespaceURI().equals(XMLResults.baseNamespace) )
- staxError("skipToHead: Unexpected tag: "+qname) ;
- if ( containsName(startElementNames, qname.getLocalPart()))
- return ;
+ if ( !qname.getNamespaceURI().equals(XMLResults.baseNamespace) )
+ staxError("skipToHead: Unexpected tag: " + qname) ;
+ if ( containsName(startElementNames, qname.getLocalPart()) )
+ return ;
break ;
- default:
+ default :
// Skip stuff
}
}
-
- if ( ! found )
- {
- String s1 = "" ;
- if ( startElementNames != null )
- s1 = StrUtils.strjoin(", ",startElementNames) ;
-
- String s2 = "" ;
- if ( stopElementNames != null )
- s2 = StrUtils.strjoin(", ",stopElementNames) ;
- Log.warn(this, "Failed to find start and stop of specified elements: "+s1+" :: "+s2) ;
- }
- }
-
- private boolean containsName(String[] elementNames, String eName)
- {
- for ( int i = 0 ; i < elementNames.length ; i++ )
- {
- String s = elementNames[i] ;
- if ( s.equals(eName) )
- return true ;
- }
- return false ;
- }
-
- private void processHead() throws XMLStreamException
- {
- // Should be at the start of head
-
- loop:
- while(parser.hasNext())
- {
- int event = parser.next();
+
+ if ( !found ) {
+ String s1 = "" ;
+ if ( startElementNames != null )
+ s1 = StrUtils.strjoin(", ", startElementNames) ;
+
+ String s2 = "" ;
+ if ( stopElementNames != null )
+ s2 = StrUtils.strjoin(", ", stopElementNames) ;
+ Log.warn(this, "Failed to find start and stop of specified elements: " + s1 + " :: " + s2) ;
+ }
+ }
+
+ private boolean containsName(String[] elementNames, String eName) {
+ for (int i = 0; i < elementNames.length; i++) {
+ String s = elementNames[i] ;
+ if ( s.equals(eName) )
+ return true ;
+ }
+ return false ;
+ }
+
+ private void processHead() throws XMLStreamException {
+ // Should be at the start of head
+
+ loop : while (parser.hasNext()) {
+ int event = parser.next() ;
String tag = null ;
-
- switch (event)
- {
- case XMLStreamConstants.END_DOCUMENT:
- break loop ;
- case XMLStreamConstants.END_ELEMENT:
+
+ switch (event) {
+ case XMLStreamConstants.END_DOCUMENT :
+ break loop ;
+ case XMLStreamConstants.END_ELEMENT :
tag = parser.getLocalName() ;
- if ( isTag(tag, XMLResults.dfHead ) )
+ if ( isTag(tag, XMLResults.dfHead) )
break loop ;
break ;
- case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.START_ELEMENT :
tag = parser.getLocalName() ;
- if ( isTag(tag, XMLResults.dfHead ) )
- break ; // This switch statement
- if ( isTag(tag, XMLResults.dfVariable ) )
- {
+ if ( isTag(tag, XMLResults.dfHead) )
+ break ; // This switch statement
+ if ( isTag(tag, XMLResults.dfVariable) ) {
String varname = parser.getAttributeValue(null, XMLResults.dfAttrVarName) ;
variables.add(varname) ;
break ;
}
- if ( isTag(tag, XMLResults.dfLink ) )
+ if ( isTag(tag, XMLResults.dfLink) )
break ;
-
- staxError("Unknown XML element: "+tag) ;
+
+ staxError("Unknown XML element: " + tag) ;
break ;
- default:
+ default :
}
}
- }
-
- // -------- Result Set
-
- private void processResults()
- { return ; }
-
- private Binding getOneSolution() throws XMLStreamException
- {
- // At the start of <result>
- BindingMap binding = BindingFactory.create() ;
- String varName = null ;
-
- while(parser.hasNext())
- {
- int event = parser.next();
- String tag = null ;
-
- switch (event)
- {
- case XMLStreamConstants.END_DOCUMENT:
- staxError("End of document while processing solution") ;
- return null ;
- case XMLStreamConstants.END_ELEMENT:
- tag = parser.getLocalName() ;
- if ( isTag(tag, XMLResults.dfSolution) )
- return binding ;
- if ( isTag(tag, XMLResults.dfResults) )
- // Hit the end of solutions.
+ }
+
+ // -------- Result Set
+
+ private void processResults() {
+ return ;
+ }
+
+ private Binding getOneSolution() throws XMLStreamException {
+ // At the start of <result>
+ BindingMap binding = BindingFactory.create() ;
+ String varName = null ;
+
+ while (parser.hasNext()) {
+ int event = parser.next() ;
+ String tag = null ;
+
+ switch (event) {
+ case XMLStreamConstants.END_DOCUMENT :
+ staxError("End of document while processing solution") ;
return null ;
- break ;
- case XMLStreamConstants.START_ELEMENT:
- tag = parser.getLocalName() ;
- if ( isTag(tag, XMLResults.dfSolution) )
- {
- binding = BindingFactory.create() ;
- break ;
- }
- if ( isTag(tag, XMLResults.dfBinding ))
- {
- varName = parser.getAttributeValue(null, XMLResults.dfAttrVarName) ;
- break ;
- }
- // URI, literal, bNode, unbound.
- if ( isTag(tag, XMLResults.dfBNode) )
- {
- String label = parser.getElementText() ;
- Node node = null ;
- //if ( inputGraphLabels.getValue() )
- if ( inputGraphLabels )
- node = NodeFactory.createAnon(new AnonId(label)) ;
- else
- node = bNodes.asNode(label) ;
- addBinding(binding, Var.alloc(varName), node) ;
- break ;
- }
-
- if ( isTag(tag, XMLResults.dfLiteral) )
- {
- String datatype = parser.getAttributeValue(null, XMLResults.dfAttrDatatype) ;
-
- //String langTag = parser.getAttributeValue(null, "lang") ;
-
- // Woodstox needs XML_NS despite the javadoc of StAX
- // "If the namespaceURI is null the namespace is not checked for equality"
- // StAX(.codehaus.org) copes both ways round
- String langTag = parser.getAttributeValue(XML_NS, "lang") ;
-
- // Works for XML literals (returning them as a string)
- String text = parser.getElementText() ;
-
- RDFDatatype dType = null ;
- if ( datatype != null )
- dType = TypeMapper.getInstance().getSafeTypeByName(datatype);
-
- Node n = NodeFactory.createLiteral(text, langTag, dType) ;
- if ( varName == null )
- throw new ResultSetException("No name for variable") ;
- addBinding(binding, Var.alloc(varName), n) ;
- break ;
- }
-
-
- if ( isTag(tag, XMLResults.dfUnbound) )
- {
+ case XMLStreamConstants.END_ELEMENT :
+ tag = parser.getLocalName() ;
+ if ( isTag(tag, XMLResults.dfSolution) )
+ return binding ;
+ if ( isTag(tag, XMLResults.dfResults) )
+ // Hit the end of solutions.
+ return null ;
break ;
- }
- if ( isTag(tag, XMLResults.dfURI) )
- {
- String uri = parser.getElementText() ;
- Node node = NodeFactory.createURI(uri) ;
- addBinding(binding, Var.alloc(varName), node) ;
+ case XMLStreamConstants.START_ELEMENT :
+ tag = parser.getLocalName() ;
+ if ( isTag(tag, XMLResults.dfSolution) ) {
+ binding = BindingFactory.create() ;
+ break ;
+ }
+ if ( isTag(tag, XMLResults.dfBinding) ) {
+ varName = parser.getAttributeValue(null, XMLResults.dfAttrVarName) ;
+ break ;
+ }
+ // URI, literal, bNode, unbound.
+ if ( isTag(tag, XMLResults.dfBNode) ) {
+ String label = parser.getElementText() ;
+ Node node = null ;
+ // if ( inputGraphLabels.getValue() )
+ if ( inputGraphLabels )
+ node = NodeFactory.createAnon(new AnonId(label)) ;
+ else
+ node = bNodes.asNode(label) ;
+ addBinding(binding, Var.alloc(varName), node) ;
+ break ;
+ }
+
+ if ( isTag(tag, XMLResults.dfLiteral) ) {
+ String datatype = parser.getAttributeValue(null, XMLResults.dfAttrDatatype) ;
+
+ // String langTag = parser.getAttributeValue(null,
+ // "lang") ;
+
+ // Woodstox needs XML_NS despite the javadoc of StAX
+ // "If the namespaceURI is null the namespace is not checked for equality"
+ // StAX(.codehaus.org) copes both ways round
+ String langTag = parser.getAttributeValue(XML_NS, "lang") ;
+
+ // Works for XML literals (returning them as a
+ // string)
+ String text = parser.getElementText() ;
+
+ RDFDatatype dType = null ;
+ if ( datatype != null )
+ dType = TypeMapper.getInstance().getSafeTypeByName(datatype) ;
+
+ Node n = NodeFactory.createLiteral(text, langTag, dType) ;
+ if ( varName == null )
+ throw new ResultSetException("No name for variable") ;
+ addBinding(binding, Var.alloc(varName), n) ;
+ break ;
+ }
+
+ if ( isTag(tag, XMLResults.dfUnbound) ) {
+ break ;
+ }
+ if ( isTag(tag, XMLResults.dfURI) ) {
+ String uri = parser.getElementText() ;
+ Node node = NodeFactory.createURI(uri) ;
+ addBinding(binding, Var.alloc(varName), node) ;
+ break ;
+ }
break ;
- }
- break ;
- default:
+ default :
+ }
}
+ staxError("getOneSolution: Hit end unexpectedly") ;
+ return null ;
}
- staxError("getOneSolution: Hit end unexpectedly") ;
- return null ;
- }
-
- private boolean isTag(String localName, String expectedName)
- {
- if ( ! parser.getNamespaceURI().equals( XMLResults.baseNamespace ))
- return false ;
- return localName.equals(expectedName) ;
- }
+ private boolean isTag(String localName, String expectedName) {
+ if ( !parser.getNamespaceURI().equals(XMLResults.baseNamespace) )
+ return false ;
+ return localName.equals(expectedName) ;
+ }
- private void staxError(String msg)
- {
- Log.warn(this, "StAX error: "+msg) ;
- throw new ResultSetException(msg) ;
- }
+ private void staxError(String msg) {
+ Log.warn(this, "StAX error: " + msg) ;
+ throw new ResultSetException(msg) ;
+ }
- private void staxError(String msg, Throwable th)
- {
- Log.warn(this, "StAX error: "+msg, th) ;
- throw new ResultSetException(msg, th) ;
- }
+ private void staxError(String msg, Throwable th) {
+ Log.warn(this, "StAX error: " + msg, th) ;
+ throw new ResultSetException(msg, th) ;
+ }
}
}