田源
2025-01-16 a13255b4129ee8a7a7b7e1ecd8e02dd2c78f7c17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.jpdl.internal.convert;
 
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
 
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.jbpm.jpdl.internal.convert.exception.IllegalCommandException;
import org.jbpm.jpdl.internal.convert.problem.Problem;
 
/**
 * Conversion command line tool main class.
 * 
 * @author Jim Ma
 */
public class JpdlConverterTool {   
  
    public static void main(String[] args) {
        JpdlConverterTool jpdlConverterTool = new JpdlConverterTool();
      
        // Parse and validate the command line arguments
        ConverterContext context = null;
        try {
            context = jpdlConverterTool.parseParam(args);
            jpdlConverterTool.validate(context);
        } catch (IllegalCommandException e) {
            System.err.println(e.getMessage());
            System.err.println(jpdlConverterTool.getUsage());
            return;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            return;
        }
        
        
        boolean verbose = false;
        if (context.get(ConverterContext.VERBOSE) != null) {
            verbose = true;
        }
        Jpdl3Converter jpdlConverter = new Jpdl3Converter((URL)context.get(context.PROCESS_FILE_URL));
        
        try {
            if (verbose) {
                System.out.println("Loading process file from URL [" + context.get(context.PROCESS_FILE_URL) + "]...");
            }
            Document jpdl4Doc = jpdlConverter.readAndConvert();
            
            if (verbose) {
                System.out.println("Converting the process file to jPDL4 version....");
            }            
            
            String outputFilePath = (String)context.get(context.OUPUTFILE);
            File outputFile = new File(outputFilePath);
            
            //well print xml to file
            Writer fileWriter = new FileWriter(outputFile);
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter( fileWriter, format );
            writer.write(jpdl4Doc);
            writer.close();
 
            
            if (verbose) {
                System.out.println("Generating converted file to:" + (outputFile.isAbsolute() ? outputFile.getAbsolutePath() : outputFile.getName()));
            }
            
        } catch (Exception e) {
 
          for (Problem problem : jpdlConverter.problems) {
                if (problem.getLevel() == problem.LEVEL_WARNING) {
                    System.err.println(problem);
                }
                if (problem.getLevel() < problem.LEVEL_WARNING) {
                    System.err.println(problem);
                    if (problem.getException() != null
                            && context.get(ConverterContext.VERBOSE) != null) {
                        problem.getException().printStackTrace(System.err);
                        System.err.println();
                    }
                }
            }
            
            
        }
    }
 
    /**
     * Parses the command line arguments and returns a context 
     * in which the conversion will happen.
     */
    public ConverterContext parseParam(String[] args) throws IllegalCommandException {
        ConverterContext context = new ConverterContext();
        
        if (args.length == 0) {
            throw new IllegalCommandException("No file for conversion is supplied");
        }
        
        for (int i = 0; i < args.length; i++) {
            String token = args[i];
            if ("-v".equalsIgnoreCase(token) || "-verbose".equalsIgnoreCase(token)) {
                context.put(ConverterContext.VERBOSE, ConverterContext.VERBOSE);
            } else if ("-o".equalsIgnoreCase(token)
                    || "-output".equalsIgnoreCase(token)) {
                int j = ++i;
                if (j < args.length) {
                    String outputFile = args[j];                    
                    context.put(ConverterContext.OUPUTFILE, args[j]);
                } else {
                    throw new IllegalCommandException("Missing output file name");
                }
            } else if (token.startsWith("-")) {
                throw new IllegalCommandException("Unknow flag [" + token +"]");
            } else if(context.get(ConverterContext.PROCESS_FILE) == null) {
                context.put(ConverterContext.PROCESS_FILE, token);
            } else {
                throw new IllegalCommandException("Duplicate input process file");
            }
 
        }
        if (context.get(ConverterContext.PROCESS_FILE) == null) {
            throw new IllegalCommandException("No input process file defined");
        }
 
        return context;
    }
 
    public void validate(ConverterContext context) throws Exception {
        String processFile = (String) context.get(ConverterContext.PROCESS_FILE);
        URL processURL = getFileURL(processFile);
        if (processURL == null) {
            throw new IllegalCommandException("Failed to load the process file [" + processFile + "]");
        }
        context.put(context.PROCESS_FILE_URL, processURL);
        //handle the output filename
        if (context.get(ConverterContext.OUPUTFILE) == null) {
            File tmpFile = new File(processURL.getFile());
            String fileName = tmpFile.getName();
            String baseName = fileName;
            
            int index = fileName.lastIndexOf(".");
            
            if (index > -1) {
                baseName = fileName.substring(0, index);
            } else {
                baseName = fileName;
            }
            
            String outputFileName =  baseName + ".converted.jpdl.xml";
            File outFile = new File(".", outputFileName);
            context.put(ConverterContext.OUPUTFILE, outFile.getAbsolutePath());
        } else {
            String outputFile = (String)context.get(ConverterContext.OUPUTFILE);
            File file = new File(outputFile);
            if (file.isAbsolute()) {
                if (!file.getParentFile().exists()) {
                    throw new IllegalCommandException("Output directory [" + file.getParent() + "] does not exist");
                }
            } else {
                file = new File(".", outputFile);                
            }
            if (!file.exists()) {
                    file.createNewFile();                
            }
            context.put(ConverterContext.OUPUTFILE, file.getAbsolutePath());
        }
        
    }
 
    public URL getFileURL(String fileName) {
        try {
            URL url = new URL(fileName);
            return url;
        } catch (MalformedURLException e1) {
            //Do nothing
        }
        File file = new File(fileName);
        if (file.exists()) {
            try {
                return file.toURL();
            } catch (MalformedURLException e) {
                // Do nothing.
            }
            
        }
        // load in current directory
        File tmpFile = new File(".", fileName);
        if (file.exists()) {
            try {
                return tmpFile.toURL();
            } catch (MalformedURLException e) {
                // Do nothing
            }
        }
        
        return null;
    }
    
    public String getUsage() {
        return "Usage : java org.jbpm.jpdl.internal.convert.JpdlConverterTool <file>\r\n"
         +     "        java org.jbpm.jpdl.internal.convert.JpdlConverterTool -v -o <outputfile> <file>";
        
    }
    
}