Source Code Cross Referenced for ElementWriter.java in  » Web-Services-apache-cxf-2.0.1 » databinding » org » apache » cxf » aegis » xml » stax » 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 » Web Services apache cxf 2.0.1 » databinding » org.apache.cxf.aegis.xml.stax 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */package org.apache.cxf.aegis.xml.stax;
019:
020:        import java.io.OutputStream;
021:
022:        import javax.xml.namespace.QName;
023:        import javax.xml.stream.XMLOutputFactory;
024:        import javax.xml.stream.XMLStreamException;
025:        import javax.xml.stream.XMLStreamWriter;
026:
027:        import org.apache.cxf.aegis.DatabindingException;
028:        import org.apache.cxf.aegis.util.NamespaceHelper;
029:        import org.apache.cxf.aegis.xml.AbstractMessageWriter;
030:        import org.apache.cxf.aegis.xml.MessageWriter;
031:
032:        /**
033:         * LiteralWriter
034:         * 
035:         * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
036:         */
037:        public class ElementWriter extends AbstractMessageWriter implements 
038:                MessageWriter {
039:            private XMLStreamWriter writer;
040:
041:            private String namespace;
042:
043:            private String name;
044:
045:            private String prefix;
046:
047:            /**
048:             * Create a LiteralWriter but without writing an element name.
049:             * 
050:             * @param writer
051:             */
052:            public ElementWriter(XMLStreamWriter writer) {
053:                this .writer = writer;
054:            }
055:
056:            public ElementWriter(XMLStreamWriter writer, String name,
057:                    String namespace) {
058:                this (writer, name, namespace, null);
059:            }
060:
061:            public ElementWriter(XMLStreamWriter streamWriter, QName name) {
062:                this (streamWriter, name.getLocalPart(), name.getNamespaceURI());
063:            }
064:
065:            public ElementWriter(XMLStreamWriter writer, String name,
066:                    String namespace, String prefix) {
067:                this .writer = writer;
068:                this .namespace = namespace;
069:                this .name = name;
070:                this .prefix = prefix;
071:
072:                try {
073:                    writeStartElement();
074:                } catch (XMLStreamException e) {
075:                    throw new DatabindingException("Error writing document.", e);
076:                }
077:            }
078:
079:            /**
080:             * @param os
081:             * @throws XMLStreamException
082:             */
083:            public ElementWriter(OutputStream os, String name, String namespace)
084:                    throws XMLStreamException {
085:                XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
086:                this .writer = ofactory.createXMLStreamWriter(os);
087:
088:                this .namespace = namespace;
089:                this .name = name;
090:
091:                try {
092:                    writeStartElement();
093:                } catch (XMLStreamException e) {
094:                    throw new DatabindingException("Error writing document.", e);
095:                }
096:            }
097:
098:            private void writeStartElement() throws XMLStreamException {
099:                if (namespace != null) {
100:                    boolean declare = false;
101:
102:                    String decPrefix = writer.getNamespaceContext().getPrefix(
103:                            namespace);
104:
105:                    // If the user didn't specify a prefix, create one
106:                    if (prefix == null && decPrefix == null) {
107:                        declare = true;
108:                        prefix = NamespaceHelper.getUniquePrefix(writer);
109:                    } else if (prefix == null) {
110:                        prefix = decPrefix;
111:                    } else if (!prefix.equals(decPrefix)) {
112:                        declare = true;
113:                    }
114:
115:                    writer.writeStartElement(prefix, name, namespace);
116:
117:                    if (declare) {
118:                        writer.setPrefix(prefix, namespace);
119:                        writer.writeNamespace(prefix, namespace);
120:                    }
121:                } else {
122:                    writer.writeStartElement(name);
123:                }
124:            }
125:
126:            /**
127:             * @see org.apache.cxf.aegis.xml.MessageWriter#writeValue(java.lang.Object)
128:             */
129:            public void writeValue(Object value) {
130:                try {
131:                    if (value != null) {
132:                        writer.writeCharacters(value.toString());
133:                    }
134:                } catch (XMLStreamException e) {
135:                    throw new DatabindingException("Error writing document.", e);
136:                }
137:            }
138:
139:            /**
140:             * @see org.apache.cxf.aegis.xml.MessageWriter#getWriter(java.lang.String)
141:             */
142:            public MessageWriter getElementWriter(String nm) {
143:                return new ElementWriter(writer, nm, namespace);
144:            }
145:
146:            public MessageWriter getElementWriter(String nm, String ns) {
147:                return new ElementWriter(writer, nm, ns);
148:            }
149:
150:            public MessageWriter getElementWriter(QName qname) {
151:                return new ElementWriter(writer, qname.getLocalPart(), qname
152:                        .getNamespaceURI(), qname.getPrefix());
153:            }
154:
155:            public String getNamespace() {
156:                return namespace;
157:            }
158:
159:            public void close() {
160:                try {
161:                    writer.writeEndElement();
162:                } catch (XMLStreamException e) {
163:                    throw new DatabindingException("Error writing document.", e);
164:                }
165:            }
166:
167:            public void flush() throws XMLStreamException {
168:                writer.flush();
169:            }
170:
171:            public XMLStreamWriter getXMLStreamWriter() {
172:                return writer;
173:            }
174:
175:            public MessageWriter getAttributeWriter(String nm) {
176:                return new AttributeWriter(writer, nm, namespace);
177:            }
178:
179:            public MessageWriter getAttributeWriter(String nm, String ns) {
180:                return new AttributeWriter(writer, nm, ns);
181:            }
182:
183:            public MessageWriter getAttributeWriter(QName qname) {
184:                return new AttributeWriter(writer, qname.getLocalPart(), qname
185:                        .getNamespaceURI());
186:            }
187:
188:            public String getPrefixForNamespace(String ns) {
189:                try {
190:                    String pfx = writer.getPrefix(ns);
191:
192:                    if (pfx == null) {
193:                        pfx = NamespaceHelper.getUniquePrefix(writer);
194:
195:                        writer.setPrefix(pfx, ns);
196:                        writer.writeNamespace(pfx, ns);
197:                    }
198:
199:                    return prefix;
200:                } catch (XMLStreamException e) {
201:                    throw new DatabindingException("Error writing document.", e);
202:                }
203:            }
204:
205:            public String getPrefixForNamespace(String ns, String hint) {
206:                try {
207:                    String pfx = writer.getPrefix(ns);
208:
209:                    if (pfx == null) {
210:                        String ns2 = writer.getNamespaceContext()
211:                                .getNamespaceURI(hint);
212:                        if (ns2 == null) {
213:                            pfx = hint;
214:                        } else if (ns2.equals(ns)) {
215:                            return pfx;
216:                        } else {
217:                            pfx = NamespaceHelper.getUniquePrefix(writer);
218:                        }
219:
220:                        writer.setPrefix(pfx, ns);
221:                        writer.writeNamespace(pfx, ns);
222:                    }
223:
224:                    return pfx;
225:                } catch (XMLStreamException e) {
226:                    throw new DatabindingException("Error writing document.", e);
227:                }
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.