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: SdkModelProvider.java 2234 2010-06-29 00:03:38Z schulte2005 $
33   *
34   */
35  // </editor-fold>
36  // SECTION-END
37  package org.jomc.sdk.model.modlet;
38  
39  import java.text.MessageFormat;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.ResourceBundle;
43  import java.util.logging.Level;
44  import javax.xml.namespace.QName;
45  import org.jomc.model.Dependencies;
46  import org.jomc.model.Dependency;
47  import org.jomc.model.Implementation;
48  import org.jomc.model.ImplementationReference;
49  import org.jomc.model.Implementations;
50  import org.jomc.model.Module;
51  import org.jomc.model.Modules;
52  import org.jomc.model.Text;
53  import org.jomc.model.Texts;
54  import org.jomc.model.modlet.ModelHelper;
55  import org.jomc.modlet.Model;
56  import org.jomc.modlet.ModelContext;
57  import org.jomc.modlet.ModelException;
58  import org.jomc.modlet.ModelProvider;
59  import org.jomc.sdk.model.support.JaxbBinderFactory;
60  import org.jomc.sdk.model.support.JaxbContextFactory;
61  import org.jomc.sdk.model.support.JaxbIntrospectorFactory;
62  import org.jomc.sdk.model.support.JaxbMarshallerFactory;
63  import org.jomc.sdk.model.support.JaxbUnmarshallerFactory;
64  import org.jomc.sdk.model.support.JaxpEntityResolverFactory;
65  import org.jomc.sdk.model.support.JaxpResourceResolverFactory;
66  import org.jomc.sdk.model.support.JaxpSchemaFactory;
67  import org.jomc.sdk.model.support.JaxpValidatorFactory;
68  import org.jomc.sdk.model.support.JaxpValidatorHandlerFactory;
69  import static javax.xml.XMLConstants.NULL_NS_URI;
70  
71  // SECTION-START[Documentation]
72  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
73  /**
74   * SDK 'ModelProvider' implementation.
75   *
76   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
77   * @version $Id: SdkModelProvider.java 2234 2010-06-29 00:03:38Z schulte2005 $
78   */
79  // </editor-fold>
80  // SECTION-END
81  // SECTION-START[Annotations]
82  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
83  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
84  // </editor-fold>
85  // SECTION-END
86  public final class SdkModelProvider implements ModelProvider
87  {
88      // SECTION-START[SdkModelProvider]
89  
90      /** {@code QName} of the {@code java-context-id} attribute of {@code schema} elements. */
91      public static final QName XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE = new QName( NULL_NS_URI, "java-context-id" );
92  
93      /** {@code QName} of the {@code java-classpath-id} attribute of {@code schema} elements. */
94      public static final QName XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE = new QName( NULL_NS_URI, "java-classpath-id" );
95  
96      /** Identifier of the {@code XML Schema Set} specification. */
97      private static final String XML_SCHEMA_SET_SPECIFICATION_IDENTIFIER = "XML Schema Set";
98  
99      public Model findModel( final ModelContext context, final Model model ) throws ModelException
100     {
101         if ( context == null )
102         {
103             throw new NullPointerException( "context" );
104         }
105         if ( model == null )
106         {
107             throw new NullPointerException( "model" );
108         }
109 
110         Model found = null;
111         Modules modules = ModelHelper.getModules( model );
112 
113         if ( modules != null )
114         {
115             if ( context.isLoggable( Level.FINE ) )
116             {
117                 context.log( Level.FINE, getMessage(
118                     "providingModel", this.getClass().getName(), model.getIdentifier() ), null );
119 
120             }
121 
122             found = new Model( model );
123             modules = ModelHelper.getModules( found );
124 
125             final Implementations schemaSets =
126                 modules.getImplementations( XML_SCHEMA_SET_SPECIFICATION_IDENTIFIER );
127 
128             if ( schemaSets != null )
129             {
130                 for ( Implementation schemaSet : schemaSets.getImplementation() )
131                 {
132                     final Module schemaSetModule = this.createSchemaSetModule( modules, schemaSet );
133 
134                     if ( modules.getModule( schemaSetModule.getName() ) == null )
135                     {
136                         modules.getModule().add( schemaSetModule );
137                     }
138                 }
139             }
140         }
141         else if ( context.isLoggable( Level.WARNING ) )
142         {
143             context.log( Level.WARNING, getMessage( "modulesNotFound", model.getIdentifier() ), null );
144         }
145 
146         return found;
147     }
148 
149     private Module createSchemaSetModule( final Modules modules, final Implementation schemaSet )
150     {
151         final Module m = new Module();
152         m.setName( getMessage( "schemaSetModuleName", schemaSet.getIdentifier() ) );
153         m.setVendor( schemaSet.getVendor() );
154         m.setVersion( schemaSet.getVersion() );
155         m.setDocumentation( getTexts( "modelProviderInfo" ) );
156         m.setImplementations( new Implementations() );
157         m.getImplementations().setDocumentation( getTexts( "modelProviderInfo" ) );
158 
159         final List<Implementation> impls = m.getImplementations().getImplementation();
160         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxbContextFactory.class ) );
161         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxbBinderFactory.class ) );
162         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxbIntrospectorFactory.class ) );
163         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxbMarshallerFactory.class ) );
164         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxbUnmarshallerFactory.class ) );
165         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxpSchemaFactory.class ) );
166         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxpValidatorFactory.class ) );
167         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxpValidatorHandlerFactory.class ) );
168         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxpEntityResolverFactory.class ) );
169         impls.add( this.createJavaSchemaSetImplementation( modules, schemaSet, JaxpResourceResolverFactory.class ) );
170         return m;
171     }
172 
173     private Implementation createJavaSchemaSetImplementation(
174         final Modules modules, final Implementation schemaSet, final Class factoryClazz )
175     {
176         final Implementation i = new Implementation();
177         i.setIdentifier( getMessage( "schemaSetImplementationIdenifier", schemaSet.getIdentifier(),
178                                      factoryClazz.getSimpleName() ) );
179 
180         i.setName( schemaSet.getName() );
181         i.setDocumentation( getTexts( "modelProviderInfo" ) );
182         i.setClazz( factoryClazz.getName() );
183         i.setFinal( true );
184         i.setStateless( true );
185         i.setVendor( schemaSet.getVendor() );
186         i.setVersion( schemaSet.getVersion() );
187         i.setProperties( schemaSet.getProperties() );
188         i.setDependencies( schemaSet.getDependencies() );
189         i.setMessages( schemaSet.getMessages() );
190 
191         final Implementation factoryDeclaration = modules.getImplementation( factoryClazz );
192 
193         if ( factoryDeclaration != null )
194         {
195             final Implementations implementationReferences = new Implementations();
196             final ImplementationReference implementationReference = new ImplementationReference();
197             i.setImplementations( implementationReferences );
198             implementationReferences.getReference().add( implementationReference );
199             implementationReference.setIdentifier( factoryDeclaration.getIdentifier() );
200             implementationReference.setVersion( factoryDeclaration.getVersion() );
201 
202             final Dependencies dependencyDeclarations = modules.getDependencies( factoryDeclaration.getIdentifier() );
203             if ( dependencyDeclarations != null )
204             {
205                 for ( Dependency dependencyDeclaration : dependencyDeclarations.getDependency() )
206                 {
207                     if ( dependencyDeclaration.getImplementationName() != null
208                          && dependencyDeclaration.getImplementationName().equals( factoryDeclaration.getName() )
209                          && !dependencyDeclaration.isFinal() )
210                     {
211                         if ( i.getDependencies() == null )
212                         {
213                             i.setDependencies( new Dependencies() );
214                         }
215 
216                         final Dependency d = new Dependency( dependencyDeclaration );
217                         d.setImplementationName( i.getName() );
218                         d.setFinal( true );
219                         d.setOverride( true );
220                         i.getDependencies().getDependency().add( d );
221                     }
222                 }
223             }
224         }
225 
226         return i;
227     }
228 
229     private static Texts getTexts( final String key, final Object... arguments )
230     {
231         final Texts texts = new Texts();
232         final Text text = new Text();
233         texts.getText().add( text );
234         texts.setDefaultLanguage( Locale.getDefault().getLanguage() );
235         text.setLanguage( texts.getDefaultLanguage() );
236         text.setValue( getMessage( key, arguments ) );
237         return texts;
238     }
239 
240     private static String getMessage( final String key, final Object... arguments )
241     {
242         return MessageFormat.format( ResourceBundle.getBundle( SdkModelProvider.class.getName().replace( '.', '/' ) ).
243             getString( key ), arguments );
244 
245     }
246 
247     // SECTION-END
248     // SECTION-START[Constructors]
249     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
250 
251     /** Creates a new {@code SdkModelProvider} instance. */
252     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
253     public SdkModelProvider()
254     {
255         // SECTION-START[Default Constructor]
256         super();
257         // SECTION-END
258     }
259     // </editor-fold>
260     // SECTION-END
261     // SECTION-START[Dependencies]
262     // SECTION-END
263     // SECTION-START[Properties]
264     // SECTION-END
265     // SECTION-START[Messages]
266     // SECTION-END
267 }