View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.xslt;
23  
24  import java.net.URI;
25  import java.util.List;
26  
27  import javax.swing.Icon;
28  import javax.swing.ImageIcon;
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.transform.TransformerConfigurationException;
31  import javax.xml.transform.TransformerException;
32  
33  import org.bounce.image.ImageLoader;
34  import org.bounce.util.URIUtils;
35  import org.xml.sax.SAXException;
36  import org.xmlhammer.Module;
37  import org.xmlhammer.PreferencesHandler;
38  import org.xmlhammer.ResultModel;
39  import org.xmlhammer.gui.ProjectView;
40  import org.xmlhammer.gui.ProjectsView;
41  import org.xmlhammer.gui.input.InputPage;
42  import org.xmlhammer.gui.output.SourceBuilder;
43  import org.xmlhammer.gui.parser.ParserPage;
44  import org.xmlhammer.gui.preferences.PropertiesDialog;
45  import org.xmlhammer.gui.util.JAXPSettings;
46  import org.xmlhammer.model.jaxp.Attribute;
47  import org.xmlhammer.model.jaxp.Feature;
48  import org.xmlhammer.model.jaxp.JAXPDocumentBuilderFactory;
49  import org.xmlhammer.model.jaxp.JAXPSAXParserFactory;
50  import org.xmlhammer.model.jaxp.JAXPSchemaFactory;
51  import org.xmlhammer.model.jaxp.JAXPTransformerFactory;
52  import org.xmlhammer.model.jaxp.Settings;
53  import org.xmlhammer.model.project.Filter;
54  import org.xmlhammer.model.project.Parameter;
55  import org.xmlhammer.model.project.ParameterisedSource;
56  import org.xmlhammer.model.project.Project;
57  import org.xmlhammer.model.tools.xslt.Associated;
58  import org.xmlhammer.model.tools.xslt.OutputProperty;
59  import org.xmlhammer.model.tools.xslt.XSLT;
60  import org.xmlhammer.model.tools.xslt.XSLT.Transform;
61  
62  /***
63   * The UI representation of a Project.
64   * 
65   * @version $Revision$, $Date$
66   * @author Edwin Dankert <edankert@gmail.com>
67   */
68  public class XSLTProjectView extends ProjectView {
69  	private static final long serialVersionUID = 4121138017445755189L;
70      private static final ImageIcon ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/transform.gif");
71  	
72  	private PropertiesDialog propertiesDialog = null;
73      private SourceBuilder source = null;
74  //    private ParametersPage parametersPage = null;
75      private StylesheetsPage stylesheetsPage = null;
76      private OutputPage outputPage = null;
77  	
78  	/***
79  	 * @param parent the projects view parent.
80  	 * @param uri the uri for the project.
81  	 */
82  	public XSLTProjectView( ProjectsView parent, URI uri) {
83  		super( parent, uri);
84          
85          source = new SourceBuilder();
86  
87          initPages();
88  	}
89  	
90  	/***
91  	 * @return the underlying project.
92  	 */
93  	public Project getProject(URI base) {
94          Project project = super.getProject(base);
95  
96          XSLT xslt = new XSLT();
97          project.setXSLT(xslt);
98          
99          if (getStylesheetsPage() != null && getOutputPage() != null) {
100 //            xslt.setParameters(getParametersPage().getParameters());
101             xslt.setOutputProperties(getOutputPage().getOutputProperties(base));
102             xslt.setTransform(getStylesheetsPage().getTransform(base));
103         }
104 
105         return project;
106 	}
107 	
108     public void setProject(Project project) {
109         if (getStylesheetsPage() != null && getOutputPage() != null) {
110             if ( project != null) {
111 //                    getParametersPage().setParameters(project.getXSLT().getParameters());
112                 getOutputPage().setOutputProperties(getURI(), project.getXSLT().getOutputProperties());
113                 getStylesheetsPage().setTransform(getURI(), project.getXSLT().getTransform());
114             } else {
115 //                    getParametersPage().setParameters(null);
116                 getOutputPage().setOutputProperties(getURI(), null);
117                 getStylesheetsPage().setTransform(getURI(), null);
118             }
119         }
120 
121         super.setProject( project);
122     }
123 
124     protected void createPages() {
125         addPage(createInputPage());
126         addPage(createParserPage());
127 //        addPage(createParametersPage());
128         addPage(createStylesheetsPage());
129         addPage(createOutputPage());
130     }
131 
132     protected InputPage createInputPage() {
133         inputPage = new InputPage(this, true);
134 
135         return inputPage;
136     }
137 
138     private StylesheetsPage createStylesheetsPage() {
139         stylesheetsPage = new StylesheetsPage( this);
140         
141         return stylesheetsPage;
142     }
143 
144     private StylesheetsPage getStylesheetsPage() {
145         return stylesheetsPage;
146     }
147 
148     private OutputPage createOutputPage() {
149         outputPage = new OutputPage( this);
150         
151         return outputPage;
152     }
153 
154     private OutputPage getOutputPage() {
155         return outputPage;
156     }
157 
158 //    private ParametersPage createParametersPage() {
159 //        parametersPage = new ParametersPage( this);
160 //        
161 //        return parametersPage;
162 //    }
163 //
164 //    private ParametersPage getParametersPage() {
165 //        return parametersPage;
166 //    }
167 
168     /***
169 	 * Show the properties dialog.
170 	 */
171 	public void showPropertiesDialog() {
172 		Settings settings = getProject(null).getJAXPSettings();
173 
174 		if ( propertiesDialog == null) {
175 			propertiesDialog = new PropertiesDialog( getProjectsView().getRoot(), true, true, false, true, true);
176 		}
177 
178 		JAXPSAXParserFactory saxFactory = settings.getJAXPSAXParserFactory();
179 		if ( saxFactory == null) {
180 			saxFactory = new JAXPSAXParserFactory();
181 		}
182 
183 		propertiesDialog.setSAXParserFactory(saxFactory);
184 		
185 		JAXPDocumentBuilderFactory documentBuilderFactory = settings.getJAXPDocumentBuilderFactory();
186 		if ( documentBuilderFactory == null) {
187 			documentBuilderFactory = new JAXPDocumentBuilderFactory();
188 		}
189 
190 		propertiesDialog.setDocumentBuilderFactory(documentBuilderFactory);
191 
192 		JAXPSchemaFactory schemaFactory = settings.getJAXPSchemaFactory();
193 		if (schemaFactory == null) {
194 			schemaFactory = new JAXPSchemaFactory();
195 		}
196 
197 		propertiesDialog.setSchemaFactory(schemaFactory);
198 
199         JAXPTransformerFactory transformerFactory = settings.getJAXPTransformerFactory();
200         if (transformerFactory == null) {
201             transformerFactory = new JAXPTransformerFactory();
202         }
203 
204         propertiesDialog.setTransformerFactory(transformerFactory);
205 
206         if (propertiesDialog.open() == PropertiesDialog.OK_OPTION) {
207  			settings.setJAXPSAXParserFactory( propertiesDialog.getSAXParserFactory());
208  			settings.setJAXPDocumentBuilderFactory( propertiesDialog.getDocumentBuilderFactory());
209  			settings.setJAXPSchemaFactory( propertiesDialog.getSchemaFactory());
210             settings.setJAXPTransformerFactory( propertiesDialog.getTransformerFactory());
211  		}
212 	}
213 
214     /***
215      * @return the result model.
216      */
217     public ResultModel getResult() {
218         URI dir = null;
219 
220         if (getInputPage() != null) {
221             Filter filter = getInputPage().getInput(null).getFilter();
222             if ( filter != null) {
223                 dir = URIUtils.createURI( filter.getDir());
224             }
225         }
226 
227         return getResultPanel().createModel( dir);
228 	}
229 	
230     public SourceBuilder getSource() {
231         StringBuilder builder = new StringBuilder();
232         
233         if (getStylesheetsPage() != null && getOutputPage() != null) {
234             builder.append("\n\n");
235             appendJavaImports(builder);
236             getParserPage().appendJavaImports(builder);
237             builder.append("\n");
238             getParserPage().appendJavaxImports(builder);
239             builder.append("\n");
240             
241             if (getParserPage().isDOMSelected()) {
242                 appendDOMImports(builder);
243             } else {
244                 appendSAXImports(builder);
245             }
246             
247             builder.append("\n");
248             getParserPage().appendSAXImports(builder);
249             builder.append("\n");
250             SourceBuilder.appendReserved(builder, "public class");
251             builder.append(" XMLTransformer {\n\n");
252             SourceBuilder.appendReserved(builder, "    public void");
253             builder.append(" transform(URI uri, File file) {\n");
254             getParserPage().appendSource(builder, true);
255             
256             if (getParserPage().isDOMSelected()) {
257                 appendDOMSource(builder);
258             } else {
259                 appendSAXSource(builder);
260             }
261             
262             builder.append("    }\n\n");
263             ParserPage.appendErrorHandler(builder);
264             builder.append("}\n");
265         }
266 
267         source.reset();
268         source.addCode(builder.toString());
269         source.appendDisclaimer();
270 
271         return source;
272     }
273     
274 	/***
275 	 * @return the module to run.
276 	 */
277 	public Module getModule() {
278         try {
279             return new XSLTModule(PreferencesHandler.getInstance().getPreferences(), getProject(null), getLogger());
280         } catch (SAXException e) {
281             showInitializationErrorMessage(e);
282         } catch (ParserConfigurationException e) {
283             showInitializationErrorMessage(e);
284         } catch (TransformerConfigurationException e) {
285             showInitializationErrorMessage(e);
286         } catch (TransformerException e) {
287             showInitializationErrorMessage(e);
288         }
289         
290         return null;
291 	}
292     
293     public void dispose() {
294         if (getInputPage() != null) {
295             getInputPage().dispose();
296         }
297 
298         if (getParserPage() != null) {
299             getParserPage().dispose();
300         }
301 
302         if (getStylesheetsPage() != null) {
303             getStylesheetsPage().dispose();
304         }
305 
306         if (getOutputPage() != null) {
307             getOutputPage().dispose();
308         }
309     }
310     
311     public Icon getIcon() {
312         return ICON;
313     }
314     
315     public void appendJavaImports(StringBuilder builder) {
316         SourceBuilder.appendImport(builder, "java.io.File");
317         SourceBuilder.appendImport(builder, "java.io.FileOutputStream");
318         if (getStylesheetsPage().isStylesheetsSelected()) {
319             SourceBuilder.appendImport(builder, "java.io.ByteArrayOutputStream");
320             SourceBuilder.appendImport(builder, "java.io.ByteArrayInputStream");
321         }
322     }
323 
324     public void appendDOMImports(StringBuilder builder) {
325         if (getStylesheetsPage().isAssociatedSelected()) {
326             SourceBuilder.appendImport(builder, "javax.xml.transform.Source");
327         }
328         SourceBuilder.appendImport(builder, "javax.xml.transform.Transformer");
329         SourceBuilder.appendImport(builder, "javax.xml.transform.TransformerException");
330         SourceBuilder.appendImport(builder, "javax.xml.transform.TransformerFactory");
331         SourceBuilder.appendImport(builder, "javax.xml.transform.dom.DOMSource");
332         SourceBuilder.appendImport(builder, "javax.xml.transform.stream.StreamResult");
333         if (getStylesheetsPage().isStylesheetsSelected()) {
334             SourceBuilder.appendImport(builder, "javax.xml.transform.stream.StreamSource");
335         }
336     }
337 
338     public void appendSAXImports(StringBuilder builder) {
339         if (getStylesheetsPage().isAssociatedSelected()) {
340             SourceBuilder.appendImport(builder, "javax.xml.transform.Source");
341         }
342         SourceBuilder.appendImport(builder, "javax.xml.transform.Transformer");
343         SourceBuilder.appendImport(builder, "javax.xml.transform.TransformerException");
344         SourceBuilder.appendImport(builder, "javax.xml.transform.TransformerFactory");
345         SourceBuilder.appendImport(builder, "javax.xml.transform.sax.SAXSource");
346         SourceBuilder.appendImport(builder, "javax.xml.transform.stream.StreamResult");
347         if (getStylesheetsPage().isStylesheetsSelected()) {
348             SourceBuilder.appendImport(builder, "javax.xml.transform.stream.StreamSource");
349         }
350     }
351 
352     public void appendDOMSource(StringBuilder builder) {
353         if (getProject().getJAXPSettings() != null) {
354             builder.append("\n");
355 
356             JAXPSettings jaxp = new JAXPSettings(PreferencesHandler.getInstance().getPreferences().getJAXPSettings(), getProject().getJAXPSettings());
357     
358             JAXPTransformerFactory.Settings settings = jaxp.getTransformerFactorySettings();
359             
360             if (settings.getValue() != null) {
361                 builder.append("        System.setProperty(");
362                 SourceBuilder.appendConstant(builder, "\"javax.xml.transform.TransformerFactory\"");
363                 builder.append(", ");
364                 SourceBuilder.appendConstant(builder, "\""+settings.getValue()+"\"");
365                 builder.append(");\n\n");
366             }
367             
368             SourceBuilder.appendReserved(builder, "        try ");
369             builder.append("{\n");
370             builder.append("            TransformerFactory transformerFactory = TransformerFactory.newInstance();\n\n");
371 
372             List<Feature> features = jaxp.getTransformerFactoryFeatures();
373 
374             for (Feature feature : features) {
375                 builder.append("            transformerFactory.setFeature(");
376                 SourceBuilder.appendConstant(builder, "\""+feature.getName()+"\"");
377                 builder.append(", ");
378                 SourceBuilder.appendConstant(builder, feature.isEnabled());
379                 builder.append(");\n");
380             }
381 
382             List<Attribute> attributes = jaxp.getTransformerFactoryAttributes();
383 
384             for (Attribute attribute : attributes) {
385                 builder.append("            transformerFactory.setAttribute(");
386                 SourceBuilder.appendConstant(builder, "\""+attribute.getName()+"\"");
387                 builder.append(", ");
388                 SourceBuilder.appendConstant(builder, "\""+attribute.getValue()+"\"");
389                 builder.append(");\n");
390             }
391 
392             Transform transform = getStylesheetsPage().getTransform(null);
393             if (transform.getAssociated() != null) {
394                 Associated associated = transform.getAssociated();
395 
396                 builder.append("            DOMSource source = ");
397                 SourceBuilder.appendReserved(builder, "new");
398                 builder.append(" DOMSource(builder.parse(");
399                 SourceBuilder.appendReserved(builder, "new");
400                 builder.append(" InputSource(uri.toString())));\n");
401 
402                 builder.append("            Source style = transformerFactory.getAssociatedStylesheet(source, ");
403                 if (associated.getMedia() != null) {
404                     SourceBuilder.appendConstant(builder, "\"" + associated.getMedia() + "\"");
405                 } else {
406                     SourceBuilder.appendConstant(builder, "null");
407                 }
408                 builder.append(", ");
409                 if (associated.getTitle() != null) {
410                     SourceBuilder.appendConstant(builder, "\"" + associated.getTitle() + "\"");
411                 } else {
412                     SourceBuilder.appendConstant(builder, "null");
413                 }
414                 builder.append(", ");
415                 if (associated.getCharset() != null) {
416                     SourceBuilder.appendConstant(builder, "\"" + associated.getCharset() + "\"");
417                 } else {
418                     SourceBuilder.appendConstant(builder, "null");
419                 }
420                 builder.append(");\n");
421                 
422                 builder.append("            Transformer transformer = transformerFactory.newTransformer(style);\n");
423 
424                 List<Parameter> parameters = associated.getParameter();
425                 for (Parameter parameter : parameters) {
426                     if (parameter.isActive()) {
427                         builder.append("            transformer.setParameter(");
428                         SourceBuilder.appendConstant(builder, "\"" + parameter.getName() + "\"");
429                         builder.append(", ");
430                         SourceBuilder.appendConstant(builder, "\"" + parameter.getValue() + "\"");
431                         builder.append(");\n");
432                     }
433                 }
434 
435                 builder.append("            StreamResult result = ");
436                 SourceBuilder.appendReserved(builder, "new");
437                 builder.append(" StreamResult(");
438                 SourceBuilder.appendReserved(builder, "new");
439                 builder.append(" FileOutputStream(file));\n");
440 
441                 List<OutputProperty> properties = getOutputPage().getOutputProperties(null).getOutputProperty();
442 
443                 for (OutputProperty property : properties) {
444                     if (property.isActive()) {
445                         builder.append("            transformer.setOutputProperty(");
446                         SourceBuilder.appendConstant(builder, "\"" + property.getName() + "\"");
447                         builder.append(", ");
448                         SourceBuilder.appendConstant(builder, "\"" + property.getValue() + "\"");
449                         builder.append(");\n");
450                     }
451                 }
452 
453                 builder.append("            transformer.transform(source, result);\n\n");
454             } else if (transform.getStylesheets() != null) {
455                 List<ParameterisedSource> stylesheets = transform.getStylesheets().getParameterisedSource();
456 
457                 for (ParameterisedSource stylesheet : stylesheets) {
458                     builder.append("            Transformer transformer"+stylesheets.indexOf(stylesheet)+" = transformerFactory.newTransformer(");
459                     SourceBuilder.appendReserved(builder, "new");
460                     builder.append(" StreamSource(");
461                     SourceBuilder.appendConstant(builder, "\""+stylesheet.getSrc()+"\"");
462                     builder.append("));\n");
463 
464                     List<Parameter> parameters = stylesheet.getParameter();
465 
466                     for (Parameter parameter : parameters) {
467                         if (parameter.isActive()) {
468                             builder.append("            transformer" +stylesheets.indexOf(stylesheet)+ ".setParameter(");
469                             SourceBuilder.appendConstant(builder, "\"" + parameter.getName() + "\"");
470                             builder.append(", ");
471                             SourceBuilder.appendConstant(builder, "\"" + parameter.getValue() + "\"");
472                             builder.append(");\n");
473                         }
474                     }
475 
476                     if (stylesheets.get(0) == stylesheet) {
477                         builder.append("            DOMSource source"+stylesheets.indexOf(stylesheet)+" = ");
478                         SourceBuilder.appendReserved(builder, "new");
479                         builder.append(" DOMSource(builder.parse(");
480                         SourceBuilder.appendReserved(builder, "new");
481                         builder.append(" InputSource(uri.toString())));\n");
482                     } else {
483                         builder.append("            DOMSource source"+stylesheets.indexOf(stylesheet)+" = ");
484                         SourceBuilder.appendReserved(builder, "new");
485                         builder.append(" DOMSource(builder.parse(");
486                         SourceBuilder.appendReserved(builder, "new");
487                         builder.append(" InputSource(");
488                         SourceBuilder.appendReserved(builder, "new");
489                         builder.append(" ByteArrayInputStream(((ByteArrayOutputStream)result"+(stylesheets.indexOf(stylesheet)-1)+".getOutputStream()).toByteArray()))));\n");
490                     }
491                   
492                     if (stylesheets.get(stylesheets.size() - 1) == stylesheet) {
493                         builder.append("            StreamResult result"
494                                 + stylesheets.indexOf(stylesheet) + " = ");
495                         SourceBuilder.appendReserved(builder, "new");
496                         builder.append(" StreamResult(");
497                         SourceBuilder.appendReserved(builder, "new");
498                         builder.append(" FileOutputStream(file));\n");
499 
500                         List<OutputProperty> properties = getOutputPage().getOutputProperties(null).getOutputProperty();
501 
502                         for (OutputProperty property : properties) {
503                             if (property.isActive()) {
504                                 builder.append("            transformer" +stylesheets.indexOf(stylesheet)+ ".setOutputProperty(");
505                                 SourceBuilder.appendConstant(builder, "\"" + property.getName() + "\"");
506                                 builder.append(", ");
507                                 SourceBuilder.appendConstant(builder, "\"" + property.getValue() + "\"");
508                                 builder.append(");\n");
509                             }
510                         }
511                     } else {
512                         builder.append("            StreamResult result" + stylesheets.indexOf(stylesheet) + " = ");
513                         SourceBuilder.appendReserved(builder, "new");
514                         builder.append(" StreamResult(");
515                         SourceBuilder.appendReserved(builder, "new");
516                         builder.append(" ByteArrayOutputStream());\n");
517                     }
518 
519                     builder.append("            transformer" + stylesheets.indexOf(stylesheet) + ".transform(source" + stylesheets.indexOf(stylesheet) + ", result" + stylesheets.indexOf(stylesheet) + ");\n\n");
520                 }
521             } else {
522                 builder.append("            Transformer transformer = transformerFactory.newTransformer();\n");
523                 builder.append("            DOMSource source = ");
524                 SourceBuilder.appendReserved(builder, "new");
525                 builder.append(" DOMSource(builder.parse(");
526                 SourceBuilder.appendReserved(builder, "new");
527                 builder.append(" InputSource(uri.toString())));\n");
528 
529                 builder.append("            StreamResult result = ");
530                 SourceBuilder.appendReserved(builder, "new");
531                 builder.append(" StreamResult(");
532                 SourceBuilder.appendReserved(builder, "new");
533                 builder.append(" FileOutputStream(file));\n");
534 
535                 List<OutputProperty> properties = getOutputPage()
536                         .getOutputProperties(null).getOutputProperty();
537 
538                 for (OutputProperty property : properties) {
539                     if (property.isActive()) {
540                         builder.append("            transformer.setOutputProperty(");
541                         SourceBuilder.appendConstant(builder, "\"" + property.getName() + "\"");
542                         builder.append(", ");
543                         SourceBuilder.appendConstant(builder, "\"" + property.getValue() + "\"");
544                         builder.append(");\n");
545                     }
546                 }
547 
548                 builder.append("            transformer.transform(source, result);\n\n");
549             }
550             
551             SourceBuilder.appendCatch(builder, "        ", "TransformerException");
552             SourceBuilder.appendCatch(builder, "        ", "SAXException");
553             SourceBuilder.appendCatch(builder, "        ", "IOException");
554             builder.append("        }\n");
555         }
556     }
557 
558     public void appendSAXSource(StringBuilder builder) {
559         if (getProject().getJAXPSettings() != null) {
560             builder.append("\n");
561 
562             JAXPSettings jaxp = new JAXPSettings(PreferencesHandler.getInstance().getPreferences().getJAXPSettings(), getProject().getJAXPSettings());
563     
564             JAXPTransformerFactory.Settings settings = jaxp.getTransformerFactorySettings();
565             
566             if (settings.getValue() != null) {
567                 builder.append("        System.setProperty(");
568                 SourceBuilder.appendConstant(builder, "\"javax.xml.transform.TransformerFactory\"");
569                 builder.append(", ");
570                 SourceBuilder.appendConstant(builder, "\""+settings.getValue()+"\"");
571                 builder.append(");\n\n");
572             }
573             
574             SourceBuilder.appendReserved(builder, "        try ");
575             builder.append("{\n");
576             builder.append("            TransformerFactory transformerFactory = TransformerFactory.newInstance();\n\n");
577 
578             List<Feature> features = jaxp.getTransformerFactoryFeatures();
579 
580             for ( Feature feature : features) {
581                 builder.append("            transformerFactory.setFeature(");
582                 SourceBuilder.appendConstant(builder, "\""+feature.getName()+"\"");
583                 builder.append(", ");
584                 SourceBuilder.appendConstant(builder, feature.isEnabled());
585                 builder.append(");\n");
586             }
587 
588             List<Attribute> attributes = jaxp.getTransformerFactoryAttributes();
589 
590             for (Attribute attribute : attributes) {
591                 builder.append("            transformerFactory.setAttribute(");
592                 SourceBuilder.appendConstant(builder, "\""+attribute.getName()+"\"");
593                 builder.append(", ");
594                 SourceBuilder.appendConstant(builder, "\""+attribute.getValue()+"\"");
595                 builder.append(");\n");
596             }
597 
598             Transform transform = getStylesheetsPage().getTransform(null);
599             if (transform.getAssociated() != null) {
600                 Associated associated = transform.getAssociated();
601 
602                 builder.append("            SAXSource source = ");
603                 SourceBuilder.appendReserved(builder, "new");
604                 builder.append(" SAXSource(reader, ");
605                 SourceBuilder.appendReserved(builder, "new");
606                 builder.append(" InputSource(uri.toString()));\n");
607 
608                 builder.append("            Source style = transformerFactory.getAssociatedStylesheet(source, ");
609                 if (associated.getMedia() != null) {
610                     SourceBuilder.appendConstant(builder, "\"" + associated.getMedia() + "\"");
611                 } else {
612                     SourceBuilder.appendConstant(builder, "null");
613                 }
614                 builder.append(", ");
615                 if (associated.getTitle() != null) {
616                     SourceBuilder.appendConstant(builder, "\"" + associated.getTitle() + "\"");
617                 } else {
618                     SourceBuilder.appendConstant(builder, "null");
619                 }
620                 builder.append(", ");
621                 if (associated.getCharset() != null) {
622                     SourceBuilder.appendConstant(builder, "\"" + associated.getCharset() + "\"");
623                 } else {
624                     SourceBuilder.appendConstant(builder, "null");
625                 }
626                 builder.append(");\n");
627                 
628                 builder.append("            Transformer transformer = transformerFactory.newTransformer(style);\n");
629 
630                 List<Parameter> parameters = associated.getParameter();
631                 for (Parameter parameter : parameters) {
632                     if (parameter.isActive()) {
633                         builder.append("            transformer.setParameter(");
634                         SourceBuilder.appendConstant(builder, "\"" + parameter.getName() + "\"");
635                         builder.append(", ");
636                         SourceBuilder.appendConstant(builder, "\"" + parameter.getValue() + "\"");
637                         builder.append(");\n");
638                     }
639                 }
640 
641                 builder.append("            StreamResult result = ");
642                 SourceBuilder.appendReserved(builder, "new");
643                 builder.append(" StreamResult(");
644                 SourceBuilder.appendReserved(builder, "new");
645                 builder.append(" FileOutputStream(file));\n");
646 
647                 List<OutputProperty> properties = getOutputPage().getOutputProperties(null).getOutputProperty();
648 
649                 for (OutputProperty property : properties) {
650                     if (property.isActive()) {
651                         builder.append("            transformer.setOutputProperty(");
652                         SourceBuilder.appendConstant(builder, "\"" + property.getName() + "\"");
653                         builder.append(", ");
654                         SourceBuilder.appendConstant(builder, "\"" + property.getValue() + "\"");
655                         builder.append(");\n");
656                     }
657                 }
658 
659                 builder.append("            transformer.transform(source, result);\n\n");
660             } else if (transform.getStylesheets() != null) {
661                 List<ParameterisedSource> stylesheets = transform.getStylesheets().getParameterisedSource();
662 
663                 for (ParameterisedSource stylesheet : stylesheets) {
664                     builder.append("            Transformer transformer"+stylesheets.indexOf(stylesheet)+" = transformerFactory.newTransformer(");
665                     SourceBuilder.appendReserved(builder, "new");
666                     builder.append(" StreamSource(");
667                     SourceBuilder.appendConstant(builder, "\""+stylesheet.getSrc()+"\"");
668                     builder.append("));\n");
669 
670                     List<Parameter> parameters = stylesheet.getParameter();
671 
672                     for (Parameter parameter : parameters) {
673                         if (parameter.isActive()) {
674                             builder.append("            transformer" +stylesheets.indexOf(stylesheet)+ ".setParameter(");
675                             SourceBuilder.appendConstant(builder, "\"" + parameter.getName() + "\"");
676                             builder.append(", ");
677                             SourceBuilder.appendConstant(builder, "\"" + parameter.getValue() + "\"");
678                             builder.append(");\n");
679                         }
680                     }
681 
682                     if (stylesheets.get(0) == stylesheet) {
683                         builder.append("            SAXSource source"+stylesheets.indexOf(stylesheet)+" = ");
684                         SourceBuilder.appendReserved(builder, "new");
685                         builder.append(" SAXSource(reader, ");
686                         SourceBuilder.appendReserved(builder, "new");
687                         builder.append(" InputSource(uri.toString()));\n");
688                     } else {
689                         builder.append("            SAXSource source"+stylesheets.indexOf(stylesheet)+" = ");
690                         SourceBuilder.appendReserved(builder, "new");
691                         builder.append(" SAXSource(reader, ");
692                         SourceBuilder.appendReserved(builder, "new");
693                         builder.append(" InputSource(");
694                         SourceBuilder.appendReserved(builder, "new");
695                         builder.append(" ByteArrayInputStream(((ByteArrayOutputStream)result"+(stylesheets.indexOf(stylesheet)-1)+".getOutputStream()).toByteArray())));\n");
696                     }
697                   
698                     if (stylesheets.get(stylesheets.size() - 1) == stylesheet) {
699                         builder.append("            StreamResult result"
700                                 + stylesheets.indexOf(stylesheet) + " = ");
701                         SourceBuilder.appendReserved(builder, "new");
702                         builder.append(" StreamResult(");
703                         SourceBuilder.appendReserved(builder, "new");
704                         builder.append(" FileOutputStream(file));\n");
705 
706                         List<OutputProperty> properties = getOutputPage().getOutputProperties(null).getOutputProperty();
707 
708                         for (OutputProperty property : properties) {
709                             if (property.isActive()) {
710                                 builder.append("            transformer" +stylesheets.indexOf(stylesheet)+ ".setOutputProperty(");
711                                 SourceBuilder.appendConstant(builder, "\"" + property.getName() + "\"");
712                                 builder.append(", ");
713                                 SourceBuilder.appendConstant(builder, "\"" + property.getValue() + "\"");
714                                 builder.append(");\n");
715                             }
716                         }
717                     } else {
718                         builder.append("            StreamResult result" + stylesheets.indexOf(stylesheet) + " = ");
719                         SourceBuilder.appendReserved(builder, "new");
720                         builder.append(" StreamResult(");
721                         SourceBuilder.appendReserved(builder, "new");
722                         builder.append(" ByteArrayOutputStream());\n");
723                     }
724 
725                     builder.append("            transformer" + stylesheets.indexOf(stylesheet) + ".transform(source" + stylesheets.indexOf(stylesheet) + ", result" + stylesheets.indexOf(stylesheet) + ");\n\n");
726                 }
727             } else {
728                 builder.append("            Transformer transformer = transformerFactory.newTransformer();\n");
729                 builder.append("            SAXSource source = ");
730                 SourceBuilder.appendReserved(builder, "new");
731                 builder.append(" SAXSource(reader, ");
732                 SourceBuilder.appendReserved(builder, "new");
733                 builder.append(" InputSource(uri.toString()));\n");
734 
735                 builder.append("            StreamResult result = ");
736                 SourceBuilder.appendReserved(builder, "new");
737                 builder.append(" StreamResult(");
738                 SourceBuilder.appendReserved(builder, "new");
739                 builder.append(" FileOutputStream(file));\n");
740 
741                 List<OutputProperty> properties = getOutputPage().getOutputProperties(null).getOutputProperty();
742 
743                 for (OutputProperty property : properties) {
744                     if (property.isActive()) {
745                         builder.append("            transformer.setOutputProperty(");
746                         SourceBuilder.appendConstant(builder, "\"" + property.getName() + "\"");
747                         builder.append(", ");
748                         SourceBuilder.appendConstant(builder, "\"" + property.getValue() + "\"");
749                         builder.append(");\n");
750                     }
751                 }
752 
753                 builder.append("            transformer.transform(source, result);\n\n");
754             }
755 
756             SourceBuilder.appendCatch(builder, "        ", "TransformerException");
757 //            SourceBuilder.appendCatch(builder, "        ", "SAXException");
758             SourceBuilder.appendCatch(builder, "        ", "IOException");
759             builder.append("        }\n");
760         }
761     }
762 
763     @Override
764     public String getHelpID() {
765         return "project.trax";
766     }
767 }