KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > convert > Converter


1 package org.jbpm.jpdl.convert;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.FilenameFilter JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.zip.ZipEntry JavaDoc;
11 import java.util.zip.ZipInputStream JavaDoc;
12 import java.util.zip.ZipOutputStream JavaDoc;
13
14 import javax.xml.transform.Transformer JavaDoc;
15 import javax.xml.transform.TransformerFactory JavaDoc;
16 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc STYLESHEET_NAME = "convert-pdl-2.0-to-3.0.xslt";
32   
33   private File JavaDoc indir;
34   private File JavaDoc outdir;
35   
36   public Document convert(Document document)
37        throws Exception JavaDoc {
38
39     // load the transformer using JAXP
40
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
41     Transformer JavaDoc transformer = factory.newTransformer(
42         new StreamSource JavaDoc( this.getClass().getResourceAsStream( STYLESHEET_NAME ) ) );
43
44     // apply the conversion stylesheet to the incoming process definition
45
DocumentSource source = new DocumentSource( document );
46     DocumentResult result = new DocumentResult();
47     transformer.transform( source, result );
48
49     // return the transformed document
50

51     return result.getDocument();
52   }
53   
54   public String JavaDoc convertPar(ProcessArchive pa) {
55       
56       try
57       {
58         // Parse the process definition XML into a DOM document
59
Document doc =
60           DocumentHelper.parseText(
61             new String JavaDoc( pa.getEntry("processdefinition.xml") ) );
62         
63         // Convert from 2.0 to 3.0 PDL
64
Document doc30 = convert(doc);
65         
66         // Serialize the resulting document as the result
67
ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc ex)
78       {
79           log.error("Unexpected error in conversion", ex);
80       }
81       
82       return null; // things did not go well
83
}
84   
85   public void serializetoXML(OutputStream JavaDoc out, Document document)
86        throws Exception JavaDoc {
87       
88      OutputFormat outformat = OutputFormat.createPrettyPrint();
89      //outformat.setEncoding(aEncodingScheme);
90
XMLWriter writer = new XMLWriter(out, outformat);
91      writer.write( document );
92      writer.flush();
93   }
94   
95   public static void main(String JavaDoc[] args)
96             throws Exception JavaDoc
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 JavaDoc[] args){
115     
116     if (args.length != 2)
117       return false;
118     
119     // Check for valid input and output directories
120
indir = new File JavaDoc( 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 JavaDoc( 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 JavaDoc {
138     
139     String JavaDoc[] files = indir.list( new FilenameFilter JavaDoc() {
140                                     public boolean accept(File JavaDoc dir, String JavaDoc name){
141                                         return name.toLowerCase().endsWith(".par");
142                                     }
143                                   });
144
145     for (int i = 0; i < files.length; i++)
146     {
147         ZipInputStream JavaDoc zip = new ZipInputStream JavaDoc(
148                                 new FileInputStream JavaDoc(
149                                         indir.getPath() + "/" + files[i]));
150                                     
151         ProcessArchive pa = new ProcessArchive(zip);
152         
153         String JavaDoc xml = convertPar( pa );
154         
155         // Create new process archive in designated output directory
156
ZipOutputStream JavaDoc zippo = new ZipOutputStream JavaDoc(
157                                   new FileOutputStream JavaDoc(
158                                             outdir.getPath() + "/" + files[i] ));
159         
160         // Copy all non-pdl entries and insert new pdl
161

162         for (Iterator JavaDoc iter = pa.getEntries().keySet().iterator(); iter.hasNext(); )
163         {
164           String JavaDoc name = (String JavaDoc) iter.next();
165           
166           zippo.putNextEntry( new ZipEntry JavaDoc( 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       } // process next PAR
184
}
185   
186   //logger
187
/////////////////////////////////////////////////////////////////////////////
188
private static final Log log = LogFactory.getLog(Converter.class);
189 }
190
Popular Tags