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.http.protocol.transaction;
007:
008: import java.io.IOException;
009: import java.io.OutputStream;
010:
011: import info.aduna.xml.XMLWriter;
012:
013: import org.openrdf.http.protocol.transaction.operations.AddStatementOperation;
014: import org.openrdf.http.protocol.transaction.operations.ClearNamespacesOperation;
015: import org.openrdf.http.protocol.transaction.operations.ClearOperation;
016: import org.openrdf.http.protocol.transaction.operations.RemoveNamespaceOperation;
017: import org.openrdf.http.protocol.transaction.operations.RemoveStatementsOperation;
018: import org.openrdf.http.protocol.transaction.operations.SetNamespaceOperation;
019: import org.openrdf.http.protocol.transaction.operations.StatementOperation;
020: import org.openrdf.http.protocol.transaction.operations.TransactionOperation;
021: import org.openrdf.model.BNode;
022: import org.openrdf.model.Literal;
023: import org.openrdf.model.Resource;
024: import org.openrdf.model.URI;
025: import org.openrdf.model.Value;
026:
027: /**
028: * Serializes of an RDF transaction.
029: */
030: public class TransactionWriter {
031:
032: public TransactionWriter() {
033: }
034:
035: /**
036: * serialize the passed list of operations to the passed writer.
037: *
038: * @param txn
039: * the operations
040: * @param writer
041: * the writer to write to
042: * @throws IllegalArgumentException
043: * when one of the parameters is null
044: */
045: public void serialize(Iterable<? extends TransactionOperation> txn,
046: OutputStream out) throws IOException {
047: assert txn != null : "operation list must not be null";
048: assert out != null : "output stream must not be null";
049:
050: XMLWriter xmlWriter = new XMLWriter(out);
051: xmlWriter.setPrettyPrint(true);
052:
053: xmlWriter.startDocument();
054: xmlWriter.startTag(TransactionXMLConstants.TRANSACTION_TAG);
055:
056: for (TransactionOperation op : txn) {
057: serialize(op, xmlWriter);
058: }
059:
060: xmlWriter.endTag(TransactionXMLConstants.TRANSACTION_TAG);
061: xmlWriter.endDocument();
062: }
063:
064: /**
065: * Serializes the supplied operation.
066: *
067: * @param op
068: * The operation to serialize
069: */
070: private void serialize(TransactionOperation op, XMLWriter xmlWriter)
071: throws IOException {
072: if (op instanceof AddStatementOperation) {
073: serialize((AddStatementOperation) op, xmlWriter);
074: } else if (op instanceof RemoveStatementsOperation) {
075: serialize((RemoveStatementsOperation) op, xmlWriter);
076: } else if (op instanceof ClearOperation) {
077: serialize((ClearOperation) op, xmlWriter);
078: } else if (op instanceof SetNamespaceOperation) {
079: serialize((SetNamespaceOperation) op, xmlWriter);
080: } else if (op instanceof RemoveNamespaceOperation) {
081: serialize((RemoveNamespaceOperation) op, xmlWriter);
082: } else if (op instanceof ClearNamespacesOperation) {
083: serialize((ClearNamespacesOperation) op, xmlWriter);
084: } else if (op == null) {
085: // ignore(?)
086: } else {
087: throw new IllegalArgumentException(
088: "Unknown operation type: " + op.getClass());
089: }
090: }
091:
092: private void serialize(AddStatementOperation op, XMLWriter xmlWriter)
093: throws IOException {
094: xmlWriter.startTag(TransactionXMLConstants.ADD_STATEMENT_TAG);
095: serialize((StatementOperation) op, xmlWriter);
096: xmlWriter.endTag(TransactionXMLConstants.ADD_STATEMENT_TAG);
097: }
098:
099: private void serialize(RemoveStatementsOperation op,
100: XMLWriter xmlWriter) throws IOException {
101: xmlWriter
102: .startTag(TransactionXMLConstants.REMOVE_STATEMENTS_TAG);
103: serialize((StatementOperation) op, xmlWriter);
104: xmlWriter.endTag(TransactionXMLConstants.REMOVE_STATEMENTS_TAG);
105: }
106:
107: private void serialize(StatementOperation op, XMLWriter xmlWriter)
108: throws IOException {
109: serialize(op.getSubject(), xmlWriter);
110: serialize(op.getPredicate(), xmlWriter);
111: serialize(op.getObject(), xmlWriter);
112: serialize(op.getContexts(), xmlWriter);
113: }
114:
115: private void serialize(ClearOperation op, XMLWriter xmlWriter)
116: throws IOException {
117: xmlWriter.startTag(TransactionXMLConstants.CLEAR_TAG);
118: serialize(op.getContexts(), xmlWriter);
119: xmlWriter.endTag(TransactionXMLConstants.CLEAR_TAG);
120: }
121:
122: private void serialize(SetNamespaceOperation op, XMLWriter xmlWriter)
123: throws IOException {
124: xmlWriter.setAttribute(TransactionXMLConstants.PREFIX_ATT, op
125: .getPrefix());
126: xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, op
127: .getName());
128: xmlWriter
129: .emptyElement(TransactionXMLConstants.SET_NAMESPACE_TAG);
130: }
131:
132: private void serialize(RemoveNamespaceOperation op,
133: XMLWriter xmlWriter) throws IOException {
134: xmlWriter.setAttribute(TransactionXMLConstants.PREFIX_ATT, op
135: .getPrefix());
136: xmlWriter
137: .emptyElement(TransactionXMLConstants.REMOVE_NAMESPACE_TAG);
138: }
139:
140: private void serialize(ClearNamespacesOperation op,
141: XMLWriter xmlWriter) throws IOException {
142: xmlWriter
143: .emptyElement(TransactionXMLConstants.CLEAR_NAMESPACES_TAG);
144: }
145:
146: private void serialize(Resource[] contexts, XMLWriter xmlWriter)
147: throws IOException {
148: if (contexts.length > 0) {
149: xmlWriter.startTag(TransactionXMLConstants.CONTEXTS_TAG);
150: for (Resource context : contexts) {
151: serialize(context, xmlWriter);
152: }
153: xmlWriter.endTag(TransactionXMLConstants.CONTEXTS_TAG);
154: } else {
155: xmlWriter
156: .emptyElement(TransactionXMLConstants.CONTEXTS_TAG);
157: }
158: }
159:
160: private void serialize(Value value, XMLWriter xmlWriter)
161: throws IOException {
162: if (value instanceof Resource) {
163: serialize((Resource) value, xmlWriter);
164: } else if (value instanceof Literal) {
165: serialize((Literal) value, xmlWriter);
166: } else if (value == null) {
167: serializeNull(xmlWriter);
168: } else {
169: throw new IllegalArgumentException("Unknown value type: "
170: + value.getClass().toString());
171: }
172: }
173:
174: private void serialize(Resource resource, XMLWriter xmlWriter)
175: throws IOException {
176: if (resource instanceof URI) {
177: serialize((URI) resource, xmlWriter);
178: } else if (resource instanceof BNode) {
179: serialize((BNode) resource, xmlWriter);
180: } else if (resource == null) {
181: serializeNull(xmlWriter);
182: } else {
183: throw new IllegalArgumentException(
184: "Unknown resource type: "
185: + resource.getClass().toString());
186: }
187: }
188:
189: private void serialize(URI uri, XMLWriter xmlWriter)
190: throws IOException {
191: if (uri != null) {
192: xmlWriter.textElement(TransactionXMLConstants.URI_TAG, uri
193: .toString());
194: } else {
195: serializeNull(xmlWriter);
196: }
197: }
198:
199: private void serialize(BNode bnode, XMLWriter xmlWriter)
200: throws IOException {
201: if (bnode != null) {
202: xmlWriter.textElement(TransactionXMLConstants.BNODE_TAG,
203: bnode.getID());
204: } else {
205: serializeNull(xmlWriter);
206: }
207: }
208:
209: private void serialize(Literal literal, XMLWriter xmlWriter)
210: throws IOException {
211: if (literal != null) {
212: if (literal.getLanguage() != null) {
213: xmlWriter.setAttribute(
214: TransactionXMLConstants.LANG_ATT, literal
215: .getLanguage());
216: }
217: if (literal.getDatatype() != null) {
218: xmlWriter.setAttribute(
219: TransactionXMLConstants.DATATYPE_ATT, literal
220: .getDatatype().toString());
221: }
222: xmlWriter.textElement(TransactionXMLConstants.LITERAL_TAG,
223: literal.getLabel());
224: } else {
225: serializeNull(xmlWriter);
226: }
227: }
228:
229: private void serializeNull(XMLWriter xmlWriter) throws IOException {
230: xmlWriter.emptyElement(TransactionXMLConstants.NULL_TAG);
231: }
232: }
|