1 // SECTION-START[License Header]
2 // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3 /*
4 * Copyright (c) 2010 The JOMC Project
5 * Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * o Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * o Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $Id: JaxbMarshallerFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
33 *
34 */
35 // </editor-fold>
36 // SECTION-END
37 package org.jomc.sdk.model.support;
38
39 import java.util.Map;
40 import javax.xml.bind.JAXBContext;
41 import javax.xml.bind.JAXBException;
42 import javax.xml.bind.Marshaller;
43 import org.jomc.sdk.model.SchemaType;
44 import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE;
45 import org.xml.sax.SAXException;
46
47 // SECTION-START[Documentation]
48 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
49 /**
50 * XML Schema Set JAXB 'Marshaller' factory implementation.
51 * <p><b>Specifications</b><ul>
52 * <li>{@code 'javax.xml.bind.Marshaller'} {@code (javax.xml.bind.Marshaller)} {@code Multiton}</li>
53 * </ul></p>
54 * <p><b>Properties</b><ul>
55 * <li>"{@link #getJaxbEncoding jaxbEncoding}"
56 * <blockquote>Property of type {@code java.lang.String}.
57 * <p>See {@link javax.xml.bind.Marshaller#JAXB_ENCODING}.</p>
58 * </blockquote></li>
59 * <li>"{@link #isJaxbFormattedOutput jaxbFormattedOutput}"
60 * <blockquote>Property of type {@code java.lang.Boolean}.
61 * <p>See {@link javax.xml.bind.Marshaller#JAXB_FORMATTED_OUTPUT}.</p>
62 * </blockquote></li>
63 * <li>"{@link #isJaxbFragment jaxbFragment}"
64 * <blockquote>Property of type {@code java.lang.Boolean}.
65 * <p>See {@link javax.xml.bind.Marshaller#JAXB_FRAGMENT}.</p>
66 * </blockquote></li>
67 * <li>"{@link #getJaxbNoNamespaceSchemaLocation jaxbNoNamespaceSchemaLocation}"
68 * <blockquote>Property of type {@code java.lang.String}.
69 * <p>See {@link javax.xml.bind.Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION}.</p>
70 * </blockquote></li>
71 * <li>"{@link #isJaxbSchemaLocation jaxbSchemaLocation}"
72 * <blockquote>Property of type {@code boolean}.
73 * <p>See {@link javax.xml.bind.Marshaller#JAXB_SCHEMA_LOCATION}.</p>
74 * </blockquote></li>
75 * <li>"{@link #getMarshallerProperties marshallerProperties}"
76 * <blockquote>Property of type {@code java.util.Map<String,Object>}.
77 * </blockquote></li>
78 * <li>"{@link #getSchemas schemas}"
79 * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
80 * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
81 * </blockquote></li>
82 * </ul></p>
83 * <p><b>Dependencies</b><ul>
84 * <li>"{@link #getAttachmentMarshaller attachmentMarshaller}"<blockquote>
85 * Dependency on {@code 'javax.xml.bind.attachment.AttachmentMarshaller'} {@code (javax.xml.bind.attachment.AttachmentMarshaller)} bound to an instance.</blockquote></li>
86 * <li>"{@link #getSchema schema}"<blockquote>
87 * Dependency on {@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} bound to an instance.</blockquote></li>
88 * <li>"{@link #getValidationEventHandler validationEventHandler}"<blockquote>
89 * Dependency on {@code 'javax.xml.bind.ValidationEventHandler'} {@code (javax.xml.bind.ValidationEventHandler)} bound to an instance.</blockquote></li>
90 * </ul></p>
91 *
92 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
93 * @version $Id: JaxbMarshallerFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
94 */
95 // </editor-fold>
96 // SECTION-END
97 // SECTION-START[Annotations]
98 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
99 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
100 // </editor-fold>
101 // SECTION-END
102 public final class JaxbMarshallerFactory
103 {
104 // SECTION-START[Marshaller]
105 // SECTION-END
106 // SECTION-START[JaxbMarshallerFactory]
107
108 public Marshaller getObject() throws JAXBException, SAXException
109 {
110 Marshaller marshaller = null;
111 final StringBuilder schemaLocation = new StringBuilder();
112 final StringBuilder packageNames = new StringBuilder();
113
114 for ( SchemaType s : this.getSchemas().getSchema() )
115 {
116 if ( s.getPublicId() != null )
117 {
118 schemaLocation.append( " " ).append( s.getPublicId() ).append( " " ).append( s.getSystemId() );
119 }
120
121 if ( s.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE ) )
122 {
123 packageNames.append( ':' ).append( s.getOtherAttributes().get( XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE ) );
124 }
125 }
126
127 if ( packageNames.length() > 0 )
128 {
129 marshaller = JAXBContext.newInstance( packageNames.substring( 1 ) ).createMarshaller();
130 marshaller.setProperty( Marshaller.JAXB_ENCODING, this.getJaxbEncoding() );
131 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, this.isJaxbFormattedOutput() );
132 marshaller.setProperty( Marshaller.JAXB_FRAGMENT, this.isJaxbFragment() );
133
134 if ( this.getJaxbNoNamespaceSchemaLocation().length() > 0 )
135 {
136 marshaller.setProperty( Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION,
137 this.getJaxbNoNamespaceSchemaLocation() );
138
139 }
140
141 if ( this.isJaxbSchemaLocation() && schemaLocation.length() > 0 )
142 {
143 marshaller.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation.substring( 1 ) );
144 }
145
146 marshaller.setAttachmentMarshaller( this.getAttachmentMarshaller() );
147 marshaller.setEventHandler( this.getValidationEventHandler() );
148 marshaller.setSchema( this.getSchema() );
149
150 for ( Map.Entry<String, Object> e : this.getMarshallerProperties().entrySet() )
151 {
152 marshaller.setProperty( e.getKey(), e.getValue() );
153 }
154 }
155
156 return marshaller;
157 }
158
159 // SECTION-END
160 // SECTION-START[Constructors]
161 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
162
163 /** Creates a new {@code JaxbMarshallerFactory} instance. */
164 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
165 public JaxbMarshallerFactory()
166 {
167 // SECTION-START[Default Constructor]
168 super();
169 // SECTION-END
170 }
171 // </editor-fold>
172 // SECTION-END
173 // SECTION-START[Dependencies]
174 // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
175
176 /**
177 * Gets the {@code attachmentMarshaller} dependency.
178 * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'javax.xml.bind.attachment.AttachmentMarshaller'} {@code (javax.xml.bind.attachment.AttachmentMarshaller)} specification.</p>
179 * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
180 * @return The {@code attachmentMarshaller} dependency.
181 * {@code null} if no object is available.
182 * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
183 */
184 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
185 private javax.xml.bind.attachment.AttachmentMarshaller getAttachmentMarshaller()
186 {
187 return (javax.xml.bind.attachment.AttachmentMarshaller) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "attachmentMarshaller" );
188 }
189
190 /**
191 * Gets the {@code schema} dependency.
192 * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} specification.</p>
193 * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
194 * @return The {@code schema} dependency.
195 * {@code null} if no object is available.
196 * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
197 */
198 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
199 private javax.xml.validation.Schema getSchema()
200 {
201 return (javax.xml.validation.Schema) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "schema" );
202 }
203
204 /**
205 * Gets the {@code validationEventHandler} dependency.
206 * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'javax.xml.bind.ValidationEventHandler'} {@code (javax.xml.bind.ValidationEventHandler)} specification.</p>
207 * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
208 * @return The {@code validationEventHandler} dependency.
209 * {@code null} if no object is available.
210 * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
211 */
212 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
213 private javax.xml.bind.ValidationEventHandler getValidationEventHandler()
214 {
215 return (javax.xml.bind.ValidationEventHandler) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "validationEventHandler" );
216 }
217 // </editor-fold>
218 // SECTION-END
219 // SECTION-START[Properties]
220 // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
221
222 /**
223 * Gets the value of the {@code jaxbEncoding} property.
224 * @return See {@link javax.xml.bind.Marshaller#JAXB_ENCODING}.
225 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
226 */
227 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
228 private java.lang.String getJaxbEncoding()
229 {
230 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "jaxbEncoding" );
231 assert _p != null : "'jaxbEncoding' property not found.";
232 return _p;
233 }
234
235 /**
236 * Gets the value of the {@code jaxbFormattedOutput} property.
237 * @return See {@link javax.xml.bind.Marshaller#JAXB_FORMATTED_OUTPUT}.
238 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
239 */
240 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
241 private java.lang.Boolean isJaxbFormattedOutput()
242 {
243 final java.lang.Boolean _p = (java.lang.Boolean) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "jaxbFormattedOutput" );
244 assert _p != null : "'jaxbFormattedOutput' property not found.";
245 return _p;
246 }
247
248 /**
249 * Gets the value of the {@code jaxbFragment} property.
250 * @return See {@link javax.xml.bind.Marshaller#JAXB_FRAGMENT}.
251 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
252 */
253 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
254 private java.lang.Boolean isJaxbFragment()
255 {
256 final java.lang.Boolean _p = (java.lang.Boolean) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "jaxbFragment" );
257 assert _p != null : "'jaxbFragment' property not found.";
258 return _p;
259 }
260
261 /**
262 * Gets the value of the {@code jaxbNoNamespaceSchemaLocation} property.
263 * @return See {@link javax.xml.bind.Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION}.
264 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
265 */
266 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
267 private java.lang.String getJaxbNoNamespaceSchemaLocation()
268 {
269 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "jaxbNoNamespaceSchemaLocation" );
270 assert _p != null : "'jaxbNoNamespaceSchemaLocation' property not found.";
271 return _p;
272 }
273
274 /**
275 * Gets the value of the {@code jaxbSchemaLocation} property.
276 * @return See {@link javax.xml.bind.Marshaller#JAXB_SCHEMA_LOCATION}.
277 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
278 */
279 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
280 private boolean isJaxbSchemaLocation()
281 {
282 final java.lang.Boolean _p = (java.lang.Boolean) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "jaxbSchemaLocation" );
283 assert _p != null : "'jaxbSchemaLocation' property not found.";
284 return _p.booleanValue();
285 }
286
287 /**
288 * Gets the value of the {@code marshallerProperties} property.
289 * @return The value of the {@code marshallerProperties} property.
290 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
291 */
292 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
293 private java.util.Map<String,Object> getMarshallerProperties()
294 {
295 final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "marshallerProperties" );
296 assert _p != null : "'marshallerProperties' property not found.";
297 return _p;
298 }
299
300 /**
301 * Gets the value of the {@code schemas} property.
302 * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).
303 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
304 */
305 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
306 private org.jomc.sdk.model.SchemasType getSchemas()
307 {
308 final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" );
309 assert _p != null : "'schemas' property not found.";
310 return _p;
311 }
312 // </editor-fold>
313 // SECTION-END
314 // SECTION-START[Messages]
315 // SECTION-END
316 }