1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  package org.jomc.cli.commands;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.Locale;
36  import java.util.logging.Level;
37  import javax.xml.bind.JAXBContext;
38  import javax.xml.bind.JAXBException;
39  import javax.xml.bind.Marshaller;
40  import javax.xml.bind.util.JAXBSource;
41  import javax.xml.transform.Source;
42  import org.apache.commons.cli.CommandLine;
43  import org.jomc.model.Implementation;
44  import org.jomc.model.Module;
45  import org.jomc.model.Specification;
46  import org.jomc.modlet.Model;
47  import org.jomc.modlet.ModelContext;
48  import org.jomc.modlet.ModelException;
49  import org.jomc.modlet.ModelValidationReport;
50  import org.jomc.modlet.ObjectFactory;
51  import org.jomc.tools.ResourceFileProcessor;
52  
53  
54  
55  
56  
57  
58  
59  
60  public final class GenerateResourcesCommand extends AbstractResourceFileProcessorCommand
61  {
62  
63      
64  
65  
66      public GenerateResourcesCommand()
67      {
68          super();
69      }
70  
71      public String getName()
72      {
73          return "generate-resources";
74      }
75  
76      public String getAbbreviatedName()
77      {
78          return "gr";
79      }
80  
81      public String getShortDescription( final Locale locale )
82      {
83          return Messages.getMessage( "generateResourcesShortDescription" );
84      }
85  
86      public String getLongDescription( final Locale locale )
87      {
88          return null;
89      }
90  
91      protected void processResourceFiles( final CommandLine commandLine ) throws CommandExecutionException
92      {
93          if ( commandLine == null )
94          {
95              throw new NullPointerException( "commandLine" );
96          }
97  
98          CommandLineClassLoader classLoader = null;
99  
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 }