This project has retired. For details please refer to its Attic page.
JaxBHelper 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.commons.impl;
20  
21  import java.io.OutputStream;
22  
23  import javax.xml.bind.JAXBContext;
24  import javax.xml.bind.JAXBElement;
25  import javax.xml.bind.JAXBException;
26  import javax.xml.bind.Marshaller;
27  import javax.xml.bind.Unmarshaller;
28  import javax.xml.bind.annotation.XmlElementDecl;
29  import javax.xml.bind.annotation.XmlRegistry;
30  import javax.xml.namespace.QName;
31  import javax.xml.stream.XMLStreamWriter;
32  
33  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
34  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisRepositoryInfoType;
35  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionType;
36  import org.apache.chemistry.opencmis.commons.impl.jaxb.ObjectFactory;
37  
38  /**
39   * JAXB helper class.
40   * 
41   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
42   * 
43   */
44  public final class JaxBHelper {
45  
46      private static final QName CMIS_OBJECT = new QName(Constants.NAMESPACE_RESTATOM, "object");
47      private static final QName CMIS_TYPE_DEFINITION = new QName(Constants.NAMESPACE_RESTATOM, "type");
48      private static final QName CMIS_REPOSITORY_INFO = new QName(Constants.NAMESPACE_RESTATOM, "repositoryInfo");
49  
50      public static final ObjectFactory CMIS_OBJECT_FACTORY = new ObjectFactory();
51      public static final CMISExtraObjectFactory CMIS_EXTRA_OBJECT_FACTORY = new CMISExtraObjectFactory();
52      public static final JAXBContext CONTEXT;
53      static {
54          JAXBContext jc = null;
55          try {
56              jc = JAXBContext.newInstance(ObjectFactory.class, CMISExtraObjectFactory.class);
57          } catch (JAXBException e) {
58              e.printStackTrace();
59          }
60          CONTEXT = jc;
61      }
62  
63      @XmlRegistry
64      public static class CMISExtraObjectFactory {
65          @XmlElementDecl(namespace = Constants.NAMESPACE_RESTATOM, name = "object")
66          public JAXBElement<CmisObjectType> createObject(CmisObjectType value) {
67              return new JAXBElement<CmisObjectType>(CMIS_OBJECT, CmisObjectType.class, value);
68          }
69  
70          @XmlElementDecl(namespace = Constants.NAMESPACE_RESTATOM, name = "type")
71          public JAXBElement<CmisTypeDefinitionType> createTypeDefinition(CmisTypeDefinitionType value) {
72              return new JAXBElement<CmisTypeDefinitionType>(CMIS_TYPE_DEFINITION, CmisTypeDefinitionType.class, value);
73          }
74  
75          @XmlElementDecl(namespace = Constants.NAMESPACE_RESTATOM, name = "repositoryInfo")
76          public JAXBElement<CmisRepositoryInfoType> createRepositoryInfo(CmisRepositoryInfoType value) {
77              return new JAXBElement<CmisRepositoryInfoType>(CMIS_REPOSITORY_INFO, CmisRepositoryInfoType.class, value);
78          }
79      }
80  
81      /**
82       * Private constructor.
83       */
84      private JaxBHelper() {
85      }
86  
87      /**
88       * Creates an Unmarshaller.
89       */
90      public static Unmarshaller createUnmarshaller() throws JAXBException {
91          return CONTEXT.createUnmarshaller();
92      }
93  
94      /**
95       * Creates an Marshaller.
96       */
97      public static Marshaller createMarshaller() throws JAXBException {
98          return CONTEXT.createMarshaller();
99      }
100 
101     /**
102      * Marshals an object to a stream.
103      */
104     public static <T> void marshal(JAXBElement<T> object, OutputStream out, boolean fragment) throws JAXBException {
105         if (object == null) {
106             return;
107         }
108 
109         Marshaller m = CONTEXT.createMarshaller();
110         if (fragment) {
111             m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
112         }
113 
114         m.marshal(object, out);
115     }
116 
117     /**
118      * Marshals an object to a XMLStreamWriter.
119      */
120     public static void marshal(Object object, XMLStreamWriter out, boolean fragment) throws JAXBException {
121         if (object == null) {
122             return;
123         }
124 
125         Marshaller m = CONTEXT.createMarshaller();
126         if (fragment) {
127             m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
128         }
129 
130         m.marshal(object, out);
131     }
132 }