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: StandaloneModelValidator.java 2240 2010-06-29 07:53:15Z schulte2005 $
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.standalone.model.modlet;
038    
039    import java.text.MessageFormat;
040    import java.util.ResourceBundle;
041    import java.util.logging.Level;
042    import javax.xml.bind.JAXBElement;
043    import org.jomc.model.Implementation;
044    import org.jomc.model.Module;
045    import org.jomc.model.Modules;
046    import org.jomc.model.ObjectFactory;
047    import org.jomc.model.modlet.ModelHelper;
048    import org.jomc.modlet.Model;
049    import org.jomc.modlet.ModelContext;
050    import org.jomc.modlet.ModelException;
051    import org.jomc.modlet.ModelValidationReport;
052    import org.jomc.modlet.ModelValidator;
053    import org.jomc.standalone.model.MethodType;
054    import org.jomc.standalone.model.MethodsType;
055    
056    // SECTION-START[Documentation]
057    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
058    /**
059     * Standalone 'ModelValidator' implementation.
060     *
061     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
062     * @version $Id: StandaloneModelValidator.java 2240 2010-06-29 07:53:15Z schulte2005 $
063     */
064    // </editor-fold>
065    // SECTION-END
066    // SECTION-START[Annotations]
067    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
068    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
069    // </editor-fold>
070    // SECTION-END
071    public final class StandaloneModelValidator implements ModelValidator
072    {
073        // SECTION-START[StandaloneModelValidator]
074    
075        public ModelValidationReport validateModel( final ModelContext context, final Model model )
076            throws ModelException
077        {
078            if ( context == null )
079            {
080                throw new NullPointerException( "context" );
081            }
082            if ( model == null )
083            {
084                throw new NullPointerException( "model" );
085            }
086    
087            final ModelValidationReport report = new ModelValidationReport();
088            final Modules modules = ModelHelper.getModules( model );
089    
090            if ( modules != null )
091            {
092                if ( context.isLoggable( Level.FINE ) )
093                {
094                    context.log( Level.FINE, getMessage(
095                        "validatingModel", this.getClass().getName(), model.getIdentifier() ), null );
096    
097                }
098    
099                for ( Module m : modules.getModule() )
100                {
101                    if ( m.getImplementations() != null )
102                    {
103                        for ( Implementation i : m.getImplementations().getImplementation() )
104                        {
105                            final JAXBElement<Implementation> detailElement = new ObjectFactory().createImplementation( i );
106    
107                            for ( MethodsType methodsType : i.getAnyObjects( MethodsType.class ) )
108                            {
109                                if ( methodsType.getExceptions() != null
110                                     && methodsType.getExceptions().getDefaultException() != null
111                                     && methodsType.getExceptions().getDefaultException().getTargetClass() != null )
112                                {
113                                    report.getDetails().add( new ModelValidationReport.Detail(
114                                        "IMPLEMENTATION_METHODS_DEFAULT_EXCEPTION_TARGET_CLASS_CONSTRAINT", Level.SEVERE,
115                                        getMessage( "implementationMethodsDefaultExceptionTargetClassConstraint",
116                                                    i.getIdentifier(),
117                                                    methodsType.getExceptions().getDefaultException().getClazz(),
118                                                    methodsType.getExceptions().getDefaultException().getTargetClass() ),
119                                        detailElement ) );
120    
121                                }
122    
123                                for ( MethodType methodType : methodsType.getMethod() )
124                                {
125                                    if ( methodType.getExceptions() != null
126                                         && methodType.getExceptions().getDefaultException() != null
127                                         && methodType.getExceptions().getDefaultException().getTargetClass() != null )
128                                    {
129                                        report.getDetails().add( new ModelValidationReport.Detail(
130                                            "IMPLEMENTATION_METHOD_DEFAULT_EXCEPTION_TARGET_CLASS_CONSTRAINT", Level.SEVERE,
131                                            getMessage( "implementationMethodDefaultExceptionTargetClassConstraint",
132                                                        i.getIdentifier(), methodType.getName(),
133                                                        methodsType.getExceptions().getDefaultException().getClazz(),
134                                                        methodsType.getExceptions().getDefaultException().getTargetClass() ),
135                                            detailElement ) );
136    
137                                    }
138                                }
139                            }
140                        }
141                    }
142                }
143            }
144            else if ( context.isLoggable( Level.WARNING ) )
145            {
146                context.log( Level.WARNING, getMessage( "modulesNotFound", model.getIdentifier() ), null );
147            }
148    
149            return report;
150        }
151    
152        private static String getMessage( final String key, final Object... arguments )
153        {
154            return MessageFormat.format( ResourceBundle.getBundle(
155                StandaloneModelValidator.class.getName().replace( '.', '/' ) ).getString( key ), arguments );
156    
157        }
158    
159        // SECTION-END
160        // SECTION-START[Constructors]
161        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
162    
163        /** Creates a new {@code StandaloneModelValidator} 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 StandaloneModelValidator()
166        {
167            // SECTION-START[Default Constructor]
168            super();
169            // SECTION-END
170        }
171        // </editor-fold>
172        // SECTION-END
173        // SECTION-START[Dependencies]
174        // SECTION-END
175        // SECTION-START[Properties]
176        // SECTION-END
177        // SECTION-START[Messages]
178        // SECTION-END
179    }