View Javadoc

1   /*
2    * $Id: XMLHammer.java,v 1.27 2007/09/27 13:26:21 edankert Exp $
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  package org.xmlhammer.cli;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.net.URI;
26  import java.text.DateFormat;
27  import java.util.Date;
28  import java.util.List;
29  
30  import javax.xml.bind.JAXBContext;
31  import javax.xml.bind.util.JAXBResult;
32  import javax.xml.parsers.ParserConfigurationException;
33  import javax.xml.transform.Transformer;
34  import javax.xml.transform.TransformerConfigurationException;
35  import javax.xml.transform.TransformerException;
36  import javax.xml.transform.TransformerFactory;
37  import javax.xml.transform.stream.StreamSource;
38  import javax.xml.xpath.XPathExpressionException;
39  
40  import org.apache.log4j.Logger;
41  import org.apache.log4j.PatternLayout;
42  import org.apache.log4j.WriterAppender;
43  import org.bounce.util.URIUtils;
44  import org.xml.sax.SAXException;
45  import org.xmlhammer.Module;
46  import org.xmlhammer.ModuleThread;
47  import org.xmlhammer.PreferencesHandler;
48  import org.xmlhammer.XMLValidatorModule;
49  import org.xmlhammer.gui.actions.OpenAction;
50  import org.xmlhammer.gui.schemavalidator.SchemaValidatorModule;
51  import org.xmlhammer.gui.stylesheetvalidator.StylesheetValidatorModule;
52  import org.xmlhammer.gui.util.ExtensionClassLoader;
53  import org.xmlhammer.gui.xpath.XPathSearchModule;
54  import org.xmlhammer.gui.xslt.XSLTModule;
55  import org.xmlhammer.model.preferences.Preferences;
56  import org.xmlhammer.model.preferences.Preferences.Classpath.Jar;
57  import org.xmlhammer.model.project.Document;
58  import org.xmlhammer.model.project.Filter;
59  import org.xmlhammer.model.project.Input;
60  import org.xmlhammer.model.project.ParameterisedSource;
61  import org.xmlhammer.model.project.Parser;
62  import org.xmlhammer.model.project.Project;
63  import org.xmlhammer.model.project.Source;
64  import org.xmlhammer.model.tools.xslt.XSLT;
65  import org.xmlhammer.model.tools.xslt.XSLT.Transform;
66  
67  /***
68   * Put comment...
69   * 
70   * @version $Revision: 1.27 $, $Date: 2007/09/27 13:26:21 $
71   * @author Edwin Dankert <edankert@gmail.com>
72   */
73  public class XMLHammer {
74      public XMLHammer(boolean defaultPrefs) {
75          ExtensionClassLoader classLoader = new ExtensionClassLoader(getClass().getClassLoader());
76          Thread.currentThread().setContextClassLoader(classLoader);
77  
78          if (defaultPrefs) {
79              PreferencesHandler.getInstance().useDefaultPreferences();
80          }
81  
82          Preferences preferences = PreferencesHandler.getInstance().getPreferences();
83  
84          List<Jar> jars = preferences.getClasspath().getJar();
85          for (Jar jar : jars) {
86              if (jar.isActive() && jar.getSrc() != null) {
87                  classLoader.add(new File(jar.getSrc()));
88              }
89          }
90          
91          PreferencesHandler.getInstance().updateProxy();
92          PreferencesHandler.getInstance().updateCatalogProperties();
93  
94          Logger.getLogger("org.xmlhammer").addAppender(new WriterAppender(new PatternLayout("%m%n"), new ByteArrayOutputStream()));
95      }
96      
97      public void run(String location, Logger logger, boolean logSettings) {
98          try {
99              Preferences preferences = PreferencesHandler.getInstance().getPreferences();
100             
101             File file = new File(location);
102 
103             JAXBContext context = JAXBContext.newInstance( "org.xmlhammer.model.project");
104             JAXBResult result = new JAXBResult(context);
105             
106             // set up XSLT transformation
107             TransformerFactory factory = TransformerFactory.newInstance();
108             Transformer transformer = factory.newTransformer(new StreamSource(OpenAction.class.getResourceAsStream("/org/xmlhammer/convert/convert-from-2006-01-to-2007.xsl")));
109                   
110             // run transformation
111             transformer.transform(new StreamSource(file.toURL().toString()), result);
112             
113             Project project = (Project)result.getResult();
114             resolveURIs(file, project);
115             
116             logger.info("Start time: "+DateFormat.getDateTimeInstance().format(new Date()));
117             Module module = getModule(project, preferences, logger, logSettings);
118             ModuleThread runner = new ModuleThread(logger, null, null, project.getInput(), module);
119             runner.run();
120 
121 //        } catch ( MalformedURLException e) {
122 //            logger.fatal("Malformed URL Error", e);
123 //        } catch ( JAXBException e) {
124 //            logger.fatal("JAXB Error", e);
125 //        } catch ( XPathFactoryConfigurationException e) {
126 //            logger.fatal("XPath Factory Configuration Error", e);
127 //        } catch ( XPathExpressionException e) {
128 //            logger.fatal("XPath Expression Error", e);
129 //        } catch ( ParserConfigurationException e) {
130 //            logger.fatal("Parser Configuration Error", e);
131 //        } catch ( SAXException e) {
132 //            logger.fatal("SAX Error", e);
133 //        } catch (TransformerConfigurationException e) {
134 //            logger.fatal("Transformer Configuration Error", e);
135 //        } catch (TransformerException e) {
136 //            logger.fatal("Transformer Error", e);
137         } catch (Exception e) {
138             logger.fatal("Execution aborted");
139             // logger.fatal("Transformer Error", e);
140         }
141     }
142     
143     private Module getModule(Project project, Preferences preferences, Logger logger, boolean logSettings) throws XPathExpressionException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
144         Module module = null;
145 
146         if (project.getXPath() != null) {
147             module = new XPathSearchModule(preferences, project, logger, logSettings);
148         } else if (project.getXSLT() != null) {
149             module = new XSLTModule(preferences, project, logger, logSettings);
150         } else if (project.getSchemaValidator() != null) {
151             module = new SchemaValidatorModule(preferences, project, logger, logSettings);
152         } else if (project.getParser() != null) {
153             module = new XMLValidatorModule(preferences, project, logger, logSettings);
154         } else {
155             module = new StylesheetValidatorModule(preferences, project, logger, logSettings);
156         }
157         
158         return module;
159     }
160     
161     private void resolveURIs(File file, Project project) {
162         URI base = file.toURI();
163         
164         Input input = project.getInput();
165         Filter filter = input.getFilter();
166         if (filter != null) {
167             filter.setDir(URIUtils.getRelativePath(null,URIUtils.composeFile(base, filter.getDir())));
168             
169             if (filter.getOutDir() != null) {
170                 filter.setOutDir(URIUtils.getRelativePath(null, URIUtils.composeFile(base, filter.getOutDir())));
171             }
172         } else {
173             List<Document> documents = input.getSourceOrSourceAndResult();
174             for (Document document : documents) {
175                 document.setSrc(URIUtils.toString(URIUtils.composeURI(base, document.getSrc())));
176             }
177         }
178         
179         Parser parser = project.getParser();
180         if (parser != null) {
181             List<Source> sources = parser.getSource();
182             for (Source source : sources) {
183                 source.setSrc(URIUtils.toString(URIUtils.composeURI(base, source.getSrc())));
184             }
185         }
186 
187         XSLT xslt = project.getXSLT();
188         if (xslt != null) {
189             Transform transform = xslt.getTransform();
190             
191             if (transform != null && transform.getStylesheets() != null) {
192                 List<ParameterisedSource> sources = transform.getStylesheets().getParameterisedSource();
193                 for (ParameterisedSource source : sources) {
194                     source.setSrc(URIUtils.toString(URIUtils.composeURI(base, source.getSrc())));
195                 }
196             }
197         }
198     }
199 }