| 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.BufferedReader; |
| 19 |
import java.io.IOException; |
| 20 |
import java.io.InputStream; |
| 21 |
import java.io.InputStreamReader; |
| 22 |
import java.nio.charset.StandardCharsets; |
| 23 |
import java.util.Arrays; |
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.HashSet; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
import org.jpype.JPypeContext; |
| 29 |
import org.w3c.dom.Attr; |
| 30 |
import org.w3c.dom.Document; |
| 31 |
|
| 32 |
public class Html |
| 33 |
{ |
| 34 |
|
| 35 |
public final static HashSet<String> VOID_ELEMENTS = new HashSet<>(); |
| 36 |
public final static HashSet<String> OPTIONAL_ELEMENTS = new HashSet<>(); |
| 37 |
public final static HashSet<String> OPTIONAL_CLOSE = new HashSet<>(); |
| 38 |
|
| 39 |
static |
| 40 |
{ |
| 41 |
VOID_ELEMENTS.addAll(Arrays.asList( |
| 42 |
"area", "base", "br", "col", "command", "embed", "hr", "img", |
| 43 |
"input", "keygen", "link", "meta", "param", "source", "track", "wbr")); |
| 44 |
OPTIONAL_ELEMENTS.addAll(Arrays.asList("html", "head", "body", "p", "dt", |
| 45 |
"dd", "li", "option", "thead", "th", "tbody", "tr", "td", "tfoot", "colgroup")); |
| 46 |
OPTIONAL_CLOSE.addAll(Arrays.asList("li:li", "dt:dd", |
| 47 |
"p:address", "p:article", "p:aside", "p:blockquote", "p:details", |
| 48 |
"p:div", "p:dl", "p:fieldset", "p:figcaption", "p:figure", |
| 49 |
"p:footer", "p:form", "p:h1", "p:h2", "p:h3", "p:h4", "p:h5", "p:h6", |
| 50 |
"p:header", "p:hgroup", "p:hr", "p:main", "p:menu", |
| 51 |
"p:nav", "p:ol", "p:p", "p:pre", "p:section", "p:table", "p:ul", |
| 52 |
"dd:dt", "dd:dd", "dt:dt", "dt:dd", "rt:rt", "rt:rp", "rp:rt", "rp:rp", |
| 53 |
"optgroup:optgroup", "option:option", "option:optiongroup", "thread:tbody", |
| 54 |
"thread:tfoot", "tbody:tfoot", "tbody:tbody", "tr:tr", "td:td", "td:th", |
| 55 |
"th:td", "p:li")); |
| 56 |
} |
| 57 |
|
| 58 |
public static Parser<Document> newParser() |
| 59 |
{ |
| 60 |
return new HtmlParser(); |
| 61 |
} |
| 62 |
|
| 63 |
public static List<Attr> parseAttributes(Document doc, String str) |
| 64 |
{ |
| 65 |
AttrParser p = new AttrParser(doc); |
| 66 |
p.parse(str); |
| 67 |
return p.attrs; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public static Map<String, Integer> ENTITIES = new HashMap<>(); |
| 72 |
|
| 73 |
static |
| 74 |
{ |
| 75 |
ClassLoader cl = ClassLoader.getSystemClassLoader(); |
| 76 |
try (InputStream is = cl.getResourceAsStream("org/jpype/html/entities.txt"); InputStreamReader isr = new InputStreamReader(is); BufferedReader rd = new BufferedReader(isr)) |
| 77 |
{ |
| 78 |
while (true) |
| 79 |
{ |
| 80 |
String line = rd.readLine(); |
| 81 |
if (line == null) |
| 82 |
break; |
| 83 |
if (line.startsWith("#")) |
| 84 |
continue; |
| 85 |
String[] parts = line.split("\\s+"); |
| 86 |
ENTITIES.put(parts[0], Integer.parseInt(parts[1])); |
| 87 |
} |
| 88 |
} catch (IOException ex) |
| 89 |
{ |
| 90 |
throw new RuntimeException(ex); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public static String decode(String s) |
| 95 |
{ |
| 96 |
if (!s.contains("&")) |
| 97 |
return s; |
| 98 |
|
| 99 |
int dead = 0; |
| 100 |
byte[] b = s.getBytes(StandardCharsets.UTF_8); |
| 101 |
for (int i = 0; i < b.length; ++i) |
| 102 |
{ |
| 103 |
if (b[i] != '&') |
| 104 |
continue; |
| 105 |
|
| 106 |
int i1 = i; |
| 107 |
int i2 = i + 1; |
| 108 |
if (i2 == b.length) |
| 109 |
break; |
| 110 |
if (b[i2] == '#') |
| 111 |
{ |
| 112 |
|
| 113 |
for (i = i2 + 1; i < b.length; ++i) |
| 114 |
{ |
| 115 |
if (!Character.isDigit(b[i])) |
| 116 |
break; |
| 117 |
} |
| 118 |
} else |
| 119 |
{ |
| 120 |
for (i = i2; i < b.length; ++i) |
| 121 |
{ |
| 122 |
if (b[i] == ';') |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
int i3 = i; |
| 127 |
int c = 0; |
| 128 |
if (b[i2] == '#') |
| 129 |
{ |
| 130 |
i2++; |
| 131 |
try |
| 132 |
{ |
| 133 |
c = Integer.parseInt(new String(b, i2, i3 - i2, StandardCharsets.UTF_8)); |
| 134 |
} catch (NumberFormatException ex) |
| 135 |
{ |
| 136 |
|
| 137 |
} |
| 138 |
} else |
| 139 |
{ |
| 140 |
String e = new String(b, i2, i3 - i2, StandardCharsets.UTF_8); |
| 141 |
Integer c2 = ENTITIES.get(e); |
| 142 |
if (c2 == null) |
| 143 |
throw new RuntimeException("Bad entity " + e); |
| 144 |
c = c2; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
if (c < 128) |
| 149 |
{ |
| 150 |
b[i1++] = (byte) c; |
| 151 |
} else if (c < 0x0800) |
| 152 |
{ |
| 153 |
b[i1++] = (byte) (0xc0 + ((c >> 6) & 0x1f)); |
| 154 |
if (i1 < b.length) |
| 155 |
b[i1++] = (byte) (0x80 + (c & 0x3f)); |
| 156 |
} else |
| 157 |
{ |
| 158 |
b[i1++] = (byte) (0xe0 + ((c >> 12) & 0x0f)); |
| 159 |
if (i1 < b.length) |
| 160 |
b[i1++] = (byte) (0x80 + ((c >> 6) & 0x3f)); |
| 161 |
if (i1 < b.length) |
| 162 |
b[i1++] = (byte) (0x80 + (c & 0x3f)); |
| 163 |
} |
| 164 |
if (i3 < b.length && b[i3] == ';') |
| 165 |
i3++; |
| 166 |
dead += i3 - i1; |
| 167 |
for (; i1 < i3; ++i1) |
| 168 |
b[i1] = 0; |
| 169 |
i = i3; |
| 170 |
} |
| 171 |
int j = 0; |
| 172 |
byte[] b2 = new byte[b.length - dead]; |
| 173 |
for (int i = 0; i < b.length; ++i) |
| 174 |
{ |
| 175 |
if (b[i] != 0) |
| 176 |
b2[j++] = b[i]; |
| 177 |
} |
| 178 |
return new String(b2, StandardCharsets.UTF_8); |
| 179 |
} |
| 180 |
|
| 181 |
} |