1 23 24 package org.enhydra.xml.io; 25 26 import java.io.BufferedOutputStream ; 27 import java.io.BufferedWriter ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.File ; 30 import java.io.FileOutputStream ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.io.OutputStreamWriter ; 34 import java.io.StringWriter ; 35 import java.io.Writer ; 36 37 import org.enhydra.xml.dom.DOMAccess; 38 import org.enhydra.xml.dom.DOMOps; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.html.HTMLDocument; 43 44 46 49 51 57 public class DOMFormatter { 58 61 private static final String XHTML_NAMESPACE_URI 62 = "http://www.w3.org/1999/xhtml"; 63 64 67 private OutputOptions fOptions; 68 69 75 public DOMFormatter(OutputOptions outputOptions) { 76 fOptions = outputOptions; 77 if (fOptions == null) { 78 fOptions = new OutputOptions(); 79 } 80 } 81 82 85 public DOMFormatter() { 86 this(null); 87 } 88 89 92 public OutputOptions getOutputOptions() { 93 return fOptions; 94 } 95 96 99 public void setOutputOptions(OutputOptions outputOptions) { 100 fOptions = outputOptions; 101 } 102 103 107 public static OutputOptions getDefaultOutputOptions(Document doc) { 108 if ((doc instanceof HTMLDocument) && !isXHTMLDocument(doc)) { 109 return HTMLFormatter.getDefaultOutputOptions(); 110 } else { 111 return XMLFormatter.getDefaultOutputOptions(); 112 } 113 } 114 115 119 private static boolean isXHTMLDocument(Document document) { 120 Element docElement = DOMAccess.accessDocumentElement(document); 122 123 return (docElement != null) 125 && XHTML_NAMESPACE_URI.equals(docElement.getNamespaceURI()); 126 } 127 128 131 private static OutputOptions.Format determineFormat(OutputOptions outputOptions, 132 Node node) { 133 OutputOptions.Format format = outputOptions.getFormat(); 134 if (format == OutputOptions.FORMAT_AUTO) { 135 Document doc = DOMOps.getDocument(node); 138 if ((doc instanceof HTMLDocument) && !isXHTMLDocument(doc)) { 139 format = OutputOptions.FORMAT_HTML; 140 } else { 141 format = OutputOptions.FORMAT_XML; 142 } 143 } 144 return format; 145 } 146 147 152 public static Formatter getFormatter(Node node, 153 OutputOptions outputOptions, 154 boolean forPreFormatting) { 155 OutputOptions.Format format = determineFormat(outputOptions, node); 156 if (format == OutputOptions.FORMAT_HTML) { 157 return new HTMLFormatter(node, outputOptions, forPreFormatting); 158 } else { 159 return new XMLFormatter(node, outputOptions, forPreFormatting); 160 } 161 } 162 163 166 public String toString(Node node) { 167 Formatter formatter = getFormatter(node, fOptions, false); 168 StringWriter writer = new StringWriter (); 169 try { 170 formatter.write(node, writer); 171 } catch (IOException except) { 172 throw new XMLIOError(except); 174 } 175 return writer.toString(); 176 } 177 178 182 public byte[] toBytes(Node node) throws IOException { 183 ByteArrayOutputStream out = new ByteArrayOutputStream (); 184 write(node, out); 185 return out.toByteArray(); 186 } 187 188 191 public void write(Node node, 192 Writer writer) throws IOException { 193 Formatter formatter = getFormatter(node, fOptions, false); 194 formatter.write(node, writer); 195 } 196 197 200 public void write(Node node, 201 OutputStream out) throws IOException { 202 Formatter formatter = getFormatter(node, fOptions, false); 203 Writer writer = new BufferedWriter (new OutputStreamWriter (out, 204 formatter.getMIMEEncoding())); 205 formatter.write(node, writer); 206 writer.flush(); 207 } 208 209 212 public void write(Node node, 213 File out) throws IOException { 214 OutputStream outStream 215 = new BufferedOutputStream (new FileOutputStream (out)); 216 try { 217 write(node, outStream); 218 } finally { 219 outStream.close(); 220 } 221 } 222 } 223 | Popular Tags |