1 | /* |
2 | * Copyright (c) 2009 The JOMC Project |
3 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * o Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * |
13 | * o Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in |
15 | * the documentation and/or other materials provided with the |
16 | * distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * |
30 | * $Id: DefaultModelProcessor.java 1352 2010-01-19 13:27:01Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.model; |
34 | |
35 | import java.io.IOException; |
36 | import java.io.InputStream; |
37 | import java.net.URL; |
38 | import java.text.MessageFormat; |
39 | import java.util.Enumeration; |
40 | import java.util.LinkedList; |
41 | import java.util.List; |
42 | import java.util.Locale; |
43 | import java.util.ResourceBundle; |
44 | import java.util.logging.Level; |
45 | import javax.xml.bind.JAXBContext; |
46 | import javax.xml.bind.JAXBElement; |
47 | import javax.xml.bind.JAXBException; |
48 | import javax.xml.bind.util.JAXBResult; |
49 | import javax.xml.bind.util.JAXBSource; |
50 | import javax.xml.transform.ErrorListener; |
51 | import javax.xml.transform.Transformer; |
52 | import javax.xml.transform.TransformerConfigurationException; |
53 | import javax.xml.transform.TransformerException; |
54 | import javax.xml.transform.TransformerFactory; |
55 | import javax.xml.transform.stream.StreamSource; |
56 | |
57 | /** |
58 | * Default {@code ModelProcessor} implementation. |
59 | * |
60 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
61 | * @version $Id: DefaultModelProcessor.java 1352 2010-01-19 13:27:01Z schulte2005 $ |
62 | * @see ModelContext#processModules(org.jomc.model.Modules) |
63 | */ |
64 | public class DefaultModelProcessor implements ModelProcessor |
65 | { |
66 | |
67 | /** |
68 | * Classpath location searched for transformers by default. |
69 | * @see #getDefaultTransformerLocation() |
70 | */ |
71 | private static final String DEFAULT_TRANSFORMER_LOCATION = "META-INF/jomc.xsl"; |
72 | |
73 | /** Default transformer location. */ |
74 | private static volatile String defaultTransformerLocation; |
75 | |
76 | /** Transformer location of the instance. */ |
77 | private String transformerLocation; |
78 | |
79 | /** Creates a new {@code DefaultModelProcessor} instance. */ |
80 | public DefaultModelProcessor() |
81 | { |
82 | super(); |
83 | } |
84 | |
85 | /** |
86 | * Gets the default location searched for transformer resources. |
87 | * <p>The default transformer location is controlled by system property |
88 | * {@code org.jomc.model.DefaultModelProcessor.defaultTransformerLocation} holding the location to search for |
89 | * transformer resources by default. If that property is not set, the {@code META-INF/jomc.xsl} default is |
90 | * returned.</p> |
91 | * |
92 | * @return The location searched for transformer resources by default. |
93 | * |
94 | * @see #setDefaultTransformerLocation(java.lang.String) |
95 | */ |
96 | public static String getDefaultTransformerLocation() |
97 | { |
98 | if ( defaultTransformerLocation == null ) |
99 | { |
100 | defaultTransformerLocation = System.getProperty( |
101 | "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation", DEFAULT_TRANSFORMER_LOCATION ); |
102 | |
103 | } |
104 | |
105 | return defaultTransformerLocation; |
106 | } |
107 | |
108 | /** |
109 | * Sets the default location searched for transformer resources. |
110 | * |
111 | * @param value The new default location to search for transformer resources or {@code null}. |
112 | * |
113 | * @see #getDefaultTransformerLocation() |
114 | */ |
115 | public static void setDefaultTransformerLocation( final String value ) |
116 | { |
117 | defaultTransformerLocation = value; |
118 | } |
119 | |
120 | /** |
121 | * Gets the location searched for transformer resources. |
122 | * |
123 | * @return The location searched for transformer resources. |
124 | * |
125 | * @see #getDefaultTransformerLocation() |
126 | * @see #setTransformerLocation(java.lang.String) |
127 | */ |
128 | public String getTransformerLocation() |
129 | { |
130 | if ( this.transformerLocation == null ) |
131 | { |
132 | this.transformerLocation = getDefaultTransformerLocation(); |
133 | } |
134 | |
135 | return this.transformerLocation; |
136 | } |
137 | |
138 | /** |
139 | * Sets the location searched for transformer resources. |
140 | * |
141 | * @param value The new location to search for transformer resources or {@code null}. |
142 | * |
143 | * @see #getTransformerLocation() |
144 | */ |
145 | public void setTransformerLocation( final String value ) |
146 | { |
147 | this.transformerLocation = value; |
148 | } |
149 | |
150 | /** |
151 | * Searches a given context for transformers. |
152 | * |
153 | * @param context The context to search for transformers. |
154 | * @param location The location to search at. |
155 | * |
156 | * @return The transformers found at {@code location} in {@code context} or {@code null} of no transformers are |
157 | * found. |
158 | * |
159 | * @throws NullPointerException if {@code context} or {@code location} is {@code null}. |
160 | * @throws ModelException if getting the transformers fails. |
161 | */ |
162 | public List<Transformer> findTransformers( final ModelContext context, final String location ) throws ModelException |
163 | { |
164 | if ( context == null ) |
165 | { |
166 | throw new NullPointerException( "context" ); |
167 | } |
168 | if ( location == null ) |
169 | { |
170 | throw new NullPointerException( "location" ); |
171 | } |
172 | |
173 | try |
174 | { |
175 | final long t0 = System.currentTimeMillis(); |
176 | final List<Transformer> transformers = new LinkedList<Transformer>(); |
177 | final TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
178 | final Enumeration<URL> resources = context.findResources( location ); |
179 | final ErrorListener errorListener = new ErrorListener() |
180 | { |
181 | |
182 | public void warning( final TransformerException exception ) throws TransformerException |
183 | { |
184 | if ( context.isLoggable( Level.WARNING ) ) |
185 | { |
186 | context.log( Level.WARNING, exception.getMessage(), exception ); |
187 | } |
188 | } |
189 | |
190 | public void error( final TransformerException exception ) throws TransformerException |
191 | { |
192 | if ( context.isLoggable( Level.SEVERE ) ) |
193 | { |
194 | context.log( Level.SEVERE, exception.getMessage(), exception ); |
195 | } |
196 | |
197 | throw exception; |
198 | } |
199 | |
200 | public void fatalError( final TransformerException exception ) throws TransformerException |
201 | { |
202 | if ( context.isLoggable( Level.SEVERE ) ) |
203 | { |
204 | context.log( Level.SEVERE, exception.getMessage(), exception ); |
205 | } |
206 | |
207 | throw exception; |
208 | } |
209 | |
210 | }; |
211 | |
212 | transformerFactory.setErrorListener( errorListener ); |
213 | |
214 | int count = 0; |
215 | while ( resources.hasMoreElements() ) |
216 | { |
217 | count++; |
218 | final URL url = resources.nextElement(); |
219 | |
220 | if ( context.isLoggable( Level.FINE ) ) |
221 | { |
222 | context.log( Level.FINE, this.getMessage( "processing", new Object[] |
223 | { |
224 | url.toExternalForm() |
225 | } ), null ); |
226 | |
227 | } |
228 | |
229 | final InputStream in = url.openStream(); |
230 | final Transformer transformer = transformerFactory.newTransformer( new StreamSource( in ) ); |
231 | in.close(); |
232 | |
233 | transformer.setErrorListener( errorListener ); |
234 | transformers.add( transformer ); |
235 | } |
236 | |
237 | if ( context.isLoggable( Level.FINE ) ) |
238 | { |
239 | context.log( Level.FINE, this.getMessage( "contextReport", new Object[] |
240 | { |
241 | count, location, Long.valueOf( System.currentTimeMillis() - t0 ) |
242 | } ), null ); |
243 | |
244 | } |
245 | |
246 | return transformers.isEmpty() ? null : transformers; |
247 | } |
248 | catch ( final IOException e ) |
249 | { |
250 | throw new ModelException( e ); |
251 | } |
252 | catch ( final TransformerConfigurationException e ) |
253 | { |
254 | throw new ModelException( e ); |
255 | } |
256 | } |
257 | |
258 | /** |
259 | * {@inheritDoc} |
260 | * |
261 | * @see #getTransformerLocation() |
262 | * @see #findTransformers(org.jomc.model.ModelContext, java.lang.String) |
263 | */ |
264 | public Modules processModules( final ModelContext context, final Modules modules ) throws ModelException |
265 | { |
266 | if ( context == null ) |
267 | { |
268 | throw new NullPointerException( "context" ); |
269 | } |
270 | if ( modules == null ) |
271 | { |
272 | throw new NullPointerException( "modules" ); |
273 | } |
274 | |
275 | try |
276 | { |
277 | final ObjectFactory objectFactory = new ObjectFactory(); |
278 | final JAXBContext jaxbContext = context.createContext(); |
279 | final List<Transformer> transformers = this.findTransformers( context, this.getTransformerLocation() ); |
280 | Modules processed = new Modules( modules ); |
281 | |
282 | if ( transformers != null ) |
283 | { |
284 | for ( Transformer t : transformers ) |
285 | { |
286 | final JAXBElement<Modules> e = objectFactory.createModules( processed ); |
287 | final JAXBSource source = new JAXBSource( jaxbContext, e ); |
288 | final JAXBResult result = new JAXBResult( jaxbContext ); |
289 | t.transform( source, result ); |
290 | processed = ( (JAXBElement<Modules>) result.getResult() ).getValue(); |
291 | } |
292 | } |
293 | |
294 | return processed; |
295 | } |
296 | catch ( final TransformerException e ) |
297 | { |
298 | throw new ModelException( e ); |
299 | } |
300 | catch ( final JAXBException e ) |
301 | { |
302 | throw new ModelException( e ); |
303 | } |
304 | } |
305 | |
306 | private String getMessage( final String key, final Object args ) |
307 | { |
308 | return new MessageFormat( |
309 | ResourceBundle.getBundle( DefaultModelProcessor.class.getName().replace( '.', '/' ), Locale.getDefault() ). |
310 | getString( key ) ).format( args ); |
311 | |
312 | } |
313 | |
314 | } |