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: FixedElementSerializer.java 3622 2006-12-12 03:04:08Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.parser;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.OutputStream;
029: import java.io.OutputStreamWriter;
030:
031: import org.w3c.dom.Element;
032:
033: import com.bostechcorp.cbesb.common.i18n.Message;
034: import com.bostechcorp.cbesb.common.i18n.Messages;
035: import com.bostechcorp.cbesb.common.mdl.IContentElement;
036: import com.bostechcorp.cbesb.common.mdl.IContentGroup;
037: import com.bostechcorp.cbesb.common.mdl.IContentNode;
038: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
039: import com.bostechcorp.cbesb.common.mdl.IFixedChildAttributes;
040: import com.bostechcorp.cbesb.common.util.ErrorUtil;
041:
042: /**
043: * The FixedElementSerializer class implements ElementSerializer. It is used to
044: * serialize Elements defined as fixed format. Basic flow of the serialize
045: * method: 1. Verify that all child Elements of the DOM Element passed in match
046: * the child elements of the ElementDefinition passed in. 2. For each child
047: * Element do the following: a. Get the appropriate ElementSerializer for the
048: * child ElementDefinition. b. Call the serialize method of the child
049: * ElementSerializer, passing in a ByteArrayOutputStream instead of the real
050: * OutputStream. c. Use the length, justification and fill character attributes
051: * of the child to format the data returned in the ByteArrayOutputStream to
052: * match the fixed field definition. d. Write the data to the OutputStream.
053: *
054: */
055: public class FixedElementSerializer extends SerializerBase implements
056: IElementSerializer {
057:
058: public void serialize(Element element,
059: IElementDefinition elementDefinition,
060: DataEscapingUtil dataEscapingUtil, OutputStream os)
061: throws ParserException {
062: // try {
063: validateElementName(element, elementDefinition);
064:
065: Element childElement = getFirstChildElement(element);
066: for (int i = 0; i < elementDefinition.getChildCount(); i++) {
067: IContentNode contentNode = elementDefinition
068: .getChildAtIndex(i);
069: if (contentNode instanceof IContentElement) {
070: IElementDefinition childElementDefinition = ((IContentElement) contentNode)
071: .getResolvedElementDef();
072: IFixedChildAttributes fixedChildAttributes = (IFixedChildAttributes) ((IContentElement) contentNode)
073: .getFormatChildAttributes();
074: if (childElement != null) {
075: validateElementName(childElement,
076: childElementDefinition);
077: serializeChild(childElement, fixedChildAttributes,
078: childElementDefinition, dataEscapingUtil,
079: os);
080: } else {
081: throw new ParserException(
082: "Serializer error for fixed message definition. Missing required child element: '"
083: + childElementDefinition
084: .getNamespaceURI()
085: + ":"
086: + childElementDefinition.getName()
087: + "' in fixed message(element) '"
088: + elementDefinition
089: .getNamespaceURI()
090: + ":"
091: + elementDefinition.getName()
092: + "'.",
093: "Make sure the required child element is populated with value.");
094: }
095:
096: childElement = getNextSiblingElement(childElement);
097: } else if (contentNode instanceof IContentGroup) {
098: // TODO LU can we skip here?
099: //See if Fixed type could have group node
100: }
101: }
102: // } catch (Exception e) {
103: //
104: // ErrorUtil.printError("Exception in serialize(): ",e);
105: // throw new ParserException(new Message(Messages.FIXED_SERIALIZER_ERROR, e.getMessage()).getMessage());
106: // }
107: }
108:
109: private void serializeChild(Element childElement,
110: IFixedChildAttributes fixedChildAttributes,
111: IElementDefinition childDef,
112: DataEscapingUtil dataEscapingUtil, OutputStream os)
113: throws ParserException {
114:
115: ByteArrayOutputStream baos = new ByteArrayOutputStream();
116: IElementSerializer serializer = ElementSerializerFactory
117: .instance().getElementSerializer(childDef);
118: serializer.serialize(childElement, childDef, dataEscapingUtil,
119: baos);
120: String childData = null;
121: try {
122: childData = new String(baos.toByteArray(), "utf-8");
123: } catch (IOException e) {
124: throw new ParserException(
125: "IOException caught when converting to UTF-8 String in serializing element '"
126: + childDef.getNamespaceURI() + ":"
127: + childDef.getName() + "'", e);
128:
129: // ErrorUtil.printError("Exception in serializeChild(): ",e);
130: // throw new ParserException(e);
131: }
132:
133: int length = fixedChildAttributes.getLength();
134: char fillChar = fixedChildAttributes.getFillChar();
135:
136: String padding = "";
137: if (childData.length() < length) {
138: int padLength = length - childData.length();
139: char[] padBuffer = new char[padLength];
140: for (int i = 0; i < padLength; i++) {
141: padBuffer[i] = fillChar;
142: }
143: padding = new String(padBuffer);
144: } else {
145: // if the length in the data is greater than the defined length, we need to truncate it
146: childData = childData.substring(0, length);
147: }
148:
149: try {
150: OutputStreamWriter writer = new OutputStreamWriter(os,
151: "utf-8");
152: if (fixedChildAttributes.getJustification() == 0) {
153: writer.write(childData);
154: writer.write(padding);
155: } else {
156: writer.write(padding);
157: writer.write(childData);
158: }
159: writer.flush();
160: } catch (IOException ioe) {
161:
162: throw new ParserException(
163: "IOExcept caught when writing out data in serializing element '"
164: + childDef.getNamespaceURI() + ":"
165: + childDef.getName() + "'", ioe);
166: // ErrorUtil.printError("Exception in serializeChild(): ",ioe);
167: // throw new ParserException(new Message(Messages.FIXED_SERIALIZER_CHILD_ERROR, ioe.getMessage()).getMessage());
168: }
169: }
170: }
|