Source Code Cross Referenced for XMLEventWriterImpl.java in  » 6.0-JDK-Modules » sjsxp » com » sun » xml » stream » writers » 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 » 6.0 JDK Modules » sjsxp » com.sun.xml.stream.writers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: XMLEventWriterImpl.java,v 1.3 2006/05/12 13:30:52 sunithareddy Exp $
003:         */
004:
005:        /*
006:         * The contents of this file are subject to the terms
007:         * of the Common Development and Distribution License
008:         * (the License).  You may not use this file except in
009:         * compliance with the License.
010:         * 
011:         * You can obtain a copy of the license at
012:         * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013:         * See the License for the specific language governing
014:         * permissions and limitations under the License.
015:         * 
016:         * When distributing Covered Code, include this CDDL
017:         * Header Notice in each file and include the License file
018:         * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019:         * If applicable, add the following below the CDDL Header,
020:         * with the fields enclosed by brackets [] replaced by
021:         * you own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         * 
024:         * [Name of File] [ver.__] [Date]
025:         * 
026:         * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027:         */
028:
029:        package com.sun.xml.stream.writers;
030:
031:        import java.util.Iterator;
032:        import javax.xml.namespace.QName;
033:        import javax.xml.stream.XMLEventWriter;
034:        import javax.xml.stream.XMLStreamException;
035:        import javax.xml.stream.events.Attribute;
036:        import javax.xml.stream.events.Characters;
037:        import javax.xml.stream.events.Comment;
038:        import javax.xml.stream.events.DTD;
039:        import javax.xml.stream.events.EntityReference;
040:        import javax.xml.stream.events.Namespace;
041:        import javax.xml.stream.events.ProcessingInstruction;
042:        import javax.xml.stream.events.StartDocument;
043:        import javax.xml.stream.events.StartElement;
044:        import javax.xml.stream.events.XMLEvent;
045:        import javax.xml.stream.XMLStreamWriter;
046:
047:        /**
048:         *
049:         * @author  Neeraj Bajaj, Sun Microsystems.
050:         *
051:         */
052:        public class XMLEventWriterImpl implements  XMLEventWriter {
053:
054:            //delegate everything to XMLStreamWriter..
055:            private XMLStreamWriter fStreamWriter;
056:            private static final boolean DEBUG = false;
057:
058:            /**
059:             *
060:             * @param streamWriter
061:             */
062:            public XMLEventWriterImpl(XMLStreamWriter streamWriter) {
063:                fStreamWriter = streamWriter;
064:            }
065:
066:            /**
067:             *
068:             * @param xMLEventReader
069:             * @throws XMLStreamException
070:             */
071:            public void add(javax.xml.stream.XMLEventReader xMLEventReader)
072:                    throws javax.xml.stream.XMLStreamException {
073:                if (xMLEventReader == null)
074:                    throw new XMLStreamException(
075:                            "Event reader shouldn't be null");
076:                while (xMLEventReader.hasNext()) {
077:                    add(xMLEventReader.nextEvent());
078:                }
079:            }
080:
081:            /**
082:             *
083:             * @param xMLEvent
084:             * @throws XMLStreamException
085:             */
086:            public void add(javax.xml.stream.events.XMLEvent xMLEvent)
087:                    throws javax.xml.stream.XMLStreamException {
088:                int type = xMLEvent.getEventType();
089:                switch (type) {
090:                case XMLEvent.DTD: {
091:                    DTD dtd = (DTD) xMLEvent;
092:                    if (DEBUG)
093:                        System.out.println("Adding DTD = " + dtd.toString());
094:                    fStreamWriter.writeDTD(dtd.getDocumentTypeDeclaration());
095:                    break;
096:                }
097:                case XMLEvent.START_DOCUMENT: {
098:                    StartDocument startDocument = (StartDocument) xMLEvent;
099:                    if (DEBUG)
100:                        System.out.println("Adding StartDocument = "
101:                                + startDocument.toString());
102:                    try {
103:                        fStreamWriter.writeStartDocument(startDocument
104:                                .getCharacterEncodingScheme(), startDocument
105:                                .getVersion());
106:                    } catch (XMLStreamException e) {
107:                        fStreamWriter.writeStartDocument(startDocument
108:                                .getVersion());
109:                    }
110:                    break;
111:                }
112:                case XMLEvent.START_ELEMENT: {
113:                    StartElement startElement = xMLEvent.asStartElement();
114:                    if (DEBUG)
115:                        System.out.println("Adding startelement = "
116:                                + startElement.toString());
117:                    QName qname = startElement.getName();
118:                    fStreamWriter.writeStartElement(qname.getPrefix(), qname
119:                            .getLocalPart(), qname.getNamespaceURI());
120:
121:                    //getNamespaces() Returns an Iterator of namespaces declared on this element. This Iterator does not contain
122:                    //previously declared namespaces unless they appear on the current START_ELEMENT. Therefore
123:                    //this list may contain redeclared namespaces and duplicate namespace declarations. Use the
124:                    //getNamespaceContext() method to get the current context of namespace declarations.
125:
126:                    //so we should be using getNamespaces() to write namespace declarations for this START_ELEMENT
127:                    Iterator iterator = startElement.getNamespaces();
128:                    while (iterator.hasNext()) {
129:                        Namespace namespace = (Namespace) iterator.next();
130:                        fStreamWriter.writeNamespace(namespace.getPrefix(),
131:                                namespace.getNamespaceURI());
132:                    }
133:                    //REVISIT: What about writing attributes ?
134:                    Iterator attributes = startElement.getAttributes();
135:                    while (attributes.hasNext()) {
136:                        Attribute attribute = (Attribute) attributes.next();
137:                        QName aqname = attribute.getName();
138:                        fStreamWriter.writeAttribute(aqname.getPrefix(), aqname
139:                                .getNamespaceURI(), aqname.getLocalPart(),
140:                                attribute.getValue());
141:                    }
142:                    break;
143:                }
144:                case XMLEvent.NAMESPACE: {
145:                    Namespace namespace = (Namespace) xMLEvent;
146:                    if (DEBUG)
147:                        System.out.println("Adding namespace = "
148:                                + namespace.toString());
149:                    fStreamWriter.writeNamespace(namespace.getPrefix(),
150:                            namespace.getNamespaceURI());
151:                    break;
152:                }
153:                case XMLEvent.COMMENT: {
154:                    Comment comment = (Comment) xMLEvent;
155:                    if (DEBUG)
156:                        System.out.println("Adding comment = "
157:                                + comment.toString());
158:                    fStreamWriter.writeComment(comment.getText());
159:                    break;
160:                }
161:                case XMLEvent.PROCESSING_INSTRUCTION: {
162:                    ProcessingInstruction processingInstruction = (ProcessingInstruction) xMLEvent;
163:                    if (DEBUG)
164:                        System.out.println("Adding processing instruction = "
165:                                + processingInstruction.toString());
166:                    fStreamWriter.writeProcessingInstruction(
167:                            processingInstruction.getTarget(),
168:                            processingInstruction.getData());
169:                    break;
170:                }
171:                case XMLEvent.CHARACTERS: {
172:                    Characters characters = xMLEvent.asCharacters();
173:                    if (DEBUG)
174:                        System.out.println("Adding characters = "
175:                                + characters.toString());
176:                    //check if the CHARACTERS are CDATA
177:                    if (characters.isCData()) {
178:                        fStreamWriter.writeCData(characters.getData());
179:                    } else {
180:                        fStreamWriter.writeCharacters(characters.getData());
181:                    }
182:                    break;
183:                }
184:                case XMLEvent.ENTITY_REFERENCE: {
185:                    EntityReference entityReference = (EntityReference) xMLEvent;
186:                    if (DEBUG)
187:                        System.out.println("Adding Entity Reference = "
188:                                + entityReference.toString());
189:                    fStreamWriter.writeEntityRef(entityReference.getName());
190:                    break;
191:                }
192:                case XMLEvent.ATTRIBUTE: {
193:                    Attribute attribute = (Attribute) xMLEvent;
194:                    if (DEBUG)
195:                        System.out.println("Adding Attribute = "
196:                                + attribute.toString());
197:                    QName qname = attribute.getName();
198:                    fStreamWriter.writeAttribute(qname.getPrefix(), qname
199:                            .getNamespaceURI(), qname.getLocalPart(), attribute
200:                            .getValue());
201:                    break;
202:                }
203:                case XMLEvent.CDATA: {
204:                    //there is no separate CDATA datatype but CDATA event can be reported
205:                    //by using vendor specific CDATA property.
206:                    Characters characters = (Characters) xMLEvent;
207:                    if (DEBUG)
208:                        System.out.println("Adding characters = "
209:                                + characters.toString());
210:                    if (characters.isCData()) {
211:                        fStreamWriter.writeCData(characters.getData());
212:                    }
213:                    break;
214:                }
215:                    //xxx: Why there isn't any event called Notation.
216:                    //case XMLEvent.NOTATION_DECLARATION:{
217:                    //}
218:
219:                case XMLEvent.END_ELEMENT: {
220:                    //we dont need to typecast it.. just call writeEndElement() and fStreamWriter will take care of it.
221:                    //EndElement endElement = (EndElement)xMLEvent;
222:                    fStreamWriter.writeEndElement();
223:                    break;
224:                }
225:                case XMLEvent.END_DOCUMENT: {
226:                    //we dont need to typecast just call writeEndDocument() and fStreamWriter will take care rest.
227:                    //EndDocument endDocument = (EndDocument)xMLEvent;
228:                    fStreamWriter.writeEndDocument();
229:                    break;
230:                }
231:                    //throw new XMLStreamException("Unknown Event type = " + type);
232:                }
233:                ;
234:
235:            }
236:
237:            /**
238:             *
239:             * @throws XMLStreamException
240:             */
241:            public void close() throws javax.xml.stream.XMLStreamException {
242:                fStreamWriter.close();
243:            }
244:
245:            /**
246:             *
247:             * @throws XMLStreamException will inturn call flush on the stream to which data is being
248:             * written.
249:             */
250:            public void flush() throws javax.xml.stream.XMLStreamException {
251:                fStreamWriter.flush();
252:            }
253:
254:            /**
255:             *
256:             * @return
257:             */
258:            public javax.xml.namespace.NamespaceContext getNamespaceContext() {
259:                return fStreamWriter.getNamespaceContext();
260:            }
261:
262:            /**
263:             *
264:             * @param namespaceURI Namespace URI
265:             * @throws XMLStreamException
266:             * @return prefix associated with the URI.
267:             */
268:            public String getPrefix(String namespaceURI)
269:                    throws javax.xml.stream.XMLStreamException {
270:                return fStreamWriter.getPrefix(namespaceURI);
271:            }
272:
273:            /**
274:             *
275:             * @param uri Namespace URI
276:             * @throws XMLStreamException
277:             */
278:            public void setDefaultNamespace(String uri)
279:                    throws javax.xml.stream.XMLStreamException {
280:                fStreamWriter.setDefaultNamespace(uri);
281:            }
282:
283:            /**
284:             *
285:             * @param namespaceContext Namespace Context
286:             * @throws XMLStreamException
287:             */
288:            public void setNamespaceContext(
289:                    javax.xml.namespace.NamespaceContext namespaceContext)
290:                    throws javax.xml.stream.XMLStreamException {
291:                fStreamWriter.setNamespaceContext(namespaceContext);
292:            }
293:
294:            /**
295:             *
296:             * @param prefix namespace prefix associated with the uri.
297:             * @param uri Namespace URI
298:             * @throws XMLStreamException
299:             */
300:            public void setPrefix(String prefix, String uri)
301:                    throws javax.xml.stream.XMLStreamException {
302:                fStreamWriter.setPrefix(prefix, uri);
303:            }
304:
305:        }//XMLEventWriterImpl
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.