1 9 package javolution.xml.stream; 10 11 import java.io.OutputStream ; 12 import java.io.Writer ; 13 14 import javolution.context.ObjectFactory; 15 16 51 public abstract class XMLOutputFactory { 52 53 57 public static final String IS_REPAIRING_NAMESPACES = "javolution.xml.stream.isRepairingNamespaces"; 58 59 65 public final static String REPAIRING_PREFIX = "javolution.xml.stream.repairingPrefix"; 66 67 72 public static final String INDENTATION = "javolution.xml.stream.indentation"; 73 74 80 public final static String AUTOMATIC_EMPTY_ELEMENTS = "javolution.xml.stream.automaticEmptyElements"; 81 82 85 protected XMLOutputFactory() { 86 } 87 88 94 public static XMLOutputFactory newInstance() { 95 return new Default(); 96 } 97 98 104 public abstract XMLStreamWriter createXMLStreamWriter(Writer writer) 105 throws XMLStreamException; 106 107 114 public abstract XMLStreamWriter createXMLStreamWriter(OutputStream stream) 115 throws XMLStreamException; 116 117 125 public abstract XMLStreamWriter createXMLStreamWriter(OutputStream stream, 126 String encoding) throws XMLStreamException; 127 128 136 public abstract void setProperty(String name, Object value) 137 throws IllegalArgumentException ; 138 139 146 public abstract Object getProperty(String name) 147 throws IllegalArgumentException ; 148 149 156 public abstract boolean isPropertySupported(String name); 157 158 161 private static final class Default extends XMLOutputFactory { 162 163 private Boolean _isRepairingNamespaces = new Boolean (false); 165 166 private String _repairingPrefix = "ns"; 168 169 private Boolean _automaticEmptyElements = new Boolean (false); 171 172 private String _indentation; 174 175 public XMLStreamWriter createXMLStreamWriter(Writer writer) 177 throws XMLStreamException { 178 XMLStreamWriterImpl xmlWriter = newWriter(); 179 xmlWriter.setOutput(writer); 180 return xmlWriter; 181 } 182 183 public XMLStreamWriter createXMLStreamWriter(OutputStream stream) 185 throws XMLStreamException { 186 XMLStreamWriterImpl xmlWriter = newWriter(); 187 xmlWriter.setOutput(stream); 188 return xmlWriter; 189 } 190 191 public XMLStreamWriter createXMLStreamWriter(OutputStream stream, 193 String encoding) throws XMLStreamException { 194 if ((encoding == null) || encoding.equals("UTF-8") 195 || encoding.equals("utf-8")) 196 return createXMLStreamWriter(stream); 197 XMLStreamWriterImpl xmlWriter = newWriter(); 198 xmlWriter.setOutput(stream, encoding); 199 return xmlWriter; 200 } 201 202 private XMLStreamWriterImpl newWriter() { 203 XMLStreamWriterImpl xmlWriter = (XMLStreamWriterImpl) XML_WRITER_FACTORY 204 .object(); 205 xmlWriter._objectFactory = XML_WRITER_FACTORY; 206 xmlWriter.setRepairingNamespaces(_isRepairingNamespaces.booleanValue()); 207 xmlWriter.setRepairingPrefix(_repairingPrefix); 208 xmlWriter.setIndentation(_indentation); 209 xmlWriter.setAutomaticEmptyElements(_automaticEmptyElements.booleanValue()); 210 return xmlWriter; 211 } 212 213 public void setProperty(String name, Object value) 215 throws IllegalArgumentException { 216 if (name.equals(IS_REPAIRING_NAMESPACES)) { 217 _isRepairingNamespaces = (Boolean ) value; 218 } else if (name.equals(REPAIRING_PREFIX)) { 219 _repairingPrefix = (String ) value; 220 } else if (name.equals(AUTOMATIC_EMPTY_ELEMENTS)) { 221 _automaticEmptyElements = (Boolean ) value; 222 } else if (name.equals(INDENTATION)) { 223 _indentation = (String ) value; 224 } else { 225 throw new IllegalArgumentException ("Property: " + name 226 + " not supported"); 227 } 228 } 229 230 public Object getProperty(String name) throws IllegalArgumentException { 232 if (name.equals(IS_REPAIRING_NAMESPACES)) { 233 return _isRepairingNamespaces; 234 } else if (name.equals(REPAIRING_PREFIX)) { 235 return _repairingPrefix; 236 } else if (name.equals(AUTOMATIC_EMPTY_ELEMENTS)) { 237 return _automaticEmptyElements; 238 } else if (name.equals(INDENTATION)) { 239 return _indentation; 240 } else { 241 throw new IllegalArgumentException ("Property: " + name 242 + " not supported"); 243 } 244 } 245 246 public boolean isPropertySupported(String name) { 248 return name.equals(IS_REPAIRING_NAMESPACES) 249 || name.equals(REPAIRING_PREFIX) 250 || name.equals(AUTOMATIC_EMPTY_ELEMENTS) 251 || name.equals(INDENTATION); 252 } 253 254 } 255 256 259 private static final ObjectFactory XML_WRITER_FACTORY = new ObjectFactory() { 260 261 protected Object create() { 262 return new XMLStreamWriterImpl(); 263 } 264 265 protected void cleanup(Object obj) { 266 ((XMLStreamWriterImpl) obj).reset(); 267 } 268 269 }; 270 271 } | Popular Tags |