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: LeafElementSerializer.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.OutputStream;
28: import java.io.OutputStreamWriter;
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:
37: /**
38: * The LeafElementSerializer class implements ElementSerializer. It is used to
39: * serialize leaf elements in the message definition. If an ElementDefinition
40: * does not specify a format definition, but does specify a datatype, then it is
41: * a leaf element. Basic flow of the serialize method: 1. Based on the datatype
42: * of the ElementDefinition, format the data appropriately. Note that in the
43: * first version, the only supported datatype will be String, and therefore, no
44: * reformatting is necessary. 2. Write the formatted data to the OutputStream.
45: *
46: */
47: public class LeafElementSerializer extends SerializerBase implements
48: IElementSerializer {
49:
50: /*
51: * (non-Javadoc)
52: *
53: * @see com.bostechcorp.cbesb.runtime.component.parser.IElementSerializer#serializer(org.w3c.dom.Element,
54: * com.bostechcorp.cbesb.common.mdl.IElementDefinition,
55: * java.io.OutputStream)
56: */
57: public void serialize(Element element,
58: IElementDefinition elementDefinition,
59: DataEscapingUtil dataEscapingUtil, OutputStream os)
60: throws ParserException {
61:
62: validateElementName(element, elementDefinition);
63:
64: String data = element.getTextContent();
65: if (data != null && data.length() > 0) {
66:
67: data = dataEscapingUtil.escapeData(data);
68:
69: OutputStreamWriter writer = null;
70: try {
71: writer = new OutputStreamWriter(os, "utf-8");
72: writer.write(data);
73: writer.flush();
74: } catch (IOException ioe) {
75: throw new ParserException(
76: "IOException caught when writing data to UTF-8 output stream in serializing element '"
77: + elementDefinition.getNamespaceURI()
78: + ":"
79: + elementDefinition.getName()
80: + "'", ioe);
81: // ErrorUtil.printError("Exception in serialize(): ",ioe);
82: // throw new ParserException(new Message(Messages.LEAF_SERIALIZER_ERROR, ioe.getMessage()).getMessage());
83: }
84: }
85: }
86: }
|