| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package org.jpype.html; |
| 17 |
|
| 18 |
import java.io.BufferedWriter; |
| 19 |
import java.io.ByteArrayOutputStream; |
| 20 |
import java.io.Closeable; |
| 21 |
import java.io.IOException; |
| 22 |
import java.io.OutputStream; |
| 23 |
import java.io.OutputStreamWriter; |
| 24 |
import org.w3c.dom.Attr; |
| 25 |
import org.w3c.dom.CDATASection; |
| 26 |
import org.w3c.dom.Comment; |
| 27 |
import org.w3c.dom.Document; |
| 28 |
import org.w3c.dom.Element; |
| 29 |
import org.w3c.dom.NamedNodeMap; |
| 30 |
import org.w3c.dom.Node; |
| 31 |
import org.w3c.dom.NodeList; |
| 32 |
import org.w3c.dom.ProcessingInstruction; |
| 33 |
import org.w3c.dom.Text; |
| 34 |
|
| 35 |
public class HtmlWriter implements Closeable |
| 36 |
{ |
| 37 |
|
| 38 |
OutputStream os; |
| 39 |
BufferedWriter writer; |
| 40 |
|
| 41 |
public HtmlWriter(OutputStream os) |
| 42 |
{ |
| 43 |
this.os = os; |
| 44 |
this.writer = new BufferedWriter(new OutputStreamWriter(os)); |
| 45 |
} |
| 46 |
|
| 47 |
public static String asString(Node node) throws IOException |
| 48 |
{ |
| 49 |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) |
| 50 |
{ |
| 51 |
HtmlWriter hw = new HtmlWriter(baos); |
| 52 |
hw.write(node); |
| 53 |
hw.close(); |
| 54 |
return baos.toString(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public void write(Node n) throws IOException |
| 59 |
{ |
| 60 |
|
| 61 |
if (n == null) |
| 62 |
{ |
| 63 |
writer.write("NULL"); |
| 64 |
return; |
| 65 |
} |
| 66 |
switch (n.getNodeType()) |
| 67 |
{ |
| 68 |
case Node.PROCESSING_INSTRUCTION_NODE: |
| 69 |
writeDirective((ProcessingInstruction) n); |
| 70 |
break; |
| 71 |
case Node.ELEMENT_NODE: |
| 72 |
writeElement((Element) n); |
| 73 |
break; |
| 74 |
case Node.DOCUMENT_FRAGMENT_NODE: |
| 75 |
writeChildren(n); |
| 76 |
break; |
| 77 |
case Node.TEXT_NODE: |
| 78 |
writeText((Text) n); |
| 79 |
break; |
| 80 |
case Node.COMMENT_NODE: |
| 81 |
writeComment((Comment) n); |
| 82 |
break; |
| 83 |
case Node.CDATA_SECTION_NODE: |
| 84 |
writeCData((CDATASection) n); |
| 85 |
break; |
| 86 |
default: |
| 87 |
throw new RuntimeException("unhandled " + n.getClass()); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public void writeChildren(Node doc) throws IOException |
| 92 |
{ |
| 93 |
NodeList children = doc.getChildNodes(); |
| 94 |
for (int i = 0; i < children.getLength(); ++i) |
| 95 |
{ |
| 96 |
write(children.item(i)); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public void writeDirective(ProcessingInstruction d) throws IOException |
| 101 |
{ |
| 102 |
writer.write("<!"); |
| 103 |
writer.write(d.getTarget()); |
| 104 |
writer.write(" "); |
| 105 |
writer.write(d.getData()); |
| 106 |
writer.write(">"); |
| 107 |
} |
| 108 |
|
| 109 |
public void writeElement(Element e) throws IOException |
| 110 |
{ |
| 111 |
String name = e.getTagName(); |
| 112 |
writer.write("<"); |
| 113 |
writer.write(name); |
| 114 |
NamedNodeMap attributes = e.getAttributes(); |
| 115 |
if (attributes.getLength() > 0) |
| 116 |
{ |
| 117 |
for (int i = 0; i < attributes.getLength(); ++i) |
| 118 |
{ |
| 119 |
writer.write(" "); |
| 120 |
Attr attr = (Attr) attributes.item(i); |
| 121 |
writer.write(attr.getName()); |
| 122 |
writer.write("=\""); |
| 123 |
writer.write(attr.getValue()); |
| 124 |
writer.write('"'); |
| 125 |
} |
| 126 |
} |
| 127 |
if (Html.VOID_ELEMENTS.contains(name)) |
| 128 |
{ |
| 129 |
writer.write(">"); |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
NodeList children = e.getChildNodes(); |
| 134 |
|
| 135 |
if (children.getLength() == 0) |
| 136 |
{ |
| 137 |
writer.write("/>"); |
| 138 |
} else |
| 139 |
{ |
| 140 |
writer.write(">"); |
| 141 |
writeChildren(e); |
| 142 |
writer.write("</"); |
| 143 |
writer.write(name); |
| 144 |
writer.write(">"); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
private void writeComment(Comment comment) throws IOException |
| 149 |
{ |
| 150 |
writer.write("<!--"); |
| 151 |
writer.write(comment.getData()); |
| 152 |
writer.write("-->"); |
| 153 |
} |
| 154 |
|
| 155 |
private void writeCData(CDATASection cData) throws IOException |
| 156 |
{ |
| 157 |
writer.write("<![CDATA["); |
| 158 |
writer.write(cData.getData()); |
| 159 |
writer.write("]]>"); |
| 160 |
} |
| 161 |
|
| 162 |
private void writeText(Text text) throws IOException |
| 163 |
{ |
| 164 |
writer.write(text.getData()); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public void close() throws IOException |
| 169 |
{ |
| 170 |
writer.close(); |
| 171 |
} |
| 172 |
} |