This project has retired. For details please refer to its
Attic page.
XMLDocumentBase xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.server.impl.atompub;
2021import java.io.OutputStream;
2223import javax.xml.stream.XMLOutputFactory;
24import javax.xml.stream.XMLStreamException;
25import javax.xml.stream.XMLStreamWriter;
2627import org.apache.chemistry.opencmis.commons.impl.Constants;
2829/**30 * Base class for XML documents.31 */32publicabstractclassXMLDocumentBase {
3334publicstaticfinal String PREFIX_ATOM = "atom";
35publicstaticfinal String PREFIX_CMIS = "cmis";
36publicstaticfinal String PREFIX_RESTATOM = "cmisra";
37publicstaticfinal String PREFIX_APP = "app";
38publicstaticfinal String PREFIX_XSI = "xsi";
3940private XMLStreamWriter writer;
4142/**43 * Sets the namespaces for the document.44 */45publicvoid setNamespaces() throws XMLStreamException {
46 writer.setPrefix(PREFIX_ATOM, Constants.NAMESPACE_ATOM);
47 writer.setPrefix(PREFIX_CMIS, Constants.NAMESPACE_CMIS);
48 writer.setPrefix(PREFIX_RESTATOM, Constants.NAMESPACE_RESTATOM);
49 writer.setPrefix(PREFIX_APP, Constants.NAMESPACE_APP);
50 writer.setPrefix(PREFIX_XSI, Constants.NAMESPACE_XSI);
51 }
5253/**54 * Writes the namespace declaration of the given URI to the current tag.55 */56publicvoid writeNamespace(String namespaceUri) throws XMLStreamException {
57 writer.writeNamespace(writer.getPrefix(namespaceUri), namespaceUri);
58 }
5960/**61 * Starts the document and sets the namespaces.62 */63publicvoid startDocument(OutputStream out) throws XMLStreamException {
64// create a writer65 XMLOutputFactory factory = XMLOutputFactory.newInstance();
66 writer = factory.createXMLStreamWriter(out, "UTF-8");
6768// start the document69 writer.writeStartDocument("UTF-8", "1.0");
70 setNamespaces();
71 }
7273/**74 * Finishes the document.75 */76publicvoid endDocument() throws XMLStreamException {
77if (writer == null) {
78return;
79 }
8081// end the document82 writer.writeEndDocument();
8384// we are done.85 writer.close();
86 }
8788/**89 * Returns the writer object.90 */91public XMLStreamWriter getWriter() {
92return writer;
93 }
9495/**96 * Sets the writer object.97 */98protectedvoid setWriter(XMLStreamWriter writer) {
99this.writer = writer;
100 }
101 }