Author: thomasobrien95
Date: Mon Jul 20 08:16:16 2009
New Revision: 3125

Added:
   trunk/lib/commons-io-1.4.jar   (contents, props changed)
   trunk/lib/velocity-1.6.2.jar   (contents, props changed)
   trunk/regress/ca/sqlpower/architect/transformation/
trunk/regress/ca/sqlpower/architect/transformation/VelocityTransformationTest.java
   trunk/src/ca/sqlpower/architect/transformation/
   trunk/src/ca/sqlpower/architect/transformation/ReportTransformer.java
   trunk/src/ca/sqlpower/architect/transformation/TransformerFactory.java
trunk/src/ca/sqlpower/architect/transformation/UnknowTemplateTypeException.java trunk/src/ca/sqlpower/architect/transformation/VelocityTransformation.java
   trunk/src/ca/sqlpower/architect/transformation/XsltTransformation.java
Modified:
   trunk/.classpath
   trunk/src/ca/sqlpower/architect/swingui/action/ExportHTMLPanel.java

Log:
Refactored the Transformation classes into the Architect as it is
dependent on an ArchitectSwingSession. Also moved it's test and
associated jar files with it. These classes have their own package now
to keep them together as well.

Modified: trunk/.classpath
==============================================================================
--- trunk/.classpath    (original)
+++ trunk/.classpath    Mon Jul 20 08:16:16 2009
@@ -36,5 +36,7 @@
        <classpathentry kind="lib" path="lib/opencsv-1.8.jar"/>
        <classpathentry kind="lib" path="lib/rsyntaxtextarea.jar"/>
<classpathentry kind="lib" path="lib/forms-1.2.1.jar" sourcepath="libsrc/forms-1.2.1-src.zip"/>
+       <classpathentry kind="lib" path="lib/velocity-1.6.2.jar"/>
+       <classpathentry kind="lib" path="lib/commons-io-1.4.jar"/>
        <classpathentry kind="output" path="build"/>
 </classpath>

Added: trunk/lib/commons-io-1.4.jar
==============================================================================
Binary file. No diff available.

Added: trunk/lib/velocity-1.6.2.jar
==============================================================================
Binary file. No diff available.

Added: trunk/regress/ca/sqlpower/architect/transformation/VelocityTransformationTest.java
==============================================================================
--- (empty file)
+++ trunk/regress/ca/sqlpower/architect/transformation/VelocityTransformationTest.java Mon Jul 20 08:16:16 2009
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2009, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package ca.sqlpower.architect.transformation;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.IOUtils;
+
+import ca.sqlpower.architect.swingui.ArchitectSwingSession;
+import ca.sqlpower.architect.swingui.SwingUIProject;
+import ca.sqlpower.architect.swingui.TestingArchitectSwingSessionContext;
+import ca.sqlpower.sql.PlDotIni;
+
+/**
+ *
+ * @author Thomas Kellerer
+ */
+public class VelocityTransformationTest extends TestCase {
+
+    public VelocityTransformationTest(String testName) {
+        super(testName);
+    }
+
+       private SwingUIProject project;
+       private static final String ENCODING="UTF-8";
+       private PlDotIni plIni;
+    private ArchitectSwingSession session;
+    private TestingArchitectSwingSessionContext context;
+
+       /*
+ * Test method for 'ca.sqlpower.architect.swingui.SwingUIProject.SwingUIProject(String)'
+        */
+       public void setUp() throws Exception {
+        context = new TestingArchitectSwingSessionContext();
+        session = context.createSession(false);
+        project = session.getProject();
+        plIni = new PlDotIni();
+               ByteArrayInputStream r = new 
ByteArrayInputStream(testData.getBytes());
+               project.load(r, plIni);
+       }
+
+       private final String testData =
+        "<?xml version='1.0'?>" +
+        "<architect-project version='0.1'>" +
+        " <project-name>TestSwingUIProject</project-name>" +
+        " <project-data-sources>" +
+        "  <data-source id='DS0'>" +
+        "   <property key='Logical' value='Not Configured' />" +
+        "  </data-source>" +
+        " </project-data-sources>" +
+        " <source-databases>" +
+        " </source-databases>" +
+        " <target-database dbcs-ref='DS0'>" +
+ " <table id='TAB0' populated='true' primaryKeyName='id' remarks='' name='Customers' >" +
+        "   <folder id='FOL1' populated='true' name='Columns' type='1' >" +
+ " <column id='COL2' populated='true' autoIncrement='false' name='id' defaultValue='' nullable='0' precision='10' primaryKeySeq='0' referenceCount='1' remarks='' scale='0' type='4' />" + + " <column id='COL3' populated='true' autoIncrement='false' name='name' defaultValue='' nullable='0' precision='10' referenceCount='1' remarks='' scale='0' type='4' />" +
+        "   </folder>" +
+ " <folder id='FOL4' populated='true' name='Exported Keys' type='3' >" +
+        "   </folder>" +
+ " <folder id='FOL5' populated='true' name='Imported Keys' type='2' >" +
+        "   </folder>" +
+ " <folder id='FOL13' populated='true' name='Indices' type='4' >" +
+        "   </folder>" +
+        "  </table>" +
+ " <table id='TAB6' populated='true' primaryKeyName='id' remarks='' name='Orders' >" +
+        "   <folder id='FOL7' populated='true' name='Columns' type='1' >" +
+ " <column id='COL8' populated='true' autoIncrement='false' name='id' defaultValue='' " + + " nullable='0' precision='10' primaryKeySeq='0' referenceCount='1' scale='0' type='4' />" + + " <column id='COL9' populated='true' autoIncrement='false' name='customer_id' defaultValue='' nullable='0' precision='10' referenceCount='1' remarks='' scale='0' type='4' />" +
+        "   </folder>" +
+ " <folder id='FOL10' populated='true' name='Exported Keys' type='3' >" +
+        "   </folder>" +
+ " <folder id='FOL11' populated='true' name='Imported Keys' type='2' >" +
+        "   </folder>" +
+ " <folder id='FOL14' populated='true' name='Indices' type='4' >" +
+        "   </folder>" +
+        "  </table>" +
+ " <table id=\"TAB1830\" populated=\"true\" name=\"mm_project\" objectType=\"TABLE\" physicalName=\"MM_PROJECT\" remarks=\"\" >" + + " <folder id=\"FOL1831\" populated=\"true\" name=\"Columns\" physicalName=\"Columns\" type=\"1\" >" + + " <column id=\"COL1832\" populated=\"true\" autoIncrement=\"true\" autoIncrementSequenceName=\"mm_project_oid_seq\" name=\"project_oid\" nullable=\"0\" physicalName=\"PROJECT_OID\" precision=\"22\" primaryKeySeq=\"0\" referenceCount=\"1\" remarks=\"\" scale=\"0\" type=\"4\" />" + + " <column id=\"COL1833\" populated=\"true\" autoIncrement=\"false\" name=\"FOLDER_OID\" nullable=\"1\" physicalName=\"FOLDER_OID\" precision=\"22\" referenceCount=\"2\" remarks=\"\" scale=\"0\" type=\"4\" />" + + " <column id=\"COL1834\" populated=\"true\" autoIncrement=\"false\" name=\"project_name\" nullable=\"1\" physicalName=\"PROJECT_NAME\" precision=\"80\" referenceCount=\"1\" remarks=\"\" scale=\"0\" type=\"12\" />" +
+        "   </folder>" +
+ " <folder id=\"FOL1889\" populated=\"true\" name=\"Exported Keys\" physicalName=\"Exported Keys\" type=\"3\" >" +
+        "   </folder>" +
+ " <folder id=\"FOL1890\" populated=\"true\" name=\"Imported Keys\" physicalName=\"Imported Keys\" type=\"2\" >" +
+        "   </folder>" +
+ " <folder id=\"FOL1891\" populated=\"true\" name=\"Indices\" physicalName=\"Indices\" type=\"4\" >" + + " <index id=\"IDX1892\" populated=\"true\" index-type=\"BTREE\" name=\"mm_project_pk\" physicalName=\"PL_MATCH_PK\" primaryKeyIndex=\"true\" unique=\"true\" >" + + " <index-column id=\"IDC1893\" populated=\"true\" ascending=\"false\" column-ref=\"COL1832\" descending=\"false\" name=\"project_oid\" physicalName=\"MATCH_OID\" />" +
+        "    </index>" +
+ " <index id=\"IDX1894\" populated=\"true\" index-type=\"BTREE\" name=\"PL_MATCH_UNIQUE\" physicalName=\"PL_MATCH_UNIQUE\" primaryKeyIndex=\"false\" unique=\"true\" >" + + " <index-column id=\"IDC1895\" populated=\"true\" ascending=\"false\" column-ref=\"COL1834\" descending=\"false\" name=\"project_name\" physicalName=\"MATCH_ID\" />" +
+        "    </index>" +
+        "   </folder>" +
+        "  </table>" +
+        "  <relationships>" +
+ " <relationship id='REL12' populated='true' deferrability='0' deleteRule='0' fk-table-ref='TAB0' fkCardinality='6' identifying='true' name='Orders_Customers_fk' pk-table-ref='TAB6' pkCardinality='2' updateRule='0' >" + + " <column-mapping id='CMP13' populated='true' fk-column-ref='COL2' pk-column-ref='COL8' />" +
+        "   </relationship>" +
+        "   <reference ref-id='REL12' />" +
+        "  </relationships>" +
+        " </target-database>" +
+ " <ddl-generator type='ca.sqlpower.architect.ddl.GenericDDLGenerator' allow-connection='true'> </ddl-generator>" + + " <compare-dm-settings ddlGenerator='ca.sqlpower.architect.ddl.SQLServerDDLGenerator' outputFormatAsString='ENGLISH'>" + + " <source-stuff datastoreTypeAsString='PROJECT' connectName='Arthur_test' " +
+        " schema='ARCHITECT_REGRESS' filepath='' />"+
+ "<target-stuff datastoreTypeAsString='FILE' filePath='Testpath' /> </compare-dm-settings>"+ + " <play-pen zoom=\"12.3\" viewportX=\"200\" viewportY=\"20\" relationship-style=\"rectilinear\" showPrimaryTag=\"true\" showForeignTag=\"true\" showAlternateTag=\"true\" columnVisibility=\"PK_FK\" >" +
+        "  <table-pane table-ref='TAB0' x='85' y='101' />" +
+        "  <table-pane table-ref='TAB6' x='196' y='38' />" +
+ " <table-link relationship-ref='REL12' pk-x='76' pk-y='60' fk-x='114' fk-y='30' />" +
+        " </play-pen>" +
+        " <profiles topNCount=\"10\">" +
+        " </profiles>" +
+        "</architect-project>";
+
+       public void testTransform() throws Exception {
+               File tempdir = new File(System.getProperty("java.io.tmpdir"));
+               File vm = new File(tempdir, "test_template.vm");
+
+               OutputStream out = new FileOutputStream(vm);
+ PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, ENCODING));
+               
+               writer.print("#foreach ($table in $tables) \n" +
+                 "Table: $table.name\n" +
+                 "#foreach ($col in $table.columns)\n" +
+                 "     $col.shortDisplayName\n" +
+                 "#end\n" +
+                 "#end\n");
+               writer.close();
+
+               File output = new File(tempdir, "test-output.txt");
+
+               VelocityTransformation vt = new VelocityTransformation();
+               vt.transform(vm, output, session);
+               assertTrue(output.exists());
+               FileInputStream fi = new FileInputStream(output);
+               InputStreamReader in = new InputStreamReader(fi, "UTF-8");
+               List<String> lines = IOUtils.readLines(in);
+               assertEquals("Table: Customers", lines.get(0));
+               assertEquals("     id: INTEGER(10)", lines.get(1));
+               assertEquals("     name: INTEGER(10)", lines.get(2));
+       }
+
+}

Modified: trunk/src/ca/sqlpower/architect/swingui/action/ExportHTMLPanel.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/action/ExportHTMLPanel.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/action/ExportHTMLPanel.java Mon Jul 20 08:16:16 2009
@@ -45,19 +45,20 @@
 import javax.swing.JPanel;
 import javax.swing.JRadioButton;
 import javax.swing.JTextField;
-
 import javax.swing.filechooser.FileFilter;
+
 import org.apache.log4j.Logger;

 import ca.sqlpower.architect.swingui.ArchitectFrame;
 import ca.sqlpower.architect.swingui.ArchitectSwingSession;
+import ca.sqlpower.architect.transformation.ReportTransformer;
+import ca.sqlpower.architect.transformation.TransformerFactory;
+import ca.sqlpower.architect.transformation.UnknowTemplateTypeException;
+import ca.sqlpower.architect.transformation.XsltTransformation;
 import ca.sqlpower.sqlobject.SQLObjectException;
 import ca.sqlpower.swingui.JDefaultButton;
 import ca.sqlpower.swingui.SPSUtils;
 import ca.sqlpower.util.BrowserUtil;
-import ca.sqlpower.util.ReportTransformer;
-import ca.sqlpower.util.TransformerFactory;
-import ca.sqlpower.util.UnknowTemplateTypeException;

 import com.jgoodies.forms.builder.DefaultFormBuilder;
 import com.jgoodies.forms.debug.FormDebugPanel;

Added: trunk/src/ca/sqlpower/architect/transformation/ReportTransformer.java
==============================================================================
--- (empty file)
+++ trunk/src/ca/sqlpower/architect/transformation/ReportTransformer.java Mon Jul 20 08:16:16 2009
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2009, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package ca.sqlpower.architect.transformation;
+
+import ca.sqlpower.architect.swingui.ArchitectSwingSession;
+import java.io.File;
+
+public interface ReportTransformer {
+
+       /**
+ * Applies a built in template to the current project's file, sending the results
+        * to file identified by result
+        *
+ * @param builtInXsltName the name of the built-in XSLT (part of the classpath)
+        * @param xml The XML that should be transformed
+ * @param result the output stream where the result of the transformation should be written to
+        */
+ void transform(String builtInTemplate, File result, ArchitectSwingSession session) throws Exception;
+
+       /**
+ * Applies an external template to the current project's file, sending the results
+        * to file identified by result
+        *
+        * @param the XSLT that should be run
+        * @param xml The XML that should be transformed
+ * @param result the output stream where the result of the transformation should be written to
+        */
+ void transform(File template, File output, ArchitectSwingSession session) throws Exception;
+
+}

Added: trunk/src/ca/sqlpower/architect/transformation/TransformerFactory.java
==============================================================================
--- (empty file)
+++ trunk/src/ca/sqlpower/architect/transformation/TransformerFactory.java Mon Jul 20 08:16:16 2009
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2009, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package ca.sqlpower.architect.transformation;
+
+import java.io.File;
+
+/**
+ *
+ * @author Thomas Kellerer
+ */
+public class TransformerFactory {
+       
+       /**
+        * Creates an instance of ReportTransformer based on the extension of
+        * the given template.
+        * <br/>
+        * Currently XSLT and Velocity are supported.
+        * <br/>
+        * The correct transformer is identified by looking at the extension of
+        * the file. The following rules apply
+        * <ul>
+        *   <li>xslt, xsl will create an XsltTransformation</li>
+        *   <li>vm will create a VelocitTransformation</li>
+        * </ul>
+        *
+        * @param template the template that should be run
+        * @return
+        */
+       public static ReportTransformer getTransformer(File template)
+               throws UnknowTemplateTypeException {
+
+               // If no template is defined, the built-in XSLT is used
+               if (template == null) return new XsltTransformation();
+               
+               String fullname = template.getAbsolutePath();
+               int pos = fullname.lastIndexOf('.');
+               if (pos == -1) throw new UnknowTemplateTypeException();
+
+               String ext = fullname.substring(pos + 1).toLowerCase();
+               if (ext.equals("xslt") || ext.equals("xsl")) {
+                       return new XsltTransformation();
+               } else if (ext.equals("vm")) {
+                       return new VelocityTransformation();
+               }
+               throw new UnknowTemplateTypeException();
+       }
+}

Added: trunk/src/ca/sqlpower/architect/transformation/UnknowTemplateTypeException.java
==============================================================================
--- (empty file)
+++ trunk/src/ca/sqlpower/architect/transformation/UnknowTemplateTypeException.java Mon Jul 20 08:16:16 2009
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2009, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package ca.sqlpower.architect.transformation;
+
+public class UnknowTemplateTypeException extends Exception {
+
+       public UnknowTemplateTypeException() {
+               super("Uknown template type. File extension missing or 
unknown");
+       }
+
+}

Added: trunk/src/ca/sqlpower/architect/transformation/VelocityTransformation.java
==============================================================================
--- (empty file)
+++ trunk/src/ca/sqlpower/architect/transformation/VelocityTransformation.java Mon Jul 20 08:16:16 2009
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2009, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package ca.sqlpower.architect.transformation;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.util.Properties;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.log4j.Logger;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+
+import ca.sqlpower.architect.swingui.ArchitectSwingSession;
+
+/**
+ *
+ * @author Thomas Kellerer
+ */
+public class VelocityTransformation implements ReportTransformer {
+ private static Logger logger = Logger.getLogger(VelocityTransformation.class);
+
+ public void transform(String builtInTemplate, File result, ArchitectSwingSession session) throws Exception {
+               throw new UnsupportedOperationException("Not supported yet.");
+       }
+       
+       /**
+        * Performs an external XSLT transformation, sending the results
+        * to the OutputStream result.
+        *
+        * @param the XSLT that should be run
+        * @param xml The XML that should be transformed
+ * @param result the output stream where the result of the transformation should be written to
+        */
+ public void transform(File template, File result, ArchitectSwingSession session) throws Exception {
+               Properties props = new Properties();
+               props.put("resource.loader", "file");
+ props.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
+               props.put("file.resource.loader.path", "");
+               props.put("runtime.log.logsystem.log4j.logger", 
logger.getName());
+               
+               VelocityEngine engine = new VelocityEngine(props);
+               
+               Template t = engine.getTemplate(template.getAbsolutePath(), 
"UTF-8");
+               VelocityContext context = new VelocityContext();
+               context.put("tables", session.getPlayPen().getTables());
+               context.put("projectName", session.getName());
+
+               FileOutputStream out = new FileOutputStream(result);
+               OutputStreamWriter writer = new OutputStreamWriter(out, 
"UTF-8");
+               try {
+                       t.merge(context, writer);
+               } catch (Exception e) {
+                       logger.error("Error running Velocity template", e);
+                       throw e;
+               } finally {
+                       IOUtils.closeQuietly(writer);
+               }
+       }
+
+}

Added: trunk/src/ca/sqlpower/architect/transformation/XsltTransformation.java
==============================================================================
--- (empty file)
+++ trunk/src/ca/sqlpower/architect/transformation/XsltTransformation.java Mon Jul 20 08:16:16 2009
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2009, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package ca.sqlpower.architect.transformation;
+
+import ca.sqlpower.architect.swingui.ArchitectSwingSession;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+/**
+ * This class transforms xml content from the InputStream passed, into the
+ * format specified by the xslt stylesheet and sends the results to an
+ * OutputStream.
+ */
+public class XsltTransformation implements ReportTransformer {
+
+       public XsltTransformation() {
+       }
+
+       /**
+ * Performs an XSLT transformation using a built-in stylesheet, sending the results
+        * to the OutputStream result.
+        *
+ * @param builtInXsltName the name of the built-in XSLT (part of the classpath)
+        * @param xml The XML that should be transformed
+ * @param result the output stream where the result of the transformation should be written to
+        */
+ public void transform(String builtInXsltName, File result, ArchitectSwingSession session) throws Exception {
+
+ InputStream xsltStylesheet = getClass().getResourceAsStream(builtInXsltName);
+               transform(xsltStylesheet, result, session);
+       }
+
+       /**
+        * Performs an external XSLT transformation, sending the results
+        * to the OutputStream result.
+        *
+        * @param the XSLT that should be run
+        * @param xml The XML that should be transformed
+ * @param result the output stream where the result of the transformation should be written to
+        */
+ public void transform(File xsltStylesheet, File output, ArchitectSwingSession session) throws Exception {
+
+               InputStream xslt = new FileInputStream(xsltStylesheet);
+               transform(xslt, output, session);
+       }
+
+       /**
+        * Performs an external XSLT transformation, sending the results
+        * to the OutputStream result.
+        *
+        * @param the XSLT that should be run
+        * @param xml The XML that should be transformed
+ * @param result the output stream where the result of the transformation should be written to
+        */
+ public void transform(InputStream xsltStylesheet, File output, ArchitectSwingSession session) throws Exception {
+
+               File project = session.getProject().getFile();
+               InputStream xml = new FileInputStream(project);
+
+               Source xmlSource = new StreamSource(xml);
+               Source xsltSource = new StreamSource(xsltStylesheet);
+               FileOutputStream result = new FileOutputStream(output);
+               
+               TransformerFactory transFact =
+                               TransformerFactory.newInstance();
+               Transformer trans = transFact.newTransformer(xsltSource);
+
+               trans.transform(xmlSource, new StreamResult(result));
+               result.flush();
+               result.close();
+               xsltStylesheet.close();
+               xml.close();
+       }
+}

Reply via email to