1 package freemarker.template; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 6 import freemarker.core.ParseException; 7 import freemarker.template.utility.StringUtil; 8 9 12 class SettingStringParser { 13 private String text; 14 private int p; 15 private int ln; 16 17 SettingStringParser(String text) { 18 this.text = text; 19 this.p = 0; 20 this.ln = text.length(); 21 } 22 23 ArrayList parseAsList() throws ParseException { 24 char c; 25 ArrayList seq = new ArrayList (); 26 while (true) { 27 c = skipWS(); 28 if (c == ' ') break; 29 seq.add(fetchStringValue()); 30 c = skipWS(); 31 if (c == ' ') break; 32 if (c != ',') throw new ParseException( 33 "Expected \",\" or the end of text but " 34 + "found \"" + c + "\"", 0, 0); 35 p++; 36 } 37 return seq; 38 } 39 40 HashMap parseAsImportList() throws ParseException { 41 char c; 42 HashMap map = new HashMap (); 43 while (true) { 44 c = skipWS(); 45 if (c == ' ') break; 46 String lib = fetchStringValue(); 47 48 c = skipWS(); 49 if (c == ' ') throw new ParseException( 50 "Unexpected end of text: expected \"as\"", 0, 0); 51 String s = fetchKeyword(); 52 if (!s.equalsIgnoreCase("as")) throw new ParseException( 53 "Expected \"as\", but found " + StringUtil.jQuote(s), 0, 0); 54 55 c = skipWS(); 56 if (c == ' ') throw new ParseException( 57 "Unexpected end of text: expected gate hash name", 0, 0); 58 String ns = fetchStringValue(); 59 60 map.put(ns, lib); 61 62 c = skipWS(); 63 if (c == ' ') break; 64 if (c != ',') throw new ParseException( 65 "Expected \",\" or the end of text but " 66 + "found \"" + c + "\"", 0, 0); 67 p++; 68 } 69 return map; 70 } 71 72 String fetchStringValue() throws ParseException { 73 String w = fetchWord(); 74 if (w.startsWith("'") || w.startsWith("\"")) { 75 w = w.substring(1, w.length() - 1); 76 } 77 return StringUtil.FTLStringLiteralDec(w); 78 } 79 80 String fetchKeyword() throws ParseException { 81 String w = fetchWord(); 82 if (w.startsWith("'") || w.startsWith("\"")) { 83 throw new ParseException( 84 "Keyword expected, but a string value found: " + w, 0, 0); 85 } 86 return w; 87 } 88 89 char skipWS() { 90 char c; 91 while (p < ln) { 92 c = text.charAt(p); 93 if (!Character.isWhitespace(c)) return c; 94 p++; 95 } 96 return ' '; 97 } 98 99 private String fetchWord() throws ParseException { 100 if (p == ln) throw new ParseException( 101 "Unexpeced end of text", 0, 0); 102 103 char c = text.charAt(p); 104 int b = p; 105 if (c == '\'' || c == '"') { 106 boolean escaped = false; 107 char q = c; 108 p++; 109 while (p < ln) { 110 c = text.charAt(p); 111 if (!escaped) { 112 if (c == '\\') { 113 escaped = true; 114 } else if (c == q) { 115 break; 116 } 117 } else { 118 escaped = false; 119 } 120 p++; 121 } 122 if (p == ln) { 123 throw new ParseException("Missing " + q, 0, 0); 124 } 125 p++; 126 return text.substring(b, p); 127 } else { 128 do { 129 c = text.charAt(p); 130 if (!(Character.isLetterOrDigit(c) 131 || c == '/' || c == '\\' || c == '_' 132 || c == '.' || c == '-' || c == '!' 133 || c == '*' || c == '?')) break; 134 p++; 135 } while (p < ln); 136 if (b == p) { 137 throw new ParseException("Unexpected character: " + c, 0, 0); 138 } else { 139 return text.substring(b, p); 140 } 141 } 142 } 143 } | Popular Tags |