001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.axiom;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import javax.xml.namespace.QName;
023: import javax.xml.transform.Source;
024:
025: import org.apache.axiom.om.OMAttribute;
026: import org.apache.axiom.om.OMElement;
027: import org.apache.axiom.om.OMException;
028: import org.apache.axiom.om.OMNamespace;
029: import org.apache.axiom.soap.SOAPFactory;
030: import org.springframework.util.Assert;
031: import org.springframework.ws.soap.SoapElement;
032: import org.springframework.xml.transform.StaxSource;
033:
034: /**
035: * Axiom-specific version of {@link SoapElement}.
036: *
037: * @author Arjen Poutsma
038: * @since 1.0.0
039: */
040: class AxiomSoapElement implements SoapElement {
041:
042: private final OMElement axiomElement;
043:
044: private final SOAPFactory axiomFactory;
045:
046: protected AxiomSoapElement(OMElement axiomElement,
047: SOAPFactory axiomFactory) {
048: Assert.notNull(axiomElement, "axiomElement must not be null");
049: Assert.notNull(axiomFactory, "axiomFactory must not be null");
050: this .axiomElement = axiomElement;
051: this .axiomFactory = axiomFactory;
052: }
053:
054: public final QName getName() {
055: try {
056: return axiomElement.getQName();
057: } catch (OMException ex) {
058: throw new AxiomSoapElementException(ex);
059: }
060: }
061:
062: public final Source getSource() {
063: try {
064: return new StaxSource(axiomElement.getXMLStreamReader());
065:
066: } catch (OMException ex) {
067: throw new AxiomSoapElementException(ex);
068: }
069: }
070:
071: public final void addAttribute(QName name, String value) {
072: try {
073: OMNamespace namespace = getAxiomFactory()
074: .createOMNamespace(name.getNamespaceURI(),
075: name.getPrefix());
076: OMAttribute attribute = getAxiomFactory()
077: .createOMAttribute(name.getLocalPart(), namespace,
078: value);
079: getAxiomElement().addAttribute(attribute);
080: } catch (OMException ex) {
081: throw new AxiomSoapElementException(ex);
082: }
083: }
084:
085: public void removeAttribute(QName name) {
086: try {
087: OMAttribute attribute = getAxiomElement()
088: .getAttribute(name);
089: if (attribute != null) {
090: getAxiomElement().removeAttribute(attribute);
091: }
092: } catch (OMException ex) {
093: throw new AxiomSoapElementException(ex);
094: }
095: }
096:
097: public final String getAttributeValue(QName name) {
098: try {
099: return getAxiomElement().getAttributeValue(name);
100: } catch (OMException ex) {
101: throw new AxiomSoapElementException(ex);
102: }
103: }
104:
105: public final Iterator getAllAttibutes() {
106: try {
107: List results = new ArrayList();
108: for (Iterator iterator = getAxiomElement()
109: .getAllAttributes(); iterator.hasNext();) {
110: OMAttribute attribute = (OMAttribute) iterator.next();
111: results.add(attribute.getQName());
112: }
113: return results.iterator();
114:
115: } catch (OMException ex) {
116: throw new AxiomSoapElementException(ex);
117: }
118: }
119:
120: protected final OMElement getAxiomElement() {
121: return axiomElement;
122: }
123:
124: protected final SOAPFactory getAxiomFactory() {
125: return axiomFactory;
126: }
127: }
|