View Javadoc

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: JaxbBinderFactory.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.Binder;
41  import javax.xml.bind.JAXBContext;
42  import javax.xml.bind.JAXBException;
43  import org.jomc.sdk.model.SchemaType;
44  import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE;
45  
46  // SECTION-START[Documentation]
47  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
48  /**
49   * XML Schema Set 'Binder' factory implementation.
50   * <p><b>Specifications</b><ul>
51   * <li>{@code 'javax.xml.bind.Binder'} {@code (javax.xml.bind.Binder)} {@code Multiton}</li>
52   * </ul></p>
53   * <p><b>Properties</b><ul>
54   * <li>"{@link #getBinderProperties binderProperties}"
55   * <blockquote>Property of type {@code java.util.Map<String,Object>}.
56   * </blockquote></li>
57   * <li>"{@link #getSchemas schemas}"
58   * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
59   * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
60   * </blockquote></li>
61   * </ul></p>
62   * <p><b>Dependencies</b><ul>
63   * <li>"{@link #getSchema schema}"<blockquote>
64   * Dependency on {@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} bound to an instance.</blockquote></li>
65   * <li>"{@link #getValidationEventHandler validationEventHandler}"<blockquote>
66   * Dependency on {@code 'javax.xml.bind.ValidationEventHandler'} {@code (javax.xml.bind.ValidationEventHandler)} bound to an instance.</blockquote></li>
67   * </ul></p>
68   *
69   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
70   * @version $Id: JaxbBinderFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
71   */
72  // </editor-fold>
73  // SECTION-END
74  // SECTION-START[Annotations]
75  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
76  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
77  // </editor-fold>
78  // SECTION-END
79  public final class JaxbBinderFactory
80  {
81      // SECTION-START[Binder]
82      // SECTION-END
83      // SECTION-START[JaxbBinderFactory]
84  
85      public Binder getObject() throws JAXBException
86      {
87          Binder binder = null;
88          final StringBuilder packages = new StringBuilder();
89  
90          for ( SchemaType s : this.getSchemas().getSchema() )
91          {
92              if ( s.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE ) )
93              {
94                  packages.append( ':' ).append( s.getOtherAttributes().get( XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE ) );
95              }
96          }
97  
98          if ( packages.length() > 0 )
99          {
100             final String ctx = packages.toString().substring( 1 );
101             binder = JAXBContext.newInstance( ctx ).createBinder();
102             binder.setSchema( this.getSchema() );
103             binder.setEventHandler( this.getValidationEventHandler() );
104 
105             for ( Map.Entry<String, Object> e : this.getBinderProperties().entrySet() )
106             {
107                 binder.setProperty( e.getKey(), e.getValue() );
108             }
109         }
110 
111         return binder;
112     }
113 
114     // SECTION-END
115     // SECTION-START[Constructors]
116     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
117 
118     /** Creates a new {@code JaxbBinderFactory} instance. */
119     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
120     public JaxbBinderFactory()
121     {
122         // SECTION-START[Default Constructor]
123         super();
124         // SECTION-END
125     }
126     // </editor-fold>
127     // SECTION-END
128     // SECTION-START[Dependencies]
129     // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
130 
131     /**
132      * Gets the {@code schema} dependency.
133      * <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>
134      * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
135      * @return The {@code schema} dependency.
136      * {@code null} if no object is available.
137      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
138      */
139     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
140     private javax.xml.validation.Schema getSchema()
141     {
142         return (javax.xml.validation.Schema) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "schema" );
143     }
144 
145     /**
146      * Gets the {@code validationEventHandler} dependency.
147      * <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>
148      * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
149      * @return The {@code validationEventHandler} dependency.
150      * {@code null} if no object is available.
151      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
152      */
153     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
154     private javax.xml.bind.ValidationEventHandler getValidationEventHandler()
155     {
156         return (javax.xml.bind.ValidationEventHandler) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "validationEventHandler" );
157     }
158     // </editor-fold>
159     // SECTION-END
160     // SECTION-START[Properties]
161     // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
162 
163     /**
164      * Gets the value of the {@code binderProperties} property.
165      * @return The value of the {@code binderProperties} property.
166      * @throws org.jomc.ObjectManagementException if getting the property instance fails.
167      */
168     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
169     private java.util.Map<String,Object> getBinderProperties()
170     {
171         final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "binderProperties" );
172         assert _p != null : "'binderProperties' property not found.";
173         return _p;
174     }
175 
176     /**
177      * Gets the value of the {@code schemas} property.
178      * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).
179      * @throws org.jomc.ObjectManagementException if getting the property instance fails.
180      */
181     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
182     private org.jomc.sdk.model.SchemasType getSchemas()
183     {
184         final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" );
185         assert _p != null : "'schemas' property not found.";
186         return _p;
187     }
188     // </editor-fold>
189     // SECTION-END
190     // SECTION-START[Messages]
191     // SECTION-END
192 }