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: FixedElementParser.java 3622 2006-12-12 03:04:08Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.parser;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.UnsupportedEncodingException;
031:
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.IContentElement;
037: import com.bostechcorp.cbesb.common.mdl.IContentGroup;
038: import com.bostechcorp.cbesb.common.mdl.IContentNode;
039: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
040: import com.bostechcorp.cbesb.common.mdl.IFixedChildAttributes;
041: import com.bostechcorp.cbesb.common.util.ErrorUtil;
042: import com.bostechcorp.cbesb.runtime.parser.impl.LegacyDataParser;
043:
044: /**
045: * The FixedElementParser class implements ElementParser. It is used to parse
046: * elements defined as fixed format.
047: * Basic flow of the parse method:
048: * 1. Create a new Element with the name and namespace provided in the ElementDefinition.
049: * 2. Loop over all of the child ContentElement nodes defined in the ElementDefinition.
050: * For each child, do the following:
051: * a. Get the length of the data for the child
052: * b. Read that many characters from the InputStream into a buffer.
053: * c. Using the justification and fill character settings, trim the buffer
054: * to contain only meaningful data.
055: * d. Create a new InputStream containing this data only.
056: * e. Get the ElementDefinition from the ContentElement that defines the child.
057: * f. Get an instance of ElementParser from ElementParserFactory using the
058: * child ElementDefinition as input.
059: * g. Call the parse method of this ElementParser passing in the new InputStream and
060: * child ElementDefinition.
061: * h. Append the returned DOM Element to the Element created in step 1.
062: *
063: */
064: public class FixedElementParser implements IElementParser {
065:
066: /* (non-Javadoc)
067: * @see com.bostechcorp.cbesb.runtime.component.parser.IElementParser#parse(java.io.InputStream, com.bostechcorp.cbesb.common.mdl.IElementDefinition)
068: */
069: public Element parse(InputStream is,
070: IElementDefinition elementDefinition)
071: throws ParserException {
072:
073: try {
074: InputStreamReader isReader = new InputStreamReader(is,
075: "utf-8");
076: Element element = LegacyDataParser.getDocument()
077: .createElementNS(
078: elementDefinition.getNamespaceURI(),
079: elementDefinition.getName());
080:
081: //Loop through the elements under content node
082: for (int i = 0; i < elementDefinition.getChildCount(); i++) {
083: IContentNode contentNode = elementDefinition
084: .getChildAtIndex(i);
085: if (contentNode instanceof IContentElement) {
086: //Get discription for this fixed contentNode
087: IFixedChildAttributes fixedChildAttributes = (IFixedChildAttributes) ((IContentElement) contentNode)
088: .getFormatChildAttributes();
089: //Descript fixed data
090: String childData = scanFixedData(
091: fixedChildAttributes, isReader);
092:
093: IElementDefinition childElementDefinition = ((IContentElement) contentNode)
094: .getResolvedElementDef();
095:
096: IElementParser childParser = ElementParserFactory
097: .instance().getElementParser(
098: childElementDefinition);
099:
100: // //Check if name of chileElementDefinition is empty mean reference
101: // if (childElementDefinition.getName().equals("")) {
102: // childElementDefinition.setName(((IContentElement)contentNode).getRef());
103: // }
104: // System.out.println("");
105: Element child = childParser.parse(
106: new ByteArrayInputStream(childData
107: .getBytes("utf-8")),
108: childElementDefinition);
109: element.appendChild(child);
110: } else if (contentNode instanceof IContentGroup) {
111: // TODO LU can we skip here?
112: //See if Fixed type could have group node
113: }
114: }
115:
116: return element;
117:
118: } catch (UnsupportedEncodingException e) {
119: // ErrorUtil.printError("Exception in parse()",e);
120: // throw new ParserException(new Message(Messages.SCAN_FIXED_DATA, e.getMessage()).getMessage());
121: throw new ParserException(
122: "IOExceptection caught when converting string to UTF-8 in parsing element '"
123: + elementDefinition.getNamespaceURI() + ":"
124: + elementDefinition.getName() + "'", e);
125: }
126: }
127:
128: private String scanFixedData(IFixedChildAttributes fixedAttr,
129: InputStreamReader reader) throws ParserException {
130:
131: String retValue = "";
132:
133: int length = fixedAttr.getLength();
134: char[] charbuffer = new char[length];
135: StringBuffer strBuf = new StringBuffer();
136: int charsRead = 0;
137:
138: try {
139: charsRead = reader.read(charbuffer, 0, length);
140: } catch (IOException ioe) {
141: ErrorUtil.printError("Exception in scanFixedData() ", ioe);
142: throw new ParserException(new Message(
143: Messages.SCAN_FIXED_DATA, ioe.getMessage())
144: .getMessage());
145: }
146:
147: if (charsRead > -1) {
148: strBuf.append(charbuffer, 0, charsRead);
149: }
150:
151: char fillChar = fixedAttr.getFillChar();
152: if (fillChar != 0) {
153: if (fixedAttr.getJustification() == IFixedChildAttributes.JUSTIFICATION_LEFT) {
154: for (int i = strBuf.length() - 1; i >= 0; i--) {
155: if (strBuf.charAt(i) != fillChar) {
156: retValue = strBuf.substring(0, i + 1);
157: break;
158: }
159: }
160: } else {
161: for (int i = 0; i < strBuf.length(); i++) {
162: if (strBuf.charAt(i) != fillChar) {
163: retValue = strBuf.substring(i);
164: break;
165: }
166: }
167: }
168: } else {
169: retValue = strBuf.toString();
170: }
171:
172: //System.out.println(retValue);
173:
174: return retValue;
175: }
176: }
|