1/* 2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 * 25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. 26 */ 27 28package com.sun.xml.internal.fastinfoset.stax.factory; 29 30import com.sun.xml.internal.fastinfoset.stax.*; 31import com.sun.xml.internal.fastinfoset.stax.events.StAXEventWriter; 32import javax.xml.transform.Result; 33import javax.xml.stream.XMLOutputFactory ; 34import javax.xml.stream.XMLEventWriter; 35import javax.xml.stream.XMLStreamWriter; 36import javax.xml.stream.XMLStreamException; 37import javax.xml.transform.stream.StreamResult; 38import java.io.File; 39import java.io.FileWriter; 40import java.io.IOException; 41import java.io.OutputStream; 42import java.io.Writer; 43import com.sun.xml.internal.fastinfoset.CommonResourceBundle; 44 45public class StAXOutputFactory extends XMLOutputFactory { 46 47 //List of supported properties and default values. 48 private StAXManager _manager = null ; 49 50 /** Creates a new instance of StAXOutputFactory */ 51 public StAXOutputFactory() { 52 _manager = new StAXManager(StAXManager.CONTEXT_WRITER); 53 } 54 55 public XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException { 56 return new StAXEventWriter(createXMLStreamWriter(result)); 57 } 58 59 public XMLEventWriter createXMLEventWriter(Writer writer) throws XMLStreamException { 60 return new StAXEventWriter(createXMLStreamWriter(writer)); 61 } 62 63 public XMLEventWriter createXMLEventWriter(OutputStream outputStream) throws XMLStreamException { 64 return new StAXEventWriter(createXMLStreamWriter(outputStream)); 65 } 66 67 public XMLEventWriter createXMLEventWriter(OutputStream outputStream, String encoding) throws XMLStreamException { 68 return new StAXEventWriter(createXMLStreamWriter(outputStream, encoding)); 69 } 70 71 public XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException { 72 if (result instanceof StreamResult) { 73 StreamResult streamResult = (StreamResult) result; 74 if (streamResult.getWriter() != null) { 75 return createXMLStreamWriter(streamResult.getWriter()); 76 } else if (streamResult.getOutputStream() != null) { 77 return createXMLStreamWriter(streamResult.getOutputStream()); 78 } else if (streamResult.getSystemId() != null) { 79 FileWriter writer = null; 80 boolean isError = true; 81 82 try { 83 writer = new FileWriter(new File(streamResult.getSystemId())); 84 final XMLStreamWriter streamWriter = createXMLStreamWriter(writer); 85 isError = false; 86 87 return streamWriter; 88 } catch (IOException ie) { 89 throw new XMLStreamException(ie); 90 } finally { 91 if (isError && writer != null) { 92 try { 93 writer.close(); 94 } catch (IOException ignored) { 95 } 96 } 97 } 98 } 99 } else { 100 FileWriter writer = null; 101 boolean isError = true; 102 103 try { 104 //xxx: should we be using FileOutputStream - nb. 105 writer = new FileWriter(new File(result.getSystemId())); 106 final XMLStreamWriter streamWriter = createXMLStreamWriter(writer); 107 isError = false; 108 109 return streamWriter; 110 } catch (IOException ie) { 111 throw new XMLStreamException(ie); 112 } finally { 113 if (isError && writer != null) { 114 try { 115 writer.close(); 116 } catch (IOException ignored) { 117 } 118 } 119 } 120 } 121 throw new java.lang.UnsupportedOperationException(); 122 } 123 124 /** this is assumed that user wants to write the file in xml format 125 * 126 */ 127 public XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException { 128 throw new java.lang.UnsupportedOperationException(); 129 } 130 131 public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream) throws XMLStreamException { 132 return new StAXDocumentSerializer(outputStream, new StAXManager(_manager)); 133 } 134 135 public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream, String encoding) throws XMLStreamException { 136 StAXDocumentSerializer serializer = new StAXDocumentSerializer(outputStream, new StAXManager(_manager)); 137 serializer.setEncoding(encoding); 138 return serializer; 139 } 140 141 public Object getProperty(String name) throws java.lang.IllegalArgumentException { 142 if(name == null){ 143 throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object[]{null})); 144 } 145 if(_manager.containsProperty(name)) 146 return _manager.getProperty(name); 147 throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object[]{name})); 148 } 149 150 public boolean isPropertySupported(String name) { 151 if(name == null) 152 return false ; 153 else 154 return _manager.containsProperty(name); 155 } 156 157 public void setProperty(String name, Object value) throws java.lang.IllegalArgumentException { 158 _manager.setProperty(name,value); 159 160 } 161 162} 163