aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.stream.buffer.stax; aoqi@0: aoqi@0: import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; aoqi@0: import com.sun.xml.internal.org.jvnet.staxex.Base64Data; aoqi@0: import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx; aoqi@0: import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx; aoqi@0: aoqi@0: import javax.activation.DataHandler; aoqi@0: import javax.xml.namespace.NamespaceContext; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import java.io.OutputStream; aoqi@0: aoqi@0: /** aoqi@0: * {@link XMLStreamWriter} that fills {@link MutableXMLStreamBuffer}. aoqi@0: *

aoqi@0: * TODO: need to retain all attributes/namespaces and then store all namespaces aoqi@0: * before the attributes. Currently it is necessary for the caller to ensure aoqi@0: * all namespaces are written before attributes and the caller must not intermix aoqi@0: * calls to the writeNamespace and writeAttribute methods. aoqi@0: * aoqi@0: */ aoqi@0: public class StreamWriterBufferCreator extends StreamBufferCreator implements XMLStreamWriterEx { aoqi@0: private final NamespaceContexHelper namespaceContext = new NamespaceContexHelper(); aoqi@0: aoqi@0: /** aoqi@0: * Nesting depth of the element. aoqi@0: * This field is ultimately used to keep track of the # of trees we created in aoqi@0: * the buffer. aoqi@0: */ aoqi@0: private int depth=0; aoqi@0: aoqi@0: public StreamWriterBufferCreator() { aoqi@0: setXMLStreamBuffer(new MutableXMLStreamBuffer()); aoqi@0: } aoqi@0: aoqi@0: public StreamWriterBufferCreator(MutableXMLStreamBuffer buffer) { aoqi@0: setXMLStreamBuffer(buffer); aoqi@0: } aoqi@0: aoqi@0: // XMLStreamWriter aoqi@0: aoqi@0: public Object getProperty(String str) throws IllegalArgumentException { aoqi@0: return null; //return null for all the property names instead of aoqi@0: //throwing unsupported operation exception. aoqi@0: } aoqi@0: aoqi@0: public void close() throws XMLStreamException { aoqi@0: } aoqi@0: aoqi@0: public void flush() throws XMLStreamException { aoqi@0: } aoqi@0: aoqi@0: public NamespaceContextEx getNamespaceContext() { aoqi@0: return namespaceContext; aoqi@0: } aoqi@0: aoqi@0: public void setNamespaceContext(NamespaceContext namespaceContext) throws XMLStreamException { aoqi@0: /* aoqi@0: * It is really unclear from the JavaDoc how to implement this method. aoqi@0: */ aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: public void setDefaultNamespace(String namespaceURI) throws XMLStreamException { aoqi@0: setPrefix("", namespaceURI); aoqi@0: } aoqi@0: aoqi@0: public void setPrefix(String prefix, String namespaceURI) throws XMLStreamException { aoqi@0: namespaceContext.declareNamespace(prefix, namespaceURI); aoqi@0: } aoqi@0: aoqi@0: public String getPrefix(String namespaceURI) throws XMLStreamException { aoqi@0: return namespaceContext.getPrefix(namespaceURI); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public void writeStartDocument() throws XMLStreamException { aoqi@0: writeStartDocument("", ""); aoqi@0: } aoqi@0: aoqi@0: public void writeStartDocument(String version) throws XMLStreamException { aoqi@0: writeStartDocument("", ""); aoqi@0: } aoqi@0: aoqi@0: public void writeStartDocument(String encoding, String version) throws XMLStreamException { aoqi@0: namespaceContext.resetContexts(); aoqi@0: aoqi@0: storeStructure(T_DOCUMENT); aoqi@0: } aoqi@0: aoqi@0: public void writeEndDocument() throws XMLStreamException { aoqi@0: storeStructure(T_END); aoqi@0: } aoqi@0: aoqi@0: public void writeStartElement(String localName) throws XMLStreamException { aoqi@0: namespaceContext.pushContext(); aoqi@0: depth++; aoqi@0: aoqi@0: final String defaultNamespaceURI = namespaceContext.getNamespaceURI(""); aoqi@0: aoqi@0: if (defaultNamespaceURI == null) aoqi@0: storeQualifiedName(T_ELEMENT_LN, null, null, localName); aoqi@0: else aoqi@0: storeQualifiedName(T_ELEMENT_LN, null, defaultNamespaceURI, localName); aoqi@0: } aoqi@0: aoqi@0: public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { aoqi@0: namespaceContext.pushContext(); aoqi@0: depth++; aoqi@0: aoqi@0: final String prefix = namespaceContext.getPrefix(namespaceURI); aoqi@0: if (prefix == null) { aoqi@0: throw new XMLStreamException(); aoqi@0: } aoqi@0: aoqi@0: namespaceContext.pushContext(); aoqi@0: storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName); aoqi@0: } aoqi@0: aoqi@0: public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { aoqi@0: namespaceContext.pushContext(); aoqi@0: depth++; aoqi@0: aoqi@0: storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName); aoqi@0: } aoqi@0: aoqi@0: public void writeEmptyElement(String localName) throws XMLStreamException { aoqi@0: writeStartElement(localName); aoqi@0: writeEndElement(); aoqi@0: } aoqi@0: aoqi@0: public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { aoqi@0: writeStartElement(namespaceURI, localName); aoqi@0: writeEndElement(); aoqi@0: } aoqi@0: aoqi@0: public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { aoqi@0: writeStartElement(prefix, localName, namespaceURI); aoqi@0: writeEndElement(); aoqi@0: } aoqi@0: aoqi@0: public void writeEndElement() throws XMLStreamException { aoqi@0: namespaceContext.popContext(); aoqi@0: aoqi@0: storeStructure(T_END); aoqi@0: if(--depth==0) aoqi@0: increaseTreeCount(); aoqi@0: } aoqi@0: aoqi@0: public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException { aoqi@0: storeNamespaceAttribute(null, namespaceURI); aoqi@0: } aoqi@0: aoqi@0: public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException { aoqi@0: if ("xmlns".equals(prefix)) aoqi@0: prefix = null; aoqi@0: storeNamespaceAttribute(prefix, namespaceURI); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public void writeAttribute(String localName, String value) throws XMLStreamException { aoqi@0: storeAttribute(null, null, localName, "CDATA", value); aoqi@0: } aoqi@0: aoqi@0: public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException { aoqi@0: final String prefix = namespaceContext.getPrefix(namespaceURI); aoqi@0: if (prefix == null) { aoqi@0: // TODO aoqi@0: throw new XMLStreamException(); aoqi@0: } aoqi@0: aoqi@0: writeAttribute(prefix, namespaceURI, localName, value); aoqi@0: } aoqi@0: aoqi@0: public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException { aoqi@0: storeAttribute(prefix, namespaceURI, localName, "CDATA", value); aoqi@0: } aoqi@0: aoqi@0: public void writeCData(String data) throws XMLStreamException { aoqi@0: storeStructure(T_TEXT_AS_STRING); aoqi@0: storeContentString(data); aoqi@0: } aoqi@0: aoqi@0: public void writeCharacters(String charData) throws XMLStreamException { aoqi@0: storeStructure(T_TEXT_AS_STRING); aoqi@0: storeContentString(charData); aoqi@0: } aoqi@0: aoqi@0: public void writeCharacters(char[] buf, int start, int len) throws XMLStreamException { aoqi@0: storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, buf, start, len); aoqi@0: } aoqi@0: aoqi@0: public void writeComment(String str) throws XMLStreamException { aoqi@0: storeStructure(T_COMMENT_AS_STRING); aoqi@0: storeContentString(str); aoqi@0: } aoqi@0: aoqi@0: public void writeDTD(String str) throws XMLStreamException { aoqi@0: // not support. just ignore. aoqi@0: } aoqi@0: aoqi@0: public void writeEntityRef(String str) throws XMLStreamException { aoqi@0: storeStructure(T_UNEXPANDED_ENTITY_REFERENCE); aoqi@0: storeContentString(str); aoqi@0: } aoqi@0: aoqi@0: public void writeProcessingInstruction(String target) throws XMLStreamException { aoqi@0: writeProcessingInstruction(target, ""); aoqi@0: } aoqi@0: aoqi@0: public void writeProcessingInstruction(String target, String data) throws XMLStreamException { aoqi@0: storeProcessingInstruction(target, data); aoqi@0: } aoqi@0: aoqi@0: // XMLStreamWriterEx aoqi@0: aoqi@0: public void writePCDATA(CharSequence charSequence) throws XMLStreamException { aoqi@0: if (charSequence instanceof Base64Data) { aoqi@0: storeStructure(T_TEXT_AS_OBJECT); aoqi@0: storeContentObject(((Base64Data)charSequence).clone()); aoqi@0: } else { aoqi@0: writeCharacters(charSequence.toString()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void writeBinary(byte[] bytes, int offset, int length, String endpointURL) throws XMLStreamException { aoqi@0: Base64Data d = new Base64Data(); aoqi@0: byte b[] = new byte[length]; aoqi@0: System.arraycopy(bytes, offset, b, 0, length); aoqi@0: d.set(b, length, null, true); aoqi@0: storeStructure(T_TEXT_AS_OBJECT); aoqi@0: storeContentObject(d); aoqi@0: } aoqi@0: aoqi@0: public void writeBinary(DataHandler dataHandler) throws XMLStreamException { aoqi@0: Base64Data d = new Base64Data(); aoqi@0: d.set(dataHandler); aoqi@0: storeStructure(T_TEXT_AS_OBJECT); aoqi@0: storeContentObject(d); aoqi@0: } aoqi@0: aoqi@0: public OutputStream writeBinary(String endpointURL) throws XMLStreamException { aoqi@0: // TODO aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: }