01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.resultio.sparqlxml;
07:
08: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BOOLEAN_TAG;
09: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BOOLEAN_TRUE;
10: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.HEAD_TAG;
11: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.NAMESPACE;
12: import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.ROOT_TAG;
13:
14: import java.io.IOException;
15: import java.io.OutputStream;
16:
17: import info.aduna.xml.XMLWriter;
18:
19: import org.openrdf.query.resultio.BooleanQueryResultFormat;
20: import org.openrdf.query.resultio.BooleanQueryResultWriter;
21:
22: /**
23: * A {@link BooleanQueryResultWriter} that writes boolean query results in the
24: * <a href="http://www.w3.org/TR/rdf-sparql-XMLres/">SPARQL Query Results XML
25: * Format</a>.
26: */
27: public class SPARQLBooleanXMLWriter implements BooleanQueryResultWriter {
28:
29: /*-----------*
30: * Variables *
31: *-----------*/
32:
33: /**
34: * XMLWriter to write XML to.
35: */
36: private XMLWriter xmlWriter;
37:
38: /*--------------*
39: * Constructors *
40: *--------------*/
41:
42: public SPARQLBooleanXMLWriter(OutputStream out) {
43: this (new XMLWriter(out));
44: }
45:
46: public SPARQLBooleanXMLWriter(XMLWriter xmlWriter) {
47: this .xmlWriter = xmlWriter;
48: this .xmlWriter.setPrettyPrint(true);
49: }
50:
51: /*---------*
52: * Methods *
53: *---------*/
54:
55: /**
56: * Enables/disables addition of indentation characters and newlines in the
57: * XML document. By default, pretty-printing is set to <tt>true</tt>. If
58: * set to <tt>false</tt>, no indentation and newlines are added to the XML
59: * document. This method has to be used before writing starts (that is,
60: * before {@link #write} is called).
61: */
62: public void setPrettyPrint(boolean prettyPrint) {
63: xmlWriter.setPrettyPrint(prettyPrint);
64: }
65:
66: public final BooleanQueryResultFormat getBooleanQueryResultFormat() {
67: return BooleanQueryResultFormat.SPARQL;
68: }
69:
70: public void write(boolean value) throws IOException {
71: xmlWriter.startDocument();
72: xmlWriter.setAttribute("xmlns", NAMESPACE);
73: xmlWriter.startTag(ROOT_TAG);
74: xmlWriter.emptyElement(HEAD_TAG);
75:
76: if (value) {
77: xmlWriter.textElement(BOOLEAN_TAG, BOOLEAN_TRUE);
78: } else {
79: xmlWriter.textElement(BOOLEAN_TAG,
80: SPARQLResultsXMLConstants.BOOLEAN_FALSE);
81: }
82:
83: xmlWriter.endTag(ROOT_TAG);
84: xmlWriter.endDocument();
85: }
86: }
|