1 38 39 40 package com.sun.xml.fastinfoset.stax; 41 42 import javax.xml.transform.Result ; 43 import javax.xml.stream.XMLOutputFactory ; 44 import javax.xml.stream.XMLEventWriter; 45 import javax.xml.stream.XMLStreamWriter; 46 import javax.xml.stream.XMLStreamException; 47 import javax.xml.transform.stream.StreamResult ; 48 import java.io.File ; 49 import java.io.FileWriter ; 50 import java.io.IOException ; 51 import java.io.OutputStream ; 52 import java.io.Writer ; 53 import com.sun.xml.fastinfoset.CommonResourceBundle; 54 55 public class StAXOutputFactory extends XMLOutputFactory { 56 57 private StAXManager _manager = null ; 59 60 61 public StAXOutputFactory() { 62 _manager = new StAXManager(StAXManager.CONTEXT_WRITER); 63 } 64 65 public XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException { 66 return new StAXEventWriter(createXMLStreamWriter(result)); 67 } 68 69 public XMLEventWriter createXMLEventWriter(Writer writer) throws XMLStreamException { 70 return new StAXEventWriter(createXMLStreamWriter(writer)); 71 } 72 73 public XMLEventWriter createXMLEventWriter(OutputStream outputStream) throws XMLStreamException { 74 return new StAXEventWriter(createXMLStreamWriter(outputStream)); 75 } 76 77 public XMLEventWriter createXMLEventWriter(OutputStream outputStream, String encoding) throws XMLStreamException { 78 return new StAXEventWriter(createXMLStreamWriter(outputStream, encoding)); 79 } 80 81 public XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException { 82 if(result instanceof StreamResult ){ 83 StreamResult streamResult = (StreamResult )result; 84 if( streamResult.getWriter() != null){ 85 return createXMLStreamWriter(streamResult.getWriter()); 86 }else if(streamResult.getOutputStream() != null ){ 87 return createXMLStreamWriter(streamResult.getOutputStream()); 88 }else if(streamResult.getSystemId()!= null){ 89 try{ 90 FileWriter writer = new FileWriter (new File (streamResult.getSystemId())); 91 return createXMLStreamWriter(writer); 92 }catch(IOException ie){ 93 throw new XMLStreamException(ie); 94 } 95 } 96 } 97 else if(result instanceof Result ){ 98 try{ 99 FileWriter writer = new FileWriter (new File (result.getSystemId())); 101 return createXMLStreamWriter(writer); 102 }catch(IOException ie){ 103 throw new XMLStreamException(ie); 104 } 105 } 106 throw new java.lang.UnsupportedOperationException (); 107 } 108 109 112 public XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException { 113 throw new java.lang.UnsupportedOperationException (); 114 } 115 116 public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream) throws XMLStreamException { 117 return new StAXDocumentSerializer(outputStream, new StAXManager(_manager)); 118 } 119 120 public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream, String encoding) throws XMLStreamException { 121 StAXDocumentSerializer serializer = new StAXDocumentSerializer(outputStream, new StAXManager(_manager)); 122 serializer.setEncoding(encoding); 123 return serializer; 124 } 125 126 public Object getProperty(String name) throws java.lang.IllegalArgumentException { 127 if(name == null){ 128 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object []{name})); 129 } 130 if(_manager.containsProperty(name)) 131 return _manager.getProperty(name); 132 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object []{name})); 133 } 134 135 public boolean isPropertySupported(String name) { 136 if(name == null) 137 return false ; 138 else 139 return _manager.containsProperty(name); 140 } 141 142 public void setProperty(String name, Object value) throws java.lang.IllegalArgumentException { 143 _manager.setProperty(name,value); 144 145 } 146 147 } 148 | Popular Tags |