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.jdom;
019:
020: import java.util.List;
021:
022: import javax.xml.namespace.QName;
023: import javax.xml.stream.XMLStreamReader;
024:
025: import org.apache.cxf.aegis.util.stax.JDOMStreamReader;
026: import org.apache.cxf.aegis.xml.AbstractMessageReader;
027: import org.apache.cxf.aegis.xml.MessageReader;
028: import org.apache.cxf.aegis.xml.stax.AttributeReader;
029: import org.jdom.Attribute;
030: import org.jdom.Element;
031: import org.jdom.Namespace;
032:
033: public class JDOMReader extends AbstractMessageReader implements
034: MessageReader {
035: private Element element;
036: private int currentChild;
037: private int currentAttribute;
038: private List elements;
039: private QName qname;
040:
041: public JDOMReader(Element element) {
042: this .element = element;
043: this .elements = element.getChildren();
044: }
045:
046: public String getValue() {
047: return element.getValue();
048: }
049:
050: public String getValue(String ns, String attr) {
051: return element.getAttributeValue(attr, ns);
052: }
053:
054: public boolean hasMoreElementReaders() {
055: return currentChild < elements.size();
056: }
057:
058: public MessageReader getNextElementReader() {
059: currentChild++;
060: return new JDOMReader((Element) elements.get(currentChild - 1));
061: }
062:
063: public QName getName() {
064: if (qname == null) {
065: qname = new QName(element.getNamespaceURI(), element
066: .getName(), element.getNamespacePrefix());
067: }
068: return qname;
069: }
070:
071: public String getLocalName() {
072: return element.getName();
073: }
074:
075: public String getNamespace() {
076: return element.getNamespaceURI();
077: }
078:
079: @Override
080: public XMLStreamReader getXMLStreamReader() {
081: return new JDOMStreamReader(element);
082: }
083:
084: public boolean hasMoreAttributeReaders() {
085: return currentAttribute < element.getAttributes().size();
086: }
087:
088: public MessageReader getAttributeReader(QName attName) {
089: String value = element.getAttributeValue(
090: attName.getLocalPart(), Namespace.getNamespace(attName
091: .getNamespaceURI()));
092: return new AttributeReader(attName, value);
093: }
094:
095: public MessageReader getNextAttributeReader() {
096: Attribute att = (Attribute) element.getAttributes().get(
097: currentAttribute);
098: currentAttribute++;
099:
100: return new AttributeReader(new QName(att.getNamespaceURI(), att
101: .getName()), att.getValue());
102: }
103:
104: public String getNamespaceForPrefix(String prefix) {
105: Namespace namespace = element.getNamespace(prefix);
106: return null == namespace ? null : namespace.getURI();
107: }
108: }
|