src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/stax/factory/StAXOutputFactory.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/stax/factory/StAXOutputFactory.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,162 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.stax.factory;
    1.32 +
    1.33 +import com.sun.xml.internal.fastinfoset.stax.*;
    1.34 +import com.sun.xml.internal.fastinfoset.stax.events.StAXEventWriter;
    1.35 +import javax.xml.transform.Result;
    1.36 +import javax.xml.stream.XMLOutputFactory ;
    1.37 +import javax.xml.stream.XMLEventWriter;
    1.38 +import javax.xml.stream.XMLStreamWriter;
    1.39 +import javax.xml.stream.XMLStreamException;
    1.40 +import javax.xml.transform.stream.StreamResult;
    1.41 +import java.io.File;
    1.42 +import java.io.FileWriter;
    1.43 +import java.io.IOException;
    1.44 +import java.io.OutputStream;
    1.45 +import java.io.Writer;
    1.46 +import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    1.47 +
    1.48 +public class StAXOutputFactory extends XMLOutputFactory {
    1.49 +
    1.50 +    //List of supported properties and default values.
    1.51 +    private StAXManager _manager = null ;
    1.52 +
    1.53 +    /** Creates a new instance of StAXOutputFactory */
    1.54 +    public StAXOutputFactory() {
    1.55 +        _manager = new StAXManager(StAXManager.CONTEXT_WRITER);
    1.56 +    }
    1.57 +
    1.58 +    public XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException {
    1.59 +        return new StAXEventWriter(createXMLStreamWriter(result));
    1.60 +    }
    1.61 +
    1.62 +    public XMLEventWriter createXMLEventWriter(Writer writer) throws XMLStreamException {
    1.63 +        return new StAXEventWriter(createXMLStreamWriter(writer));
    1.64 +    }
    1.65 +
    1.66 +    public XMLEventWriter createXMLEventWriter(OutputStream outputStream) throws XMLStreamException {
    1.67 +        return new StAXEventWriter(createXMLStreamWriter(outputStream));
    1.68 +    }
    1.69 +
    1.70 +    public XMLEventWriter createXMLEventWriter(OutputStream outputStream, String encoding) throws XMLStreamException {
    1.71 +        return new StAXEventWriter(createXMLStreamWriter(outputStream, encoding));
    1.72 +    }
    1.73 +
    1.74 +    public XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException {
    1.75 +        if (result instanceof StreamResult) {
    1.76 +            StreamResult streamResult = (StreamResult) result;
    1.77 +            if (streamResult.getWriter() != null) {
    1.78 +                return createXMLStreamWriter(streamResult.getWriter());
    1.79 +            } else if (streamResult.getOutputStream() != null) {
    1.80 +                return createXMLStreamWriter(streamResult.getOutputStream());
    1.81 +            } else if (streamResult.getSystemId() != null) {
    1.82 +                FileWriter writer = null;
    1.83 +                boolean isError = true;
    1.84 +
    1.85 +                try {
    1.86 +                    writer = new FileWriter(new File(streamResult.getSystemId()));
    1.87 +                    final XMLStreamWriter streamWriter = createXMLStreamWriter(writer);
    1.88 +                    isError = false;
    1.89 +
    1.90 +                    return streamWriter;
    1.91 +                } catch (IOException ie) {
    1.92 +                    throw new XMLStreamException(ie);
    1.93 +                } finally {
    1.94 +                    if (isError && writer != null) {
    1.95 +                        try {
    1.96 +                            writer.close();
    1.97 +                        } catch (IOException ignored) {
    1.98 +                        }
    1.99 +                    }
   1.100 +                }
   1.101 +            }
   1.102 +        } else {
   1.103 +            FileWriter writer = null;
   1.104 +            boolean isError = true;
   1.105 +
   1.106 +            try {
   1.107 +                //xxx: should we be using FileOutputStream - nb.
   1.108 +                writer = new FileWriter(new File(result.getSystemId()));
   1.109 +                final XMLStreamWriter streamWriter = createXMLStreamWriter(writer);
   1.110 +                isError = false;
   1.111 +
   1.112 +                return streamWriter;
   1.113 +            } catch (IOException ie) {
   1.114 +                throw new XMLStreamException(ie);
   1.115 +            } finally {
   1.116 +                if (isError && writer != null) {
   1.117 +                    try {
   1.118 +                        writer.close();
   1.119 +                    } catch (IOException ignored) {
   1.120 +                    }
   1.121 +                }
   1.122 +            }
   1.123 +        }
   1.124 +        throw new java.lang.UnsupportedOperationException();
   1.125 +    }
   1.126 +
   1.127 +    /** this is assumed that user wants to write the file in xml format
   1.128 +     *
   1.129 +     */
   1.130 +    public XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException {
   1.131 +        throw new java.lang.UnsupportedOperationException();
   1.132 +    }
   1.133 +
   1.134 +    public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream) throws XMLStreamException {
   1.135 +        return new StAXDocumentSerializer(outputStream, new StAXManager(_manager));
   1.136 +    }
   1.137 +
   1.138 +    public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream, String encoding) throws XMLStreamException {
   1.139 +        StAXDocumentSerializer serializer = new StAXDocumentSerializer(outputStream, new StAXManager(_manager));
   1.140 +        serializer.setEncoding(encoding);
   1.141 +        return serializer;
   1.142 +    }
   1.143 +
   1.144 +    public Object getProperty(String name) throws java.lang.IllegalArgumentException {
   1.145 +        if(name == null){
   1.146 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object[]{null}));
   1.147 +        }
   1.148 +        if(_manager.containsProperty(name))
   1.149 +            return _manager.getProperty(name);
   1.150 +        throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object[]{name}));
   1.151 +    }
   1.152 +
   1.153 +    public boolean isPropertySupported(String name) {
   1.154 +        if(name == null)
   1.155 +            return false ;
   1.156 +        else
   1.157 +            return _manager.containsProperty(name);
   1.158 +    }
   1.159 +
   1.160 +    public void setProperty(String name, Object value) throws java.lang.IllegalArgumentException {
   1.161 +        _manager.setProperty(name,value);
   1.162 +
   1.163 +    }
   1.164 +
   1.165 +}

mercurial