001: /* Generated By:JavaCC: Do not edit this line. PropertyListParser.java */
002: package org.apache.commons.configuration.plist;
003:
004: import java.util.List;
005: import java.util.ArrayList;
006:
007: import org.apache.commons.configuration.HierarchicalConfiguration;
008: import org.apache.commons.configuration.HierarchicalConfiguration.Node;
009:
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.commons.codec.binary.Hex;
012:
013: /**
014: * JavaCC based parser for the PropertyList format.
015: *
016: * @author Emmanuel Bourg
017: * @version $Revision: 348244 $, $Date: 2005-11-22 21:40:57 +0100 (Di, 22 Nov 2005) $
018: */
019: class PropertyListParser implements PropertyListParserConstants {
020:
021: /**
022: * Remove the quotes at the beginning and at the end of the specified String.
023: */
024: protected String removeQuotes(String s) {
025: if (s == null) {
026: return null;
027: }
028:
029: if (s.startsWith("\"") && s.endsWith("\"") && s.length() >= 2) {
030: s = s.substring(1, s.length() - 1);
031: }
032:
033: return s;
034: }
035:
036: protected String unescapeQuotes(String s) {
037: return StringUtils.replace(s, "\\\"", "\"");
038: }
039:
040: /**
041: * Remove the white spaces and the data delimiters from the specified
042: * string and parse it as a byte array.
043: */
044: protected byte[] filterData(String s) throws ParseException {
045: if (s == null) {
046: return null;
047: }
048:
049: // remove the delimiters
050: if (s.startsWith("<") && s.endsWith(">") && s.length() >= 2) {
051: s = s.substring(1, s.length() - 1);
052: }
053:
054: // remove the white spaces
055: s = StringUtils.replaceChars(s, " \t\n\r", "");
056:
057: // add a leading 0 to ensure well formed bytes
058: if (s.length() % 2 != 0) {
059: s = "0" + s;
060: }
061:
062: // parse and return the bytes
063: try {
064: return Hex.decodeHex(s.toCharArray());
065: } catch (Exception e) {
066: throw new ParseException(e.getMessage());
067: }
068: }
069:
070: final public PropertyListConfiguration parse()
071: throws ParseException {
072: PropertyListConfiguration configuration = null;
073: configuration = Dictionary();
074: jj_consume_token(0);
075:
076: return configuration;
077: }
078:
079: final public PropertyListConfiguration Dictionary()
080: throws ParseException {
081: PropertyListConfiguration configuration = new PropertyListConfiguration();
082: List children = new ArrayList();
083: Node child = null;
084: jj_consume_token(DICT_BEGIN);
085: label_1: while (true) {
086: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
087: case STRING:
088: case QUOTED_STRING:
089: ;
090: break;
091: default:
092: jj_la1[0] = jj_gen;
093: break label_1;
094: }
095: child = Property();
096: if (child.getValue() instanceof HierarchicalConfiguration) {
097: // prune & graft the nested configuration to the parent configuration
098: HierarchicalConfiguration conf = (HierarchicalConfiguration) child
099: .getValue();
100: Node root = conf.getRoot();
101: root.setName(child.getName());
102: children.add(root);
103: } else {
104: children.add(child);
105: }
106: }
107: jj_consume_token(DICT_END);
108: for (int i = 0; i < children.size(); i++) {
109: child = (Node) children.get(i);
110: configuration.getRoot().addChild(child);
111: }
112:
113: return configuration;
114: }
115:
116: final public Node Property() throws ParseException {
117: Node node = new Node();
118: String key = String();
119: node.setName(key);
120: jj_consume_token(EQUAL);
121: Object value = Element();
122: node.setValue(value);
123: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
124: case DICT_SEPARATOR:
125: jj_consume_token(DICT_SEPARATOR);
126: break;
127: default:
128: jj_la1[1] = jj_gen;
129: ;
130: }
131:
132: return node;
133: }
134:
135: final public Object Element() throws ParseException {
136: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
137: case ARRAY_BEGIN:
138: return Array();
139:
140: case DICT_BEGIN:
141: return Dictionary();
142:
143: case STRING:
144: case QUOTED_STRING:
145: return String();
146:
147: case DATA:
148: return Data();
149:
150: default:
151: jj_la1[2] = jj_gen;
152: jj_consume_token(-1);
153: throw new ParseException();
154: }
155: }
156:
157: final public List Array() throws ParseException {
158: List list = new ArrayList();
159: Object element = null;
160: jj_consume_token(ARRAY_BEGIN);
161: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
162: case ARRAY_BEGIN:
163: case DICT_BEGIN:
164: case DATA:
165: case STRING:
166: case QUOTED_STRING:
167: element = Element();
168: list.add(element);
169: label_2: while (true) {
170: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
171: case ARRAY_SEPARATOR:
172: ;
173: break;
174: default:
175: jj_la1[3] = jj_gen;
176: break label_2;
177: }
178: jj_consume_token(ARRAY_SEPARATOR);
179: element = Element();
180: list.add(element);
181: }
182: break;
183: default:
184: jj_la1[4] = jj_gen;
185: ;
186: }
187: jj_consume_token(ARRAY_END);
188:
189: return list;
190: }
191:
192: final public String String() throws ParseException {
193: Token token = null;
194: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
195: case QUOTED_STRING:
196: token = jj_consume_token(QUOTED_STRING);
197: return unescapeQuotes(removeQuotes(token.image));
198:
199: case STRING:
200: token = jj_consume_token(STRING);
201: return token.image;
202:
203: default:
204: jj_la1[5] = jj_gen;
205: jj_consume_token(-1);
206: throw new ParseException();
207: }
208: }
209:
210: final public byte[] Data() throws ParseException {
211: Token token;
212: token = jj_consume_token(DATA);
213:
214: return filterData(token.image);
215: }
216:
217: public PropertyListParserTokenManager token_source;
218: SimpleCharStream jj_input_stream;
219: public Token token, jj_nt;
220: private int jj_ntk;
221: private int jj_gen;
222: final private int[] jj_la1 = new int[6];
223: static private int[] jj_la1_0;
224:
225: static {
226: jj_la1_0();
227: }
228:
229: private static void jj_la1_0() {
230: jj_la1_0 = new int[] { 0x180000, 0x400, 0x1c0120, 0x80,
231: 0x1c0120, 0x180000, };
232: }
233:
234: public PropertyListParser(java.io.InputStream stream) {
235: jj_input_stream = new SimpleCharStream(stream, 1, 1);
236: token_source = new PropertyListParserTokenManager(
237: jj_input_stream);
238: token = new Token();
239: jj_ntk = -1;
240: jj_gen = 0;
241: for (int i = 0; i < 6; i++)
242: jj_la1[i] = -1;
243: }
244:
245: public PropertyListParser(java.io.Reader stream) {
246: jj_input_stream = new SimpleCharStream(stream, 1, 1);
247: token_source = new PropertyListParserTokenManager(
248: jj_input_stream);
249: token = new Token();
250: jj_ntk = -1;
251: jj_gen = 0;
252: for (int i = 0; i < 6; i++)
253: jj_la1[i] = -1;
254: }
255:
256: final private Token jj_consume_token(int kind)
257: throws ParseException {
258: Token oldToken;
259: if ((oldToken = token).next != null)
260: token = token.next;
261: else
262: token = token.next = token_source.getNextToken();
263: jj_ntk = -1;
264: if (token.kind == kind) {
265: jj_gen++;
266: return token;
267: }
268: token = oldToken;
269: jj_kind = kind;
270: throw generateParseException();
271: }
272:
273: final private int jj_ntk() {
274: if ((jj_nt = token.next) == null)
275: return (jj_ntk = (token.next = token_source.getNextToken()).kind);
276: else
277: return (jj_ntk = jj_nt.kind);
278: }
279:
280: private java.util.Vector jj_expentries = new java.util.Vector();
281: private int[] jj_expentry;
282: private int jj_kind = -1;
283:
284: public ParseException generateParseException() {
285: jj_expentries.removeAllElements();
286: boolean[] la1tokens = new boolean[22];
287: for (int i = 0; i < 22; i++) {
288: la1tokens[i] = false;
289: }
290: if (jj_kind >= 0) {
291: la1tokens[jj_kind] = true;
292: jj_kind = -1;
293: }
294: for (int i = 0; i < 6; i++) {
295: if (jj_la1[i] == jj_gen) {
296: for (int j = 0; j < 32; j++) {
297: if ((jj_la1_0[i] & (1 << j)) != 0) {
298: la1tokens[j] = true;
299: }
300: }
301: }
302: }
303: for (int i = 0; i < 22; i++) {
304: if (la1tokens[i]) {
305: jj_expentry = new int[1];
306: jj_expentry[0] = i;
307: jj_expentries.addElement(jj_expentry);
308: }
309: }
310: int[][] exptokseq = new int[jj_expentries.size()][];
311: for (int i = 0; i < jj_expentries.size(); i++) {
312: exptokseq[i] = (int[]) jj_expentries.elementAt(i);
313: }
314: return new ParseException(token, exptokseq, tokenImage);
315: }
316:
317: }
|