001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-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.BINDING_NAME_ATT;
009: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BINDING_TAG;
010: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BNODE_TAG;
011: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.HEAD_TAG;
012: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_DATATYPE_ATT;
013: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_LANG_ATT;
014: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_TAG;
015: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.NAMESPACE;
016: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.RESULT_SET_TAG;
017: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.RESULT_TAG;
018: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.ROOT_TAG;
019: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.URI_TAG;
020: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.VAR_NAME_ATT;
021: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.VAR_TAG;
022:
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.util.List;
026:
027: import info.aduna.xml.XMLWriter;
028:
029: import org.openrdf.model.BNode;
030: import org.openrdf.model.Literal;
031: import org.openrdf.model.URI;
032: import org.openrdf.model.Value;
033: import org.openrdf.query.Binding;
034: import org.openrdf.query.BindingSet;
035: import org.openrdf.query.TupleQueryResultHandlerException;
036: import org.openrdf.query.resultio.TupleQueryResultFormat;
037: import org.openrdf.query.resultio.TupleQueryResultWriter;
038:
039: /**
040: * A {@link TupleQueryResultWriter} that writes tuple query results in the <a
041: * href="http://www.w3.org/TR/rdf-sparql-XMLres/">SPARQL Query Results XML
042: * Format</a>.
043: */
044: public class SPARQLResultsXMLWriter implements TupleQueryResultWriter {
045:
046: /*-----------*
047: * Variables *
048: *-----------*/
049:
050: /**
051: * XMLWriter to write XML to.
052: */
053: private XMLWriter xmlWriter;
054:
055: /*--------------*
056: * Constructors *
057: *--------------*/
058:
059: public SPARQLResultsXMLWriter(OutputStream out) {
060: this (new XMLWriter(out));
061: }
062:
063: public SPARQLResultsXMLWriter(XMLWriter xmlWriter) {
064: this .xmlWriter = xmlWriter;
065: this .xmlWriter.setPrettyPrint(true);
066: }
067:
068: /*---------*
069: * Methods *
070: *---------*/
071:
072: public final TupleQueryResultFormat getTupleQueryResultFormat() {
073: return TupleQueryResultFormat.SPARQL;
074: }
075:
076: /**
077: * Enables/disables addition of indentation characters and newlines in the
078: * XML document. By default, pretty-printing is set to <tt>true</tt>. If
079: * set to <tt>false</tt>, no indentation and newlines are added to the XML
080: * document. This method has to be used before writing starts (that is,
081: * before {@link #startTupleSet} is called).
082: */
083: public void setPrettyPrint(boolean prettyPrint) {
084: xmlWriter.setPrettyPrint(prettyPrint);
085: }
086:
087: public void startQueryResult(List<String> bindingNames)
088: throws TupleQueryResultHandlerException {
089: try {
090: xmlWriter.startDocument();
091:
092: xmlWriter.setAttribute("xmlns", NAMESPACE);
093: xmlWriter.startTag(ROOT_TAG);
094:
095: // Write header
096: xmlWriter.startTag(HEAD_TAG);
097: for (String name : bindingNames) {
098: xmlWriter.setAttribute(VAR_NAME_ATT, name);
099: xmlWriter.emptyElement(VAR_TAG);
100: }
101: xmlWriter.endTag(HEAD_TAG);
102:
103: // Write start of results
104: xmlWriter.startTag(RESULT_SET_TAG);
105: } catch (IOException e) {
106: throw new TupleQueryResultHandlerException(e);
107: }
108: }
109:
110: public void endQueryResult()
111: throws TupleQueryResultHandlerException {
112: try {
113: xmlWriter.endTag(RESULT_SET_TAG);
114: xmlWriter.endTag(ROOT_TAG);
115:
116: xmlWriter.endDocument();
117: } catch (IOException e) {
118: throw new TupleQueryResultHandlerException(e);
119: }
120: }
121:
122: public void handleSolution(BindingSet bindingSet)
123: throws TupleQueryResultHandlerException {
124: try {
125: xmlWriter.startTag(RESULT_TAG);
126:
127: for (Binding binding : bindingSet) {
128: xmlWriter.setAttribute(BINDING_NAME_ATT, binding
129: .getName());
130: xmlWriter.startTag(BINDING_TAG);
131:
132: writeValue(binding.getValue());
133:
134: xmlWriter.endTag(BINDING_TAG);
135: }
136:
137: xmlWriter.endTag(RESULT_TAG);
138: } catch (IOException e) {
139: throw new TupleQueryResultHandlerException(e);
140: }
141: }
142:
143: private void writeValue(Value value) throws IOException {
144: if (value instanceof URI) {
145: writeURI((URI) value);
146: } else if (value instanceof BNode) {
147: writeBNode((BNode) value);
148: } else if (value instanceof Literal) {
149: writeLiteral((Literal) value);
150: }
151: }
152:
153: private void writeURI(URI uri) throws IOException {
154: xmlWriter.textElement(URI_TAG, uri.toString());
155: }
156:
157: private void writeBNode(BNode bNode) throws IOException {
158: xmlWriter.textElement(BNODE_TAG, bNode.getID());
159: }
160:
161: private void writeLiteral(Literal literal) throws IOException {
162: if (literal.getLanguage() != null) {
163: xmlWriter.setAttribute(LITERAL_LANG_ATT, literal
164: .getLanguage());
165: } else if (literal.getDatatype() != null) {
166: URI datatype = literal.getDatatype();
167: xmlWriter.setAttribute(LITERAL_DATATYPE_ATT, datatype
168: .toString());
169: }
170:
171: xmlWriter.textElement(LITERAL_TAG, literal.getLabel());
172: }
173: }
|