From 3ac2d800d840f03618fc364090d786effde84b1f Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:13:16 +0000 Subject: Moving 1.x branches git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835142 13f79535-47bb-0310-9956-ffa450edef68 --- .../jaxb/axiom/ext/XMLStreamWriterWithOS.java | 246 +++++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-20080910/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/XMLStreamWriterWithOS.java (limited to 'sca-java-1.x/branches/sca-java-20080910/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/XMLStreamWriterWithOS.java') diff --git a/sca-java-1.x/branches/sca-java-20080910/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/XMLStreamWriterWithOS.java b/sca-java-1.x/branches/sca-java-20080910/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/XMLStreamWriterWithOS.java new file mode 100644 index 0000000000..44e34c3647 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-20080910/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/XMLStreamWriterWithOS.java @@ -0,0 +1,246 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.jaxb.axiom.ext; + +import java.io.OutputStream; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.axiom.om.util.StAXUtils; + +/** + * XMLStreamReader that exposes direct access to the OutputStream. + * Writing to the output stream is faster in some cases. + */ +public class XMLStreamWriterWithOS implements XMLStreamWriter { + private XMLStreamWriter writer; + private String charSetEncoding; + private OutputStream os; + + public XMLStreamWriterWithOS(OutputStream os, String charSetEncoding) throws XMLStreamException { + super(); + writer = null; // Writer is created when needed + this.os = os; + this.charSetEncoding = charSetEncoding; + } + + /** + * The writer is created lazily. + * If only the output stream is used, then the writer is never created. + */ + private void createWriter() throws XMLStreamException { + if (writer == null) { + writer = StAXUtils.createXMLStreamWriter(os, charSetEncoding); + } + } + + public void close() throws XMLStreamException { + if (writer != null) { + writer.close(); + } + } + + public void flush() throws XMLStreamException { + if (writer != null) { + writer.flush(); + } + } + + public NamespaceContext getNamespaceContext() { + try { + createWriter(); + } catch (Exception e) { + throw new RuntimeException(e); + } + return writer.getNamespaceContext(); + } + + public String getPrefix(String arg0) throws XMLStreamException { + createWriter(); + return writer.getPrefix(arg0); + } + + public Object getProperty(String arg0) throws IllegalArgumentException { + try { + createWriter(); + } catch (XMLStreamException e) { + throw new IllegalArgumentException(e); + } + return writer.getProperty(arg0); + } + + public void setDefaultNamespace(String arg0) throws XMLStreamException { + createWriter(); + writer.setDefaultNamespace(arg0); + } + + public void setNamespaceContext(NamespaceContext arg0) throws XMLStreamException { + createWriter(); + writer.setNamespaceContext(arg0); + } + + public void setPrefix(String arg0, String arg1) throws XMLStreamException { + createWriter(); + writer.setPrefix(arg0, arg1); + } + + public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException { + createWriter(); + writer.writeAttribute(arg0, arg1, arg2, arg3); + } + + public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException { + createWriter(); + writer.writeAttribute(arg0, arg1, arg2); + } + + public void writeAttribute(String arg0, String arg1) throws XMLStreamException { + createWriter(); + writer.writeAttribute(arg0, arg1); + } + + public void writeCData(String arg0) throws XMLStreamException { + createWriter(); + writer.writeCData(arg0); + } + + public void writeCharacters(char[] arg0, int arg1, int arg2) throws XMLStreamException { + createWriter(); + writer.writeCharacters(arg0, arg1, arg2); + } + + public void writeCharacters(String arg0) throws XMLStreamException { + createWriter(); + writer.writeCharacters(arg0); + } + + public void writeComment(String arg0) throws XMLStreamException { + createWriter(); + writer.writeComment(arg0); + } + + public void writeDefaultNamespace(String arg0) throws XMLStreamException { + createWriter(); + writer.writeDefaultNamespace(arg0); + } + + public void writeDTD(String arg0) throws XMLStreamException { + createWriter(); + writer.writeDTD(arg0); + } + + public void writeEmptyElement(String arg0, String arg1, String arg2) throws XMLStreamException { + createWriter(); + writer.writeEmptyElement(arg0, arg1, arg2); + } + + public void writeEmptyElement(String arg0, String arg1) throws XMLStreamException { + createWriter(); + writer.writeEmptyElement(arg0, arg1); + } + + public void writeEmptyElement(String arg0) throws XMLStreamException { + createWriter(); + writer.writeEmptyElement(arg0); + } + + public void writeEndDocument() throws XMLStreamException { + createWriter(); + writer.writeEndDocument(); + } + + public void writeEndElement() throws XMLStreamException { + createWriter(); + writer.writeEndElement(); + } + + public void writeEntityRef(String arg0) throws XMLStreamException { + createWriter(); + writer.writeEntityRef(arg0); + } + + public void writeNamespace(String arg0, String arg1) throws XMLStreamException { + createWriter(); + writer.writeNamespace(arg0, arg1); + } + + public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException { + createWriter(); + writer.writeProcessingInstruction(arg0, arg1); + } + + public void writeProcessingInstruction(String arg0) throws XMLStreamException { + createWriter(); + writer.writeProcessingInstruction(arg0); + } + + public void writeStartDocument() throws XMLStreamException { + createWriter(); + writer.writeStartDocument(); + } + + public void writeStartDocument(String arg0, String arg1) throws XMLStreamException { + createWriter(); + writer.writeStartDocument(arg0, arg1); + } + + public void writeStartDocument(String arg0) throws XMLStreamException { + createWriter(); + writer.writeStartDocument(arg0); + } + + public void writeStartElement(String arg0, String arg1, String arg2) throws XMLStreamException { + createWriter(); + writer.writeStartElement(arg0, arg1, arg2); + } + + public void writeStartElement(String arg0, String arg1) throws XMLStreamException { + createWriter(); + writer.writeStartElement(arg0, arg1); + } + + public void writeStartElement(String arg0) throws XMLStreamException { + createWriter(); + writer.writeStartElement(arg0); + } + + /** + * If this XMLStreamWriter is connected to an OutputStream + * then the OutputStream is returned. This allows a node + * (perhaps an OMSourcedElement) to write its content + * directly to the OutputStream. + * @return OutputStream or null + */ + public OutputStream getOutputStream() throws XMLStreamException { + + if (os != null) { + // Flush the state of the writer..Many times the + // write defers the writing of tag characters (>) + // until the next write. Flush out this character + if (writer != null) { + this.writeCharacters(""); + this.flush(); + } + } + return os; + } +} -- cgit v1.2.3