0001: /*
0002: * ChainBuilder ESB
0003: * Visual Enterprise Integration
0004: *
0005: * Copyright (C) 2006 Bostech Corporation
0006: *
0007: * This program is free software; you can redistribute it and/or modify it
0008: * under the terms of the GNU General Public License as published by the
0009: * Free Software Foundation; either version 2 of the License, or (at your option)
0010: * any later version.
0011: *
0012: * This program is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
0014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0015: * for more details.
0016: *
0017: * You should have received a copy of the GNU General Public License along with
0018: * this program; if not, write to the Free Software Foundation, Inc.,
0019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0020: *
0021: *
0022: * $Id: TransformerCompiler.java 12236 2008-03-06 03:23:18Z lzheng $
0023: */
0024: package com.bostechcorp.cbesb.common.trn.compiler;
0025:
0026: import java.io.File;
0027: import java.util.HashMap;
0028: import java.util.HashSet;
0029: import java.util.Iterator;
0030: import java.util.Set;
0031: import java.util.Vector;
0032:
0033: import org.apache.commons.logging.Log;
0034: import org.apache.commons.logging.LogFactory;
0035: import org.w3c.dom.Document;
0036: import org.w3c.dom.NamedNodeMap;
0037: import org.w3c.dom.Node;
0038:
0039: import com.bostechcorp.cbesb.common.i18n.CoreMessageConstants;
0040: import com.bostechcorp.cbesb.common.i18n.Message;
0041: import com.bostechcorp.cbesb.common.util.Dom;
0042: import com.bostechcorp.cbesb.common.util.TabbingPrintWriter;
0043: import com.bostechcorp.cbesb.common.util.macro.MacroUtil;
0044: import com.bostechcorp.cbesb.common.util.project.ProjectRuntimeUtil;
0045:
0046: public class TransformerCompiler {
0047:
0048: private static final String PACKAGE_NAME = "com.bostechcorp.cbesb.runtime.transformer.engine";
0049: // private static final String TRANSFORMER_CONTROL_SCHEMA = "src/resources/transformControl.xsd";
0050: // private static final String TRANSFORMER_CONTROL_SCHEMA = "transformControl.xsd";
0051:
0052: private static final int MAX_NUMBER = 1000;
0053: private String inFormatFileName;
0054: private String outFormatFileName;
0055: private String inFormat;
0056: private String outFormat;
0057: public String outputRootNode;
0058: public String inputRootNode;
0059: private String packageName = "";
0060:
0061: /*
0062: * List of the addresses used.
0063: */
0064: private int addressCount = 0;
0065: private Vector<DataAddress> dataAddresses = new Vector<DataAddress>();
0066:
0067: /*
0068: * List of contexts and user variables. The contexts contain the context name and parent
0069: * name. The variables only have a name.
0070: */
0071: public HashMap<String, String> contextList = new HashMap<String, String>();
0072: private HashSet<String> userVariableList = new HashSet<String>();
0073:
0074: /*
0075: * Keeps track of the number of class operations used.
0076: */
0077: int classCount = 0;
0078:
0079: int classInstanceMethodCount = 0;
0080:
0081: boolean ifSuppress = false;
0082:
0083: int contextCount = 0;
0084: int contextinitializeMethodCount = 0;
0085:
0086: int operIndex = 0;
0087: int operCount = 0;
0088: int classIndex = 0;
0089: /*
0090: * parent node for all operations
0091: */
0092: IOperation operationsParent = OperationFactory.createOper("root",
0093: "root");
0094:
0095: /*
0096: * files and miscellaneous variables
0097: */
0098: private static final Log log = LogFactory
0099: .getLog(TransformerCompiler.class);
0100: Document controlDoc;
0101: TabbingPrintWriter javaWriter;
0102: String transformerClassName;
0103: private String projName;
0104:
0105: private String getVariableName(String var) {
0106: String varName = "";
0107: int i = var.indexOf("(");
0108: if (i >= 0) {
0109: varName = var.substring(i + 1, var.indexOf(")"));
0110: return varName;
0111: } else
0112: return var;
0113: }
0114:
0115: /*
0116: * constructor
0117: */
0118:
0119: public TransformerCompiler(File controlFile, File javaFile)
0120: throws Exception {
0121: this (controlFile, javaFile, "");
0122: }
0123:
0124: public TransformerCompiler(File controlFile, File javaFile,
0125: String packageName) throws Exception {
0126: // controlDoc = Dom.getDomTree(controlFile, new File(TRANSFORMER_CONTROL_SCHEMA));
0127: // File schmaFile=new File(new URI(getClass().getClassLoader().getResource(TRANSFORMER_CONTROL_SCHEMA).toString()));
0128: // controlDoc = Dom.getDomTree(controlFile,schmaFile );
0129:
0130: controlDoc = Dom.getDomTree(controlFile, null);
0131: transformerClassName = javaFile.getName();
0132: transformerClassName = transformerClassName.substring(0,
0133: transformerClassName.indexOf('.'));
0134: this .packageName = packageName;
0135: javaWriter = new TabbingPrintWriter(javaFile);
0136: this .projName = ProjectRuntimeUtil
0137: .getProjectNameFromFilePath(controlFile
0138: .getAbsolutePath());
0139:
0140: }
0141:
0142: /*
0143: * Generate the Java file.
0144: */
0145: public void generateJavaCode() throws Exception {
0146: processTopLevel(controlDoc.getFirstChild());
0147: generate();
0148: javaWriter.close();
0149: }
0150:
0151: /*
0152: * Process the top level of the control file. We are expecting transformerControl with formats and
0153: * operationList contained in it. Ignore everything else.
0154: */
0155: void processTopLevel(Node n) throws Exception {
0156: for (; n != null; n = n.getNextSibling()) {
0157: if (n.getNodeType() == Node.ELEMENT_NODE) {
0158: String tagName = n.getNodeName();
0159: if (tagName.endsWith(":formats")) {
0160: //log.info("found formats");
0161: processFormats(n.getFirstChild());
0162: } else if (tagName.endsWith(":operationList")) {
0163: //log.info("found operationList");
0164: processOperations(operationsParent, n
0165: .getFirstChild());
0166: } else if (tagName.endsWith(":transformerControl")) {
0167: //log.info(("found transformerControl"));
0168: n = n.getFirstChild();
0169: } else {
0170: //log.info("skipping unknown top level element: name="+n.getNodeName());
0171: }
0172: } else {
0173: //log.info("skipping unknown top level content: type="+n.getNodeType()+" name="+n.getNodeName());
0174: }
0175: }
0176: }
0177:
0178: /*
0179: * Process the formats tag
0180: */
0181: private void processFormats(Node n) throws Exception {
0182: for (; n != null; n = n.getNextSibling()) {
0183: if (n.getNodeType() == Node.ELEMENT_NODE) {
0184: String tag = n.getNodeName();
0185: NamedNodeMap attrs = n.getAttributes();
0186: Node attrNode = attrs.getNamedItem("name");
0187: if (attrNode == null)
0188: throw new Exception(new Message(
0189: CoreMessageConstants.MISSING_ATTRIBUTE,
0190: "name", "formats").getMessage());
0191: String name = attrNode.getNodeValue();
0192: Node formatNode = attrs.getNamedItem("format");
0193: if (formatNode == null)
0194: throw new Exception(new Message(
0195: CoreMessageConstants.MISSING_ATTRIBUTE,
0196: "format", "formats").getMessage());
0197: String format = formatNode.getNodeValue();
0198: if (tag.endsWith(":input")) {
0199: inFormatFileName = MacroUtil.resolveBuiltinMacro(
0200: name, projName);
0201: inFormat = format;
0202: log.debug("input format: " + inFormat);
0203: attrNode = attrs.getNamedItem("root");
0204: if (attrNode != null)
0205: inputRootNode = attrNode.getNodeValue();
0206:
0207: } else if (tag.endsWith(":output")) {
0208:
0209: outFormatFileName = MacroUtil.resolveBuiltinMacro(
0210: name, projName);
0211: outFormat = format;
0212: log.debug("Output format: " + outFormat);
0213: attrNode = attrs.getNamedItem("root");
0214: if (attrNode != null)
0215: outputRootNode = attrNode.getNodeValue();
0216: } else
0217: throw new Exception(
0218: new Message(
0219: CoreMessageConstants.UNKNOWN_OBJECT_ATTRIBUTE,
0220: "formats tag", tag).getMessage());
0221: }
0222: }
0223: }
0224:
0225: /*
0226: * Process the contents of an operationList
0227: */
0228: private void processOperations(IOperation parent, Node n)
0229: throws Exception {
0230: for (; n != null; n = n.getNextSibling()) {
0231: if (n.getNodeType() == Node.ELEMENT_NODE
0232: && n.getNodeName().endsWith(":operation")) {
0233: processOperation(parent, n);
0234: } else if (n.getNodeType() == Node.COMMENT_NODE) {
0235: processComment(parent, n);
0236: } else {
0237: //log.info("skipping unknown operation content "+n.getNodeType()+" name="+n.getNodeName()+" val="+n.getNodeValue());
0238: }
0239: }
0240: }
0241:
0242: /*
0243: * Process a comment (do nothing for now)
0244: */
0245: void processComment(IOperation parent, Node n) {
0246: // System.out.println("comment="+n.getNodeValue());
0247: }
0248:
0249: /*
0250: * Process one operation. Node is an operation element.
0251: */
0252: void processOperation(IOperation parent, Node n) throws Exception {
0253: Vector<DataAddress> sources = new Vector<DataAddress>();
0254: Vector<DataAddress> targets = new Vector<DataAddress>();
0255: NamedNodeMap attrs;
0256: Node attrNode;
0257:
0258: // create an operation and link it to the tree
0259: IOperation op = null;
0260:
0261: // get the attributes
0262: attrs = n.getAttributes();
0263:
0264: // get operation type and name
0265: attrNode = attrs.getNamedItem("type");
0266: String strOpType;
0267: if (attrNode != null) {
0268: strOpType = attrNode.getNodeValue();
0269:
0270: } else
0271: throw new Exception(new Message(
0272: CoreMessageConstants.MISSING_ATTRIBUTE, "type",
0273: "operation").getMessage());
0274:
0275: String opName = "";
0276: attrNode = attrs.getNamedItem("name");
0277: if (attrNode != null) {
0278: opName = attrNode.getNodeValue();
0279: }
0280:
0281: Node attrClassNode = attrs.getNamedItem("class");
0282:
0283: if (attrClassNode != null) {
0284: opName = attrClassNode.getNodeValue();
0285: }
0286: if (attrNode == null && attrClassNode == null)
0287: throw new Exception(new Message(
0288: CoreMessageConstants.MISSING_ATTRIBUTE, "name",
0289: "operation").getMessage());
0290:
0291: op = OperationFactory.createOper(strOpType, opName);
0292:
0293: if (op == null) {
0294: throw new Exception(new Message(
0295: CoreMessageConstants.UNKNOWN_OPERATION, "built-in",
0296: opName).getMessage());
0297:
0298: }
0299:
0300: if (parent.getNestedOperations() == null) {
0301: parent.setNestedOperations(op);
0302: parent.setLastNested(op);
0303: parent.setChildCount(parent.getChildCount() + 1);
0304: } else {
0305: parent.getLastNested().setNextOperation(op);
0306: parent.setLastNested(op);
0307: parent.setChildCount(parent.getChildCount() + 1);
0308:
0309: }
0310:
0311: if (op instanceof OperClass) {
0312: ((OperClass) op).setClassIndex(classCount++);
0313: }
0314: //need to think about user class
0315:
0316: // loop through children (parameters and nested operationList)
0317: for (Node child = n.getFirstChild(); child != null; child = child
0318: .getNextSibling()) {
0319: if (child.getNodeType() == Node.ELEMENT_NODE
0320: && child.getNodeName().endsWith(":parameters")) {
0321: /*
0322: * process the parameters
0323: */
0324: for (Node paramNode = child.getFirstChild(); paramNode != null; paramNode = paramNode
0325: .getNextSibling()) {
0326: if (paramNode.getNodeType() == Node.ELEMENT_NODE
0327: && paramNode.getNodeName().endsWith(
0328: ":property")) {
0329: NamedNodeMap propertyAttributes = paramNode
0330: .getAttributes();
0331: if (propertyAttributes.getNamedItem("name") != null
0332: && propertyAttributes
0333: .getNamedItem("value") != null) {
0334: String propertyName = propertyAttributes
0335: .getNamedItem("name")
0336: .getNodeValue();
0337: String propertyValue = propertyAttributes
0338: .getNamedItem("value")
0339: .getNodeValue();
0340: op.handleProperty(propertyName,
0341: propertyValue);
0342: }
0343:
0344: } else if (paramNode.getNodeType() == Node.ELEMENT_NODE
0345: && (paramNode.getNodeName().endsWith(
0346: ":propertylist") || paramNode
0347: .getNodeName().endsWith(
0348: ":propertyList"))) {
0349: NamedNodeMap plAttributes = paramNode
0350: .getAttributes();
0351: String plName = plAttributes.getNamedItem(
0352: "name").getNodeValue();
0353: /*
0354: * Get property lists
0355: */
0356: for (Node plNode = paramNode.getFirstChild(); plNode != null; plNode = plNode
0357: .getNextSibling()) {
0358: if (plNode.getNodeType() == Node.ELEMENT_NODE
0359: && plNode.getNodeName().endsWith(
0360: ":setting")) {
0361: NamedNodeMap propertyAttributes = plNode
0362: .getAttributes();
0363:
0364: if (propertyAttributes
0365: .getNamedItem("name") != null
0366: && propertyAttributes
0367: .getNamedItem("value") != null) {
0368:
0369: String propertyName = propertyAttributes
0370: .getNamedItem("name")
0371: .getNodeValue();
0372: String propertyValue = propertyAttributes
0373: .getNamedItem("value")
0374: .getNodeValue();
0375: op
0376: .handlePropertyList(plName,
0377: propertyName,
0378: propertyValue);
0379: }
0380: }
0381: }
0382: /*
0383: * Get source and target parameters
0384: */
0385: } else if (paramNode.getNodeType() == Node.ELEMENT_NODE
0386: && paramNode.getNodeName().endsWith(
0387: ":source")) {
0388: sources.add(getDataAddress(paramNode));
0389: } else if (paramNode.getNodeType() == Node.ELEMENT_NODE
0390: && paramNode.getNodeName().endsWith(
0391: ":target")) {
0392: targets.add(getDataAddress(paramNode));
0393: }
0394: }
0395: op.setSources(sources.toArray(new DataAddress[0]));
0396: op.setTargets(targets.toArray(new DataAddress[0]));
0397:
0398: if (op instanceof OperIterate) {
0399: String context = ((OperIterate) op).getContext();
0400: if (((OperIterate) op).getContext() != null)
0401: registerContext(op, context);
0402: }
0403:
0404: } else if (child.getNodeType() == Node.ELEMENT_NODE
0405: && child.getNodeName().endsWith(":operationList")) {
0406: /*
0407: * process nested operations
0408: */
0409: processOperations(op, child.getFirstChild());
0410: } else {
0411: //log.info("skipping unknown operation content type="+child.getNodeType()+" text="+child.getTextContent());
0412: }
0413: }
0414: }
0415:
0416: /*
0417: * Determine the context parent (if any) and save context in the list.
0418: */
0419: private void registerContext(IOperation op, String context) {
0420: String parentContext = null;
0421: for (int i = 0; i < op.getSources().length; i++)
0422: if (op.getSources()[i].getAddrType() == DataAddress.ADDR_CONTEXT) {
0423: parentContext = op.getSources()[i].getValue();
0424: break;
0425: }
0426: contextList.put(context, parentContext);
0427: this .contextCount++;
0428: }
0429:
0430: /*
0431: * Construct a DataAddress from a source or target node.
0432: */
0433: private DataAddress getDataAddress(Node n) throws Exception {
0434: NamedNodeMap attributes = n.getAttributes();
0435: DataAddress dat = new DataAddress();
0436: String addrType = attributes.getNamedItem("type")
0437: .getNodeValue();
0438: if (addrType.equals("absolute"))
0439: dat.setAddrType(DataAddress.ADDR_ABSOLUTE);
0440: else if (addrType.equals("context"))
0441: dat.setAddrType(DataAddress.ADDR_CONTEXT);
0442: else if (addrType.equals("literal"))
0443: dat.setAddrType(DataAddress.ADDR_LITERAL);
0444: else if (addrType.equals("variable"))
0445: dat.setAddrType(DataAddress.ADDR_VARIABLE);
0446: else if (addrType.equals("indirect"))
0447: dat.setAddrType(DataAddress.ADDR_INDIRECT);
0448: else if (addrType.equals("metadata"))
0449: dat.setAddrType(DataAddress.ADDR_METADATA);
0450: else
0451: throw new Exception(new Message(
0452: CoreMessageConstants.UNKNOWN_OBJECT_ATTRIBUTE,
0453: "data address type", addrType).getMessage());
0454: if (dat.getAddrType() != DataAddress.ADDR_ABSOLUTE
0455: && dat.getAddrType() != DataAddress.ADDR_LITERAL) {
0456: Node val = attributes.getNamedItem("name");
0457: if (val == null)
0458: throw new Exception(new Message(
0459: CoreMessageConstants.MISSING_ATTRIBUTE, "name",
0460: "data address").getMessage());
0461: dat.setValue(val.getNodeValue());
0462: }
0463: if (dat.getAddrType() == DataAddress.ADDR_LITERAL) {
0464: Node val = attributes.getNamedItem("value");
0465: if (val == null)
0466: throw new Exception(new Message(
0467: CoreMessageConstants.MISSING_ATTRIBUTE,
0468: "value", "data address").getMessage());
0469: dat.setValue(val.getNodeValue());
0470: }
0471:
0472: if (dat.getAddrType() == DataAddress.ADDR_ABSOLUTE
0473: || dat.getAddrType() == DataAddress.ADDR_CONTEXT) {
0474: Node path = attributes.getNamedItem("path");
0475: if (path == null)
0476: throw new Exception(new Message(
0477: CoreMessageConstants.MISSING_ATTRIBUTE, "path",
0478: "data address").getMessage());
0479: dat.setPath(path.getNodeValue());
0480: }
0481: if (dat.getAddrType() == DataAddress.ADDR_VARIABLE) {
0482: String varName = getVariableName(dat.getValue());
0483: if (!userVariableList.contains(varName))
0484: userVariableList.add(varName);
0485: }
0486:
0487: dat.setAddressIndex(addressCount++);
0488: dataAddresses.add(dat);
0489: return dat;
0490: }
0491:
0492: private void generateDataAddressInit(DataAddress dat, int index) {
0493: javaWriter.addTab();
0494: javaWriter.print("dataAddresses[" + index
0495: + "]=RuntimeAddressFactory.newInstance((short)"
0496: + dat.getAddrType() + ", ");
0497: //Second para: pcontext
0498: if (dat.getAddrType() == DataAddress.ADDR_CONTEXT)
0499: javaWriter.printNoTab(dat.getValue() + ", ");
0500: else
0501: javaWriter.printNoTab("null, ");
0502: //Third para: pvariable
0503: if (dat.getAddrType() == DataAddress.ADDR_VARIABLE
0504: || dat.getAddrType() == DataAddress.ADDR_INDIRECT)
0505: javaWriter.printNoTab("userVars."
0506: + getVariableName(dat.getValue()) + ", ");
0507: else
0508: javaWriter.printNoTab("null, ");
0509: //Forth para: pvalue
0510: if (dat.getAddrType() == DataAddress.ADDR_LITERAL
0511: || dat.getAddrType() == DataAddress.ADDR_VARIABLE
0512: || dat.getAddrType() == DataAddress.ADDR_INDIRECT
0513: || dat.getAddrType() == DataAddress.ADDR_METADATA)
0514: javaWriter.printNoTab("\"" + dat.getValue() + "\", ");
0515: else
0516: javaWriter.printNoTab("null, ");
0517: //Fifth para: ppath
0518: javaWriter.printNoTab("\"" + dat.getPath() + "\",inFormatRoot");
0519: javaWriter.printlnNoTab(");");
0520: javaWriter.delTab();
0521:
0522: }
0523:
0524: /*************************************************************************************************************************
0525: * The remainder of this file handles generation of the transformer Java code. *
0526: ************************************************************************************************************************/
0527:
0528: private void generate() throws Exception {
0529: /*
0530: * start the class definition
0531: */
0532:
0533: if (!packageName.equals(""))
0534: javaWriter.println("package " + packageName + ";");
0535:
0536: javaWriter
0537: .println("import com.bostechcorp.cbesb.runtime.ccsl.lib.ITransformationOperation;");
0538: javaWriter.println("import " + PACKAGE_NAME + ".*;");
0539: javaWriter
0540: .println("import com.bostechcorp.cbesb.common.util.xfm.IXmlFormatModel;");
0541: javaWriter
0542: .println("import com.bostechcorp.cbesb.common.util.xfm.XmlFormatModelNode;");
0543: javaWriter
0544: .println("import com.bostechcorp.cbesb.common.util.xfm.XmlFormatModelFactory;");
0545: javaWriter
0546: .println("import com.bostechcorp.cbesb.common.util.RuntimeClassLoader;");
0547: javaWriter.println("import org.w3c.dom.Node;");
0548: javaWriter.println("import org.w3c.dom.Document;");
0549: javaWriter.println("import javax.xml.parsers.DocumentBuilder;");
0550: javaWriter
0551: .println("import javax.xml.parsers.DocumentBuilderFactory;");
0552: javaWriter.println("import java.util.Vector;");
0553: javaWriter.println("import java.util.HashMap;");
0554: javaWriter.println("import java.io.File;");
0555: javaWriter.println("import java.lang.reflect.*;");
0556: javaWriter.println("import javax.activation.DataHandler;");
0557: //Uncomment to use Axiom DOM implementation
0558: // javaWriter.println("import org.apache.axiom.om.impl.dom.jaxp.DocumentBuilderFactoryImpl;");
0559:
0560: javaWriter.println("");
0561: javaWriter.println("");
0562: javaWriter.println("public class " + transformerClassName
0563: + " implements ITransformer {");
0564: javaWriter.addTab();
0565: javaWriter.println("");
0566: javaWriter.println("private String saName;");
0567: javaWriter
0568: .println("public void setServiceAssemblyName(String saName) {");
0569: javaWriter.addTab();
0570: javaWriter.println("this.saName = saName;");
0571: javaWriter.delTab();
0572: javaWriter.println("}");
0573: javaWriter.println("private String getServiceAssemblyName() {");
0574: javaWriter.addTab();
0575: javaWriter.println("return saName;");
0576: javaWriter.println("}");
0577: javaWriter.delTab();
0578: javaWriter.println("");
0579: javaWriter.println("private IXmlFormatModel outFormat;");
0580: javaWriter.println("private XmlFormatModelNode outFormatRoot;");
0581: javaWriter.println("private IXmlFormatModel inFormat;");
0582: javaWriter.println("private XmlFormatModelNode inFormatRoot;");
0583: javaWriter.println("private DocumentBuilder targetDocBuilder;");
0584: javaWriter.println("private Document targetDoc;");
0585: javaWriter.println("private RepeatNode outTree;");
0586: javaWriter.println("private String[] inStrings;");
0587: javaWriter.println("private String[] outStrings;");
0588: javaWriter.println("private boolean opResult;");
0589: javaWriter.println("private Node sourceTree;");
0590: javaWriter.println("private Vector outputVector=new Vector();");
0591: javaWriter
0592: .println("private HashMap<String, Object> transformContext;");
0593: javaWriter
0594: .println("private HashMap<String, Object> metadataMap; //Exchange properties");
0595: javaWriter
0596: .println("private HashMap<String, Object> sourceMetadataMap; //In msg properties");
0597: javaWriter
0598: .println("private HashMap<String, Object> targetMetadataMap; //Out msg properties");
0599: javaWriter
0600: .println("private HashMap<String, DataHandler> sourceAttachmentMap;");
0601: javaWriter
0602: .println("private HashMap<String, DataHandler> targetAttachmentMap;");
0603:
0604: /*
0605: * The user variables are string buffers so I can pass them to a method
0606: * and have the contents altered.
0607: */
0608: javaWriter.println("/*");
0609: javaWriter.println(" * declare user variables");
0610: javaWriter.println(" */");
0611: // if (userVariableList.size() > 0) {
0612: javaWriter.println("private class UserVars {");
0613: for (Iterator i = userVariableList.iterator(); i.hasNext();) {
0614: String varName = (String) i.next();
0615: // varName=getVariableName(varName);
0616: javaWriter.println(" StringBuffer " + varName
0617: + " = new StringBuffer();");
0618: }
0619: javaWriter.println("}");
0620: javaWriter.println("UserVars userVars=new UserVars();");
0621:
0622: // }
0623: javaWriter.println("");
0624: javaWriter.println("");
0625:
0626: javaWriter.println("/*");
0627: javaWriter.println(" * declare iteration context variables");
0628: javaWriter.println(" */");
0629: Set contextSet = contextList.keySet();
0630: for (Iterator i = contextSet.iterator(); i.hasNext();) {
0631: String varName = (String) i.next();
0632: javaWriter.println("private IterationContext " + varName
0633: + " = new IterationContext();");
0634: }
0635: javaWriter.println("");
0636: javaWriter.println("");
0637:
0638: javaWriter.println("/*");
0639: javaWriter
0640: .println(" * create the operation class table and the runtime address table");
0641: javaWriter.println(" */");
0642: javaWriter
0643: .println("ITransformationOperation[] opClasses = new ITransformationOperation["
0644: + classCount + "];");
0645: javaWriter
0646: .println("IRuntimeAddress[] dataAddresses = new IRuntimeAddress["
0647: + addressCount + "];");
0648:
0649: javaWriter.println("public String getInFormatDef(){");
0650: javaWriter.addTab();
0651: javaWriter.println("return \"" + inFormatFileName + "\";");
0652: javaWriter.delTab();
0653: javaWriter.println("}");
0654:
0655: javaWriter.println("public String getOutFormatDef(){");
0656: javaWriter.addTab();
0657: javaWriter.println("return \"" + outFormatFileName + "\";");
0658: javaWriter.delTab();
0659: javaWriter.println("}");
0660:
0661: javaWriter.println("public String getInRoot(){");
0662: javaWriter.addTab();
0663: javaWriter.println("return \"" + inputRootNode + "\";");
0664: javaWriter.delTab();
0665: javaWriter.println("}");
0666:
0667: javaWriter.println("public String getOutRoot(){");
0668: javaWriter.addTab();
0669: javaWriter.println("return \"" + outputRootNode + "\";");
0670: javaWriter.delTab();
0671: javaWriter.println("}");
0672:
0673: javaWriter
0674: .println("public XmlFormatModelNode getInRootFormat(){");
0675: javaWriter.addTab();
0676: javaWriter.println("return inFormatRoot;");
0677: javaWriter.delTab();
0678: javaWriter.println("}");
0679:
0680: javaWriter
0681: .println("public XmlFormatModelNode getOutRootFormat(){");
0682: javaWriter.addTab();
0683: javaWriter.println("return outFormatRoot;");
0684: javaWriter.delTab();
0685: javaWriter.println("}");
0686:
0687: javaWriter.println("public int getInFormatType(){");
0688: javaWriter.addTab();
0689: if (inFormat.equals("xsd"))
0690: javaWriter.println("return FORMATTYPE_XSD;");
0691: else if (inFormat.equals("mdl"))
0692: javaWriter.println("return FORMATTYPE_MDL;");
0693: else if (inFormat.equals("x12"))
0694: javaWriter.println("return FORMATTYPE_X12;");
0695: else if (inFormat.equals("hl7"))
0696: javaWriter.println("return FORMATTYPE_HL7;");
0697:
0698: else
0699: javaWriter.println("return FORMATTYPE_UNDEFINED;");
0700:
0701: javaWriter.delTab();
0702: javaWriter.println("}");
0703:
0704: javaWriter.println("public int getOutFormatType(){");
0705: javaWriter.addTab();
0706: if (outFormat.equals("xsd"))
0707: javaWriter.println("return FORMATTYPE_XSD;");
0708: else if (outFormat.equals("mdl"))
0709: javaWriter.println("return FORMATTYPE_MDL;");
0710: else if (outFormat.equals("x12"))
0711: javaWriter.println("return FORMATTYPE_X12;");
0712: else if (outFormat.equals("hl7"))
0713: javaWriter.println("return FORMATTYPE_HL7;");
0714:
0715: else
0716: javaWriter.println("return FORMATTYPE_UNDEFINED;");
0717:
0718: javaWriter.delTab();
0719: javaWriter.println("}");
0720:
0721: int addressInitCount = addressCount / MAX_NUMBER;
0722: if (addressInitCount * MAX_NUMBER <= addressCount - 1) {
0723: addressInitCount++;
0724: }
0725: int index = 0;
0726: for (int i = 0; i < addressInitCount; i++) {
0727: javaWriter.println("private void dataAddressinit" + i
0728: + "(){");
0729: javaWriter.addTab();
0730: for (int j = 0; j < MAX_NUMBER; j++) {
0731: DataAddress dat = dataAddresses.elementAt(index);
0732: generateDataAddressInit(dat, index);
0733: index++;
0734: if (index == addressCount)
0735: break;
0736: }
0737: javaWriter.delTab();
0738: javaWriter.println("}");
0739:
0740: }
0741:
0742: makeClassInstancesMethod(operationsParent);
0743:
0744: this .classInstanceMethodCount = (this .classIndex + 1)
0745: / MAX_NUMBER;
0746: if (classInstanceMethodCount * MAX_NUMBER <= classIndex - 1) {
0747: classInstanceMethodCount++;
0748: }
0749:
0750: this .contextinitializeMethodCount = this .contextCount
0751: / MAX_NUMBER;
0752: if (contextinitializeMethodCount * MAX_NUMBER <= this .contextCount - 1) {
0753: contextinitializeMethodCount++;
0754: }
0755: index = 0;
0756: javaWriter.println("private void contextInitialize0(){");
0757: javaWriter.addTab();
0758:
0759: contextSet = contextList.keySet();
0760: for (Iterator it = contextSet.iterator(); it.hasNext();) {
0761: String varName = (String) it.next();
0762: javaWriter.println(varName + ".initialize();");
0763: index++;
0764: if (index % MAX_NUMBER == 0) {
0765: int page = index / MAX_NUMBER;
0766: javaWriter.delTab();
0767: javaWriter.println("}");
0768: if (index < this .contextCount) {
0769: javaWriter.println("private void contextInitialize"
0770: + page + "(){");
0771: javaWriter.addTab();
0772: }
0773: }
0774: }
0775: javaWriter.delTab();
0776: javaWriter.println("}");
0777:
0778: generateOperationCode(operationsParent);
0779:
0780: /*
0781: * start the default constructor
0782: */
0783: javaWriter.println("public " + transformerClassName
0784: + "() throws Exception{");
0785: javaWriter.addTab();
0786:
0787: javaWriter.println("/*");
0788: javaWriter
0789: .println(" * create the XML format model from the output schema");
0790: javaWriter.println(" */");
0791: javaWriter
0792: .println("outFormat = XmlFormatModelFactory.newInstance(\""
0793: + outFormatFileName
0794: + "\",\""
0795: + outFormat
0796: + "\");");
0797: javaWriter
0798: .println("outFormatRoot = outFormat.getRootElement(\""
0799: + outputRootNode + "\");");
0800: javaWriter.println("/*");
0801: javaWriter
0802: .println(" * create the XML format model from the input schema");
0803: javaWriter.println(" */");
0804: javaWriter
0805: .println("inFormat = XmlFormatModelFactory.newInstance(\""
0806: + inFormatFileName
0807: + "\",\""
0808: + inFormat
0809: + "\");");
0810: javaWriter.println("inFormatRoot = inFormat.getRootElement(\""
0811: + inputRootNode + "\");");
0812:
0813: javaWriter.println("/*");
0814: javaWriter.println(" * initialize the addresses");
0815: javaWriter.println(" */");
0816: for (int i = 0; i < addressInitCount; i++) {
0817: javaWriter.println("dataAddressinit" + i + "();");
0818: }
0819: javaWriter.println("");
0820: javaWriter.println("");
0821: /*
0822: * instantiate class operations
0823: */
0824: //makeClassInstances(operationsParent);
0825: for (int i = 0; i < classInstanceMethodCount; i++) {
0826: javaWriter.println("makeClassInstances" + i + "();");
0827: }
0828:
0829: /*
0830: * end of constructor
0831: */
0832: javaWriter.delTab();
0833: javaWriter.println("}");
0834: javaWriter.println("");
0835: javaWriter.println("");
0836:
0837: javaWriter.println("/*");
0838: javaWriter.println(" * do a transformation");
0839: javaWriter.println("*/");
0840: javaWriter
0841: .println("public void doTransform(TransformerMessage source, Vector outputVector, HashMap metadataMap) throws Exception {");
0842: javaWriter.addTab();
0843: javaWriter.println("");
0844: javaWriter.println("");
0845: javaWriter
0846: .println("Document sourceDoc = source.getDocument();");
0847: javaWriter.println("this.outputVector= outputVector;");
0848:
0849: // Uncomment to use Axiom DOM implementation
0850: // javaWriter.println("// use Axiom DOM implementation for best performance with CBESB HTTP Component");
0851: // javaWriter.println("DocumentBuilderFactoryImpl.setDOOMRequired(true);");
0852:
0853: javaWriter
0854: .println("targetDocBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();");
0855:
0856: javaWriter.println("targetDoc=targetDocBuilder.newDocument();");
0857:
0858: // Uncomment to use Axiom DOM implementation
0859: // javaWriter.println("// turn off Axiom DOM implementation now that doc is created.");
0860: // javaWriter.println("DocumentBuilderFactoryImpl.setDOOMRequired(false);");
0861:
0862: javaWriter.println("outTree = new RepeatNode();");
0863: javaWriter.println("outTree.formatModelNode=outFormatRoot;");
0864:
0865: javaWriter.println("");
0866: javaWriter.println("");
0867: // javaWriter.println("String[] inStrings;");
0868: // javaWriter.println("String[] outStrings;");
0869: // javaWriter.println("boolean opResult;");
0870: javaWriter.println("sourceTree = sourceDoc.getFirstChild();");
0871:
0872: javaWriter.println("this.metadataMap = metadataMap;");
0873: javaWriter
0874: .println("sourceMetadataMap = source.getMetadataMap();");
0875: javaWriter
0876: .println("targetMetadataMap = new HashMap<String, Object>();");
0877: javaWriter
0878: .println("sourceAttachmentMap = source.getAttachmentMap();");
0879: javaWriter
0880: .println("targetAttachmentMap = new HashMap<String, DataHandler>();");
0881:
0882: javaWriter.println("/*");
0883: javaWriter.println(" * initialize iteration context variables");
0884: javaWriter.println(" */");
0885: for (int i = 0; i < this .contextinitializeMethodCount; i++) {
0886: javaWriter.println("contextInitialize" + i + "();");
0887: }
0888:
0889: javaWriter.println("");
0890: javaWriter.println("");
0891:
0892: javaWriter.println("/*");
0893: javaWriter.println(" * initialize user variables");
0894: javaWriter.println(" */");
0895:
0896: javaWriter.println("");
0897: javaWriter.println("");
0898:
0899: javaWriter.println("/*");
0900: javaWriter.println(" * initialize class operations");
0901: javaWriter.println(" */");
0902: javaWriter
0903: .println("transformContext = new HashMap<String, Object>();");
0904: javaWriter
0905: .println("transformContext.put(\"saName\",this.saName);");
0906: javaWriter
0907: .println("transformContext.put(\"Source.MetadataMap\", this.sourceMetadataMap);");
0908: javaWriter
0909: .println("transformContext.put(\"Target.MetadataMap\", this.targetMetadataMap);");
0910: javaWriter
0911: .println("transformContext.put(\"Source.AttachmentMap\", this.sourceAttachmentMap);");
0912: javaWriter
0913: .println("transformContext.put(\"Target.AttachmentMap\", this.targetAttachmentMap);");
0914: javaWriter.println("for (int i=0; i<opClasses.length; i++)");
0915: javaWriter.addTab();
0916: javaWriter
0917: .println("if(opClasses[i]!=null )opClasses[i].initialize(transformContext);");
0918: javaWriter.delTab();
0919: javaWriter.println("");
0920: javaWriter.println("");
0921:
0922: /*
0923: * generate operation code
0924: */
0925: //makeOperationCode(operationsParent.getNestedOperations());
0926: javaWriter.println("oper();");
0927:
0928: if (!ifSuppress) {
0929: javaWriter
0930: .println("Node output = outTree.makeDomTree(targetDoc, \""
0931: + outputRootNode + "\");");
0932: javaWriter.println("targetDoc.appendChild(output);");
0933: javaWriter
0934: .println("TransformerMessage target = new TransformerMessage();");
0935: javaWriter.println("target.setDocument(targetDoc);");
0936: javaWriter
0937: .println("target.setMetadataMap(targetMetadataMap);");
0938: javaWriter
0939: .println("target.setAttachmentMap(targetAttachmentMap);");
0940: javaWriter.println("outputVector.addElement(target);");
0941: }
0942:
0943: javaWriter.println("/*");
0944: javaWriter.println(" * cleanup class operations");
0945: javaWriter.println(" */");
0946: javaWriter
0947: .println("for (int i=0; i<" + classCount + "; i++) {");
0948: javaWriter.addTab();
0949: javaWriter.println("try {");
0950: javaWriter.addTab();
0951: javaWriter
0952: .println("if(opClasses[i]!=null) opClasses[i].cleanup(transformContext);");
0953: javaWriter.delTab();
0954: javaWriter.println("}");
0955: javaWriter.println("catch (Throwable t) {");
0956: javaWriter.addTab();
0957: javaWriter
0958: .println("//TODO -- save and report exceptions in cleanup method calls");
0959: javaWriter.delTab();
0960: javaWriter.println("}");
0961: javaWriter.delTab();
0962: javaWriter.println("}");
0963: javaWriter.println("");
0964: javaWriter.println("");
0965:
0966: /*
0967: * end doTransform
0968: */
0969: javaWriter.delTab();
0970: javaWriter.println("}");
0971:
0972: /*
0973: * end of class
0974: */
0975: javaWriter.delTab();
0976: javaWriter.println("}");
0977: }
0978:
0979: private void makeClassInstancesMethod(IOperation parent) {
0980: this .classIndex = 0;
0981: javaWriter
0982: .println("private void makeClassInstances0() throws Exception {");
0983: javaWriter.addTab();
0984: javaWriter.println("Class userClass=null;");
0985: makeClassInstances(parent);
0986: javaWriter.delTab();
0987: javaWriter.println("}");
0988:
0989: }
0990:
0991: /*
0992: * Make an instance of the operation class for each class or user operation
0993: */
0994: private void makeClassInstances(IOperation parent) {
0995:
0996: for (IOperation op = parent.getNestedOperations(); op != null; op = op
0997: .getNextOperation()) {
0998: op.makeClassInstances(javaWriter, this .projName);
0999: if (op instanceof OperClass) {
1000: this .classIndex++;
1001: if (this .classIndex % MAX_NUMBER == 0) {
1002: int i = this .classIndex / MAX_NUMBER;
1003: javaWriter.delTab();
1004: javaWriter.println("}");
1005: if (this .classIndex < this .classCount) {
1006: javaWriter
1007: .println("private void makeClassInstances"
1008: + i + "() throws Exception {");
1009: javaWriter.addTab();
1010: javaWriter.println("Class userClass=null;");
1011: }
1012: }
1013:
1014: }
1015:
1016: if (op.getNestedOperations() != null)
1017: makeClassInstances(op);
1018: }
1019:
1020: }
1021:
1022: /*
1023: * generate code for an operation list
1024: */
1025: private void generateOperationCode(IOperation op) throws Exception {
1026: javaWriter.println("private void oper() throws Exception{");
1027: javaWriter.addTab();
1028: this .operIndex = 0;
1029: // for(int i=0;i<op.getChildCount();i++)
1030: javaWriter.println("oper0_0();");
1031:
1032: javaWriter.println("}");
1033: javaWriter.delTab();
1034:
1035: makeOperationCode(op, 0);
1036:
1037: }
1038:
1039: /*
1040: * generate code for an operation list
1041: */
1042: private void makeOperationCode(IOperation op, int methodIndex)
1043: throws Exception {
1044:
1045: int n = 0;
1046: for (; op != null; op = op.getNextOperation()) {
1047: // System.out.println("generating for op="+op.getName());
1048: javaWriter.println("private void oper" + methodIndex + "_"
1049: + n + "() throws Exception{");
1050: javaWriter.addTab();
1051: this .operIndex++;
1052: boolean bIf = false;
1053: if (op instanceof OperIF || op instanceof OperElseIF
1054: || op instanceof OperElse)
1055: bIf = true;
1056:
1057: if (!bIf)
1058: startCode(op);
1059: if (op.getNestedOperations() != null) {
1060: IOperation opSub = op.getNestedOperations();
1061: for (int i = 0; i < op.getChildCount(); i++) {
1062: boolean bSubIf = false;
1063: if (opSub instanceof OperIF
1064: || opSub instanceof OperElseIF
1065: || opSub instanceof OperElse) {
1066: bSubIf = true;
1067: startCode(opSub);
1068: } else if (opSub instanceof OperBeginComment) {
1069: javaWriter.println("/*");
1070: }
1071: javaWriter.println("oper" + this .operIndex + "_"
1072: + i + "();");
1073:
1074: if (opSub instanceof OperEndComment) {
1075: javaWriter.println("*/");
1076: }
1077:
1078: if (bSubIf)
1079: endCode(opSub);
1080: opSub = opSub.getNextOperation();
1081:
1082: }
1083: }
1084: if (!bIf)
1085: endCode(op);
1086: javaWriter.println("}");
1087: javaWriter.delTab();
1088:
1089: if (op.getNestedOperations() != null) {
1090: makeOperationCode(op.getNestedOperations(),
1091: this .operIndex);
1092: }
1093: n++;
1094: }
1095: }
1096:
1097: /*
1098: * Generate start code (before nested operations are generated)
1099: */
1100: private void startCode(IOperation op) throws Exception {
1101: op.startCode(this );
1102: }
1103:
1104: /*
1105: * Generate end code (after nested operations are generated
1106: */
1107: private void endCode(IOperation op) throws Exception {
1108: op.endCode(javaWriter);
1109: }
1110:
1111: /**
1112: * @return the projName
1113: */
1114: public String getProjName() {
1115: return projName;
1116: }
1117:
1118: }
|