src/java.xml/share/classes/com/sun/xml/internal/stream/XMLOutputFactoryImpl.java
changeset 57915 8570f22b9b6a
parent 48412 d4412e380f6b
equal deleted inserted replaced
57914:9c98f8788762 57915:8570f22b9b6a
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.xml.internal.stream;
    26 package com.sun.xml.internal.stream;
    27 
    27 
    28 import java.io.File;
       
    29 import java.io.FileWriter;
       
    30 import java.io.IOException;
    28 import java.io.IOException;
    31 import java.io.OutputStream;
    29 import java.io.OutputStream;
    32 import java.io.Writer;
    30 import java.io.Writer;
    33 
    31 import javax.xml.stream.XMLEventWriter;
    34 import javax.xml.stream.XMLOutputFactory ;
    32 import javax.xml.stream.XMLOutputFactory;
    35 import javax.xml.stream.XMLStreamException;
    33 import javax.xml.stream.XMLStreamException;
       
    34 import javax.xml.stream.XMLStreamWriter;
    36 import javax.xml.transform.Result;
    35 import javax.xml.transform.Result;
    37 import javax.xml.transform.dom.DOMResult;
    36 import javax.xml.transform.dom.DOMResult;
    38 import javax.xml.transform.stream.StreamResult;
    37 import javax.xml.transform.stream.StreamResult;
    39 import javax.xml.transform.stax.StAXResult;
    38 import javax.xml.transform.stax.StAXResult;
    40 import com.sun.org.apache.xerces.internal.impl.Constants;
    39 import com.sun.org.apache.xerces.internal.impl.Constants;
    41 import com.sun.org.apache.xerces.internal.impl.PropertyManager;
    40 import com.sun.org.apache.xerces.internal.impl.PropertyManager;
    42 
       
    43 import com.sun.xml.internal.stream.writers.XMLDOMWriterImpl;
    41 import com.sun.xml.internal.stream.writers.XMLDOMWriterImpl;
    44 import com.sun.xml.internal.stream.writers.XMLEventWriterImpl;
    42 import com.sun.xml.internal.stream.writers.XMLEventWriterImpl;
    45 import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
    43 import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
    46 
    44 
    47 /**
    45 /**
    48  * This class provides the implementation of XMLOutputFactory.
    46  * This class provides the implementation of XMLOutputFactory.
    49  *
    47  *
    50  * @author  Neeraj Bajaj,
    48  * @author Neeraj Bajaj,
    51  * @author k venugopal
    49  * @author k venugopal
    52  */
    50  */
    53 public class XMLOutputFactoryImpl extends XMLOutputFactory {
    51 public class XMLOutputFactoryImpl extends XMLOutputFactory {
    54 
    52 
    55     //List of supported properties and default values.
    53     //List of supported properties and default values.
    61     /**
    59     /**
    62      * TODO: at the current time, XMLStreamWriters are not Thread safe.
    60      * TODO: at the current time, XMLStreamWriters are not Thread safe.
    63      */
    61      */
    64     boolean fReuseInstance = false;
    62     boolean fReuseInstance = false;
    65 
    63 
    66     /** Creates a new instance of XMLOutputFactory */
    64     /**
       
    65      * Creates a new instance of XMLOutputFactory
       
    66      */
    67     public XMLOutputFactoryImpl() {
    67     public XMLOutputFactoryImpl() {
    68     }
    68     }
    69 
    69 
    70     public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.OutputStream outputStream) throws javax.xml.stream.XMLStreamException {
    70     public XMLEventWriter createXMLEventWriter(OutputStream outputStream)
    71         return createXMLEventWriter(outputStream,  null);
    71             throws XMLStreamException {
    72     }
    72         return createXMLEventWriter(outputStream, null);
    73 
    73     }
    74     public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.OutputStream outputStream, String encoding) throws javax.xml.stream.XMLStreamException {
    74 
       
    75     public XMLEventWriter createXMLEventWriter(OutputStream outputStream, String encoding)
       
    76             throws XMLStreamException {
    75         return new XMLEventWriterImpl(createXMLStreamWriter(outputStream, encoding));
    77         return new XMLEventWriterImpl(createXMLStreamWriter(outputStream, encoding));
    76     }
    78     }
    77 
    79 
    78     public javax.xml.stream.XMLEventWriter createXMLEventWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {
    80     public XMLEventWriter createXMLEventWriter(Result result)
    79 
    81             throws XMLStreamException {
    80         if (result instanceof StAXResult && ((StAXResult)result).getXMLEventWriter() != null)
    82 
    81             return ((StAXResult)result).getXMLEventWriter();
    83         if (result instanceof StAXResult && ((StAXResult) result).getXMLEventWriter() != null) {
       
    84             return ((StAXResult) result).getXMLEventWriter();
       
    85         }
    82 
    86 
    83         return new XMLEventWriterImpl(createXMLStreamWriter(result));
    87         return new XMLEventWriterImpl(createXMLStreamWriter(result));
    84     }
    88     }
    85 
    89 
    86     public javax.xml.stream.XMLEventWriter createXMLEventWriter(java.io.Writer writer) throws javax.xml.stream.XMLStreamException {
    90     public XMLEventWriter createXMLEventWriter(java.io.Writer writer)
       
    91             throws XMLStreamException {
    87         return new XMLEventWriterImpl(createXMLStreamWriter(writer));
    92         return new XMLEventWriterImpl(createXMLStreamWriter(writer));
    88     }
    93     }
    89 
    94 
    90     public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException {
    95     public XMLStreamWriter createXMLStreamWriter(Result result)
       
    96             throws XMLStreamException {
    91 
    97 
    92         if (result instanceof StreamResult) {
    98         if (result instanceof StreamResult) {
    93             return createXMLStreamWriter((StreamResult) result, null);
    99             return createXMLStreamWriter((StreamResult) result, null);
    94         } else if (result instanceof DOMResult) {
   100         } else if (result instanceof DOMResult) {
    95             return new XMLDOMWriterImpl((DOMResult) result);
   101             return new XMLDOMWriterImpl((DOMResult) result);
    96         } else if (result instanceof StAXResult) {
   102         } else if (result instanceof StAXResult) {
    97             if (((StAXResult) result).getXMLStreamWriter() != null) {
   103             if (((StAXResult) result).getXMLStreamWriter() != null) {
    98                 return ((StAXResult) result).getXMLStreamWriter();
   104                 return ((StAXResult) result).getXMLStreamWriter();
    99             } else {
   105             } else {
   100                 throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported");
   106                 throw new UnsupportedOperationException(
   101             }
   107                         "Result of type " + result + " is not supported");
       
   108             }
       
   109         } else if (result.getSystemId() != null) {
       
   110             //this is not correct impl of SAXResult. Keep it for now for compatibility
       
   111             return createXMLStreamWriter(new StreamResult(result.getSystemId()));
   102         } else {
   112         } else {
   103             if (result.getSystemId() !=null) {
   113             throw new UnsupportedOperationException(
   104                 //this is not correct impl of SAXResult. Keep it for now for compatibility
   114                     "Result of type " + result + " is not supported. Supported result "
   105                 return createXMLStreamWriter(new StreamResult(result.getSystemId()));
   115                             + "types are: DOMResult, StAXResult and StreamResult.");
   106             } else {
   116         }
   107                 throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " +
   117 
   108                         "Supported result types are: DOMResult, StAXResult and StreamResult.");
   118     }
   109             }
   119 
   110         }
   120     public XMLStreamWriter createXMLStreamWriter(java.io.Writer writer)
   111 
   121             throws XMLStreamException {
   112     }
   122         return createXMLStreamWriter(toStreamResult(null, writer, null), null);
   113 
   123     }
   114     public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.Writer writer) throws javax.xml.stream.XMLStreamException {
   124 
   115         return createXMLStreamWriter(toStreamResult(null, writer, null) , null);
   125     public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream)
   116     }
   126             throws XMLStreamException {
   117 
       
   118     public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.OutputStream outputStream) throws javax.xml.stream.XMLStreamException {
       
   119         return createXMLStreamWriter(outputStream, null);
   127         return createXMLStreamWriter(outputStream, null);
   120     }
   128     }
   121 
   129 
   122     public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(java.io.OutputStream outputStream, String encoding) throws javax.xml.stream.XMLStreamException {
   130     public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream, String encoding)
   123         return createXMLStreamWriter(toStreamResult(outputStream, null, null) , encoding);
   131             throws XMLStreamException {
   124     }
   132         return createXMLStreamWriter(toStreamResult(outputStream, null, null), encoding);
   125 
   133     }
   126     public Object getProperty(String name) throws java.lang.IllegalArgumentException {
   134 
   127         if(name == null){
   135     public Object getProperty(String name)
       
   136             throws IllegalArgumentException {
       
   137         if (name == null) {
   128             throw new IllegalArgumentException("Property not supported");
   138             throw new IllegalArgumentException("Property not supported");
   129         }
   139         }
   130         if(fPropertyManager.containsProperty(name))
   140         if (fPropertyManager.containsProperty(name)) {
   131             return fPropertyManager.getProperty(name);
   141             return fPropertyManager.getProperty(name);
       
   142         }
   132         throw new IllegalArgumentException("Property not supported");
   143         throw new IllegalArgumentException("Property not supported");
   133     }
   144     }
   134 
   145 
   135     public boolean isPropertySupported(String name) {
   146     public boolean isPropertySupported(String name) {
   136         if(name == null){
   147         if (name == null) {
   137             return false ;
   148             return false;
   138         }
   149         } else {
   139         else{
       
   140             return fPropertyManager.containsProperty(name);
   150             return fPropertyManager.containsProperty(name);
   141         }
   151         }
   142     }
   152     }
   143 
   153 
   144     public void setProperty(String name, Object value) throws java.lang.IllegalArgumentException {
   154     public void setProperty(String name, Object value)
   145         if(name == null || value == null || !fPropertyManager.containsProperty(name) ){
   155             throws IllegalArgumentException {
   146             throw new IllegalArgumentException("Property "+name+"is not supported");
   156         if (name == null || value == null || !fPropertyManager.containsProperty(name)) {
   147         }
   157             throw new IllegalArgumentException("Property " + name + "is not supported");
   148         if(name == Constants.REUSE_INSTANCE || name.equals(Constants.REUSE_INSTANCE)){
   158         }
   149             fReuseInstance = ((Boolean)value).booleanValue();
   159         if (name == Constants.REUSE_INSTANCE || name.equals(Constants.REUSE_INSTANCE)) {
   150             if(DEBUG)System.out.println("fReuseInstance is set to " + fReuseInstance);
   160             fReuseInstance = (Boolean)value;
       
   161             if (DEBUG) {
       
   162                 System.out.println("fReuseInstance is set to " + fReuseInstance);
       
   163             }
   151 
   164 
   152             // TODO: XMLStreamWriters are not Thread safe,
   165             // TODO: XMLStreamWriters are not Thread safe,
   153             // don't let application think it is optimizing
   166             // don't let application think it is optimizing
   154             if (fReuseInstance) {
   167             if (fReuseInstance) {
   155                 throw new IllegalArgumentException(
   168                 throw new IllegalArgumentException(
   156                         "Property "
   169                         "Property "
   157                         + name
   170                         + name
   158                         + " is not supported: XMLStreamWriters are not Thread safe");
   171                         + " is not supported: XMLStreamWriters are not Thread safe");
   159             }
   172             }
   160         }else{//for any other property set the flag
   173         } else {//for any other property set the flag
   161             //REVISIT: Even in this case instance can be reused, by passing PropertyManager
   174             //REVISIT: Even in this case instance can be reused, by passing PropertyManager
   162             fPropertyChanged = true;
   175             fPropertyChanged = true;
   163         }
   176         }
   164         fPropertyManager.setProperty(name,value);
   177         fPropertyManager.setProperty(name, value);
   165     }
   178     }
   166 
   179 
   167     /** StreamResult object is re-used and the values are set appropriately.
   180     /**
   168      */
   181      * StreamResult object is re-used and the values are set appropriately.
   169     StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
   182      */
       
   183     StreamResult toStreamResult(OutputStream os, Writer writer, String systemId) {
   170         StreamResult sr = new StreamResult();
   184         StreamResult sr = new StreamResult();
   171         sr.setOutputStream(os);
   185         sr.setOutputStream(os);
   172         sr.setWriter(writer);
   186         sr.setWriter(writer);
   173         sr.setSystemId(systemId);
   187         sr.setSystemId(systemId);
   174         return sr;
   188         return sr;
   175     }
   189     }
   176 
   190 
   177     javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.stream.StreamResult sr, String encoding) throws javax.xml.stream.XMLStreamException {
   191     XMLStreamWriter createXMLStreamWriter(StreamResult sr, String encoding)
       
   192             throws XMLStreamException {
   178         //if factory is configured to reuse the instance & this instance can be reused
   193         //if factory is configured to reuse the instance & this instance can be reused
   179         //& the setProperty() hasn't been called
   194         //& the setProperty() hasn't been called
   180         try{
   195         try {
   181             if(fReuseInstance && fStreamWriter != null && fStreamWriter.canReuse() && !fPropertyChanged){
   196             if (fReuseInstance && fStreamWriter != null && fStreamWriter.canReuse()
       
   197                     && !fPropertyChanged) {
   182                 fStreamWriter.reset();
   198                 fStreamWriter.reset();
   183                 fStreamWriter.setOutput(sr, encoding);
   199                 fStreamWriter.setOutput(sr, encoding);
   184                 if(DEBUG)System.out.println("reusing instance, object id : " + fStreamWriter);
   200                 if (DEBUG) {
       
   201                     System.out.println("reusing instance, object id : " + fStreamWriter);
       
   202                 }
   185                 return fStreamWriter;
   203                 return fStreamWriter;
   186             }
   204             }
   187             return fStreamWriter = new XMLStreamWriterImpl(sr, encoding, new PropertyManager(fPropertyManager));
   205             return fStreamWriter = new XMLStreamWriterImpl(sr, encoding,
   188         }catch(java.io.IOException io){
   206                     new PropertyManager(fPropertyManager));
       
   207         } catch (IOException io) {
   189             throw new XMLStreamException(io);
   208             throw new XMLStreamException(io);
   190         }
   209         }
   191     }//createXMLStreamWriter(StreamResult,String)
   210     }//createXMLStreamWriter(StreamResult,String)
   192 
   211 
   193     private static final boolean DEBUG = false;
   212     private static final boolean DEBUG = false;
   194 
   213 
   195     /** This flag indicates the change of property. If true,
   214     /**
       
   215      * This flag indicates the change of property. If true,
   196      * <code>PropertyManager</code> should be passed when creating
   216      * <code>PropertyManager</code> should be passed when creating
   197      * <code>XMLStreamWriterImpl</code> */
   217      * <code>XMLStreamWriterImpl</code>
   198     private boolean fPropertyChanged ;
   218      */
       
   219     private boolean fPropertyChanged;
   199 }//XMLOutputFactory
   220 }//XMLOutputFactory