annotate src/goodjava/html/Css.java @ 1805:7793bcb5f34a

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 08 May 2024 11:13:16 -0600
parents 31a82b0d0a87
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
1 package goodjava.html;
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
3 import java.util.Map;
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
4 import java.util.LinkedHashMap;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
5 import goodjava.parser.Parser;
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 public final class Css {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
10 public static Map<String,String> style(String text) {
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
11 return new Css(text).parseStyle();
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 private final Parser parser;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
16 private Css(String text) {
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 this.parser = new Parser(text);
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
20 private Map<String,String> parseStyle() {
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
21 Map<String,String> map = new LinkedHashMap<String,String>();
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 while( !parser.endOfInput() ) {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 int start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 if( !matchPropertyChar() )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 while( matchPropertyChar() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 String prop = parser.textFrom(start).toLowerCase();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 if( !parser.match(':') )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 while( !parser.endOfInput() && parser.noneOf(";") );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 String val = parser.textFrom(start).trim();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
38 map.put(prop,val);
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 parser.match(';');
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 }
1714
31a82b0d0a87 bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
42 return map;
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 private boolean matchPropertyChar() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 return parser.inCharRange('a','z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 || parser.inCharRange('A','Z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 || parser.inCharRange('0','9')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 || parser.anyOf("_-")
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 ;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 private boolean matchSpace() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 return parser.anyOf(" \t\r\n");
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }