001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.jpdl.convert;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.FilenameFilter;
029: import java.io.OutputStream;
030: import java.util.Iterator;
031: import java.util.zip.ZipEntry;
032: import java.util.zip.ZipInputStream;
033: import java.util.zip.ZipOutputStream;
034:
035: import javax.xml.transform.Transformer;
036: import javax.xml.transform.TransformerFactory;
037: import javax.xml.transform.stream.StreamSource;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.dom4j.Document;
042: import org.dom4j.DocumentException;
043: import org.dom4j.DocumentHelper;
044: import org.dom4j.io.DocumentResult;
045: import org.dom4j.io.DocumentSource;
046: import org.dom4j.io.OutputFormat;
047: import org.dom4j.io.XMLWriter;
048: import org.jbpm.jpdl.par.ProcessArchive;
049:
050: public class Converter {
051:
052: private static final String STYLESHEET_NAME = "convert-pdl-2.0-to-3.0.xslt";
053:
054: File indir;
055: File outdir;
056:
057: public Document convert(Document document) throws Exception {
058:
059: // load the transformer using JAXP
060: TransformerFactory factory = TransformerFactory.newInstance();
061: Transformer transformer = factory
062: .newTransformer(new StreamSource(this .getClass()
063: .getResourceAsStream(STYLESHEET_NAME)));
064:
065: // apply the conversion stylesheet to the incoming process definition
066: DocumentSource source = new DocumentSource(document);
067: DocumentResult result = new DocumentResult();
068: transformer.transform(source, result);
069:
070: // return the transformed document
071:
072: return result.getDocument();
073: }
074:
075: public String convertPar(ProcessArchive pa) {
076:
077: try {
078: // Parse the process definition XML into a DOM document
079: Document doc = DocumentHelper.parseText(new String(pa
080: .getEntry("processdefinition.xml")));
081:
082: // Convert from 2.0 to 3.0 PDL
083: Document doc30 = convert(doc);
084:
085: // Serialize the resulting document as the result
086: ByteArrayOutputStream bos = new ByteArrayOutputStream();
087: serializetoXML(bos, doc30);
088:
089: return bos.toString();
090:
091: } catch (DocumentException de) {
092: log
093: .error(
094: "Conversion had trouble parsing the 2.0 process definition",
095: de);
096: } catch (Exception ex) {
097: log.error("Unexpected error in conversion", ex);
098: }
099:
100: return null; // things did not go well
101: }
102:
103: public void serializetoXML(OutputStream out, Document document)
104: throws Exception {
105:
106: OutputFormat outformat = OutputFormat.createPrettyPrint();
107: //outformat.setEncoding(aEncodingScheme);
108: XMLWriter writer = new XMLWriter(out, outformat);
109: writer.write(document);
110: writer.flush();
111: }
112:
113: public static void main(String[] args) throws Exception {
114:
115: Converter converter = new Converter();
116:
117: if (!converter.parse(args)) {
118: System.err.println();
119: System.err
120: .println("Usage: java -jar converter.jar input-directory output-directory\n\n"
121: + "input-directory is the directory where you have 2.0 process archives (*.par)\n"
122: + "The converted process files will be placed in the output-directory");
123: System.exit(1);
124: }
125:
126: converter.convertPars();
127:
128: }
129:
130: boolean parse(String[] args) {
131:
132: if (args.length != 2)
133: return false;
134:
135: // Check for valid input and output directories
136: indir = new File(args[0]);
137: if (!indir.isDirectory()) {
138: System.err.println("Input file " + args[0]
139: + " is not a valid directory name.");
140: return false;
141: }
142:
143: outdir = new File(args[1]);
144: if (!outdir.isDirectory()) {
145: System.err.println("Output file " + args[1]
146: + " is not a valid directory name.");
147: return false;
148: }
149:
150: return true;
151: }
152:
153: void convertPars() throws Exception {
154:
155: String[] files = indir.list(new FilenameFilter() {
156: public boolean accept(File dir, String name) {
157: return name.toLowerCase().endsWith(".par");
158: }
159: });
160:
161: for (int i = 0; i < files.length; i++) {
162: ZipInputStream zip = new ZipInputStream(
163: new FileInputStream(indir.getPath() + "/"
164: + files[i]));
165:
166: ProcessArchive pa = new ProcessArchive(zip);
167:
168: String xml = convertPar(pa);
169:
170: // Create new process archive in designated output directory
171: ZipOutputStream zippo = new ZipOutputStream(
172: new FileOutputStream(outdir.getPath() + "/"
173: + files[i]));
174:
175: // Copy all non-pdl entries and insert new pdl
176:
177: for (Iterator iter = pa.getEntries().keySet().iterator(); iter
178: .hasNext();) {
179: String name = (String) iter.next();
180:
181: zippo.putNextEntry(new ZipEntry(name));
182: if ("processdefinition.xml".equalsIgnoreCase(name)) {
183: zippo.write(xml.getBytes());
184: } else {
185: zippo.write(pa.getEntry(name));
186: }
187:
188: zippo.closeEntry();
189: }
190:
191: zippo.close();
192:
193: System.out.println("Converted " + files[i]);
194:
195: } // process next PAR
196: }
197:
198: private static final Log log = LogFactory.getLog(Converter.class);
199: }
|