Path: blob/jdk8u272-b10-aarch32-20201026/jaxp/src/com/sun/xml/internal/stream/XMLOutputFactoryImpl.java
83408 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.xml.internal.stream;2627import java.io.File;28import java.io.FileWriter;29import java.io.IOException;30import java.io.OutputStream;31import java.io.Writer;3233import javax.xml.stream.XMLOutputFactory ;34import javax.xml.stream.XMLStreamException;35import javax.xml.transform.Result;36import javax.xml.transform.dom.DOMResult;37import javax.xml.transform.stream.StreamResult;38import javax.xml.transform.stax.StAXResult;39import com.sun.org.apache.xerces.internal.impl.Constants;40import com.sun.org.apache.xerces.internal.impl.PropertyManager;4142import com.sun.xml.internal.stream.writers.XMLDOMWriterImpl;43import com.sun.xml.internal.stream.writers.XMLEventWriterImpl;44import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;4546/**47* This class provides the implementation of XMLOutputFactory.48*49* @author Neeraj Bajaj,50* @author [email protected]51*/52public class XMLOutputFactoryImpl extends XMLOutputFactory {5354//List of supported properties and default values.55private PropertyManager fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_WRITER);5657//cache the instance of XMLStreamWriterImpl58private XMLStreamWriterImpl fStreamWriter = null;5960/**61* TODO: at the current time, XMLStreamWriters are not Thread safe.62*/63boolean fReuseInstance = false;6465/** Creates a new instance of XMLOutputFactory */66public XMLOutputFactoryImpl() {67}6869public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.OutputStream outputStream) throws javax.xml.stream.XMLStreamException {70return createXMLEventWriter(outputStream, null);71}7273public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.OutputStream outputStream, String encoding) throws javax.xml.stream.XMLStreamException {74return new XMLEventWriterImpl(createXMLStreamWriter(outputStream, encoding));75}7677public javax.xml.stream.XMLEventWriter createXMLEventWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {7879if (result instanceof StAXResult && ((StAXResult)result).getXMLEventWriter() != null)80return ((StAXResult)result).getXMLEventWriter();8182return new XMLEventWriterImpl(createXMLStreamWriter(result));83}8485public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.Writer writer) throws javax.xml.stream.XMLStreamException {86return new XMLEventWriterImpl(createXMLStreamWriter(writer));87}8889public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {9091if (result instanceof StreamResult) {92return createXMLStreamWriter((StreamResult) result, null);93} else if (result instanceof DOMResult) {94return new XMLDOMWriterImpl((DOMResult) result);95} else if (result instanceof StAXResult) {96if (((StAXResult) result).getXMLStreamWriter() != null) {97return ((StAXResult) result).getXMLStreamWriter();98} else {99throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");100}101} else {102if (result.getSystemId() !=null) {103//this is not correct impl of SAXResult. Keep it for now for compatibility104return createXMLStreamWriter(new StreamResult(result.getSystemId()));105} else {106throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +107"Supported result types are: DOMResult, StAXResult and StreamResult.");108}109}110111}112113public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.Writer writer) throws javax.xml.stream.XMLStreamException {114return createXMLStreamWriter(toStreamResult(null, writer, null) , null);115}116117public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.OutputStream outputStream) throws javax.xml.stream.XMLStreamException {118return createXMLStreamWriter(outputStream, null);119}120121public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.OutputStream outputStream, String encoding) throws javax.xml.stream.XMLStreamException {122return createXMLStreamWriter(toStreamResult(outputStream, null, null) , encoding);123}124125public Object getProperty(String name) throws java.lang.IllegalArgumentException {126if(name == null){127throw new IllegalArgumentException("Property not supported");128}129if(fPropertyManager.containsProperty(name))130return fPropertyManager.getProperty(name);131throw new IllegalArgumentException("Property not supported");132}133134public boolean isPropertySupported(String name) {135if(name == null){136return false ;137}138else{139return fPropertyManager.containsProperty(name);140}141}142143public void setProperty(String name, Object value) throws java.lang.IllegalArgumentException {144if(name == null || value == null || !fPropertyManager.containsProperty(name) ){145throw new IllegalArgumentException("Property "+name+"is not supported");146}147if(name == Constants.REUSE_INSTANCE || name.equals(Constants.REUSE_INSTANCE)){148fReuseInstance = ((Boolean)value).booleanValue();149if(DEBUG)System.out.println("fReuseInstance is set to " + fReuseInstance);150151// TODO: XMLStreamWriters are not Thread safe,152// don't let application think it is optimizing153if (fReuseInstance) {154throw new IllegalArgumentException(155"Property "156+ name157+ " is not supported: XMLStreamWriters are not Thread safe");158}159}else{//for any other property set the flag160//REVISIT: Even in this case instance can be reused, by passing PropertyManager161fPropertyChanged = true;162}163fPropertyManager.setProperty(name,value);164}165166/** StreamResult object is re-used and the values are set appropriately.167*/168StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){169StreamResult sr = new StreamResult();170sr.setOutputStream(os);171sr.setWriter(writer);172sr.setSystemId(systemId);173return sr;174}175176javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.stream.StreamResult sr, String encoding) throws javax.xml.stream.XMLStreamException {177//if factory is configured to reuse the instance & this instance can be reused178//& the setProperty() hasn't been called179try{180if(fReuseInstance && fStreamWriter != null && fStreamWriter.canReuse() && !fPropertyChanged){181fStreamWriter.reset();182fStreamWriter.setOutput(sr, encoding);183if(DEBUG)System.out.println("reusing instance, object id : " + fStreamWriter);184return fStreamWriter;185}186return fStreamWriter = new XMLStreamWriterImpl(sr, encoding, new PropertyManager(fPropertyManager));187}catch(java.io.IOException io){188throw new XMLStreamException(io);189}190}//createXMLStreamWriter(StreamResult,String)191192private static final boolean DEBUG = false;193194/** This flag indicates the change of property. If true,195* <code>PropertyManager</code> should be passed when creating196* <code>XMLStreamWriterImpl</code> */197private boolean fPropertyChanged ;198}//XMLOutputFactory199200201