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.staxutils;
019:
020: import java.util.ArrayList;
021:
022: import javax.xml.namespace.NamespaceContext;
023: import javax.xml.namespace.QName;
024: import javax.xml.stream.XMLStreamException;
025:
026: import org.w3c.dom.Attr;
027: import org.w3c.dom.CDATASection;
028: import org.w3c.dom.Comment;
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031: import org.w3c.dom.EntityReference;
032: import org.w3c.dom.NamedNodeMap;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.Text;
035:
036: import org.apache.cxf.helpers.DOMUtils;
037: import org.apache.cxf.staxutils.AbstractDOMStreamReader.ElementFrame;
038:
039: public class W3CDOMStreamReader extends AbstractDOMStreamReader {
040: private Node content;
041:
042: private Document document;
043:
044: private W3CNamespaceContext context;
045:
046: /**
047: * @param element
048: */
049: public W3CDOMStreamReader(Element element) {
050: super (new ElementFrame(element, null));
051: newFrame(getCurrentFrame());
052:
053: this .document = element.getOwnerDocument();
054:
055: }
056:
057: /**
058: * Get the document associated with this stream.
059: *
060: * @return
061: */
062: public Document getDocument() {
063: return document;
064: }
065:
066: /**
067: * Find name spaces declaration in atrributes and move them to separate
068: * collection.
069: */
070: @Override
071: protected final void newFrame(ElementFrame frame) {
072: Element element = getCurrentElement();
073: frame.uris = new ArrayList<String>();
074: frame.prefixes = new ArrayList<String>();
075: frame.attributes = new ArrayList<Object>();
076:
077: if (context == null) {
078: context = new W3CNamespaceContext();
079: }
080:
081: context.setElement(element);
082:
083: NamedNodeMap nodes = element.getAttributes();
084:
085: String ePrefix = element.getPrefix();
086: if (ePrefix == null) {
087: ePrefix = "";
088: }
089:
090: if (nodes != null) {
091: for (int i = 0; i < nodes.getLength(); i++) {
092: Node node = nodes.item(i);
093: String prefix = node.getPrefix();
094: String localName = node.getLocalName();
095: String value = node.getNodeValue();
096: String name = node.getNodeName();
097:
098: if (prefix == null) {
099: prefix = "";
100: }
101:
102: if (name != null && "xmlns".equals(name)) {
103: frame.uris.add(value);
104: frame.prefixes.add("");
105: } else if (prefix.length() > 0
106: && "xmlns".equals(prefix)) {
107: frame.uris.add(value);
108: frame.prefixes.add(localName);
109: } else if (name.startsWith("xmlns:")) {
110: prefix = name.substring(6);
111: frame.uris.add(value);
112: frame.prefixes.add(prefix);
113: } else {
114: frame.attributes.add(node);
115: }
116: }
117: }
118: }
119:
120: @Override
121: protected void endElement() {
122: super .endElement();
123: }
124:
125: public final Element getCurrentElement() {
126: return (Element) getCurrentFrame().element;
127: }
128:
129: @Override
130: protected ElementFrame getChildFrame(int currentChild) {
131: return new ElementFrame(getCurrentElement().getChildNodes()
132: .item(currentChild), getCurrentFrame());
133: }
134:
135: @Override
136: protected int getChildCount() {
137: return getCurrentElement().getChildNodes().getLength();
138: }
139:
140: @Override
141: protected int moveToChild(int currentChild) {
142: this .content = getCurrentElement().getChildNodes().item(
143: currentChild);
144:
145: if (content instanceof Text) {
146: return CHARACTERS;
147: } else if (content instanceof Element) {
148: return START_ELEMENT;
149: } else if (content instanceof CDATASection) {
150: return CDATA;
151: } else if (content instanceof Comment) {
152: return CHARACTERS;
153: } else if (content instanceof EntityReference) {
154: return ENTITY_REFERENCE;
155: }
156:
157: throw new IllegalStateException();
158: }
159:
160: @Override
161: public String getElementText() throws XMLStreamException {
162: String result = DOMUtils.getContent(content);
163:
164: ElementFrame frame = getCurrentFrame();
165: frame.ended = true;
166: currentEvent = END_ELEMENT;
167: endElement();
168:
169: // we should not return null according to the StAx API javadoc
170: return result != null ? result : "";
171: }
172:
173: @Override
174: public String getNamespaceURI(String prefix) {
175: ElementFrame frame = getCurrentFrame();
176:
177: while (null != frame) {
178: int index = frame.prefixes.indexOf(prefix);
179: if (index != -1) {
180: return frame.uris.get(index);
181: }
182:
183: frame = frame.parent;
184: }
185:
186: return null;
187: }
188:
189: public String getAttributeValue(String ns, String local) {
190: Attr at;
191: if (ns == null || ns.equals("")) {
192: at = getCurrentElement().getAttributeNode(local);
193: } else {
194: at = getCurrentElement().getAttributeNodeNS(ns, local);
195: }
196:
197: if (at == null) {
198: return null;
199: }
200:
201: return DOMUtils.getContent(at);
202: }
203:
204: public int getAttributeCount() {
205: return getCurrentFrame().attributes.size();
206: }
207:
208: Attr getAttribute(int i) {
209: return (Attr) getCurrentFrame().attributes.get(i);
210: }
211:
212: private String getLocalName(Attr attr) {
213:
214: String name = attr.getLocalName();
215: if (name == null) {
216: name = attr.getNodeName();
217: }
218: return name;
219: }
220:
221: public QName getAttributeName(int i) {
222: Attr at = getAttribute(i);
223:
224: String prefix = at.getPrefix();
225: String ln = getLocalName(at);
226: // at.getNodeName();
227: String ns = at.getNamespaceURI();
228:
229: if (prefix == null) {
230: return new QName(ns, ln);
231: } else {
232: return new QName(ns, ln, prefix);
233: }
234: }
235:
236: public String getAttributeNamespace(int i) {
237: return getAttribute(i).getNamespaceURI();
238: }
239:
240: public String getAttributeLocalName(int i) {
241: Attr attr = getAttribute(i);
242: return getLocalName(attr);
243: }
244:
245: public String getAttributePrefix(int i) {
246: return getAttribute(i).getPrefix();
247: }
248:
249: public String getAttributeType(int i) {
250: return toStaxType(getAttribute(i).getNodeType());
251: }
252:
253: public static String toStaxType(short jdom) {
254: switch (jdom) {
255: default:
256: return null;
257: }
258: }
259:
260: public String getAttributeValue(int i) {
261: return getAttribute(i).getValue();
262: }
263:
264: public boolean isAttributeSpecified(int i) {
265: return getAttribute(i).getValue() != null;
266: }
267:
268: public int getNamespaceCount() {
269: return getCurrentFrame().prefixes.size();
270: }
271:
272: public String getNamespacePrefix(int i) {
273: return getCurrentFrame().prefixes.get(i);
274: }
275:
276: public String getNamespaceURI(int i) {
277: return getCurrentFrame().uris.get(i);
278: }
279:
280: public NamespaceContext getNamespaceContext() {
281: return context;
282: }
283:
284: public String getText() {
285: return DOMUtils.getRawContent(getCurrentElement());
286: }
287:
288: public char[] getTextCharacters() {
289: return getText().toCharArray();
290: }
291:
292: public int getTextStart() {
293: return 0;
294: }
295:
296: public int getTextLength() {
297: return getText().length();
298: }
299:
300: public String getEncoding() {
301: return null;
302: }
303:
304: public QName getName() {
305: Element el = getCurrentElement();
306:
307: String prefix = getPrefix();
308: String ln = getLocalName();
309:
310: if (prefix == null) {
311: return new QName(el.getNamespaceURI(), ln);
312: } else {
313: return new QName(el.getNamespaceURI(), ln, prefix);
314: }
315: }
316:
317: public String getLocalName() {
318: String ln = getCurrentElement().getLocalName();
319: if (ln == null) {
320: ln = getCurrentElement().getNodeName();
321: }
322: return ln;
323: }
324:
325: public String getNamespaceURI() {
326: String ln = getCurrentElement().getLocalName();
327: if (ln == null) {
328: ln = getCurrentElement().getNodeName();
329: if (ln.indexOf(":") == -1) {
330: ln = getNamespaceURI("");
331: } else {
332: ln = getNamespaceURI(ln.substring(0, ln.indexOf(":")));
333: }
334: return ln;
335: }
336: return getCurrentElement().getNamespaceURI();
337: }
338:
339: public String getPrefix() {
340: String prefix = getCurrentElement().getPrefix();
341: if (prefix == null) {
342: prefix = "";
343: }
344: return prefix;
345: }
346:
347: public String getPITarget() {
348: throw new UnsupportedOperationException();
349: }
350:
351: public String getPIData() {
352: throw new UnsupportedOperationException();
353: }
354: }
|