001/*
002 * Copyright (C) 2009 Christian Schulte <cs@schulte.it>
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions
007 * are met:
008 *
009 *   o Redistributions of source code must retain the above copyright
010 *     notice, this list of conditions and the following disclaimer.
011 *
012 *   o Redistributions in binary form must reproduce the above copyright
013 *     notice, this list of conditions and the following disclaimer in
014 *     the documentation and/or other materials provided with the
015 *     distribution.
016 *
017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 * $JOMC: GenerateResourcesCommand.java 5251 2016-04-25 19:46:04Z schulte $
029 *
030 */
031package org.jomc.cli.commands;
032
033import java.io.File;
034import java.io.IOException;
035import java.util.Locale;
036import java.util.logging.Level;
037import javax.xml.bind.JAXBContext;
038import javax.xml.bind.JAXBException;
039import javax.xml.bind.Marshaller;
040import javax.xml.bind.util.JAXBSource;
041import javax.xml.transform.Source;
042import org.apache.commons.cli.CommandLine;
043import org.jomc.model.Implementation;
044import org.jomc.model.Module;
045import org.jomc.model.Specification;
046import org.jomc.modlet.Model;
047import org.jomc.modlet.ModelContext;
048import org.jomc.modlet.ModelException;
049import org.jomc.modlet.ModelValidationReport;
050import org.jomc.modlet.ObjectFactory;
051import org.jomc.tools.ResourceFileProcessor;
052
053// SECTION-START[Documentation]
054// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
055/**
056 * {@code generate-resources} command implementation.
057 *
058 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
059 */
060public final class GenerateResourcesCommand extends AbstractResourceFileProcessorCommand
061{
062
063    /**
064     * Creates a new {@code GenerateResourcesCommand} instance.
065     */
066    public GenerateResourcesCommand()
067    {
068        super();
069    }
070
071    public String getName()
072    {
073        return "generate-resources";
074    }
075
076    public String getAbbreviatedName()
077    {
078        return "gr";
079    }
080
081    public String getShortDescription( final Locale locale )
082    {
083        return Messages.getMessage( "generateResourcesShortDescription" );
084    }
085
086    public String getLongDescription( final Locale locale )
087    {
088        return null;
089    }
090
091    protected void processResourceFiles( final CommandLine commandLine ) throws CommandExecutionException
092    {
093        if ( commandLine == null )
094        {
095            throw new NullPointerException( "commandLine" );
096        }
097
098        CommandLineClassLoader classLoader = null;
099
100        try
101        {
102            classLoader = new CommandLineClassLoader( commandLine );
103            final ModelContext context = this.createModelContext( commandLine, classLoader );
104            final Model model = this.getModel( context, commandLine );
105            final JAXBContext jaxbContext = context.createContext( model.getIdentifier() );
106            final Marshaller marshaller = context.createMarshaller( model.getIdentifier() );
107            final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
108            final ModelValidationReport validationReport = context.validateModel( model.getIdentifier(), source );
109            this.log( validationReport, marshaller );
110
111            if ( !validationReport.isModelValid() )
112            {
113                throw new CommandExecutionException( Messages.getMessage( "invalidModel",
114                                                                          this.getModel( commandLine ) ) );
115
116            }
117
118            final ResourceFileProcessor tool = this.createResourceFileProcessor( commandLine );
119            tool.setModel( model );
120
121            final File resourcesDirectory =
122                new File( commandLine.getOptionValue( Options.RESOURCE_DIRECTORY_OPTION.getOpt() ) );
123
124            final Specification specification = this.getSpecification( commandLine, model );
125            final Implementation implementation = this.getImplementation( commandLine, model );
126            final Module module = this.getModule( commandLine, model );
127
128            if ( specification != null )
129            {
130                tool.writeResourceBundleResourceFiles( specification, resourcesDirectory );
131            }
132
133            if ( implementation != null )
134            {
135                tool.writeResourceBundleResourceFiles( implementation, resourcesDirectory );
136            }
137
138            if ( module != null )
139            {
140                tool.writeResourceBundleResourceFiles( module, resourcesDirectory );
141            }
142
143            if ( this.isModulesProcessingRequested( commandLine ) )
144            {
145                tool.writeResourceBundleResourceFiles( resourcesDirectory );
146            }
147
148            classLoader.close();
149            classLoader = null;
150        }
151        catch ( final JAXBException e )
152        {
153            String message = Messages.getMessage( e );
154            if ( message == null )
155            {
156                message = Messages.getMessage( e.getLinkedException() );
157            }
158
159            throw new CommandExecutionException( message, e );
160        }
161        catch ( final ModelException e )
162        {
163            throw new CommandExecutionException( Messages.getMessage( e ), e );
164        }
165        catch ( final IOException e )
166        {
167            throw new CommandExecutionException( Messages.getMessage( e ), e );
168        }
169        finally
170        {
171            try
172            {
173                if ( classLoader != null )
174                {
175                    classLoader.close();
176                }
177            }
178            catch ( final IOException e )
179            {
180                this.log( Level.SEVERE, Messages.getMessage( e ), e );
181            }
182        }
183    }
184
185}