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 javax.xml.namespace.QName;
021: import javax.xml.stream.XMLStreamWriter;
022:
023: import org.apache.cxf.aegis.util.NamespaceHelper;
024: import org.apache.cxf.aegis.xml.AbstractMessageWriter;
025: import org.apache.cxf.aegis.xml.MessageWriter;
026: import org.jdom.Attribute;
027: import org.jdom.Element;
028: import org.jdom.Namespace;
029:
030: public class JDOMWriter extends AbstractMessageWriter {
031: private Element element;
032:
033: public JDOMWriter(Element element) {
034: this .element = element;
035: }
036:
037: public void writeValue(Object value) {
038: element.addContent(value.toString());
039: }
040:
041: public void writeValue(Object value, String ns, String attr) {
042: String prefix = NamespaceHelper.getUniquePrefix(element, ns);
043:
044: element.setAttribute(new Attribute(attr, value.toString(),
045: Namespace.getNamespace(prefix, ns)));
046: }
047:
048: public MessageWriter getElementWriter(String name) {
049: return getElementWriter(name, element.getNamespaceURI());
050: }
051:
052: public MessageWriter getElementWriter(String name, String namespace) {
053: String prefix = NamespaceHelper.getUniquePrefix(element,
054: namespace);
055:
056: Element child = new Element(name, Namespace.getNamespace(
057: prefix, namespace));
058: element.addContent(child);
059:
060: return new JDOMWriter(child);
061: }
062:
063: public MessageWriter getElementWriter(QName qname) {
064: return getElementWriter(qname.getLocalPart(), qname
065: .getNamespaceURI());
066: }
067:
068: public String getPrefixForNamespace(String namespace) {
069: return NamespaceHelper.getUniquePrefix(element, namespace);
070: }
071:
072: public XMLStreamWriter getXMLStreamWriter() {
073: throw new UnsupportedOperationException(
074: "Stream writing not supported from a JDOMWriter.");
075: }
076:
077: public String getPrefixForNamespace(String namespace, String hint) {
078: // todo: this goes for the option of ignoring the hint - we should
079: // probably at least attempt to honour it
080: return NamespaceHelper.getUniquePrefix(element, namespace);
081: }
082:
083: public MessageWriter getAttributeWriter(String name) {
084: Attribute att = new Attribute(name, "", element.getNamespace());
085: element.setAttribute(att);
086: return new AttributeWriter(att);
087: }
088:
089: public MessageWriter getAttributeWriter(String name,
090: String namespace) {
091: Attribute att;
092: if (namespace != null && namespace.length() > 0) {
093: String prefix = NamespaceHelper.getUniquePrefix(element,
094: namespace);
095: att = new Attribute(name, "", Namespace.getNamespace(
096: prefix, namespace));
097: } else {
098: att = new Attribute(name, "");
099: }
100:
101: element.setAttribute(att);
102: return new AttributeWriter(att);
103: }
104:
105: public MessageWriter getAttributeWriter(QName qname) {
106: return getAttributeWriter(qname.getLocalPart(), qname
107: .getNamespaceURI());
108: }
109:
110: public void close() {
111: }
112: }
|