001: /*
002: * Copyright 2006 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.xml.stream;
018:
019: import java.util.Iterator;
020: import javax.xml.namespace.QName;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.stream.XMLStreamWriter;
023:
024: import org.springframework.util.StringUtils;
025: import org.springframework.xml.namespace.QNameUtils;
026: import org.springframework.xml.namespace.SimpleNamespaceContext;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.Locator;
029:
030: /**
031: * SAX <code>ContentHandler</code> that writes to a <code>XMLStreamWriter</code>.
032: *
033: * @author Arjen Poutsma
034: * @see XMLStreamWriter
035: * @since 1.0.0
036: */
037: public class StaxStreamContentHandler extends
038: AbstractStaxContentHandler {
039:
040: private final XMLStreamWriter streamWriter;
041:
042: /**
043: * Constructs a new instance of the <code>StaxStreamContentHandler</code> that writes to the given
044: * <code>XMLStreamWriter</code>.
045: *
046: * @param streamWriter the stream writer to write to
047: */
048: public StaxStreamContentHandler(XMLStreamWriter streamWriter) {
049: this .streamWriter = streamWriter;
050: }
051:
052: public void setDocumentLocator(Locator locator) {
053: }
054:
055: protected void charactersInternal(char[] ch, int start, int length)
056: throws XMLStreamException {
057: streamWriter.writeCharacters(ch, start, length);
058: }
059:
060: protected void endDocumentInternal() throws XMLStreamException {
061: streamWriter.writeEndDocument();
062: }
063:
064: protected void endElementInternal(QName name,
065: SimpleNamespaceContext namespaceContext)
066: throws XMLStreamException {
067: streamWriter.writeEndElement();
068: }
069:
070: protected void ignorableWhitespaceInternal(char[] ch, int start,
071: int length) throws XMLStreamException {
072: streamWriter.writeCharacters(ch, start, length);
073: }
074:
075: protected void processingInstructionInternal(String target,
076: String data) throws XMLStreamException {
077: streamWriter.writeProcessingInstruction(target, data);
078: }
079:
080: protected void skippedEntityInternal(String name) {
081: }
082:
083: protected void startDocumentInternal() throws XMLStreamException {
084: streamWriter.writeStartDocument();
085: }
086:
087: protected void startElementInternal(QName name,
088: Attributes attributes,
089: SimpleNamespaceContext namespaceContext)
090: throws XMLStreamException {
091: streamWriter.writeStartElement(QNameUtils.getPrefix(name), name
092: .getLocalPart(), name.getNamespaceURI());
093: String defaultNamespaceUri = namespaceContext
094: .getNamespaceURI("");
095: if (StringUtils.hasLength(defaultNamespaceUri)) {
096: streamWriter.writeNamespace("", defaultNamespaceUri);
097: streamWriter.setDefaultNamespace(defaultNamespaceUri);
098: }
099: for (Iterator iterator = namespaceContext.getBoundPrefixes(); iterator
100: .hasNext();) {
101: String prefix = (String) iterator.next();
102: streamWriter.writeNamespace(prefix, namespaceContext
103: .getNamespaceURI(prefix));
104: streamWriter.setPrefix(prefix, namespaceContext
105: .getNamespaceURI(prefix));
106: }
107: for (int i = 0; i < attributes.getLength(); i++) {
108: QName attrName = QNameUtils.toQName(attributes.getURI(i),
109: attributes.getQName(i));
110: String attrPrefix = QNameUtils.getPrefix(attrName);
111: if (!("xmlns".equals(attrName.getLocalPart()) || "xmlns"
112: .equals(attrPrefix))) {
113: streamWriter.writeAttribute(attrPrefix, attrName
114: .getNamespaceURI(), attrName.getLocalPart(),
115: attributes.getValue(i));
116: }
117: }
118: }
119: }
|