This project has retired. For details please refer to its Attic page.
XMLDocumentBase xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl.atompub;
20  
21  import java.io.OutputStream;
22  
23  import javax.xml.stream.XMLOutputFactory;
24  import javax.xml.stream.XMLStreamException;
25  import javax.xml.stream.XMLStreamWriter;
26  
27  import org.apache.chemistry.opencmis.commons.impl.Constants;
28  
29  /**
30   * Base class for XML documents.
31   */
32  public abstract class XMLDocumentBase {
33  
34      public static final String PREFIX_ATOM = "atom";
35      public static final String PREFIX_CMIS = "cmis";
36      public static final String PREFIX_RESTATOM = "cmisra";
37      public static final String PREFIX_APP = "app";
38      public static final String PREFIX_XSI = "xsi";
39  
40      private XMLStreamWriter writer;
41  
42      /**
43       * Sets the namespaces for the document.
44       */
45      public void 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      }
52  
53      /**
54       * Writes the namespace declaration of the given URI to the current tag.
55       */
56      public void writeNamespace(String namespaceUri) throws XMLStreamException {
57          writer.writeNamespace(writer.getPrefix(namespaceUri), namespaceUri);
58      }
59  
60      /**
61       * Starts the document and sets the namespaces.
62       */
63      public void startDocument(OutputStream out) throws XMLStreamException {
64          // create a writer
65          XMLOutputFactory factory = XMLOutputFactory.newInstance();
66          writer = factory.createXMLStreamWriter(out, "UTF-8");
67  
68          // start the document
69          writer.writeStartDocument("UTF-8", "1.0");
70          setNamespaces();
71      }
72  
73      /**
74       * Finishes the document.
75       */
76      public void endDocument() throws XMLStreamException {
77          if (writer == null) {
78              return;
79          }
80  
81          // end the document
82          writer.writeEndDocument();
83  
84          // we are done.
85          writer.close();
86      }
87  
88      /**
89       * Returns the writer object.
90       */
91      public XMLStreamWriter getWriter() {
92          return writer;
93      }
94  
95      /**
96       * Sets the writer object.
97       */
98      protected void setWriter(XMLStreamWriter writer) {
99          this.writer = writer;
100     }
101 }