001: /**
002: *
003: */package com.bostechcorp.cbesb.runtime.parser.impl;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.ByteArrayOutputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009:
010: import org.w3c.dom.Document;
011:
012: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
013: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
014: import com.bostechcorp.cbesb.common.mdl.IProperty;
015: import com.bostechcorp.cbesb.common.mdl.impl.PropertyImpl;
016: import com.bostechcorp.cbesb.common.util.ErrorUtil;
017: import com.bostechcorp.cbesb.runtime.parser.ParserException;
018:
019: public abstract class VariantParser extends LegacyDataParser {
020:
021: private String projectName;
022: private String version;
023: private String variant = null;
024: private IMDLDocument mdlDoc;
025:
026: /**
027: * @return the mdlDoc
028: */
029: public IMDLDocument getMdlDoc() {
030: return mdlDoc;
031: }
032:
033: /**
034: * @param mdlDoc the mdlDoc to set
035: */
036: public void setMdlDoc(IMDLDocument mdlDoc) {
037: this .mdlDoc = mdlDoc;
038: }
039:
040: /**
041: * @return the projectName
042: */
043: public String getProjectName() {
044: return projectName;
045: }
046:
047: /**
048: * @param projectName the projectName to set
049: */
050: public void setProjectName(String projectName) {
051: this .projectName = projectName;
052: }
053:
054: /**
055: * @return the variant
056: */
057: public String getVariant() {
058: return variant;
059: }
060:
061: /**
062: * @param variant the variant to set
063: */
064: public void setVariant(String variant) {
065: this .variant = variant;
066: }
067:
068: /**
069: * @return the version
070: */
071: public String getVersion() {
072: return version;
073: }
074:
075: /**
076: * @param version the version to set
077: */
078: public void setVersion(String version) {
079: this .version = version;
080: }
081:
082: protected abstract void preProcessMessage(StringBuffer msgData)
083: throws ParserException;
084:
085: protected abstract String getFsmClassName(String messageName);
086:
087: public Document parse(InputStream is, IMessageDefinition msgDef)
088: throws IOException, ParserException {
089: StringBuffer msgData;
090:
091: // Get message data into a StringBuffer
092: msgData = getMsgDataFromInputStream(is);
093:
094: //Do any format specific pre-processing
095: preProcessMessage(msgData);
096:
097: try {
098:
099: //Get the FSM class name
100: this .setFsmClassName(getFsmClassName(msgDef.getName()));
101:
102: //Convert back into an InputStream and call the LegacyDataParser
103: ByteArrayInputStream bais = new ByteArrayInputStream(
104: msgData.toString().getBytes("utf-8"));
105: return super .parse(bais, msgDef);
106: } catch (ParserException pe) {
107:
108: // can't find FSM class in the variant; use the default base and try again;
109:
110: this .setVariant("");
111: // Get the FSM class name
112: this .setFsmClassName(getFsmClassName(msgDef.getName()));
113:
114: //Convert back into an InputStream and call the LegacyDataParser
115: ByteArrayInputStream bais = new ByteArrayInputStream(
116: msgData.toString().getBytes("utf-8"));
117: return super .parse(bais, msgDef);
118:
119: }
120: // catch (IOException e) {
121: //
122: // ErrorUtil.printError("VariantParser caught IOException",e);
123: // throw new ParserException("VariantParser caught IOException", e);
124: // }
125: }
126:
127: private StringBuffer getMsgDataFromInputStream(InputStream is)
128: throws IOException {
129: ByteArrayOutputStream baos = new ByteArrayOutputStream();
130: int i = is.read();
131: while (i != -1) {
132: baos.write(i);
133: i = is.read();
134: }
135: return new StringBuffer(baos.toString("utf-8"));
136: }
137:
138: protected void overrideMsgDefProperty(String propertyName,
139: String propertyValue) {
140:
141: String namespaceURI = mdlDoc.getTargetNamespace();
142: IProperty prop = mdlDoc.getProperty(namespaceURI, propertyName);
143: if (prop == null) {
144: prop = new PropertyImpl();
145: mdlDoc.addProperty(prop);
146: prop.setNamespaceURI(namespaceURI);
147: prop.setName(propertyName);
148: }
149: prop.setValue(propertyValue);
150:
151: }
152: }
|