Source Code Cross Referenced for FixedElementSerializer.java in  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » runtime » parser » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » cbesb 1.2 » com.bostechcorp.cbesb.runtime.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.