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.io.StringWriter;
36 import java.util.Locale;
37 import java.util.logging.Level;
38 import javax.xml.bind.JAXBContext;
39 import javax.xml.bind.JAXBException;
40 import javax.xml.bind.Marshaller;
41 import javax.xml.bind.util.JAXBSource;
42 import javax.xml.transform.Source;
43 import org.apache.commons.cli.CommandLine;
44 import org.jomc.cli.commands.AbstractModletCommand.CommandLineClassLoader;
45 import org.jomc.model.Instance;
46 import org.jomc.model.Module;
47 import org.jomc.model.Modules;
48 import org.jomc.model.Specification;
49 import org.jomc.model.modlet.ModelHelper;
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
56
57
58
59
60
61 public final class ShowModelCommand extends AbstractModelCommand
62 {
63
64
65
66
67 public ShowModelCommand()
68 {
69 super();
70 }
71
72 @Override
73 public org.apache.commons.cli.Options getOptions()
74 {
75 final org.apache.commons.cli.Options options = super.getOptions();
76 options.addOption( Options.DOCUMENT_OPTION );
77 options.addOption( Options.DOCUMENT_ENCODING_OPTION );
78 options.addOption( Options.IMPLEMENTATION_OPTION );
79 options.addOption( Options.MODULE_OPTION );
80 options.addOption( Options.SPECIFICATION_OPTION );
81 return options;
82 }
83
84 public String getName()
85 {
86 return "show-model";
87 }
88
89 public String getAbbreviatedName()
90 {
91 return "sm";
92 }
93
94 public String getShortDescription( final Locale locale )
95 {
96 return Messages.getMessage( "showModelShortDescription" );
97 }
98
99 public String getLongDescription( final Locale locale )
100 {
101 return null;
102 }
103
104 protected void executeCommand( final CommandLine commandLine ) throws CommandExecutionException
105 {
106 if ( commandLine == null )
107 {
108 throw new NullPointerException( "commandLine" );
109 }
110
111 CommandLineClassLoader classLoader = null;
112
113 try
114 {
115 classLoader = new CommandLineClassLoader( commandLine );
116 final ModelContext context = this.createModelContext( commandLine, classLoader );
117 final Model model = this.getModel( context, commandLine );
118 final JAXBContext jaxbContext = context.createContext( model.getIdentifier() );
119 final Marshaller marshaller = context.createMarshaller( model.getIdentifier() );
120 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
121 final ModelValidationReport validationReport = context.validateModel( model.getIdentifier(), source );
122 final Modules modules = ModelHelper.getModules( model );
123 this.log( validationReport, marshaller );
124
125 if ( !validationReport.isModelValid() )
126 {
127 throw new CommandExecutionException( Messages.getMessage( "invalidModel",
128 this.getModel( commandLine ) ) );
129
130 }
131
132 final Model displayModel = new Model();
133 displayModel.setIdentifier( model.getIdentifier() );
134
135 boolean displayModules = true;
136
137 if ( commandLine.hasOption( Options.IMPLEMENTATION_OPTION.getOpt() ) )
138 {
139 final String identifier = commandLine.getOptionValue( Options.IMPLEMENTATION_OPTION.getOpt() );
140 final Instance instance = modules != null ? modules.getInstance( identifier ) : null;
141 displayModules = false;
142
143 if ( instance != null )
144 {
145 displayModel.getAny().add( new org.jomc.model.ObjectFactory().createInstance( instance ) );
146 }
147 else if ( this.isLoggable( Level.WARNING ) )
148 {
149 this.log( Level.WARNING,
150 Messages.getMessage( "implementationNotFoundWarning", identifier ),
151 null );
152
153 }
154 }
155
156 if ( commandLine.hasOption( Options.SPECIFICATION_OPTION.getOpt() ) )
157 {
158 final String identifier = commandLine.getOptionValue( Options.SPECIFICATION_OPTION.getOpt() );
159 final Specification specification = modules != null ? modules.getSpecification( identifier ) : null;
160 displayModules = false;
161
162 if ( specification != null )
163 {
164 displayModel.getAny().add(
165 new org.jomc.model.ObjectFactory().createSpecification( specification ) );
166
167 }
168 else if ( this.isLoggable( Level.WARNING ) )
169 {
170 this.log( Level.WARNING,
171 Messages.getMessage( "specificationNotFoundWarning", identifier ),
172 null );
173
174 }
175 }
176
177 if ( commandLine.hasOption( Options.MODULE_OPTION.getOpt() ) )
178 {
179 final String moduleName = commandLine.getOptionValue( Options.MODULE_OPTION.getOpt() );
180 final Module m = modules != null ? modules.getModule( moduleName ) : null;
181 displayModules = false;
182
183 if ( m != null )
184 {
185 displayModel.getAny().add( new org.jomc.model.ObjectFactory().createModule( m ) );
186 }
187 else if ( this.isLoggable( Level.WARNING ) )
188 {
189 this.log( Level.WARNING,
190 Messages.getMessage( "moduleNotFoundWarning", moduleName ),
191 null );
192
193 }
194 }
195
196 if ( displayModules )
197 {
198 ModelHelper.setModules( displayModel, modules );
199 }
200
201 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
202
203 if ( commandLine.hasOption( Options.DOCUMENT_ENCODING_OPTION.getOpt() ) )
204 {
205 marshaller.setProperty( Marshaller.JAXB_ENCODING,
206 commandLine.getOptionValue( Options.DOCUMENT_ENCODING_OPTION.getOpt() ) );
207
208 }
209
210 if ( commandLine.hasOption( Options.DOCUMENT_OPTION.getOpt() ) )
211 {
212 final File documentFile = new File( commandLine.getOptionValue( Options.DOCUMENT_OPTION.getOpt() ) );
213
214 if ( this.isLoggable( Level.INFO ) )
215 {
216 this.log( Level.INFO,
217 Messages.getMessage( "writingResource", documentFile.getAbsolutePath() ),
218 null );
219
220 }
221
222 marshaller.marshal( new ObjectFactory().createModel( displayModel ), documentFile );
223 }
224 else if ( this.isLoggable( Level.INFO ) )
225 {
226 final StringWriter stringWriter = new StringWriter();
227 marshaller.marshal( new ObjectFactory().createModel( displayModel ), stringWriter );
228 this.log( Level.INFO, stringWriter.toString(), null );
229 }
230
231 classLoader.close();
232 classLoader = null;
233 }
234 catch ( final IOException e )
235 {
236 throw new CommandExecutionException( Messages.getMessage( e ), e );
237 }
238 catch ( final JAXBException e )
239 {
240 String message = Messages.getMessage( e );
241 if ( message == null )
242 {
243 message = Messages.getMessage( e.getLinkedException() );
244 }
245
246 throw new CommandExecutionException( message, e );
247 }
248 catch ( final ModelException e )
249 {
250 throw new CommandExecutionException( Messages.getMessage( e ), e );
251 }
252 finally
253 {
254 try
255 {
256 if ( classLoader != null )
257 {
258 classLoader.close();
259 }
260 }
261 catch ( final IOException e )
262 {
263 this.log( Level.SEVERE, Messages.getMessage( e ), e );
264 }
265 }
266 }
267
268 }