01: /**
02: *
03: */package com.bostechcorp.cbesb.runtime.parser.impl;
04:
05: import java.io.ByteArrayOutputStream;
06: import java.io.IOException;
07: import java.io.OutputStream;
08:
09: import org.w3c.dom.Document;
10:
11: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
12: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
13: import com.bostechcorp.cbesb.common.mdl.IProperty;
14: import com.bostechcorp.cbesb.common.mdl.impl.PropertyImpl;
15: import com.bostechcorp.cbesb.common.util.ErrorUtil;
16: import com.bostechcorp.cbesb.runtime.parser.ParserException;
17:
18: public abstract class VariantSerializer extends LegacyDataSerializer {
19:
20: private IMDLDocument mdlDoc;
21:
22: /**
23: * @return the mdlDoc
24: */
25: public IMDLDocument getMdlDoc() {
26: return mdlDoc;
27: }
28:
29: /**
30: * @param mdlDoc the mdlDoc to set
31: */
32: public void setMdlDoc(IMDLDocument mdlDoc) {
33: this .mdlDoc = mdlDoc;
34: }
35:
36: public abstract void preSerialize(Document doc,
37: IMessageDefinition messageDefinition)
38: throws ParserException;
39:
40: public abstract void postSerialize(StringBuffer msgData,
41: IMessageDefinition messageDefinition)
42: throws ParserException;
43:
44: public void serialize(Document doc,
45: IMessageDefinition messageDefinition, OutputStream os)
46: throws ParserException {
47:
48: try {
49: preSerialize(doc, messageDefinition);
50:
51: ByteArrayOutputStream baos = new ByteArrayOutputStream();
52: super .serialize(doc, messageDefinition, baos);
53:
54: StringBuffer msgData = new StringBuffer(baos
55: .toString("utf-8"));
56: postSerialize(msgData, messageDefinition);
57: os.write(msgData.toString().getBytes("utf-8"));
58: } catch (IOException e) {
59:
60: // ErrorUtil.printError("VariantSerializer caught IOException ",e);
61: throw new ParserException(
62: "IOException cuaght in serializing document '",
63: messageDefinition.getNamespaceURI() + ":"
64: + messageDefinition.getName() + "'", e);
65: }
66: }
67:
68: protected void overrideMsgDefProperty(String propertyName,
69: String propertyValue) {
70:
71: String namespaceURI = mdlDoc.getTargetNamespace();
72: IProperty prop = mdlDoc.getProperty(namespaceURI, propertyName);
73: if (prop == null) {
74: prop = new PropertyImpl();
75: mdlDoc.addProperty(prop);
76: prop.setNamespaceURI(namespaceURI);
77: prop.setName(propertyName);
78: }
79: prop.setValue(propertyValue);
80:
81: }
82: }
|