1 /* |
1 /* |
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
52 */ |
52 */ |
53 final class SourceUtils { |
53 final class SourceUtils { |
54 |
54 |
55 int srcType; |
55 int srcType; |
56 |
56 |
57 private final int domSource = 1; |
57 private static final int domSource = 1; |
58 private final int streamSource = 2; |
58 private static final int streamSource = 2; |
59 private final int saxSource=4; |
59 private static final int saxSource=4; |
60 |
60 |
61 public SourceUtils(Source src) { |
61 public SourceUtils(Source src) { |
62 if(src instanceof StreamSource){ |
62 if(src instanceof StreamSource){ |
63 srcType = streamSource; |
63 srcType = streamSource; |
64 }else if(src instanceof DOMSource){ |
64 }else if(src instanceof DOMSource){ |
93 public QName sniff(Source src, RootElementSniffer sniffer){ |
93 public QName sniff(Source src, RootElementSniffer sniffer){ |
94 String localName = null; |
94 String localName = null; |
95 String namespaceUri = null; |
95 String namespaceUri = null; |
96 |
96 |
97 if(isDOMSource()){ |
97 if(isDOMSource()){ |
98 DOMSource domSource = (DOMSource)src; |
98 DOMSource domSrc = (DOMSource)src; |
99 Node n = domSource.getNode(); |
99 Node n = domSrc.getNode(); |
100 if(n.getNodeType()== Node.DOCUMENT_NODE) { |
100 if(n.getNodeType()== Node.DOCUMENT_NODE) { |
101 n = ((Document)n).getDocumentElement(); |
101 n = ((Document)n).getDocumentElement(); |
102 } |
102 } |
103 localName = n.getLocalName(); |
103 localName = n.getLocalName(); |
104 namespaceUri = n.getNamespaceURI(); |
104 namespaceUri = n.getNamespaceURI(); |
144 writer.writeStartElement(localName); |
144 writer.writeStartElement(localName); |
145 } else { |
145 } else { |
146 writer.writeStartElement(uri, localName); |
146 writer.writeStartElement(uri, localName); |
147 } |
147 } |
148 } else { |
148 } else { |
149 assert uri != null; |
149 // assert uri != null; |
150 |
150 |
151 if(prefix.length() > 0){ |
151 if(prefix.length() > 0){ |
152 /** |
152 /** |
153 * Before we write the |
153 * Before we write the |
154 */ |
154 */ |
155 String writerURI = null; |
155 String writerURI = null; |
156 if (writer.getNamespaceContext() != null) |
156 if (writer.getNamespaceContext() != null) { |
157 writerURI = writer.getNamespaceContext().getNamespaceURI(prefix); |
157 writerURI = writer.getNamespaceContext().getNamespaceURI(prefix); |
|
158 } |
158 String writerPrefix = writer.getPrefix(uri); |
159 String writerPrefix = writer.getPrefix(uri); |
159 if(declarePrefix(prefix, uri, writerPrefix, writerURI)){ |
160 if(declarePrefix(prefix, uri, writerPrefix, writerURI)){ |
160 writer.writeStartElement(prefix, localName, uri); |
161 writer.writeStartElement(prefix, localName, uri); |
161 writer.setPrefix(prefix, uri != null ? uri : ""); |
162 writer.setPrefix(prefix, uri != null ? uri : ""); |
162 writer.writeNamespace(prefix, uri); |
163 writer.writeNamespace(prefix, uri); |
170 |
171 |
171 int n = reader.getNamespaceCount(); |
172 int n = reader.getNamespaceCount(); |
172 // Write namespace declarations |
173 // Write namespace declarations |
173 for (int i = 0; i < n; i++) { |
174 for (int i = 0; i < n; i++) { |
174 String nsPrefix = reader.getNamespacePrefix(i); |
175 String nsPrefix = reader.getNamespacePrefix(i); |
175 if (nsPrefix == null) nsPrefix = ""; |
176 if (nsPrefix == null) { |
|
177 nsPrefix = ""; |
|
178 } |
176 // StAX returns null for default ns |
179 // StAX returns null for default ns |
177 String writerURI = null; |
180 String writerURI = null; |
178 if (writer.getNamespaceContext() != null) |
181 if (writer.getNamespaceContext() != null) { |
179 writerURI = writer.getNamespaceContext().getNamespaceURI(nsPrefix); |
182 writerURI = writer.getNamespaceContext().getNamespaceURI(nsPrefix); |
|
183 } |
180 |
184 |
181 // Zephyr: Why is this returning null? |
185 // Zephyr: Why is this returning null? |
182 // Compare nsPrefix with prefix because of [1] (above) |
186 // Compare nsPrefix with prefix because of [1] (above) |
183 String readerURI = reader.getNamespaceURI(i); |
187 String readerURI = reader.getNamespaceURI(i); |
184 |
188 |
213 case XMLStreamConstants.END_ELEMENT: |
217 case XMLStreamConstants.END_ELEMENT: |
214 writer.writeEndElement(); |
218 writer.writeEndElement(); |
215 break; |
219 break; |
216 case XMLStreamConstants.CHARACTERS: |
220 case XMLStreamConstants.CHARACTERS: |
217 writer.writeCharacters(reader.getText()); |
221 writer.writeCharacters(reader.getText()); |
|
222 break; |
|
223 default: |
|
224 break; |
218 } |
225 } |
219 } while (state != XMLStreamConstants.END_DOCUMENT); |
226 } while (state != XMLStreamConstants.END_DOCUMENT); |
220 reader.close(); |
227 reader.close(); |
221 } |
228 } |
222 |
229 |
226 * @param writer |
233 * @param writer |
227 * @throws XMLStreamException |
234 * @throws XMLStreamException |
228 */ |
235 */ |
229 private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException { |
236 private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException { |
230 String writerURI = null; |
237 String writerURI = null; |
231 if (writer.getNamespaceContext() != null) |
238 if (writer.getNamespaceContext() != null) { |
232 writerURI = writer.getNamespaceContext().getNamespaceURI(prefix); |
239 writerURI = writer.getNamespaceContext().getNamespaceURI(prefix); |
|
240 } |
233 |
241 |
234 if (writerURI == null) { |
242 if (writerURI == null) { |
235 writer.setPrefix(prefix, readerURI != null ? readerURI : ""); |
243 writer.setPrefix(prefix, readerURI != null ? readerURI : ""); |
236 writer.writeNamespace(prefix, readerURI != null ? readerURI : ""); |
244 writer.writeNamespace(prefix, readerURI != null ? readerURI : ""); |
237 } |
245 } |
244 * @param wPrefix |
252 * @param wPrefix |
245 * @param wUri |
253 * @param wUri |
246 */ |
254 */ |
247 private static boolean declarePrefix(String rPrefix, String rUri, String wPrefix, String wUri){ |
255 private static boolean declarePrefix(String rPrefix, String rUri, String wPrefix, String wUri){ |
248 if (wUri == null ||((wPrefix != null) && !rPrefix.equals(wPrefix))|| |
256 if (wUri == null ||((wPrefix != null) && !rPrefix.equals(wPrefix))|| |
249 (rUri != null && !wUri.equals(rUri))) |
257 (rUri != null && !wUri.equals(rUri))) { |
250 return true; |
258 return true; |
|
259 } |
251 return false; |
260 return false; |
252 } |
261 } |
253 } |
262 } |