001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program 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: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: Mdl2Fsm.java 6617 2007-04-11 01:04:44Z zjin $
024: */
025:
026: package com.bostechcorp.cbesb.common.mdl.fsmparser;
027:
028: import java.io.File;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.tools.ant.Project;
033: import org.apache.tools.ant.ProjectHelper;
034:
035: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
036: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
037: import com.bostechcorp.cbesb.common.mdl.util.FormatDefUtil;
038:
039: public class Mdl2Fsm {
040:
041: protected static transient Log logger = LogFactory
042: .getLog(Mdl2Fsm.class);
043:
044: // private static Configuration config;
045: // static Configuration config = ConfigFactory.instance().getConfiguration();
046:
047: public Mdl2Fsm() {
048: super ();
049: }
050:
051: /**
052: * To Finite State Machine - Load from MDL to generate Finite State Machine
053: * parser
054: *
055: * @param mdlFile
056: * File of MDL need to load for Finite State Machine
057: * @param packageName
058: * @param jarFileName
059: * @param outputDir
060: * @param targetJarDir
061: * @param optionalClassPath
062: */
063: public static void mdl2JJ(File mdlFile, String packageName,
064: String outputDir, boolean useDefaultMdlResolver) {
065:
066: IMDLDocument mdlDoc = FormatDefUtil.getMdlDocFromPath(mdlFile
067: .getAbsolutePath(), useDefaultMdlResolver);
068:
069: if (mdlDoc == null)
070: return;
071:
072: for (int i = 0; i < mdlDoc.getAllMessageDefinitions().length; i++) {
073:
074: IElementDefinition msgDef = mdlDoc.getMessageDefinition(
075: mdlDoc.getTargetNamespace(), mdlDoc
076: .getAllMessageDefinitions()[i].getName());
077:
078: // Generate JJ File
079: VariableParserGenerator generator = new VariableParserGenerator(
080: packageName + "." + msgDef.getName(), null,
081: outputDir, null, null);
082:
083: generator.generateParser(msgDef);
084:
085: // System.out.println("######Finish generate jj file######");
086: }
087: }
088:
089: /**
090: * To Finite State Machine - Load from MDL to generate Finite State Machine
091: * parser
092: *
093: * @param mdlFile
094: * File of MDL need to load for Finite State Machine
095: * @param packageName
096: * @param jarFileName
097: * @param outputDir
098: * @param targetJarDir
099: * @param optionalClassPath
100: */
101: public static void toFsm(File mdlFile, String packageName,
102: String jarFileName, String outputDir, String targetJarDir,
103: String optionalClassPath) {
104: IMDLDocument mdlDoc = FormatDefUtil.getMdlDocFromPath(mdlFile
105: .getAbsolutePath());
106:
107: IElementDefinition msgDef = null;
108:
109: for (int i = 0; i < mdlDoc.getAllMessageDefinitions().length; i++) {
110: msgDef = mdlDoc.getMessageDefinition(mdlDoc
111: .getTargetNamespace(), mdlDoc
112: .getAllMessageDefinitions()[i].getName());
113:
114: toFsm(msgDef, packageName, jarFileName, outputDir,
115: targetJarDir, optionalClassPath);
116: }
117:
118: }
119:
120: /**
121: * To Finite State Machine - Load from MDL to generate Finite State Machine
122: * parser
123: *
124: * @param elementDefinition
125: * Element definition need to parse to fsm parser
126: * @param packageName
127: * @param jarFileName
128: * @param outputDir
129: * @param targetJarDir
130: * @param optionalClassPath
131: */
132: public static void toFsm(IElementDefinition elementDefinition,
133: String packageName, String jarFileName, String outputDir,
134: String targetJarDir, String optionalClassPath) {
135:
136: // Generate JJ File
137: VariableParserGenerator generator = new VariableParserGenerator(
138: packageName, jarFileName, outputDir, targetJarDir,
139: optionalClassPath);
140: generator.generateParser(elementDefinition);
141:
142: // build build.xml
143: generator.generateAntScript(elementDefinition.getName());
144:
145: // System.out.println("######Finish generate xml ant script file######");
146:
147: // invoke ant
148: try {
149: Project ant = new Project();
150: ant.init();
151:
152: ProjectHelper.configureProject(ant, new File(outputDir
153: + File.separator + elementDefinition.getName()
154: + ".xml"));
155:
156: if (jarFileName == null && targetJarDir == null)
157: ant.executeTarget("compileOnly");
158: else
159: ant.executeTarget("compile");
160: } catch (Exception e) {
161: logger.error("Exception in toFsm(): " + e.getMessage());
162: if (logger.isDebugEnabled()) {
163: logger.debug("Exception in toFsm():", e);
164: }
165: }
166: }
167:
168: public static void main(String args[]) {
169: try {
170:
171: Mdl2Fsm.mdl2JJ(new File(args[0]), args[1], args[2], Boolean
172: .parseBoolean(args[3]));
173:
174: } catch (Exception e) {
175: e.printStackTrace();
176: System.exit(1);
177: }
178:
179: }
180:
181: }
|