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.ArrayList;
36 import java.util.List;
37 import java.util.Locale;
38 import java.util.logging.Level;
39 import javax.xml.bind.JAXBContext;
40 import javax.xml.bind.JAXBException;
41 import javax.xml.bind.Marshaller;
42 import javax.xml.bind.util.JAXBSource;
43 import javax.xml.transform.Source;
44 import javax.xml.transform.Transformer;
45 import javax.xml.transform.stream.StreamSource;
46 import org.apache.commons.cli.CommandLine;
47 import org.jomc.model.Implementation;
48 import org.jomc.model.Module;
49 import org.jomc.model.Specification;
50 import org.jomc.modlet.Model;
51 import org.jomc.modlet.ModelContext;
52 import org.jomc.modlet.ModelException;
53 import org.jomc.modlet.ModelValidationReport;
54 import org.jomc.modlet.ObjectFactory;
55 import org.jomc.tools.ClassFileProcessor;
56
57
58
59
60
61
62 public final class CommitClassesCommand extends AbstractClassFileProcessorCommand
63 {
64
65
66
67
68 public CommitClassesCommand()
69 {
70 super();
71 }
72
73 @Override
74 public org.apache.commons.cli.Options getOptions()
75 {
76 final org.apache.commons.cli.Options options = super.getOptions();
77 options.addOption( Options.CLASSES_DIRECTORY_OPTION );
78 options.addOption( Options.STYLESHEET_OPTION );
79 return options;
80 }
81
82 public String getName()
83 {
84 return "commit-classes";
85 }
86
87 public String getAbbreviatedName()
88 {
89 return "cc";
90 }
91
92 public String getShortDescription( final Locale locale )
93 {
94 return Messages.getMessage( "commitClassesShortDescription" );
95 }
96
97 public String getLongDescription( final Locale locale )
98 {
99 return null;
100 }
101
102 protected void processClassFiles( final CommandLine commandLine ) throws CommandExecutionException
103 {
104 if ( commandLine == null )
105 {
106 throw new NullPointerException( "commandLine" );
107 }
108
109 CommandLineClassLoader classLoader = null;
110
111 try
112 {
113 classLoader = new CommandLineClassLoader( commandLine );
114 final ModelContext context = this.createModelContext( commandLine, classLoader );
115 final Model model = this.getModel( context, commandLine );
116 final JAXBContext jaxbContext = context.createContext( model.getIdentifier() );
117 final Marshaller marshaller = context.createMarshaller( model.getIdentifier() );
118 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
119 final ModelValidationReport validationReport = context.validateModel( model.getIdentifier(), source );
120 this.log( validationReport, marshaller );
121
122 if ( !validationReport.isModelValid() )
123 {
124 throw new CommandExecutionException( Messages.getMessage( "invalidModel",
125 this.getModel( commandLine ) ) );
126
127 }
128
129 final ClassFileProcessor tool = this.createClassFileProcessor( commandLine );
130 tool.setModel( model );
131
132 final File classesDirectory =
133 new File( commandLine.getOptionValue( Options.CLASSES_DIRECTORY_OPTION.getOpt() ) );
134
135 final List<Transformer> transformers = new ArrayList<Transformer>( 1 );
136
137 if ( commandLine.hasOption( Options.STYLESHEET_OPTION.getOpt() ) )
138 {
139 final File stylesheetFile =
140 new File( commandLine.getOptionValue( Options.STYLESHEET_OPTION.getOpt() ) );
141
142 transformers.add( this.createTransformer( new StreamSource( stylesheetFile ) ) );
143 }
144
145 final Specification specification = this.getSpecification( commandLine, model );
146 final Implementation implementation = this.getImplementation( commandLine, model );
147 final Module module = this.getModule( commandLine, model );
148
149 if ( specification != null )
150 {
151 tool.commitModelObjects( specification, context, classesDirectory );
152
153 if ( !transformers.isEmpty() )
154 {
155 tool.transformModelObjects( specification, context, classesDirectory, transformers );
156 }
157 }
158
159 if ( implementation != null )
160 {
161 tool.commitModelObjects( implementation, context, classesDirectory );
162
163 if ( !transformers.isEmpty() )
164 {
165 tool.transformModelObjects( implementation, context, classesDirectory, transformers );
166 }
167 }
168
169 if ( module != null )
170 {
171 tool.commitModelObjects( module, context, classesDirectory );
172
173 if ( !transformers.isEmpty() )
174 {
175 tool.transformModelObjects( module, context, classesDirectory, transformers );
176 }
177 }
178
179 if ( this.isModulesProcessingRequested( commandLine ) )
180 {
181 tool.commitModelObjects( context, classesDirectory );
182
183 if ( !transformers.isEmpty() )
184 {
185 tool.transformModelObjects( context, classesDirectory, transformers );
186 }
187 }
188
189 classLoader.close();
190 classLoader = null;
191 }
192 catch ( final JAXBException e )
193 {
194 String message = Messages.getMessage( e );
195 if ( message == null )
196 {
197 message = Messages.getMessage( e.getLinkedException() );
198 }
199
200 throw new CommandExecutionException( message, e );
201 }
202 catch ( final ModelException e )
203 {
204 throw new CommandExecutionException( Messages.getMessage( e ), e );
205 }
206 catch ( final IOException e )
207 {
208 throw new CommandExecutionException( Messages.getMessage( e ), e );
209 }
210 finally
211 {
212 try
213 {
214 if ( classLoader != null )
215 {
216 classLoader.close();
217 }
218 }
219 catch ( final IOException e )
220 {
221 this.log( Level.SEVERE, Messages.getMessage( e ), e );
222 }
223 }
224 }
225
226 }