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 it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * 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 MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: LegacyDataParser.java 3622 2006-12-12 03:04:08Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.parser.impl;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028:
029: import javax.xml.parsers.DocumentBuilderFactory;
030:
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033:
034: import com.bostechcorp.cbesb.common.i18n.Message;
035: import com.bostechcorp.cbesb.common.i18n.Messages;
036: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
037: import com.bostechcorp.cbesb.common.util.ErrorUtil;
038: import com.bostechcorp.cbesb.runtime.parser.ElementParserFactory;
039: import com.bostechcorp.cbesb.runtime.parser.IElementParser;
040: import com.bostechcorp.cbesb.runtime.parser.ParserException;
041: import com.bostechcorp.cbesb.runtime.parser.VariableTaggedElementParser;
042:
043: /**
044: * The LegacyDataParser class is the main interface to parse a non-xml message.
045: * All other classes in this API are used by this class to provide the different
046: * parsing functions. The class that instantiates the LegacyDataParser should
047: * also have an instance of MDLDocumentFactory to load the MessageDefinition
048: * object used to parse the data.
049: * The parse method of this class takes an InputStream containing the message data
050: * and the MessageDefinition as input. If no errors occur, then the fully parsed
051: * DOM Document is returned.
052: * Basic flow of the parse method:
053: * 1. Create a new empty DOM Document.
054: * 2. Retrieve the appropriate ElementParser implementation from the
055: * ElementParserFactory.
056: * 3. Call the ElementParser.parse method passing in the same InputStream and
057: * MessageDefinition.
058: * 4. If the ElementParser returns successfully, it will return a DOM Element.
059: * This is the root Element and is then appended to the Document.
060: * 5. Return the Document.
061: *
062: */
063: public class LegacyDataParser {
064:
065: protected String fsmClassName;
066: protected String saName;
067:
068: protected static Document document;
069:
070: //protected ElementParserFactory elementParserFactory;
071:
072: public LegacyDataParser() {
073: try {
074: document = DocumentBuilderFactory.newInstance()
075: .newDocumentBuilder().newDocument();
076: } catch (Exception e) {
077:
078: ErrorUtil
079: .printError("Exception in LegacyDataParser(): ", e);
080: }
081: }
082:
083: /**
084: * @return the fsmClassName
085: */
086: public String getFsmClassName() {
087: return fsmClassName;
088: }
089:
090: /**
091: * @param fsmClassName the fsmClassName to set
092: */
093: public void setFsmClassName(String fsmClassName) {
094: this .fsmClassName = fsmClassName;
095: }
096:
097: /**
098: * @return the saName
099: */
100: public String getSaName() {
101: return saName;
102: }
103:
104: /**
105: * @param saName the saName to set
106: */
107: public void setSaName(String saName) {
108: this .saName = saName;
109: }
110:
111: public Document parse(InputStream is, IMessageDefinition msgDef)
112: throws IOException, ParserException {
113: Element root = null;
114: try {
115: IElementParser parser = ElementParserFactory.instance()
116: .getElementParser(msgDef);
117: if (parser instanceof VariableTaggedElementParser) {
118: ((VariableTaggedElementParser) parser)
119: .setFsmClassName(fsmClassName);
120: ((VariableTaggedElementParser) parser)
121: .setSaName(saName);
122: }
123: root = parser.parse(is, msgDef);
124: document.appendChild(root);
125: }
126:
127: catch (Exception e) {
128: if (root != null) {
129: document.appendChild(document.importNode(root, true));
130: return document;
131: }
132: // ErrorUtil.printError("Exception in parse(): ",e);
133: //throw new ParserException(new Message(Messages.LEGACY_PARSER_NOFOUND, e.getMessage()).getMessage()); }
134: if (e instanceof ParserException)
135: throw (ParserException) e;
136: else
137: throw new ParserException(e.getMessage());
138: }
139:
140: return document;
141: }
142:
143: public static Document getDocument() {
144: return document;
145: }
146: }
|