CPD Results

The following document contains the results of PMD's CPD 4.3.

Duplications

FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java811
org\jomc\mojo\JomcResourceTransformer.java828
        this.modletMarshaller.marshal( element, file );
    }

    private <T> JAXBElement<T> transformModletObject( final JAXBElement<? extends ModletObject> element,
                                                      final Class<T> boundType )
        throws ModelException, TransformerException, JAXBException, IOException, URISyntaxException,
               InstantiationException
    {
        if ( element == null )
        {
            throw new NullPointerException( "element" );
        }
        if ( !boundType.isInstance( element.getValue() ) )
        {
            throw new IllegalArgumentException( element.toString() );
        }

        @SuppressWarnings( "unchecked" )
        JAXBElement<T> transformed = (JAXBElement<T>) element;

        if ( this.modletObjectStylesheet != null )
        {
            final Transformer transformer = TransformerFactory.newInstance().newTransformer(
                new StreamSource( this.getResource( this.modletObjectStylesheet ).toURI().toASCIIString() ) );

            final ModelContext modletContext = this.createModelContext();
            final Marshaller marshaller = modletContext.createMarshaller( ModletObject.MODEL_PUBLIC_ID );
            final Unmarshaller unmarshaller = modletContext.createUnmarshaller( ModletObject.MODEL_PUBLIC_ID );
            final JAXBSource source = new JAXBSource( marshaller, element );
            final JAXBResult result = new JAXBResult( unmarshaller );

            for ( Map.Entry<Object, Object> e : System.getProperties().entrySet() )
            {
                transformer.setParameter( e.getKey().toString(), e.getValue() );
            }

            transformer.transform( source, result );

            if ( result.getResult() instanceof JAXBElement<?>
                 && boundType.isInstance( ( (JAXBElement<?>) result.getResult() ).getValue() ) )
            {
                @SuppressWarnings( "unchecked" ) final JAXBElement<T> e = (JAXBElement<T>) result.getResult();
                transformed = e;
            }
            else
            {
                throw new ModelException( Messages.getMessage(
                    "illegalModletTransformationResult", this.modletObjectStylesheet ) );

            }
        }

        return transformed;
    }

    private static String normalizeResourceName( final String name )
    {
        String normalized = name;

        if ( normalized != null )
        {
            normalized = normalized.replace( '\\', '/' );

            if ( normalized.startsWith( "/" ) )
            {
                normalized = normalized.substring( 1 );
            }

            if ( normalized.endsWith( "/" ) )
            {
                normalized = normalized.substring( 0, normalized.length() );
            }
        }

        return normalized;
    }

    private ModelContext createModelContext() throws ModelException, InstantiationException
    {
        final ModelContextFactory modelContextFactory;
        if ( this.modelContextFactoryClassName != null )
        {
            modelContextFactory = ModelContextFactory.newInstance( this.modelContextFactoryClassName );
        }
        else
        {
            modelContextFactory = ModelContextFactory.newInstance();
        }

        final ModelContext modelContext = modelContextFactory.newModelContext();
        modelContext.setModletSchemaSystemId( this.modletSchemaSystemId );

        if ( this.providerLocation != null )
        {
            modelContext.setAttribute( DefaultModelContext.PROVIDER_LOCATION_ATTRIBUTE_NAME, this.providerLocation );
        }

        if ( this.platformProviderLocation != null )
        {
            modelContext.setAttribute( DefaultModelContext.PLATFORM_PROVIDER_LOCATION_ATTRIBUTE_NAME,
                                       this.platformProviderLocation );

        }

        if ( this.modletLocation != null )
        {
            modelContext.setAttribute( DefaultModletProvider.MODLET_LOCATION_ATTRIBUTE_NAME, this.modletLocation );
        }

        if ( this.modelContextAttributes != null )
        {
            for ( ModelContextAttribute e : this.modelContextAttributes )
            {
                final Object object = e.getObject();

                if ( object != null )
                {
                    modelContext.setAttribute( e.getKey(), object );
                }
                else
                {
                    modelContext.clearAttribute( e.getKey() );
                }
            }
        }

        return modelContext;
    }

}
FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java715
org\jomc\mojo\JomcResourceTransformer.java732
        this.jomcMarshaller.marshal( element, file );
    }

    private <T> JAXBElement<T> transformModelObject( final JAXBElement<? extends ModelObject> element,
                                                     final Class<T> boundType )
        throws ModelException, TransformerException, JAXBException, IOException, URISyntaxException,
               InstantiationException
    {
        if ( element == null )
        {
            throw new NullPointerException( "element" );
        }
        if ( !boundType.isInstance( element.getValue() ) )
        {
            throw new IllegalArgumentException( element.toString() );
        }

        @SuppressWarnings( "unchecked" )
        JAXBElement<T> transformed = (JAXBElement<T>) element;

        if ( this.modelObjectStylesheet != null )
        {
            final Transformer transformer = TransformerFactory.newInstance().newTransformer(
                new StreamSource( this.getResource( this.modelObjectStylesheet ).toURI().toASCIIString() ) );

            final ModelContext modelContext = this.createModelContext();
            final Marshaller marshaller = modelContext.createMarshaller( this.model );
            final Unmarshaller unmarshaller = modelContext.createUnmarshaller( this.model );
            final JAXBSource source = new JAXBSource( marshaller, element );
            final JAXBResult result = new JAXBResult( unmarshaller );

            for ( Map.Entry<Object, Object> e : System.getProperties().entrySet() )
            {
                transformer.setParameter( e.getKey().toString(), e.getValue() );
            }

            transformer.transform( source, result );

            if ( result.getResult() instanceof JAXBElement<?>
                 && boundType.isInstance( ( (JAXBElement<?>) result.getResult() ).getValue() ) )
            {
                @SuppressWarnings( "unchecked" ) final JAXBElement<T> e = (JAXBElement<T>) result.getResult();
                transformed = e;
            }
            else
            {
                throw new ModelException( Messages.getMessage(
                    "illegalModuleTransformationResult", this.modelObjectStylesheet ) );

            }
        }

        return transformed;
    }

    private Object unmarshalModletObject( final InputStream in )
        throws ModelException, JAXBException, InstantiationException
    {
        if ( in == null )
        {
            throw new NullPointerException( "in" );
        }

        if ( this.modletUnmarshaller == null )
        {
            this.modletUnmarshaller = this.createModelContext().createUnmarshaller( ModletObject.MODEL_PUBLIC_ID );
        }

        return this.modletUnmarshaller.unmarshal( in );
    }

    private void marshalModletObject( final JAXBElement<? extends ModletObject> element, final File file )
FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java340
org\jomc\mojo\JomcResourceTransformer.java489
                archiver.addFile( moduleFile, normalizeResourceName( this.moduleResource ) );
            }

            if ( !this.modlets.getModlet().isEmpty() )
            {
                if ( this.modletIncludes != null )
                {
                    for ( final Iterator<Modlet> it = this.modlets.getModlet().iterator(); it.hasNext(); )
                    {
                        final Modlet m = it.next();

                        if ( !this.modletIncludes.contains( m.getName() ) )
                        {
                            it.remove();

                            if ( this.getLogger() != null && this.getLogger().isInfoEnabled() )
                            {
                                this.getLogger().info( LOG_PREFIX + Messages.getMessage(
                                    "excludingModlet", m.getName() ) );

                            }
                        }
                    }
                }

                if ( this.modletExcludes != null )
                {
                    for ( String exclude : this.modletExcludes )
                    {
                        final Modlet excluded = this.modlets.getModlet( exclude );

                        if ( excluded != null )
                        {
                            this.modlets.getModlet().remove( excluded );

                            if ( this.getLogger() != null && this.getLogger().isInfoEnabled() )
                            {
                                this.getLogger().info( LOG_PREFIX + Messages.getMessage(
                                    "excludingModlet", excluded.getName() ) );

                            }
                        }
                    }
                }

                if ( this.getLogger() != null && this.getLogger().isInfoEnabled() )
                {
                    for ( Modlet m : this.modlets.getModlet() )
                    {
                        this.getLogger().info( LOG_PREFIX + Messages.getMessage( "includingModlet", m.getName() ) );
                    }
                }

                final Modlet mergedModlet = this.modlets.getMergedModlet( this.modletName, this.model );
                mergedModlet.setVendor( this.modletVendor );
                mergedModlet.setVersion( this.modletVersion );

                final JAXBElement<Modlet> transformedModlet = this.transformModletObject(
                    new org.jomc.modlet.ObjectFactory().createModlet( mergedModlet ), Modlet.class );
FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java278
org\jomc\mojo\JomcResourceTransformer.java431
            if ( !this.modules.getModule().isEmpty() )
            {
                if ( this.moduleIncludes != null )
                {
                    for ( final Iterator<Module> it = this.modules.getModule().iterator(); it.hasNext(); )
                    {
                        final Module m = it.next();

                        if ( !this.moduleIncludes.contains( m.getName() ) )
                        {
                            it.remove();

                            if ( this.getLogger() != null && this.getLogger().isInfoEnabled() )
                            {
                                this.getLogger().info( LOG_PREFIX + Messages.getMessage(
                                    "excludingModule", m.getName() ) );

                            }
                        }
                    }
                }

                if ( this.moduleExcludes != null )
                {
                    for ( String exclude : this.moduleExcludes )
                    {
                        final Module excluded = this.modules.getModule( exclude );

                        if ( excluded != null )
                        {
                            this.modules.getModule().remove( excluded );

                            if ( this.getLogger() != null && this.getLogger().isInfoEnabled() )
                            {
                                this.getLogger().info( LOG_PREFIX + Messages.getMessage(
                                    "excludingModule", excluded.getName() ) );

                            }
                        }
                    }
                }

                if ( this.getLogger() != null && this.getLogger().isInfoEnabled() )
                {
                    for ( Module m : this.modules.getModule() )
                    {
                        this.getLogger().info( LOG_PREFIX + Messages.getMessage( "includingModule", m.getName() ) );
                    }
                }

                final Module mergedModule = this.modules.getMergedModule( this.moduleName );
                mergedModule.setVersion( this.moduleVersion );
                mergedModule.setVendor( this.moduleVendor );

                final JAXBElement<Module> transformedModule = this.transformModelObject(
                    new org.jomc.model.ObjectFactory().createModule( mergedModule ), Module.class );
FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java594
org\jomc\mojo\JomcResourceTransformer.java609
        }
    }

    /**
     * Creates an {@code URL} for a given resource location.
     * <p>This method first searches the class loader of the class for a single resource matching {@code location}. If
     * such a resource is found, the URL of that resource is returned. If no such resource is found, an attempt is made
     * to parse the given location to an URL. On successful parsing, that URL is returned. Failing that, the given
     * location is interpreted as a file name. If that file is found, the URL of that file is returned. Otherwise an
     * {@code IOException} is thrown.</p>
     *
     * @param location The location to create an {@code URL} from.
     *
     * @return An {@code URL} for {@code location}.
     *
     * @throws NullPointerException if {@code location} is {@code null}.
     * @throws IOException if creating an URL fails.
     */
    protected URL getResource( final String location ) throws IOException
    {
        if ( location == null )
        {
            throw new NullPointerException( "location" );
        }

        try
        {
            String absolute = location;
            if ( !absolute.startsWith( "/" ) )
            {
                absolute = "/" + location;
            }

            URL resource = this.getClass().getResource( absolute );
            if ( resource == null )
            {
                try
                {
                    resource = new URL( location );
                }
                catch ( final MalformedURLException e )
                {
                    if ( this.getLogger() != null && this.getLogger().isDebugEnabled() )
                    {
                        this.getLogger().debug( Messages.getMessage( e ), e );
                    }

                    resource = null;
                }
            }

            if ( resource == null )
            {
                final File f = new File( location );

                if ( f.isFile() )
                {
                    resource = f.toURI().toURL();
                }
            }

            if ( resource == null )
            {
                throw new IOException( Messages.getMessage( "resourceNotFound", location ) );
            }

            return resource;
        }
        catch ( final MalformedURLException e )
        {
            String m = Messages.getMessage( e );
            m = m == null ? "" : " " + m;

            // JDK: As of JDK 6, "new IOException( message, cause )".
            throw (IOException) new IOException( Messages.getMessage(
                "malformedLocation", location, m ) ).initCause( e );

        }
    }

    private Object unmarshalModelObject( final InputStream in )
        throws ModelException, JAXBException, InstantiationException
    {
        if ( in == null )
        {
            throw new NullPointerException( "in" );
        }

        if ( this.jomcUnmarshaller == null )
        {
            this.jomcUnmarshaller = this.createModelContext().createUnmarshaller( this.model );
        }

        return this.jomcUnmarshaller.unmarshal( in );
    }

    private void marshalModelObject( final JAXBElement<? extends ModelObject> element, final File file )
FileLine
org\jomc\mojo\AbstractJomcMojo.java1142
org\jomc\mojo\AbstractJomcMojo.java1190
        for ( final Iterator<?> it = compileArtifacts.iterator(); it.hasNext(); )
        {
            final Artifact a = (Artifact) it.next();
            final Artifact pluginArtifact = this.getPluginArtifact( a );

            if ( a.getFile() == null )
            {
                if ( this.isLoggable( Level.WARNING ) )
                {
                    this.log( Level.WARNING, Messages.getMessage( "ignoringArtifact", a.toString() ), null );
                }

                continue;
            }

            if ( pluginArtifact != null )
            {
                if ( this.isLoggable( Level.FINER ) )
                {
                    this.log( Level.FINER, Messages.getMessage(
                        "ignoringPluginArtifact", a.toString(), pluginArtifact.toString() ), null );

                }

                continue;
            }

            final String element = a.getFile().getAbsolutePath();
            elements.add( element );
        }

        return elements;
    }

    /**
     * Gets the project's test class path elements.
     *
     * @return A set of class path element strings.
     *
     * @throws MojoExecutionException if getting the class path elements fails.
     */
    protected Set<String> getTestClasspathElements() throws MojoExecutionException
FileLine
org\jomc\mojo\AbstractJomcMojo.java1111
org\jomc\mojo\AbstractJomcMojo.java1142
org\jomc\mojo\AbstractJomcMojo.java1190
        for ( final Iterator<?> it = runtimeArtifacts.iterator(); it.hasNext(); )
        {
            final Artifact a = (Artifact) it.next();
            final Artifact pluginArtifact = this.getPluginArtifact( a );

            if ( a.getFile() == null )
            {
                if ( this.isLoggable( Level.WARNING ) )
                {
                    this.log( Level.WARNING, Messages.getMessage( "ignoringArtifact", a.toString() ), null );
                }

                continue;
            }

            if ( pluginArtifact != null )
            {
                if ( this.isLoggable( Level.FINER ) )
                {
                    this.log( Level.FINER, Messages.getMessage(
                        "ignoringPluginArtifact", a.toString(), pluginArtifact.toString() ), null );

                }

                continue;
            }

            final String element = a.getFile().getAbsolutePath();
            elements.add( element );
        }
FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java140
org\jomc\mojo\JomcResourceTransformer.java143
    private static final String LOG_PREFIX = "[JOMC] ";

    /** The identifier of the model to process. */
    private String model = ModelObject.MODEL_PUBLIC_ID;

    /** The encoding of the assembled module. */
    private String moduleEncoding;

    /** The name of the assembled module. */
    private String moduleName;

    /** The version of the assembled module. */
    private String moduleVersion;

    /** The vendor of the assembled module. */
    private String moduleVendor;

    /** The resource name of the assembled module. */
    private String moduleResource = DefaultModelProvider.getDefaultModuleLocation();

    /** Names of resources to process. */
    private String[] moduleResources =
    {
        DefaultModelProvider.getDefaultModuleLocation()
    };

    /** Included modules. */
    private List<String> moduleIncludes;

    /** Excluded modules. */
    private List<String> moduleExcludes;

    /** The encoding of the assembled modlet. */
    private String modletEncoding;

    /** The name of the assembled modlet. */
    private String modletName;

    /** The version of the assembled modlet. */
    private String modletVersion;

    /** The vendor of the assembled modlet. */
    private String modletVendor;

    /** The resource name of the assembled modlet resources. */
    private String modletResource = DefaultModletProvider.getDefaultModletLocation();

    /** Names of modlet resources to process. */
    private String[] modletResources =
    {
        DefaultModletProvider.getDefaultModletLocation()
    };

    /** Included modlets. */
    private List<String> modletIncludes;

    /** Excluded modlets. */
    private List<String> modletExcludes;

    /** Location of a XSLT document to use for transforming the merged model document. */
    private String modelObjectStylesheet;

    /** Location of a XSLT document to use for transforming the merged modlet document. */
    private String modletObjectStylesheet;

    /** The location to search for providers. */
    private String providerLocation;

    /** The location to search for platform providers. */
    private String platformProviderLocation;

    /** The system id of the modlet schema. */
    private String modletSchemaSystemId;

    /** The location to search for modlets. */
    private String modletLocation;

    /** Name of the {@code ModelContextFactory} implementation class. */
    private String modelContextFactoryClassName;

    /** {@code ModelContext} attributes to apply. */
    private List<ModelContextAttribute> modelContextAttributes;

    /** Modlet resources. */
    private Modlets modlets = new Modlets();

    /** Model resources. */
    private Modules modules = new Modules();

    /** The JOMC JAXB marshaller of the instance. */
    private Marshaller jomcMarshaller;
FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java742
org\jomc\mojo\JomcContainerDescriptorHandler.java838
org\jomc\mojo\JomcResourceTransformer.java759
org\jomc\mojo\JomcResourceTransformer.java855
            final Unmarshaller unmarshaller = modelContext.createUnmarshaller( this.model );
            final JAXBSource source = new JAXBSource( marshaller, element );
            final JAXBResult result = new JAXBResult( unmarshaller );

            for ( Map.Entry<Object, Object> e : System.getProperties().entrySet() )
            {
                transformer.setParameter( e.getKey().toString(), e.getValue() );
            }

            transformer.transform( source, result );

            if ( result.getResult() instanceof JAXBElement<?>
                 && boundType.isInstance( ( (JAXBElement<?>) result.getResult() ).getValue() ) )
            {
                @SuppressWarnings( "unchecked" ) final JAXBElement<T> e = (JAXBElement<T>) result.getResult();
                transformed = e;
            }
            else
            {
                throw new ModelException( Messages.getMessage(
FileLine
org\jomc\mojo\AbstractJomcMojo.java1026
org\jomc\mojo\AbstractJomcMojo.java1074
                this.log( Level.FINEST, Messages.getMessage( "mainClasspathInfo" ), null );
            }

            int i = 0;
            final URL[] urls = new URL[ uris.size() ];
            for ( URI uri : uris )
            {
                urls[i++] = uri.toURL();

                if ( this.isLoggable( Level.FINEST ) )
                {
                    this.log( Level.FINEST, "\t" + urls[i - 1].toExternalForm(), null );
                }
            }

            return new URLClassLoader( urls, Thread.currentThread().getContextClassLoader() );
        }
        catch ( final IOException e )
        {
            throw new MojoExecutionException( Messages.getMessage( e ), e );
        }
    }

    /**
     * Gets the project's test class loader of the instance.
     *
     * @return The project's test class loader of the instance.
     *
     * @throws MojoExecutionException if getting the class loader fails.
     */
    protected ClassLoader getTestClassLoader() throws MojoExecutionException
FileLine
org\jomc\mojo\MainInstanceShowMojo.java71
org\jomc\mojo\TestInstanceShowMojo.java71
    public MainInstanceShowMojo()
    {
        super();
    }

    @Override
    protected Model getDisplayModel( final ModelContext modelContext ) throws MojoExecutionException
    {
        final Model model = this.getModel( modelContext );
        final Modules modules = ModelHelper.getModules( model );
        final Instance instance = modules != null ? modules.getInstance( this.identifier ) : null;
        Model displayModel = null;

        if ( instance != null )
        {
            displayModel = new Model();
            displayModel.setIdentifier( model.getIdentifier() );
            displayModel.getAny().add( new ObjectFactory().createInstance( instance ) );
        }

        return displayModel;
    }

    @Override
    protected ClassLoader getDisplayClassLoader() throws MojoExecutionException
    {
        return this.getMainClassLoader();
FileLine
org\jomc\mojo\MainSpecificationShowMojo.java71
org\jomc\mojo\TestSpecificationShowMojo.java71
    public MainSpecificationShowMojo()
    {
        super();
    }

    @Override
    protected Model getDisplayModel( final ModelContext modelContext ) throws MojoExecutionException
    {
        final Model model = this.getModel( modelContext );
        final Modules modules = ModelHelper.getModules( model );
        final Specification specification = modules != null ? modules.getSpecification( this.identifier ) : null;
        Model displayModel = null;

        if ( specification != null )
        {
            displayModel = new Model();
            displayModel.setIdentifier( model.getIdentifier() );
            displayModel.getAny().add( new ObjectFactory().createSpecification( specification ) );
        }

        return displayModel;
    }

    @Override
    protected ClassLoader getDisplayClassLoader() throws MojoExecutionException
    {
        return this.getMainClassLoader();
FileLine
org\jomc\mojo\JomcContainerDescriptorHandler.java573
org\jomc\mojo\JomcResourceTransformer.java372
        }
        catch ( final InstantiationException e )
        {
            // JDK: As of JDK 6, "new IOException( message, cause )".
            throw (IOException) new IOException( Messages.getMessage( e ) ).initCause( e );
        }
        catch ( final JAXBException e )
        {
            String message = Messages.getMessage( e );
            if ( message == null && e.getLinkedException() != null )
            {
                message = Messages.getMessage( e.getLinkedException() );
            }

            // JDK: As of JDK 6, "new IOException( message, cause )".
            throw (IOException) new IOException( message ).initCause( e );
        }
        catch ( final ModelException e )
        {
            // JDK: As of JDK 6, "new IOException( message, cause )".
            throw (IOException) new IOException( Messages.getMessage( e ) ).initCause( e );
        }
    }