1 package org.jbpm.jpdl.convert; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.io.FileOutputStream ; 7 import java.io.FilenameFilter ; 8 import java.io.OutputStream ; 9 import java.util.Iterator ; 10 import java.util.zip.ZipEntry ; 11 import java.util.zip.ZipInputStream ; 12 import java.util.zip.ZipOutputStream ; 13 14 import javax.xml.transform.Transformer ; 15 import javax.xml.transform.TransformerFactory ; 16 import javax.xml.transform.stream.StreamSource ; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.dom4j.Document; 21 import org.dom4j.DocumentException; 22 import org.dom4j.DocumentHelper; 23 import org.dom4j.io.DocumentResult; 24 import org.dom4j.io.DocumentSource; 25 import org.dom4j.io.OutputFormat; 26 import org.dom4j.io.XMLWriter; 27 import org.jbpm.jpdl.par.ProcessArchive; 28 29 public class Converter { 30 31 private static final String STYLESHEET_NAME = "convert-pdl-2.0-to-3.0.xslt"; 32 33 private File indir; 34 private File outdir; 35 36 public Document convert(Document document) 37 throws Exception { 38 39 TransformerFactory factory = TransformerFactory.newInstance(); 41 Transformer transformer = factory.newTransformer( 42 new StreamSource ( this.getClass().getResourceAsStream( STYLESHEET_NAME ) ) ); 43 44 DocumentSource source = new DocumentSource( document ); 46 DocumentResult result = new DocumentResult(); 47 transformer.transform( source, result ); 48 49 51 return result.getDocument(); 52 } 53 54 public String convertPar(ProcessArchive pa) { 55 56 try 57 { 58 Document doc = 60 DocumentHelper.parseText( 61 new String ( pa.getEntry("processdefinition.xml") ) ); 62 63 Document doc30 = convert(doc); 65 66 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 68 serializetoXML( bos, doc30) ; 69 70 return bos.toString(); 71 72 } 73 catch(DocumentException de) 74 { 75 log.error("Conversion had trouble parsing the 2.0 process definition", de); 76 } 77 catch(Exception ex) 78 { 79 log.error("Unexpected error in conversion", ex); 80 } 81 82 return null; } 84 85 public void serializetoXML(OutputStream out, Document document) 86 throws Exception { 87 88 OutputFormat outformat = OutputFormat.createPrettyPrint(); 89 XMLWriter writer = new XMLWriter(out, outformat); 91 writer.write( document ); 92 writer.flush(); 93 } 94 95 public static void main(String [] args) 96 throws Exception 97 { 98 99 Converter converter = new Converter(); 100 101 if (!converter.parse(args)){ 102 System.err.println(); 103 System.err.println("Usage: java -jar converter.jar input-directory output-directory\n\n" + 104 "input-directory is the directory where you have 2.0 process archives (*.par)\n" + 105 "The converted par files will be placed in the output-directory"); 106 System.exit(1); 107 } 108 109 110 converter.convertPars(); 111 112 } 113 114 boolean parse(String [] args){ 115 116 if (args.length != 2) 117 return false; 118 119 indir = new File ( args[0] ); 121 if (!indir.isDirectory()) 122 { 123 System.err.println("Input file " + args[0] + " is not a valid directory name."); 124 return false; 125 } 126 127 outdir = new File ( args[1] ); 128 if (!outdir.isDirectory()) 129 { 130 System.err.println("Output file " + args[1] + " is not a valid directory name."); 131 return false; 132 } 133 134 return true; 135 } 136 137 void convertPars() throws Exception { 138 139 String [] files = indir.list( new FilenameFilter () { 140 public boolean accept(File dir, String name){ 141 return name.toLowerCase().endsWith(".par"); 142 } 143 }); 144 145 for (int i = 0; i < files.length; i++) 146 { 147 ZipInputStream zip = new ZipInputStream ( 148 new FileInputStream ( 149 indir.getPath() + "/" + files[i])); 150 151 ProcessArchive pa = new ProcessArchive(zip); 152 153 String xml = convertPar( pa ); 154 155 ZipOutputStream zippo = new ZipOutputStream ( 157 new FileOutputStream ( 158 outdir.getPath() + "/" + files[i] )); 159 160 162 for (Iterator iter = pa.getEntries().keySet().iterator(); iter.hasNext(); ) 163 { 164 String name = (String ) iter.next(); 165 166 zippo.putNextEntry( new ZipEntry ( name ) ); 167 if ("processdefinition.xml".equalsIgnoreCase(name)) 168 { 169 zippo.write( xml.getBytes( ) ); 170 } 171 else 172 { 173 zippo.write( pa.getEntry(name) ); 174 } 175 176 zippo.closeEntry(); 177 } 178 179 zippo.close(); 180 181 System.out.println("Converted " + files[i]); 182 183 } } 185 186 private static final Log log = LogFactory.getLog(Converter.class); 189 } 190 | Popular Tags |