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

changeset 286
f50545b5e2f1
child 384
8f2986ff0235
     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	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,137 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, 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 +                try{
    1.83 +                    FileWriter writer = new FileWriter(new File(streamResult.getSystemId()));
    1.84 +                    return createXMLStreamWriter(writer);
    1.85 +                }catch(IOException ie){
    1.86 +                    throw new XMLStreamException(ie);
    1.87 +                }
    1.88 +            }
    1.89 +        }
    1.90 +        else {
    1.91 +            try{
    1.92 +                //xxx: should we be using FileOutputStream - nb.
    1.93 +                FileWriter writer = new FileWriter(new File(result.getSystemId()));
    1.94 +                return createXMLStreamWriter(writer);
    1.95 +            }catch(IOException ie){
    1.96 +                throw new XMLStreamException(ie);
    1.97 +            }
    1.98 +        }
    1.99 +        throw new java.lang.UnsupportedOperationException();
   1.100 +    }
   1.101 +
   1.102 +    /** this is assumed that user wants to write the file in xml format
   1.103 +     *
   1.104 +     */
   1.105 +    public XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException {
   1.106 +        throw new java.lang.UnsupportedOperationException();
   1.107 +    }
   1.108 +
   1.109 +    public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream) throws XMLStreamException {
   1.110 +        return new StAXDocumentSerializer(outputStream, new StAXManager(_manager));
   1.111 +    }
   1.112 +
   1.113 +    public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream, String encoding) throws XMLStreamException {
   1.114 +        StAXDocumentSerializer serializer = new StAXDocumentSerializer(outputStream, new StAXManager(_manager));
   1.115 +        serializer.setEncoding(encoding);
   1.116 +        return serializer;
   1.117 +    }
   1.118 +
   1.119 +    public Object getProperty(String name) throws java.lang.IllegalArgumentException {
   1.120 +        if(name == null){
   1.121 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object[]{null}));
   1.122 +        }
   1.123 +        if(_manager.containsProperty(name))
   1.124 +            return _manager.getProperty(name);
   1.125 +        throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object[]{name}));
   1.126 +    }
   1.127 +
   1.128 +    public boolean isPropertySupported(String name) {
   1.129 +        if(name == null)
   1.130 +            return false ;
   1.131 +        else
   1.132 +            return _manager.containsProperty(name);
   1.133 +    }
   1.134 +
   1.135 +    public void setProperty(String name, Object value) throws java.lang.IllegalArgumentException {
   1.136 +        _manager.setProperty(name,value);
   1.137 +
   1.138 +    }
   1.139 +
   1.140 +}

mercurial