View Javadoc

    1   /* Generated By:JavaCC: Do not edit this line. PropertyListParser.java */
    2   package org.apache.commons.configuration.plist;
    3
    4   import java.util.Date;
    5   import java.util.List;
    6   import java.util.ArrayList;
    7
    8   import org.apache.commons.configuration.HierarchicalConfiguration;
    9   import org.apache.commons.configuration.HierarchicalConfiguration.Node;
    10
    11  import org.apache.commons.lang.StringUtils;
    12  import org.apache.commons.codec.binary.Hex;
    13
    14  /***
    15   * JavaCC based parser for the PropertyList format.
    16   *
    17   * @author Emmanuel Bourg
    18   * @version $Revision: 590474 $, $Date: 2007-10-30 22:35:11 +0100 (Di, 30 Okt 2007) $
    19   */
    20  class PropertyListParser implements PropertyListParserConstants {
    21
    22      /***
    23       * Remove the quotes at the beginning and at the end of the specified String.
    24       */
    25      protected String removeQuotes(String s)
    26      {
    27          if (s == null)
    28          {
    29              return null;
    30          }
    31
    32          if (s.startsWith("\"") && s.endsWith("\"") && s.length() >= 2)
    33          {
    34              s = s.substring(1, s.length() - 1);
    35          }
    36
    37          return s;
    38      }
    39
    40      protected String unescapeQuotes(String s)
    41      {
    42          return StringUtils.replace(s, "//\"", "\"");
    43      }
    44
    45      /***
    46       * Remove the white spaces and the data delimiters from the specified
    47       * string and parse it as a byte array.
    48       */
    49      protected byte[] filterData(String s) throws ParseException
    50      {
    51          if (s == null)
    52          {
    53              return null;
    54          }
    55
    56          // remove the delimiters
    57          if (s.startsWith("<") && s.endsWith(">") && s.length() >= 2)
    58          {
    59              s = s.substring(1, s.length() - 1);
    60          }
    61
    62          // remove the white spaces
    63          s = StringUtils.replaceChars(s, " \t\n\r", "");
    64
    65          // add a leading 0 to ensure well formed bytes
    66          if (s.length() % 2 != 0)
    67          {
    68              s = "0" + s;
    69          }
    70
    71          // parse and return the bytes
    72          try
    73          {
    74              return Hex.decodeHex(s.toCharArray());
    75          }
    76          catch (Exception e)
    77          {
    78              throw (ParseException) new ParseException("Unable to parse the byte[] : " + e.getMessage());
    79          }
    80      }
    81
    82      /***
    83       * Parse a date formatted as <*D2002-03-22 11:30:00 +0100>
    84       */
    85      protected Date parseDate(String s) throws ParseException
    86      {
    87          return PropertyListConfiguration.parseDate(s);
    88      }
    89
    90    final public PropertyListConfiguration parse() throws ParseException {
    91      PropertyListConfiguration configuration = null;
    92      configuration = Dictionary();
    93      jj_consume_token(0);
    94        {if (true) return configuration;}
    95      throw new Error("Missing return statement in function");
    96    }
    97
    98    final public PropertyListConfiguration Dictionary() throws ParseException {
    99      PropertyListConfiguration configuration = new PropertyListConfiguration();
    100     List children = new ArrayList();
    101     Node child = null;
    102     jj_consume_token(DICT_BEGIN);
    103     label_1:
    104     while (true) {
    105       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    106       case STRING:
    107       case QUOTED_STRING:
    108         ;
    109         break;
    110       default:
    111         jj_la1[0] = jj_gen;
    112         break label_1;
    113       }
    114       child = Property();
    115             if (child.getValue() instanceof HierarchicalConfiguration)
    116             {
    117                 // prune & graft the nested configuration to the parent configuration
    118                 HierarchicalConfiguration conf = (HierarchicalConfiguration) child.getValue();
    119                 Node root = conf.getRoot();
    120                 root.setName(child.getName());
    121                 children.add(root);
    122             }
    123             else
    124             {
    125                 children.add(child);
    126             }
    127     }
    128     jj_consume_token(DICT_END);
    129         for (int i = 0; i < children.size(); i++)
    130         {
    131             child = (Node) children.get(i);
    132             configuration.getRoot().addChild(child);
    133         }
    134
    135         {if (true) return configuration;}
    136     throw new Error("Missing return statement in function");
    137   }
    138
    139   final public Node Property() throws ParseException {
    140     String key = null;
    141     Object value = null;
    142     Node node = new Node();
    143     key = String();
    144       node.setName(key);
    145     jj_consume_token(EQUAL);
    146     value = Element();
    147       node.setValue(value);
    148     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    149     case DICT_SEPARATOR:
    150       jj_consume_token(DICT_SEPARATOR);
    151       break;
    152     default:
    153       jj_la1[1] = jj_gen;
    154       ;
    155     }
    156       {if (true) return node;}
    157     throw new Error("Missing return statement in function");
    158   }
    159
    160   final public Object Element() throws ParseException {
    161     Object value = null;
    162     if (jj_2_1(2)) {
    163       value = Array();
    164       {if (true) return value;}
    165     } else {
    166       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    167       case DICT_BEGIN:
    168         value = Dictionary();
    169       {if (true) return value;}
    170         break;
    171       case STRING:
    172       case QUOTED_STRING:
    173         value = String();
    174       {if (true) return value;}
    175         break;
    176       case DATA:
    177         value = Data();
    178       {if (true) return value;}
    179         break;
    180       case DATE:
    181         value = Date();
    182       {if (true) return value;}
    183         break;
    184       default:
    185         jj_la1[2] = jj_gen;
    186         jj_consume_token(-1);
    187         throw new ParseException();
    188       }
    189     }
    190     throw new Error("Missing return statement in function");
    191   }
    192
    193   final public List Array() throws ParseException {
    194     List list = new ArrayList();
    195     Object element = null;
    196     jj_consume_token(ARRAY_BEGIN);
    197     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    198     case ARRAY_BEGIN:
    199     case DICT_BEGIN:
    200     case DATA:
    201     case DATE:
    202     case STRING:
    203     case QUOTED_STRING:
    204       element = Element();
    205           list.add(element);
    206       label_2:
    207       while (true) {
    208         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    209         case ARRAY_SEPARATOR:
    210           ;
    211           break;
    212         default:
    213           jj_la1[3] = jj_gen;
    214           break label_2;
    215         }
    216         jj_consume_token(ARRAY_SEPARATOR);
    217         element = Element();
    218               list.add(element);
    219       }
    220       break;
    221     default:
    222       jj_la1[4] = jj_gen;
    223       ;
    224     }
    225     jj_consume_token(ARRAY_END);
    226       {if (true) return list;}
    227     throw new Error("Missing return statement in function");
    228   }
    229
    230   final public String String() throws ParseException {
    231     Token token = null;
    232     String value = null;
    233     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    234     case QUOTED_STRING:
    235       token = jj_consume_token(QUOTED_STRING);
    236       {if (true) return unescapeQuotes(removeQuotes(token.image));}
    237       break;
    238     case STRING:
    239       token = jj_consume_token(STRING);
    240       {if (true) return token.image;}
    241       break;
    242     default:
    243       jj_la1[5] = jj_gen;
    244       jj_consume_token(-1);
    245       throw new ParseException();
    246     }
    247     throw new Error("Missing return statement in function");
    248   }
    249
    250   final public byte[] Data() throws ParseException {
    251     Token token;
    252     token = jj_consume_token(DATA);
    253       {if (true) return filterData(token.image);}
    254     throw new Error("Missing return statement in function");
    255   }
    256
    257   final public Date Date() throws ParseException {
    258     Token token;
    259     token = jj_consume_token(DATE);
    260       {if (true) return parseDate(token.image);}
    261     throw new Error("Missing return statement in function");
    262   }
    263
    264   final private boolean jj_2_1(int xla) {
    265     jj_la = xla; jj_lastpos = jj_scanpos = token;
    266     try { return !jj_3_1(); }
    267     catch(LookaheadSuccess ls) { return true; }
    268     finally { jj_save(0, xla); }
    269   }
    270
    271   final private boolean jj_3R_14() {
    272     if (jj_scan_token(QUOTED_STRING)) return true;
    273     return false;
    274   }
    275
    276   final private boolean jj_3R_11() {
    277     Token xsp;
    278     xsp = jj_scanpos;
    279     if (jj_3R_14()) {
    280     jj_scanpos = xsp;
    281     if (jj_3R_15()) return true;
    282     }
    283     return false;
    284   }
    285
    286   final private boolean jj_3R_13() {
    287     if (jj_scan_token(DATE)) return true;
    288     return false;
    289   }
    290
    291   final private boolean jj_3R_10() {
    292     if (jj_scan_token(DICT_BEGIN)) return true;
    293     return false;
    294   }
    295
    296   final private boolean jj_3R_9() {
    297     if (jj_3R_13()) return true;
    298     return false;
    299   }
    300
    301   final private boolean jj_3R_8() {
    302     if (jj_3R_12()) return true;
    303     return false;
    304   }
    305
    306   final private boolean jj_3R_12() {
    307     if (jj_scan_token(DATA)) return true;
    308     return false;
    309   }
    310
    311   final private boolean jj_3R_7() {
    312     if (jj_3R_11()) return true;
    313     return false;
    314   }
    315
    316   final private boolean jj_3R_4() {
    317     if (jj_3R_5()) return true;
    318     return false;
    319   }
    320
    321   final private boolean jj_3R_6() {
    322     if (jj_3R_10()) return true;
    323     return false;
    324   }
    325
    326   final private boolean jj_3R_15() {
    327     if (jj_scan_token(STRING)) return true;
    328     return false;
    329   }
    330
    331   final private boolean jj_3R_3() {
    332     if (jj_scan_token(ARRAY_BEGIN)) return true;
    333     Token xsp;
    334     xsp = jj_scanpos;
    335     if (jj_3R_4()) jj_scanpos = xsp;
    336     if (jj_scan_token(ARRAY_END)) return true;
    337     return false;
    338   }
    339
    340   final private boolean jj_3_1() {
    341     if (jj_3R_3()) return true;
    342     return false;
    343   }
    344
    345   final private boolean jj_3R_5() {
    346     Token xsp;
    347     xsp = jj_scanpos;
    348     if (jj_3_1()) {
    349     jj_scanpos = xsp;
    350     if (jj_3R_6()) {
    351     jj_scanpos = xsp;
    352     if (jj_3R_7()) {
    353     jj_scanpos = xsp;
    354     if (jj_3R_8()) {
    355     jj_scanpos = xsp;
    356     if (jj_3R_9()) return true;
    357     }
    358     }
    359     }
    360     }
    361     return false;
    362   }
    363
    364   public PropertyListParserTokenManager token_source;
    365   SimpleCharStream jj_input_stream;
    366   public Token token, jj_nt;
    367   private int jj_ntk;
    368   private Token jj_scanpos, jj_lastpos;
    369   private int jj_la;
    370   public boolean lookingAhead = false;
    371   private boolean jj_semLA;
    372   private int jj_gen;
    373   final private int[] jj_la1 = new int[6];
    374   static private int[] jj_la1_0;
    375   static {
    376       jj_la1_0();
    377    }
    378    private static void jj_la1_0() {
    379       jj_la1_0 = new int[] {0x600000,0x400,0x780100,0x80,0x780120,0x600000,};
    380    }
    381   final private JJCalls[] jj_2_rtns = new JJCalls[1];
    382   private boolean jj_rescan = false;
    383   private int jj_gc = 0;
    384
    385   public PropertyListParser(java.io.InputStream stream) {
    386     jj_input_stream = new SimpleCharStream(stream, 1, 1);
    387     token_source = new PropertyListParserTokenManager(jj_input_stream);
    388     token = new Token();
    389     jj_ntk = -1;
    390     jj_gen = 0;
    391     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
    392     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
    393   }
    394
    395   public void ReInit(java.io.InputStream stream) {
    396     jj_input_stream.ReInit(stream, 1, 1);
    397     token_source.ReInit(jj_input_stream);
    398     token = new Token();
    399     jj_ntk = -1;
    400     jj_gen = 0;
    401     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
    402     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
    403   }
    404
    405   public PropertyListParser(java.io.Reader stream) {
    406     jj_input_stream = new SimpleCharStream(stream, 1, 1);
    407     token_source = new PropertyListParserTokenManager(jj_input_stream);
    408     token = new Token();
    409     jj_ntk = -1;
    410     jj_gen = 0;
    411     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
    412     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
    413   }
    414
    415   public void ReInit(java.io.Reader stream) {
    416     jj_input_stream.ReInit(stream, 1, 1);
    417     token_source.ReInit(jj_input_stream);
    418     token = new Token();
    419     jj_ntk = -1;
    420     jj_gen = 0;
    421     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
    422     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
    423   }
    424
    425   public PropertyListParser(PropertyListParserTokenManager tm) {
    426     token_source = tm;
    427     token = new Token();
    428     jj_ntk = -1;
    429     jj_gen = 0;
    430     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
    431     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
    432   }
    433
    434   public void ReInit(PropertyListParserTokenManager tm) {
    435     token_source = tm;
    436     token = new Token();
    437     jj_ntk = -1;
    438     jj_gen = 0;
    439     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
    440     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
    441   }
    442
    443   final private Token jj_consume_token(int kind) throws ParseException {
    444     Token oldToken;
    445     if ((oldToken = token).next != null) token = token.next;
    446     else token = token.next = token_source.getNextToken();
    447     jj_ntk = -1;
    448     if (token.kind == kind) {
    449       jj_gen++;
    450       if (++jj_gc > 100) {
    451         jj_gc = 0;
    452         for (int i = 0; i < jj_2_rtns.length; i++) {
    453           JJCalls c = jj_2_rtns[i];
    454           while (c != null) {
    455             if (c.gen < jj_gen) c.first = null;
    456             c = c.next;
    457           }
    458         }
    459       }
    460       return token;
    461     }
    462     token = oldToken;
    463     jj_kind = kind;
    464     throw generateParseException();
    465   }
    466
    467   static private final class LookaheadSuccess extends java.lang.Error { }
    468   final private LookaheadSuccess jj_ls = new LookaheadSuccess();
    469   final private boolean jj_scan_token(int kind) {
    470     if (jj_scanpos == jj_lastpos) {
    471       jj_la--;
    472       if (jj_scanpos.next == null) {
    473         jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
    474       } else {
    475         jj_lastpos = jj_scanpos = jj_scanpos.next;
    476       }
    477     } else {
    478       jj_scanpos = jj_scanpos.next;
    479     }
    480     if (jj_rescan) {
    481       int i = 0; Token tok = token;
    482       while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
    483       if (tok != null) jj_add_error_token(kind, i);
    484     }
    485     if (jj_scanpos.kind != kind) return true;
    486     if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
    487     return false;
    488   }
    489
    490   final public Token getNextToken() {
    491     if (token.next != null) token = token.next;
    492     else token = token.next = token_source.getNextToken();
    493     jj_ntk = -1;
    494     jj_gen++;
    495     return token;
    496   }
    497
    498   final public Token getToken(int index) {
    499     Token t = lookingAhead ? jj_scanpos : token;
    500     for (int i = 0; i < index; i++) {
    501       if (t.next != null) t = t.next;
    502       else t = t.next = token_source.getNextToken();
    503     }
    504     return t;
    505   }
    506
    507   final private int jj_ntk() {
    508     if ((jj_nt=token.next) == null)
    509       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
    510     else
    511       return (jj_ntk = jj_nt.kind);
    512   }
    513
    514   private java.util.Vector jj_expentries = new java.util.Vector();
    515   private int[] jj_expentry;
    516   private int jj_kind = -1;
    517   private int[] jj_lasttokens = new int[100];
    518   private int jj_endpos;
    519
    520   private void jj_add_error_token(int kind, int pos) {
    521     if (pos >= 100) return;
    522     if (pos == jj_endpos + 1) {
    523       jj_lasttokens[jj_endpos++] = kind;
    524     } else if (jj_endpos != 0) {
    525       jj_expentry = new int[jj_endpos];
    526       for (int i = 0; i < jj_endpos; i++) {
    527         jj_expentry[i] = jj_lasttokens[i];
    528       }
    529       boolean exists = false;
    530       for (java.util.Enumeration e = jj_expentries.elements(); e.hasMoreElements();) {
    531         int[] oldentry = (int[])(e.nextElement());
    532         if (oldentry.length == jj_expentry.length) {
    533           exists = true;
    534           for (int i = 0; i < jj_expentry.length; i++) {
    535             if (oldentry[i] != jj_expentry[i]) {
    536               exists = false;
    537               break;
    538             }
    539           }
    540           if (exists) break;
    541         }
    542       }
    543       if (!exists) jj_expentries.addElement(jj_expentry);
    544       if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
    545     }
    546   }
    547
    548   public ParseException generateParseException() {
    549     jj_expentries.removeAllElements();
    550     boolean[] la1tokens = new boolean[24];
    551     for (int i = 0; i < 24; i++) {
    552       la1tokens[i] = false;
    553     }
    554     if (jj_kind >= 0) {
    555       la1tokens[jj_kind] = true;
    556       jj_kind = -1;
    557     }
    558     for (int i = 0; i < 6; i++) {
    559       if (jj_la1[i] == jj_gen) {
    560         for (int j = 0; j < 32; j++) {
    561           if ((jj_la1_0[i] & (1<<j)) != 0) {
    562             la1tokens[j] = true;
    563           }
    564         }
    565       }
    566     }
    567     for (int i = 0; i < 24; i++) {
    568       if (la1tokens[i]) {
    569         jj_expentry = new int[1];
    570         jj_expentry[0] = i;
    571         jj_expentries.addElement(jj_expentry);
    572       }
    573     }
    574     jj_endpos = 0;
    575     jj_rescan_token();
    576     jj_add_error_token(0, 0);
    577     int[][] exptokseq = new int[jj_expentries.size()][];
    578     for (int i = 0; i < jj_expentries.size(); i++) {
    579       exptokseq[i] = (int[])jj_expentries.elementAt(i);
    580     }
    581     return new ParseException(token, exptokseq, tokenImage);
    582   }
    583
    584   final public void enable_tracing() {
    585   }
    586
    587   final public void disable_tracing() {
    588   }
    589
    590   final private void jj_rescan_token() {
    591     jj_rescan = true;
    592     for (int i = 0; i < 1; i++) {
    593       JJCalls p = jj_2_rtns[i];
    594       do {
    595         if (p.gen > jj_gen) {
    596           jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
    597           switch (i) {
    598             case 0: jj_3_1(); break;
    599           }
    600         }
    601         p = p.next;
    602       } while (p != null);
    603     }
    604     jj_rescan = false;
    605   }
    606
    607   final private void jj_save(int index, int xla) {
    608     JJCalls p = jj_2_rtns[index];
    609     while (p.gen > jj_gen) {
    610       if (p.next == null) { p = p.next = new JJCalls(); break; }
    611       p = p.next;
    612     }
    613     p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
    614   }
    615
    616   static final class JJCalls {
    617     int gen;
    618     Token first;
    619     int arg;
    620     JJCalls next;
    621   }
    622
    623 }