annotate src/goodjava/html/Html.java @ 1779:96dbbdf1e368

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 15 Aug 2023 23:52:20 -0600
parents 31a82b0d0a87
children a045f30fa67d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.html;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.List;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.ArrayList;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Set;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.HashSet;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.Map;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.HashMap;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.Collections;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.util.regex.Pattern;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import java.util.regex.Matcher;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 import goodjava.parser.Parser;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public final class Html {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 private static final Pattern entityPtn = Pattern.compile(
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 "&(#?[0-9a-zA-Z]+;)"
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 public static String encode(String s) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 //s = s.replace("&","&amp;");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 s = entityPtn.matcher(s).replaceAll("&amp;$1");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 s = s.replace("<","&lt;");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 s = s.replace(">","&gt;");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 s = s.replace("\"","&quot;");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 return s;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 private static final Pattern entityNumPtn = Pattern.compile(
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 "&#(\\d+);"
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 public static String decode(String s) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 Matcher m = entityNumPtn.matcher(s);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 if( m.find() ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 StringBuffer buf = new StringBuffer();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 do {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 String entity = new String(new char[]{(char)Integer.parseInt(m.group(1))});
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 m.appendReplacement(buf,entity);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 } while( m.find() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 m.appendTail(buf);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 s = buf.toString();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 s = s.replace("&nbsp;"," ");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 s = s.replace("&quot;","\"");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 s = s.replace("&gt;",">");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 s = s.replace("&lt;","<");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 s = s.replace("&amp;","&");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 return s;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 public static final class Comment {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 public final String text;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 private Comment(String text) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 this.text = text;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 public static final class CData {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 public final String text;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 private CData(String text) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 this.text = text;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 public static final class Tag {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 public final String name;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 public final Map<String,Object> attributes;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 public final boolean isEmpty;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 public final String raw;
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
75 public final Map<String,String> style;
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 private Tag(String name,Map<String,Object> attributes,boolean isEmpty,String raw) {
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
78 this(name,attributes,isEmpty,raw,null);
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
79 }
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
80
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
81 private Tag(String name,Map<String,Object> attributes,boolean isEmpty,String raw,Map<String,String> style) {
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 this.name = name;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 this.attributes = attributes;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 this.isEmpty = isEmpty;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 this.raw = raw;
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
86 this.style = style;
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 public static final class Container {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 public final Tag tag;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 public final String text;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 private Container(Tag tag,String text) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 this.tag = tag;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 this.text = text;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 private static Set<String> defaultContainerTags = new HashSet<String>();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 static {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 Collections.addAll( defaultContainerTags, "script", "style", "textarea" );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 public static List parse(String text) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 return parse(text,defaultContainerTags);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 public static List parse(String text,Set<String> containerTags) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 return new Html(text,containerTags).parse();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 private final Parser parser;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 private final Set<String> containerTags;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 private Html(String text,Set<String> containerTags) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 this.parser = new Parser(text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 this.containerTags = containerTags;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 private List parse() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 List list = new ArrayList();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 StringBuilder sb = new StringBuilder();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 while( !parser.endOfInput() ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 if( parser.test('<') ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 Tag tag = parseTag();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 if( tag != null ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 Object el = tag;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 String tagName = tag.name;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 if( containerTags.contains(tagName) ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 Container container = parseContainer(tag);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 if( container != null )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 el = container;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 if( el != null
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 || (el = parseComment()) != null
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 || (el = parseCdata()) != null
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 add(list,sb);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 list.add(el);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 continue;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 sb.append( parser.currentChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 parser.anyChar();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 add(list,sb);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 return list;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 private static void add(List list,StringBuilder sb) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 if( sb.length() > 0 ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 list.add(decode(sb.toString()));
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 sb.setLength(0);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 private Comment parseComment() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 if( !parser.match("<!--") )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 while( !parser.test("-->") ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 String text = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 Comment comment = new Comment(text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 return parser.success(comment);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 private CData parseCdata() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 if( !parser.match("<![CDATA[") )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 while( !parser.test("]]>") ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 String text = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 CData cdata = new CData(text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 return parser.success(cdata);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 private Container parseContainer(Tag tag) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 String endTagName = '/' + tag.name;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 int start = parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 int end;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 while(true) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 if( parser.test('<') ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 end = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 Tag tag2 = parseTag();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 if( tag2.name.equals(endTagName) )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 break;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 String text = parser.text.substring(start,end);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 Container container = new Container(tag,text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203 return parser.success(container);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 private Tag parseTag() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207 int tagStart = parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 if( !parser.match('<') )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
209 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211 parser.match('/');
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
212 if( !matchNameChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 while( matchNameChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
215 String name = parser.textFrom(start).toLowerCase();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
216 Map<String,Object> attributes = new HashMap<String,Object>();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
217 String attrName;
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
218 Map<String,String> style = null;
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
219 while( (attrName = parseAttrName()) != null ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
220 String attrValue = parseAttrValue();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
221 attributes.put( attrName, attrValue!=null ? attrValue : true );
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
222 if( attrName.equals("style") && attrValue!=null && style==null ) {
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
223 style = Css.style(attrValue);
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
224 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
225 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
226 while( matchSpace() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
227 boolean isEmpty = parser.match('/');
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
228 if( !parser.match('>') )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
229 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
230 String raw = parser.textFrom(tagStart);
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1712
diff changeset
231 Tag tag = new Tag(name,attributes,isEmpty,raw,style);
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
232 return parser.success(tag);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
233 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
234
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
235 private String parseAttrName() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
236 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
237 if( !matchSpace() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
238 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
239 while( matchSpace() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
240 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
241 if( !matchNameChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
242 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
243 while( matchNameChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
244 String name = parser.textFrom(start).toLowerCase();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
245 return parser.success(name);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
246 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
247
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
248 private String parseAttrValue() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
249 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
250 while( matchSpace() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
251 if( !parser.match('=') )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
252 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253 while( matchSpace() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254 if( parser.anyOf("\"'") ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
255 char quote = parser.lastChar();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
256 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
257 while( !parser.test(quote) ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
258 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
259 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
260 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
261 String value = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
262 parser.match(quote);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
263 value = decode(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
264 return parser.success(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
265 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
266 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
267 if( !matchValueChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
268 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269 while( matchValueChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
270 String value = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271 value = decode(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
272 return parser.success(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
273 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
274
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
275 private boolean matchNameChar() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
276 return parser.inCharRange('a','z')
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
277 || parser.inCharRange('A','Z')
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
278 || parser.inCharRange('0','9')
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279 || parser.anyOf("_.-:")
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280 ;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
281 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
282
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
283 private boolean matchValueChar() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
284 return parser.noneOf(" \t\r\n\"'>/=");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
285 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
286
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
287 private boolean matchSpace() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
288 return parser.anyOf(" \t\r\n");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
289 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
290
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
291 }