1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
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  
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  
56  
57      public void writeNamespace( String namespaceUri ) throws XMLStreamException
58      {
59          writer.writeNamespace( writer.getPrefix( namespaceUri ), namespaceUri );
60      }
61  
62      
63  
64  
65      public void startDocument( OutputStream out ) throws XMLStreamException
66      {
67          
68          XMLOutputFactory factory = XMLOutputFactory.newInstance(  );
69          writer = factory.createXMLStreamWriter( out, "UTF-8" );
70  
71          
72          writer.writeStartDocument( "UTF-8", "1.0" );
73          setNamespaces(  );
74      }
75  
76      
77  
78  
79      public void endDocument(  ) throws XMLStreamException
80      {
81          if ( writer == null )
82          {
83              return;
84          }
85  
86          
87          writer.writeEndDocument(  );
88  
89          
90          writer.close(  );
91      }
92  
93      
94  
95  
96      public XMLStreamWriter getWriter(  )
97      {
98          return writer;
99      }
100 
101     
102 
103 
104     protected void setWriter( XMLStreamWriter writer )
105     {
106         this.writer = writer;
107     }
108 }