01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2006 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id: LeafElementParser.java 3622 2006-12-12 03:04:08Z lzheng $
23: */
24: package com.bostechcorp.cbesb.runtime.parser;
25:
26: import java.io.IOException;
27: import java.io.InputStream;
28: import java.io.InputStreamReader;
29:
30: import org.w3c.dom.Element;
31:
32: import com.bostechcorp.cbesb.common.i18n.Message;
33: import com.bostechcorp.cbesb.common.i18n.Messages;
34: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
35: import com.bostechcorp.cbesb.common.util.ErrorUtil;
36: import com.bostechcorp.cbesb.runtime.parser.impl.LegacyDataParser;
37:
38: /**
39: * The LeafElementParser class implements ElementParser. It is used to parse
40: * leaf elements in the message definition. If an ElementDefinition does not
41: * specify a format definition, but does specify a datatype, then it is a leaf
42: * element. Basic flow of the parse method: 1. Create a new Element with the
43: * name and namespace provided in the ElementDefinition. 2. Read the entire
44: * contents of the InputStream. It is assumed that the InputStream contains only
45: * data for this element at this point. 3. Based on the datatype of the
46: * ElementDefinition, format the data appropriately. Note that in the first
47: * version, the only supported datatype will be String, and therefore, no
48: * reformatting is necessary. 4. Create a new Text Node containing the formatted
49: * data from the InputStream. 5. Append the Text Node to the new Element. 6.
50: * Return the Element.
51: *
52: */
53: public class LeafElementParser implements IElementParser {
54:
55: private static final int BUFFERSIZE = 512;
56:
57: public Element parse(InputStream is,
58: IElementDefinition elementDefinition)
59: throws ParserException {
60: try {
61: StringBuffer buffer = new StringBuffer();
62: InputStreamReader reader = new InputStreamReader(is,
63: "utf-8");
64: Element element = LegacyDataParser.getDocument()
65: .createElementNS(
66: elementDefinition.getNamespaceURI(),
67: elementDefinition.getName());
68:
69: char[] charbuffer = new char[BUFFERSIZE];
70: int length;
71: while ((length = reader.read(charbuffer, 0, BUFFERSIZE)) > -1) {
72: buffer.append(charbuffer, 0, length);
73: }
74:
75: element.appendChild((LegacyDataParser.getDocument())
76: .createTextNode(buffer.toString()));
77: return element;
78: } catch (IOException ioe) {
79:
80: // ErrorUtil.printError("Exception in parse(): ",ioe);
81: // throw new ParserException(new Message(Messages.LEAF_PARSER_READ_ERROR,
82: // ioe.getMessage()).getMessage());
83: throw new ParserException(
84: "IOException caught when converting to UTF-8 string in parsing element '"
85: + elementDefinition.getNamespaceURI() + ":"
86: + elementDefinition.getName() + "'", ioe);
87: }
88:
89: // } catch (Exception e) {
90: // ErrorUtil.printError("Exception in parse(): ",e);
91: // throw new ParserException(new Message(Messages.LEAF_PARSER_ERROR,
92: // e.getMessage()).getMessage());
93: // }
94: }
95: }
|