This project has retired. For details please refer to its Attic page.
InMemoryJaxbHelper 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.inmemory.types;
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.Constants;
34  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionType;
35  import org.apache.chemistry.opencmis.commons.impl.jaxb.ObjectFactory;
36  
37  public class InMemoryJaxbHelper {
38      private static final QName CMIS_TYPE_DEFINITIONS = new QName("http://chemistry.apache.org/schemas/TypeDefnitions", "typeDefinitions");
39      private static final QName CMIS_TYPE_DEFINITION = new QName(Constants.NAMESPACE_RESTATOM, "type");
40  
41      public static final ObjectFactory CMIS_OBJECT_FACTORY = new ObjectFactory();
42      public static final CMISExtraObjectFactory CMIS_EXTRA_OBJECT_FACTORY = new CMISExtraObjectFactory();
43      public static final JAXBContext CONTEXT;
44      static {
45          JAXBContext jc = null;
46          try {
47              jc = JAXBContext.newInstance(ObjectFactory.class, CMISExtraObjectFactory.class);
48          } catch (JAXBException e) {
49              e.printStackTrace();
50          }
51          CONTEXT = jc;
52      }
53  
54      @XmlRegistry
55      public static class CMISExtraObjectFactory {
56          @XmlElementDecl(namespace="http://chemistry.apache.org/schemas/TypeDefnitions", name = "typeDefinitions")
57          public JAXBElement<TypeDefinitions> createObject(TypeDefinitions value) {
58              return new JAXBElement<TypeDefinitions>(CMIS_TYPE_DEFINITIONS, TypeDefinitions.class, value);
59          }
60  
61          @XmlElementDecl(namespace = Constants.NAMESPACE_RESTATOM, name = "type")
62          public JAXBElement<CmisTypeDefinitionType> createTypeDefinition(CmisTypeDefinitionType value) {
63              return new JAXBElement<CmisTypeDefinitionType>(CMIS_TYPE_DEFINITION, CmisTypeDefinitionType.class, value);
64          }
65      }
66  
67      /**
68       * Private constructor.
69       */
70      private InMemoryJaxbHelper() {
71      }
72  
73      /**
74       * Creates an Unmarshaller.
75       */
76      public static Unmarshaller createUnmarshaller() throws JAXBException {
77          return CONTEXT.createUnmarshaller();
78      }
79  
80      /**
81       * Creates an Marshaller.
82       */
83      public static Marshaller createMarshaller() throws JAXBException {
84          return CONTEXT.createMarshaller();
85      }
86  
87      /**
88       * Marshals an object to a stream.
89       */
90      public static <T> void marshal(JAXBElement<T> object, OutputStream out, boolean fragment) throws JAXBException {
91          if (object == null) {
92              return;
93          }
94  
95          Marshaller m = CONTEXT.createMarshaller();
96          if (fragment) {
97              m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
98          }
99  
100         m.marshal(object, out);
101     }
102 
103     /**
104      * Marshals an object to a XMLStreamWriter.
105      */
106     public static void marshal(Object object, XMLStreamWriter out, boolean fragment) throws JAXBException {
107         if (object == null) {
108             return;
109         }
110 
111         Marshaller m = CONTEXT.createMarshaller();
112         if (fragment) {
113             m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
114         }
115 
116         m.marshal(object, out);
117     }
118 }