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: SdkModelProvider.java 2234 2010-06-29 00:03:38Z schulte2005 $
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.sdk.model.modlet;
038    
039    import java.text.MessageFormat;
040    import java.util.List;
041    import java.util.Locale;
042    import java.util.ResourceBundle;
043    import java.util.logging.Level;
044    import javax.xml.namespace.QName;
045    import org.jomc.model.Dependencies;
046    import org.jomc.model.Dependency;
047    import org.jomc.model.Implementation;
048    import org.jomc.model.ImplementationReference;
049    import org.jomc.model.Implementations;
050    import org.jomc.model.Module;
051    import org.jomc.model.Modules;
052    import org.jomc.model.Text;
053    import org.jomc.model.Texts;
054    import org.jomc.model.modlet.ModelHelper;
055    import org.jomc.modlet.Model;
056    import org.jomc.modlet.ModelContext;
057    import org.jomc.modlet.ModelException;
058    import org.jomc.modlet.ModelProvider;
059    import org.jomc.sdk.model.support.JaxbBinderFactory;
060    import org.jomc.sdk.model.support.JaxbContextFactory;
061    import org.jomc.sdk.model.support.JaxbIntrospectorFactory;
062    import org.jomc.sdk.model.support.JaxbMarshallerFactory;
063    import org.jomc.sdk.model.support.JaxbUnmarshallerFactory;
064    import org.jomc.sdk.model.support.JaxpEntityResolverFactory;
065    import org.jomc.sdk.model.support.JaxpResourceResolverFactory;
066    import org.jomc.sdk.model.support.JaxpSchemaFactory;
067    import org.jomc.sdk.model.support.JaxpValidatorFactory;
068    import org.jomc.sdk.model.support.JaxpValidatorHandlerFactory;
069    import static javax.xml.XMLConstants.NULL_NS_URI;
070    
071    // SECTION-START[Documentation]
072    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
073    /**
074     * SDK 'ModelProvider' implementation.
075     *
076     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
077     * @version $Id: SdkModelProvider.java 2234 2010-06-29 00:03:38Z schulte2005 $
078     */
079    // </editor-fold>
080    // SECTION-END
081    // SECTION-START[Annotations]
082    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
083    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
084    // </editor-fold>
085    // SECTION-END
086    public final class SdkModelProvider implements ModelProvider
087    {
088        // SECTION-START[SdkModelProvider]
089    
090        /** {@code QName} of the {@code java-context-id} attribute of {@code schema} elements. */
091        public static final QName XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE = new QName( NULL_NS_URI, "java-context-id" );
092    
093        /** {@code QName} of the {@code java-classpath-id} attribute of {@code schema} elements. */
094        public static final QName XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE = new QName( NULL_NS_URI, "java-classpath-id" );
095    
096        /** Identifier of the {@code XML Schema Set} specification. */
097        private static final String XML_SCHEMA_SET_SPECIFICATION_IDENTIFIER = "XML Schema Set";
098    
099        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    }