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: X12Parser.java 3622 2006-12-12 03:04:08Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.parser.util;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028:
029: import org.w3c.dom.Document;
030:
031: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
032: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
033: import com.bostechcorp.cbesb.common.mdl.util.FormatDefUtil;
034: import com.bostechcorp.cbesb.common.runtime.CbesbException;
035: import com.bostechcorp.cbesb.common.runtime.ConfigurationException;
036: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
037: import com.bostechcorp.cbesb.runtime.parser.ParserException;
038: import com.bostechcorp.cbesb.runtime.parser.impl.Hl7Parser;
039: import com.bostechcorp.cbesb.runtime.parser.impl.LegacyDataParser;
040: import com.bostechcorp.cbesb.runtime.parser.impl.X12Parser;
041:
042: public class ParserUtil {
043:
044: private char x12SegmentTerminator = 0;
045: private char x12FieldSeparator = 0;
046: private char x12CompositeSeparator = 0;
047: private String saName = null;
048:
049: public ParserUtil(String saName) {
050: super ();
051: this .saName = saName;
052: }
053:
054: /**
055: * @return the x12CompositeSeparator
056: */
057: public char getX12CompositeSeparator() {
058: return x12CompositeSeparator;
059: }
060:
061: /**
062: * @param compositeSeparator the x12CompositeSeparator to set
063: */
064: public void setX12CompositeSeparator(char compositeSeparator) {
065: x12CompositeSeparator = compositeSeparator;
066: }
067:
068: /**
069: * @return the x12FieldSeparator
070: */
071: public char getX12FieldSeparator() {
072: return x12FieldSeparator;
073: }
074:
075: /**
076: * @param fieldSeparator the x12FieldSeparator to set
077: */
078: public void setX12FieldSeparator(char fieldSeparator) {
079: x12FieldSeparator = fieldSeparator;
080: }
081:
082: /**
083: * @return the x12SegmentTerminator
084: */
085: public char getX12SegmentTerminator() {
086: return x12SegmentTerminator;
087: }
088:
089: /**
090: * @param segmentTerminator the x12SegmentTerminator to set
091: */
092: public void setX12SegmentTerminator(char segmentTerminator) {
093: x12SegmentTerminator = segmentTerminator;
094: }
095:
096: public Document parse(InputStream is, String format,
097: String formatSpec, String rootName) throws CbesbException,
098: ParserException {
099: IMDLDocument mdlDoc = FormatDefUtil.getMdlDocFromFormatSpec(
100: format, formatSpec);
101: IMessageDefinition msgDef = mdlDoc.getMessageDefinition(mdlDoc
102: .getTargetNamespace(), rootName);
103:
104: LegacyDataParser parser;
105: if (format.equals("x12")) {
106: String[] msgDefs = EsbPathHelper
107: .getVariantMessage(formatSpec);
108: parser = new X12Parser();
109: ((X12Parser) parser).setMdlDoc(mdlDoc);
110: ((X12Parser) parser).setProjectName(msgDefs[0]);
111: ((X12Parser) parser).setVersion(msgDefs[1]);
112: ((X12Parser) parser).setVariant(msgDefs[2]);
113: if (this .saName != null)
114: ((X12Parser) parser).setSaName(this .saName);
115: else
116: ((X12Parser) parser).setSaName(msgDefs[4]);
117:
118: ((X12Parser) parser)
119: .setSegmentTerminator(x12SegmentTerminator);
120: ((X12Parser) parser).setFieldSeparator(x12FieldSeparator);
121: ((X12Parser) parser)
122: .setCompositeSeparator(x12CompositeSeparator);
123:
124: } else if (format.equals("hl7")) {
125: String[] msgDefs = EsbPathHelper
126: .getVariantMessage(formatSpec);
127: parser = new Hl7Parser();
128: ((Hl7Parser) parser).setMdlDoc(mdlDoc);
129: ((Hl7Parser) parser).setProjectName(msgDefs[0]);
130: ((Hl7Parser) parser).setVersion(msgDefs[1]);
131: ((Hl7Parser) parser).setVariant(msgDefs[2]);
132: if (this .saName != null)
133: ((Hl7Parser) parser).setSaName(this .saName);
134: else
135: ((Hl7Parser) parser).setSaName(msgDefs[4]);
136:
137: } else {
138: parser = new LegacyDataParser();
139: if (msgDef.isIdMethodByTag()) {
140: String[] result = { null, null, null };
141: try {
142: result = EsbPathHelper.getFsmClassName(formatSpec
143: + ":" + rootName);
144: } catch (Exception e) {
145: throw new ConfigurationException(
146: "Can not find the required finite state machine (FSM) class for parsing '"
147: + rootName + "'.",
148: "Make sure the project is built.", e);
149: }
150: String fsmClass = result[0];
151: parser.setFsmClassName(fsmClass);
152: if (this .saName != null)
153: parser.setSaName(this .saName);
154: else
155: parser.setSaName(result[2]);
156: }
157:
158: }
159:
160: try {
161: return parser.parse(is, msgDef);
162: } catch (IOException e) {
163: throw new ConfigurationException(
164: "IOException caught when parsing the message based on the definition '"
165: + msgDef + "'.",
166: "Make sure the message definition is created", e);
167: }
168: }
169:
170: }
|