286N/A/*
286N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream;
286N/A
286N/Aimport java.io.File;
286N/Aimport java.io.FileWriter;
286N/Aimport java.io.IOException;
286N/Aimport java.io.OutputStream;
286N/Aimport java.io.Writer;
286N/A
286N/Aimport javax.xml.stream.XMLOutputFactory ;
286N/Aimport javax.xml.stream.XMLStreamException;
286N/Aimport javax.xml.transform.Result;
286N/Aimport javax.xml.transform.dom.DOMResult;
286N/Aimport javax.xml.transform.stream.StreamResult;
286N/Aimport javax.xml.transform.stax.StAXResult;
286N/Aimport com.sun.org.apache.xerces.internal.impl.Constants;
286N/Aimport com.sun.org.apache.xerces.internal.impl.PropertyManager;
286N/A
286N/Aimport com.sun.xml.internal.stream.writers.XMLDOMWriterImpl;
286N/Aimport com.sun.xml.internal.stream.writers.XMLEventWriterImpl;
286N/Aimport com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
286N/A
286N/A/**
286N/A * This class provides the implementation of XMLOutputFactory.
286N/A *
286N/A * @author Neeraj Bajaj,
286N/A * @author k.venugopal@sun.com
286N/A */
286N/Apublic class XMLOutputFactoryImpl extends XMLOutputFactory {
286N/A
286N/A //List of supported properties and default values.
286N/A private PropertyManager fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_WRITER);
286N/A
286N/A //cache the instance of XMLStreamWriterImpl
286N/A private XMLStreamWriterImpl fStreamWriter = null;
286N/A
286N/A /**
286N/A * TODO: at the current time, XMLStreamWriters are not Thread safe.
286N/A */
286N/A boolean fReuseInstance = false;
286N/A
286N/A /** Creates a new instance of XMLOutputFactory */
286N/A public XMLOutputFactoryImpl() {
286N/A }
286N/A
286N/A public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.OutputStream outputStream) throws javax.xml.stream.XMLStreamException {
286N/A return createXMLEventWriter(outputStream, null);
286N/A }
286N/A
286N/A public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.OutputStream outputStream, String encoding) throws javax.xml.stream.XMLStreamException {
286N/A return new XMLEventWriterImpl(createXMLStreamWriter(outputStream, encoding));
286N/A }
286N/A
286N/A public javax.xml.stream.XMLEventWriter createXMLEventWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {
286N/A
286N/A if (result instanceof StAXResult && ((StAXResult)result).getXMLEventWriter() != null)
286N/A return ((StAXResult)result).getXMLEventWriter();
286N/A
286N/A return new XMLEventWriterImpl(createXMLStreamWriter(result));
286N/A }
286N/A
286N/A public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.Writer writer) throws javax.xml.stream.XMLStreamException {
286N/A return new XMLEventWriterImpl(createXMLStreamWriter(writer));
286N/A }
286N/A
286N/A public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {
286N/A
286N/A if (result instanceof StreamResult) {
286N/A return createXMLStreamWriter((StreamResult) result, null);
286N/A } else if (result instanceof DOMResult) {
286N/A return new XMLDOMWriterImpl((DOMResult) result);
286N/A } else if (result instanceof StAXResult) {
286N/A if (((StAXResult) result).getXMLStreamWriter() != null) {
286N/A return ((StAXResult) result).getXMLStreamWriter();
286N/A } else {
286N/A throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
286N/A }
286N/A } else {
286N/A if (result.getSystemId() !=null) {
286N/A //this is not correct impl of SAXResult. Keep it for now for compatibility
286N/A return createXMLStreamWriter(new StreamResult(result.getSystemId()));
286N/A } else {
286N/A throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
286N/A "Supported result types are: DOMResult, StAXResult and StreamResult.");
286N/A }
286N/A }
286N/A
286N/A }
286N/A
286N/A public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.Writer writer) throws javax.xml.stream.XMLStreamException {
286N/A return createXMLStreamWriter(toStreamResult(null, writer, null) , null);
286N/A }
286N/A
286N/A public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.OutputStream outputStream) throws javax.xml.stream.XMLStreamException {
286N/A return createXMLStreamWriter(outputStream, null);
286N/A }
286N/A
286N/A public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.OutputStream outputStream, String encoding) throws javax.xml.stream.XMLStreamException {
286N/A return createXMLStreamWriter(toStreamResult(outputStream, null, null) , encoding);
286N/A }
286N/A
286N/A public Object getProperty(String name) throws java.lang.IllegalArgumentException {
286N/A if(name == null){
286N/A throw new IllegalArgumentException("Property not supported");
286N/A }
286N/A if(fPropertyManager.containsProperty(name))
286N/A return fPropertyManager.getProperty(name);
286N/A throw new IllegalArgumentException("Property not supported");
286N/A }
286N/A
286N/A public boolean isPropertySupported(String name) {
286N/A if(name == null){
286N/A return false ;
286N/A }
286N/A else{
286N/A return fPropertyManager.containsProperty(name);
286N/A }
286N/A }
286N/A
286N/A public void setProperty(String name, Object value) throws java.lang.IllegalArgumentException {
286N/A if(name == null || value == null || !fPropertyManager.containsProperty(name) ){
286N/A throw new IllegalArgumentException("Property "+name+"is not supported");
286N/A }
286N/A if(name == Constants.REUSE_INSTANCE || name.equals(Constants.REUSE_INSTANCE)){
286N/A fReuseInstance = ((Boolean)value).booleanValue();
286N/A if(DEBUG)System.out.println("fReuseInstance is set to " + fReuseInstance);
286N/A
286N/A // TODO: XMLStreamWriters are not Thread safe,
286N/A // don't let application think it is optimizing
286N/A if (fReuseInstance) {
286N/A throw new IllegalArgumentException(
286N/A "Property "
286N/A + name
286N/A + " is not supported: XMLStreamWriters are not Thread safe");
286N/A }
286N/A }else{//for any other property set the flag
286N/A //REVISIT: Even in this case instance can be reused, by passing PropertyManager
286N/A fPropertyChanged = true;
286N/A }
286N/A fPropertyManager.setProperty(name,value);
286N/A }
286N/A
286N/A /** StreamResult object is re-used and the values are set appropriately.
286N/A */
286N/A StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
286N/A StreamResult sr = new StreamResult();
286N/A sr.setOutputStream(os);
286N/A sr.setWriter(writer);
286N/A sr.setSystemId(systemId);
286N/A return sr;
286N/A }
286N/A
286N/A javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.stream.StreamResult sr, String encoding) throws javax.xml.stream.XMLStreamException {
286N/A //if factory is configured to reuse the instance & this instance can be reused
286N/A //& the setProperty() hasn't been called
286N/A try{
286N/A if(fReuseInstance && fStreamWriter != null && fStreamWriter.canReuse() && !fPropertyChanged){
286N/A fStreamWriter.reset();
286N/A fStreamWriter.setOutput(sr, encoding);
286N/A if(DEBUG)System.out.println("reusing instance, object id : " + fStreamWriter);
286N/A return fStreamWriter;
286N/A }
286N/A return fStreamWriter = new XMLStreamWriterImpl(sr, encoding, new PropertyManager(fPropertyManager));
286N/A }catch(java.io.IOException io){
286N/A throw new XMLStreamException(io);
286N/A }
286N/A }//createXMLStreamWriter(StreamResult,String)
286N/A
286N/A private static final boolean DEBUG = false;
286N/A
286N/A /** This flag indicates the change of property. If true,
286N/A * <code>PropertyManager</code> should be passed when creating
286N/A * <code>XMLStreamWriterImpl</code> */
286N/A private boolean fPropertyChanged ;
286N/A}//XMLOutputFactory