Source Code Cross Referenced for StdXMLBuilder.java in  » XML-UI » XUI » net » n3 » nanoxml » 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 » XML UI » XUI » net.n3.nanoxml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* StdXMLBuilder.java                                              NanoXML/Java
002:         *
003:         * $Revision: 1.17 $
004:         * $Date: 2005/01/05 17:20:04 $
005:         * $Name:  $
006:         *
007:         * This file is part of NanoXML 2 for Java.
008:         * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
009:         *
010:         * This software is provided 'as-is', without any express or implied warranty.
011:         * In no event will the authors be held liable for any damages arising from the
012:         * use of this software.
013:         *
014:         * Permission is granted to anyone to use this software for any purpose,
015:         * including commercial applications, and to alter it and redistribute it
016:         * freely, subject to the following restrictions:
017:         *
018:         *  1. The origin of this software must not be misrepresented; you must not
019:         *     claim that you wrote the original software. If you use this software in
020:         *     a product, an acknowledgment in the product documentation would be
021:         *     appreciated but is not required.
022:         *
023:         *  2. Altered source versions must be plainly marked as such, and must not be
024:         *     misrepresented as being the original software.
025:         *
026:         *  3. This notice may not be removed or altered from any source distribution.
027:         */
028:
029:        package net.n3.nanoxml;
030:
031:        import java.io.IOException;
032:        import java.io.Reader;
033:        import java.util.Stack;
034:
035:        /**
036:         * StdXMLBuilder is a concrete implementation of IXMLBuilder which creates a
037:         * tree of IXMLElement from an XML data source.
038:         *
039:         * @see net.n3.nanoxml.XMLElement
040:         *
041:         * @author Marc De Scheemaecker
042:         * @version $Name:  $, $Revision: 1.17 $
043:         */
044:        public class StdXMLBuilder implements  IXMLBuilder {
045:
046:            /**
047:             * This stack contains the current element and its parents.
048:             */
049:            private Stack stack;
050:
051:            /**
052:             * The root element of the parsed XML tree.
053:             */
054:            private IXMLElement root;
055:
056:            /**
057:             * Prototype element for creating the tree.
058:             */
059:            private IXMLElement prototype;
060:
061:            /**
062:             * Creates the builder.
063:             */
064:            public StdXMLBuilder() {
065:                this (new XMLElement());
066:            }
067:
068:            /**
069:             * Creates the builder.
070:             *
071:             * @param prototype the prototype to use when building the tree.
072:             */
073:            public StdXMLBuilder(IXMLElement prototype) {
074:                this .stack = null;
075:                this .root = null;
076:                this .prototype = prototype;
077:            }
078:
079:            /**
080:             * Cleans up the object when it's destroyed.
081:             */
082:            protected void finalize() throws Throwable {
083:                this .prototype = null;
084:                this .root = null;
085:                this .stack.removeAllElements();
086:                this .stack = null;
087:                super .finalize();
088:            }
089:
090:            /**
091:             * This method is called before the parser starts processing its input.
092:             *
093:             * @param systemID the system ID of the XML data source.
094:             * @param lineNr   the line on which the parsing starts.
095:             */
096:            public void startBuilding(String systemID, int lineNr) {
097:                this .stack = new Stack();
098:                this .root = null;
099:            }
100:
101:            /**
102:             * This method is called when a processing instruction is encountered.
103:             * PIs with target "xml" are handled by the parser.
104:             *
105:             * @param target the PI target.
106:             * @param reader to read the data from the PI.
107:             */
108:            public void newProcessingInstruction(String target, Reader reader) {
109:                // nothing to do
110:            }
111:
112:            /**
113:             * This method is called when a new XML element is encountered.
114:             *
115:             * @see #endElement
116:             *
117:             * @param name       the name of the element.
118:             * @param nsPrefix   the prefix used to identify the namespace. If no
119:             *                   namespace has been specified, this parameter is null.
120:             * @param nsURI      the URI associated with the namespace. If no
121:             *                   namespace has been specified, or no URI is
122:             *                   associated with nsPrefix, this parameter is null.
123:             * @param systemID   the system ID of the XML data source.
124:             * @param lineNr     the line in the source where the element starts.
125:             */
126:            public void startElement(String name, String nsPrefix,
127:                    String nsURI, String systemID, int lineNr) {
128:                String fullName = name;
129:
130:                if (nsPrefix != null) {
131:                    fullName = nsPrefix + ':' + name;
132:                }
133:
134:                IXMLElement elt = this .prototype.createElement(fullName, nsURI,
135:                        systemID, lineNr);
136:
137:                if (this .stack.empty()) {
138:                    this .root = elt;
139:                } else {
140:                    IXMLElement top = (IXMLElement) this .stack.peek();
141:                    top.addChild(elt);
142:                }
143:
144:                this .stack.push(elt);
145:            }
146:
147:            /**
148:             * This method is called when the attributes of an XML element have been
149:             * processed.
150:             *
151:             * @see #startElement
152:             * @see #addAttribute
153:             *
154:             * @param name       the name of the element.
155:             * @param nsPrefix   the prefix used to identify the namespace. If no
156:             *                   namespace has been specified, this parameter is null.
157:             * @param nsURI      the URI associated with the namespace. If no
158:             *                   namespace has been specified, or no URI is
159:             *                   associated with nsPrefix, this parameter is null.
160:             */
161:            public void elementAttributesProcessed(String name,
162:                    String nsPrefix, String nsURI) {
163:                // nothing to do
164:            }
165:
166:            /**
167:             * This method is called when the end of an XML elemnt is encountered.
168:             *
169:             * @see #startElement
170:             *
171:             * @param name       the name of the element.
172:             * @param nsPrefix   the prefix used to identify the namespace. If no
173:             *                   namespace has been specified, this parameter is null.
174:             * @param nsURI      the URI associated with the namespace. If no
175:             *                   namespace has been specified, or no URI is
176:             *                   associated with nsPrefix, this parameter is null.
177:             */
178:            public void endElement(String name, String nsPrefix, String nsURI) {
179:                IXMLElement elt = (IXMLElement) this .stack.pop();
180:
181:                if (elt.getChildrenCount() == 1) {
182:                    IXMLElement child = elt.getChildAtIndex(0);
183:
184:                    if (child.getName() == null) {
185:                        elt.setContent(child.getContent());
186:                        elt.removeChildAtIndex(0);
187:                    }
188:                }
189:            }
190:
191:            /**
192:             * This method is called when a new attribute of an XML element is
193:             * encountered.
194:             *
195:             * @param key        the key (name) of the attribute.
196:             * @param nsPrefix   the prefix used to identify the namespace. If no
197:             *                   namespace has been specified, this parameter is null.
198:             * @param nsURI      the URI associated with the namespace. If no
199:             *                   namespace has been specified, or no URI is
200:             *                   associated with nsPrefix, this parameter is null.
201:             * @param value      the value of the attribute.
202:             * @param type       the type of the attribute. If no type is known,
203:             *                   "CDATA" is returned.
204:             *
205:             * @throws java.lang.Exception
206:             *     If an exception occurred while processing the event.
207:             */
208:            public void addAttribute(String key, String nsPrefix, String nsURI,
209:                    String value, String type) throws Exception {
210:                String fullName = key;
211:
212:                if (nsPrefix != null) {
213:                    fullName = nsPrefix + ':' + key;
214:                }
215:
216:                IXMLElement top = (IXMLElement) this .stack.peek();
217:
218:                if (top.hasAttribute(fullName)) {
219:                    throw new XMLParseException(top.getSystemID(), top
220:                            .getLineNr(), "Duplicate attribute: " + key);
221:                }
222:
223:                if (nsPrefix != null) {
224:                    top.setAttribute(fullName, nsURI, value);
225:                } else {
226:                    top.setAttribute(fullName, value);
227:                }
228:            }
229:
230:            /**
231:             * This method is called when a PCDATA element is encountered. A Java
232:             * reader is supplied from which you can read the data. The reader will
233:             * only read the data of the element. You don't need to check for
234:             * boundaries. If you don't read the full element, the rest of the data
235:             * is skipped. You also don't have to care about entities; they are
236:             * resolved by the parser.
237:             *
238:             * @param reader   the Java reader from which you can retrieve the data.
239:             * @param systemID the system ID of the XML data source.
240:             * @param lineNr   the line in the source where the element starts.
241:             */
242:            public void addPCData(Reader reader, String systemID, int lineNr) {
243:                int bufSize = 2048;
244:                int sizeRead = 0;
245:                StringBuffer str = new StringBuffer(bufSize);
246:                char[] buf = new char[bufSize];
247:
248:                for (;;) {
249:                    if (sizeRead >= bufSize) {
250:                        bufSize *= 2;
251:                        str.ensureCapacity(bufSize);
252:                    }
253:
254:                    int size;
255:
256:                    try {
257:                        size = reader.read(buf);
258:                    } catch (IOException e) {
259:                        break;
260:                    }
261:
262:                    if (size < 0) {
263:                        break;
264:                    }
265:
266:                    str.append(buf, 0, size);
267:                    sizeRead += size;
268:                }
269:
270:                IXMLElement elt = this .prototype.createElement(null, systemID,
271:                        lineNr);
272:                elt.setContent(str.toString());
273:
274:                if (!this .stack.empty()) {
275:                    IXMLElement top = (IXMLElement) this .stack.peek();
276:                    top.addChild(elt);
277:                }
278:            }
279:
280:            /**
281:             * Returns the result of the building process. This method is called just
282:             * before the <I>parse</I> method of IXMLParser returns.
283:             *
284:             * @see net.n3.nanoxml.IXMLParser#parse
285:             *
286:             * @return the result of the building process.
287:             */
288:            public Object getResult() {
289:                return this.root;
290:            }
291:
292:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.