001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Copyright (c) 2010 The JOMC Project
005     *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     *
032     *   $Id: JaxbMarshallerFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.sdk.model.support;
038    
039    import java.util.Map;
040    import javax.xml.bind.JAXBContext;
041    import javax.xml.bind.JAXBException;
042    import javax.xml.bind.Marshaller;
043    import org.jomc.sdk.model.SchemaType;
044    import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE;
045    import org.xml.sax.SAXException;
046    
047    // SECTION-START[Documentation]
048    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
049    /**
050     * XML Schema Set JAXB 'Marshaller' factory implementation.
051     * <p><b>Specifications</b><ul>
052     * <li>{@code 'javax.xml.bind.Marshaller'} {@code (javax.xml.bind.Marshaller)} {@code Multiton}</li>
053     * </ul></p>
054     * <p><b>Properties</b><ul>
055     * <li>"{@link #getJaxbEncoding jaxbEncoding}"
056     * <blockquote>Property of type {@code java.lang.String}.
057     * <p>See {@link javax.xml.bind.Marshaller#JAXB_ENCODING}.</p>
058     * </blockquote></li>
059     * <li>"{@link #isJaxbFormattedOutput jaxbFormattedOutput}"
060     * <blockquote>Property of type {@code java.lang.Boolean}.
061     * <p>See {@link javax.xml.bind.Marshaller#JAXB_FORMATTED_OUTPUT}.</p>
062     * </blockquote></li>
063     * <li>"{@link #isJaxbFragment jaxbFragment}"
064     * <blockquote>Property of type {@code java.lang.Boolean}.
065     * <p>See {@link javax.xml.bind.Marshaller#JAXB_FRAGMENT}.</p>
066     * </blockquote></li>
067     * <li>"{@link #getJaxbNoNamespaceSchemaLocation jaxbNoNamespaceSchemaLocation}"
068     * <blockquote>Property of type {@code java.lang.String}.
069     * <p>See {@link javax.xml.bind.Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION}.</p>
070     * </blockquote></li>
071     * <li>"{@link #isJaxbSchemaLocation jaxbSchemaLocation}"
072     * <blockquote>Property of type {@code boolean}.
073     * <p>See {@link javax.xml.bind.Marshaller#JAXB_SCHEMA_LOCATION}.</p>
074     * </blockquote></li>
075     * <li>"{@link #getMarshallerProperties marshallerProperties}"
076     * <blockquote>Property of type {@code java.util.Map<String,Object>}.
077     * </blockquote></li>
078     * <li>"{@link #getSchemas schemas}"
079     * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
080     * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
081     * </blockquote></li>
082     * </ul></p>
083     * <p><b>Dependencies</b><ul>
084     * <li>"{@link #getAttachmentMarshaller attachmentMarshaller}"<blockquote>
085     * Dependency on {@code 'javax.xml.bind.attachment.AttachmentMarshaller'} {@code (javax.xml.bind.attachment.AttachmentMarshaller)} bound to an instance.</blockquote></li>
086     * <li>"{@link #getSchema schema}"<blockquote>
087     * Dependency on {@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} bound to an instance.</blockquote></li>
088     * <li>"{@link #getValidationEventHandler validationEventHandler}"<blockquote>
089     * Dependency on {@code 'javax.xml.bind.ValidationEventHandler'} {@code (javax.xml.bind.ValidationEventHandler)} bound to an instance.</blockquote></li>
090     * </ul></p>
091     *
092     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
093     * @version $Id: JaxbMarshallerFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
094     */
095    // </editor-fold>
096    // SECTION-END
097    // SECTION-START[Annotations]
098    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
099    @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    }