View Javadoc
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 org.apache.chemistry.opencmis.commons.impl.Constants;
22  
23  import java.io.OutputStream;
24  
25  import javax.xml.stream.XMLOutputFactory;
26  import javax.xml.stream.XMLStreamException;
27  import javax.xml.stream.XMLStreamWriter;
28  
29  
30  /**
31   * Base class for XML documents.
32   */
33  public abstract class XMLDocumentBase
34  {
35      public static final String PREFIX_ATOM = "atom";
36      public static final String PREFIX_CMIS = "cmis";
37      public static final String PREFIX_RESTATOM = "cmisra";
38      public static final String PREFIX_APP = "app";
39      public static final String PREFIX_XSI = "xsi";
40      private XMLStreamWriter writer;
41  
42      /**
43       * Sets the namespaces for the document.
44       */
45      public void setNamespaces(  ) throws XMLStreamException
46      {
47          writer.setPrefix( PREFIX_ATOM, Constants.NAMESPACE_ATOM );
48          writer.setPrefix( PREFIX_CMIS, Constants.NAMESPACE_CMIS );
49          writer.setPrefix( PREFIX_RESTATOM, Constants.NAMESPACE_RESTATOM );
50          writer.setPrefix( PREFIX_APP, Constants.NAMESPACE_APP );
51          writer.setPrefix( PREFIX_XSI, Constants.NAMESPACE_XSI );
52      }
53  
54      /**
55       * Writes the namespace declaration of the given URI to the current tag.
56       */
57      public void writeNamespace( String namespaceUri ) throws XMLStreamException
58      {
59          writer.writeNamespace( writer.getPrefix( namespaceUri ), namespaceUri );
60      }
61  
62      /**
63       * Starts the document and sets the namespaces.
64       */
65      public void startDocument( OutputStream out ) throws XMLStreamException
66      {
67          // create a writer
68          XMLOutputFactory factory = XMLOutputFactory.newInstance(  );
69          writer = factory.createXMLStreamWriter( out, "UTF-8" );
70  
71          // start the document
72          writer.writeStartDocument( "UTF-8", "1.0" );
73          setNamespaces(  );
74      }
75  
76      /**
77       * Finishes the document.
78       */
79      public void endDocument(  ) throws XMLStreamException
80      {
81          if ( writer == null )
82          {
83              return;
84          }
85  
86          // end the document
87          writer.writeEndDocument(  );
88  
89          // we are done.
90          writer.close(  );
91      }
92  
93      /**
94       * Returns the writer object.
95       */
96      public XMLStreamWriter getWriter(  )
97      {
98          return writer;
99      }
100 
101     /**
102      * Sets the writer object.
103      */
104     protected void setWriter( XMLStreamWriter writer )
105     {
106         this.writer = writer;
107     }
108 }