annotate src/goodjava/html/Html.java @ 1712:36c28be6d432

improve html and bbcode
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Jul 2022 22:14:21 -0600
parents
children 31a82b0d0a87
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;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 private Tag(String name,Map<String,Object> attributes,boolean isEmpty,String raw) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 this.name = name;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 this.attributes = attributes;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 this.isEmpty = isEmpty;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 this.raw = raw;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 public static final class Container {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 public final Tag tag;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 public final String text;
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 private Container(Tag tag,String text) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 this.tag = tag;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 this.text = text;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 }
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 static Set<String> defaultContainerTags = new HashSet<String>();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 static {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 Collections.addAll( defaultContainerTags, "script", "style", "textarea" );
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 public static List parse(String text) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 return parse(text,defaultContainerTags);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 public static List parse(String text,Set<String> containerTags) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 return new Html(text,containerTags).parse();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 private final Parser parser;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 private final Set<String> containerTags;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 private Html(String text,Set<String> containerTags) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 this.parser = new Parser(text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 this.containerTags = containerTags;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 private List parse() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 List list = new ArrayList();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 StringBuilder sb = new StringBuilder();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 while( !parser.endOfInput() ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 if( parser.test('<') ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 Tag tag = parseTag();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 if( tag != null ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 Object el = tag;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 String tagName = tag.name;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 if( containerTags.contains(tagName) ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 Container container = parseContainer(tag);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 if( container != null )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 el = container;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 if( el != null
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 || (el = parseComment()) != null
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 || (el = parseCdata()) != null
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 add(list,sb);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 list.add(el);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 continue;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 }
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 sb.append( parser.currentChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 parser.anyChar();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 add(list,sb);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 return list;
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
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 private static void add(List list,StringBuilder sb) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 if( sb.length() > 0 ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 list.add(decode(sb.toString()));
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 sb.setLength(0);
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
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 private Comment parseComment() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 if( !parser.match("<!--") )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 while( !parser.test("-->") ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 String text = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 Comment comment = new Comment(text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 return parser.success(comment);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 private CData parseCdata() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 if( !parser.match("<![CDATA[") )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 while( !parser.test("]]>") ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 String text = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 CData cdata = new CData(text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 return parser.success(cdata);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 private Container parseContainer(Tag tag) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 String endTagName = '/' + tag.name;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 int start = parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 int end;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 while(true) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 if( parser.test('<') ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 end = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 Tag tag2 = parseTag();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 if( tag2.name.equals(endTagName) )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 break;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 String text = parser.text.substring(start,end);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 Container container = new Container(tag,text);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 return parser.success(container);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 private Tag parseTag() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 int tagStart = parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 if( !parser.match('<') )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205 parser.match('/');
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 if( !matchNameChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 while( matchNameChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
209 String name = parser.textFrom(start).toLowerCase();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 Map<String,Object> attributes = new HashMap<String,Object>();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211 String attrName;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
212 while( (attrName = parseAttrName()) != null ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 String attrValue = parseAttrValue();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 attributes.put( attrName, attrValue!=null ? attrValue : true );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
215 /*
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
216 if( attrName.equals("style") && attrValue!=null ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
217 LuanTable style = Css.style(attrValue);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
218 if( style!=null )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
219 tbl.rawPut("style",style);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
220 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
221 */
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
222 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
223 while( matchSpace() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
224 boolean isEmpty = parser.match('/');
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
225 if( !parser.match('>') )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
226 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
227 String raw = parser.textFrom(tagStart);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
228 Tag tag = new Tag(name,attributes,isEmpty,raw);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
229 return parser.success(tag);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
230 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
231
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
232 private String parseAttrName() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
233 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
234 if( !matchSpace() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
235 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
236 while( matchSpace() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
237 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
238 if( !matchNameChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
239 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
240 while( matchNameChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
241 String name = parser.textFrom(start).toLowerCase();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
242 return parser.success(name);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
243 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
244
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
245 private String parseAttrValue() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
246 parser.begin();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
247 while( matchSpace() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
248 if( !parser.match('=') )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
249 return parser.failure(null);
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.anyOf("\"'") ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
252 char quote = parser.lastChar();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254 while( !parser.test(quote) ) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
255 if( !parser.anyChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
256 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
257 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
258 String value = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
259 parser.match(quote);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
260 value = decode(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
261 return parser.success(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
262 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
263 int start = parser.currentIndex();
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
264 if( !matchValueChar() )
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
265 return parser.failure(null);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
266 while( matchValueChar() );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
267 String value = parser.textFrom(start);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
268 value = decode(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269 return parser.success(value);
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
270 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
272 private boolean matchNameChar() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
273 return parser.inCharRange('a','z')
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
274 || parser.inCharRange('A','Z')
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
275 || parser.inCharRange('0','9')
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
276 || parser.anyOf("_.-:")
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
277 ;
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
278 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280 private boolean matchValueChar() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
281 return parser.noneOf(" \t\r\n\"'>/=");
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
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
284 private boolean matchSpace() {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
285 return parser.anyOf(" \t\r\n");
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
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
288 }