PropertyListParser.java

  1. /* Generated By:JavaCC: Do not edit this line. PropertyListParser.java */
  2. package org.apache.commons.configuration2.plist;

  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.ArrayList;

  6. import org.apache.commons.configuration2.HierarchicalConfiguration;
  7. import org.apache.commons.configuration2.tree.ImmutableNode;

  8. import org.apache.commons.codec.binary.Hex;

  9. /**
  10.  * JavaCC based parser for the PropertyList format generated from {@code src/main/javacc/PropertyListParser.jj}.
  11.  */
  12. class PropertyListParser implements PropertyListParserConstants {

  13.     /**
  14.      * Remove the quotes at the beginning and at the end of the specified String.
  15.      */
  16.     protected String removeQuotes(String s) {
  17.         if (s == null) {
  18.             return null;
  19.         }
  20.         if (s.startsWith("\u005c"") && s.endsWith("\u005c"") && s.length() >= 2) {
  21.             s = s.substring(1, s.length() - 1);
  22.         }
  23.         return s;
  24.     }

  25.     protected String unescapeQuotes(String s) {
  26.         return s.replaceAll("\u005c\u005c\u005c\u005c\u005c"", "\u005c"");
  27.     }

  28.     /**
  29.      * Remove the white spaces and the data delimiters from the specified
  30.      * string and parse it as a byte array.
  31.      */
  32.     protected byte[] filterData(String s) throws ParseException {
  33.         if (s == null) {
  34.             return null;
  35.         }
  36.         // remove the delimiters
  37.         if (s.startsWith("<") && s.endsWith(">") && s.length() >= 2) {
  38.             s = s.substring(1, s.length() - 1);
  39.         }
  40.         // remove the white spaces
  41.         s = s.replaceAll("\u005c\u005cs", "");
  42.         // add a leading 0 to ensure well formed bytes
  43.         if (s.length() % 2 != 0) {
  44.             s = "0" + s;
  45.         }
  46.         // parse and return the bytes
  47.         try {
  48.             return Hex.decodeHex(s.toCharArray());
  49.         } catch (Exception e) {
  50.             throw (ParseException) new ParseException("Unable to parse the byte[] : " + e.getMessage());
  51.         }
  52.     }

  53.     /**
  54.      * Parse a date formatted as <*D2002-03-22 11:30:00 +0100>
  55.      */
  56.     protected Date parseDate(String s) throws ParseException {
  57.         return PropertyListConfiguration.parseDate(s);
  58.     }

  59.   final public PropertyListConfiguration parse() throws ParseException {
  60.     PropertyListConfiguration configuration = null;
  61.     configuration = Dictionary();
  62.     jj_consume_token(0);
  63.       {if (true) return configuration;}
  64.     throw new Error("Missing return statement in function");
  65.   }

  66.   final public PropertyListConfiguration Dictionary() throws ParseException {
  67.     ImmutableNode.Builder builder = new ImmutableNode.Builder();
  68.     ImmutableNode child = null;
  69.     jj_consume_token(DICT_BEGIN);
  70.     label_1:
  71.     while (true) {
  72.       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  73.       case STRING:
  74.       case QUOTED_STRING:
  75.         ;
  76.         break;
  77.       default:
  78.         jj_la1[0] = jj_gen;
  79.         break label_1;
  80.       }
  81.       child = Property();
  82.             if (child.getValue() instanceof HierarchicalConfiguration)
  83.             {
  84.                 // prune & graft the nested configuration to the parent configuration
  85.                 @SuppressWarnings("unchecked") // we created this configuration
  86.                 HierarchicalConfiguration<ImmutableNode> conf =
  87.                     (HierarchicalConfiguration<ImmutableNode>) child.getValue();
  88.                 ImmutableNode root = conf.getNodeModel().getNodeHandler().getRootNode();
  89.                 ImmutableNode.Builder childBuilder = new ImmutableNode.Builder();
  90.                 childBuilder.name(child.getNodeName()).value(root.getValue())
  91.                   .addChildren(root.getChildren());
  92.                 builder.addChild(childBuilder.create());
  93.             }
  94.             else
  95.             {
  96.                 builder.addChild(child);
  97.             }
  98.     }
  99.     jj_consume_token(DICT_END);
  100.         {if (true) return new PropertyListConfiguration(builder.create());}
  101.     throw new Error("Missing return statement in function");
  102.   }

  103.   final public ImmutableNode Property() throws ParseException {
  104.     String key = null;
  105.     Object value = null;
  106.     ImmutableNode.Builder node = new ImmutableNode.Builder();
  107.     key = String();
  108.       node.name(key);
  109.     jj_consume_token(EQUAL);
  110.     value = Element();
  111.       node.value(value);
  112.     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  113.     case DICT_SEPARATOR:
  114.       jj_consume_token(DICT_SEPARATOR);
  115.       break;
  116.     default:
  117.       jj_la1[1] = jj_gen;
  118.       ;
  119.     }
  120.       {if (true) return node.create();}
  121.     throw new Error("Missing return statement in function");
  122.   }

  123.   final public Object Element() throws ParseException {
  124.     Object value = null;
  125.     if (jj_2_1(2)) {
  126.       value = Array();
  127.       {if (true) return value;}
  128.     } else {
  129.       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  130.       case DICT_BEGIN:
  131.         value = Dictionary();
  132.       {if (true) return value;}
  133.         break;
  134.       case STRING:
  135.       case QUOTED_STRING:
  136.         value = String();
  137.       {if (true) return value;}
  138.         break;
  139.       case DATA:
  140.         value = Data();
  141.       {if (true) return value;}
  142.         break;
  143.       case DATE:
  144.         value = Date();
  145.       {if (true) return value;}
  146.         break;
  147.       default:
  148.         jj_la1[2] = jj_gen;
  149.         jj_consume_token(-1);
  150.         throw new ParseException();
  151.       }
  152.     }
  153.     throw new Error("Missing return statement in function");
  154.   }

  155.   final public List Array() throws ParseException {
  156.     List<Object> list = new ArrayList<Object>();
  157.     Object element = null;
  158.     jj_consume_token(ARRAY_BEGIN);
  159.     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  160.     case ARRAY_BEGIN:
  161.     case DICT_BEGIN:
  162.     case DATA:
  163.     case DATE:
  164.     case STRING:
  165.     case QUOTED_STRING:
  166.       element = Element();
  167.           list.add(element);
  168.       label_2:
  169.       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.       {if (true) return list;}
  189.     throw new Error("Missing return statement in function");
  190.   }

  191.   final public String String() throws ParseException {
  192.     Token token = null;
  193.     String value = null;
  194.     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  195.     case QUOTED_STRING:
  196.       token = jj_consume_token(QUOTED_STRING);
  197.       {if (true) return unescapeQuotes(removeQuotes(token.image));}
  198.       break;
  199.     case STRING:
  200.       token = jj_consume_token(STRING);
  201.       {if (true) return token.image;}
  202.       break;
  203.     default:
  204.       jj_la1[5] = jj_gen;
  205.       jj_consume_token(-1);
  206.       throw new ParseException();
  207.     }
  208.     throw new Error("Missing return statement in function");
  209.   }

  210.   final public byte[] Data() throws ParseException {
  211.     Token token;
  212.     token = jj_consume_token(DATA);
  213.       {if (true) return filterData(token.image);}
  214.     throw new Error("Missing return statement in function");
  215.   }

  216.   final public Date Date() throws ParseException {
  217.     Token token;
  218.     token = jj_consume_token(DATE);
  219.       {if (true) return parseDate(token.image);}
  220.     throw new Error("Missing return statement in function");
  221.   }

  222.   private boolean jj_2_1(int xla) {
  223.     jj_la = xla; jj_lastpos = jj_scanpos = token;
  224.     try { return !jj_3_1(); }
  225.     catch(LookaheadSuccess ls) { return true; }
  226.     finally { jj_save(0, xla); }
  227.   }

  228.   private boolean jj_3R_15() {
  229.     if (jj_scan_token(STRING)) return true;
  230.     return false;
  231.   }

  232.   private boolean jj_3R_3() {
  233.     if (jj_scan_token(ARRAY_BEGIN)) return true;
  234.     Token xsp;
  235.     xsp = jj_scanpos;
  236.     if (jj_3R_4()) jj_scanpos = xsp;
  237.     if (jj_scan_token(ARRAY_END)) return true;
  238.     return false;
  239.   }

  240.   private boolean jj_3_1() {
  241.     if (jj_3R_3()) return true;
  242.     return false;
  243.   }

  244.   private boolean jj_3R_5() {
  245.     Token xsp;
  246.     xsp = jj_scanpos;
  247.     if (jj_3_1()) {
  248.     jj_scanpos = xsp;
  249.     if (jj_3R_6()) {
  250.     jj_scanpos = xsp;
  251.     if (jj_3R_7()) {
  252.     jj_scanpos = xsp;
  253.     if (jj_3R_8()) {
  254.     jj_scanpos = xsp;
  255.     if (jj_3R_9()) return true;
  256.     }
  257.     }
  258.     }
  259.     }
  260.     return false;
  261.   }

  262.   private boolean jj_3R_14() {
  263.     if (jj_scan_token(QUOTED_STRING)) return true;
  264.     return false;
  265.   }

  266.   private boolean jj_3R_11() {
  267.     Token xsp;
  268.     xsp = jj_scanpos;
  269.     if (jj_3R_14()) {
  270.     jj_scanpos = xsp;
  271.     if (jj_3R_15()) return true;
  272.     }
  273.     return false;
  274.   }

  275.   private boolean jj_3R_10() {
  276.     if (jj_scan_token(DICT_BEGIN)) return true;
  277.     return false;
  278.   }

  279.   private boolean jj_3R_13() {
  280.     if (jj_scan_token(DATE)) return true;
  281.     return false;
  282.   }

  283.   private boolean jj_3R_9() {
  284.     if (jj_3R_13()) return true;
  285.     return false;
  286.   }

  287.   private boolean jj_3R_8() {
  288.     if (jj_3R_12()) return true;
  289.     return false;
  290.   }

  291.   private boolean jj_3R_12() {
  292.     if (jj_scan_token(DATA)) return true;
  293.     return false;
  294.   }

  295.   private boolean jj_3R_7() {
  296.     if (jj_3R_11()) return true;
  297.     return false;
  298.   }

  299.   private boolean jj_3R_4() {
  300.     if (jj_3R_5()) return true;
  301.     return false;
  302.   }

  303.   private boolean jj_3R_6() {
  304.     if (jj_3R_10()) return true;
  305.     return false;
  306.   }

  307.   /** Generated Token Manager. */
  308.   public PropertyListParserTokenManager token_source;
  309.   SimpleCharStream jj_input_stream;
  310.   /** Current token. */
  311.   public Token token;
  312.   /** Next token. */
  313.   public Token jj_nt;
  314.   private int jj_ntk;
  315.   private Token jj_scanpos, jj_lastpos;
  316.   private int jj_la;
  317.   private int jj_gen;
  318.   final private int[] jj_la1 = new int[6];
  319.   static private int[] jj_la1_0;
  320.   static {
  321.       jj_la1_init_0();
  322.    }
  323.    private static void jj_la1_init_0() {
  324.       jj_la1_0 = new int[] {0x18000000,0x10000,0x1e004000,0x2000,0x1e004800,0x18000000,};
  325.    }
  326.   final private JJCalls[] jj_2_rtns = new JJCalls[1];
  327.   private boolean jj_rescan = false;
  328.   private int jj_gc = 0;

  329.   /** Constructor with InputStream. */
  330.   public PropertyListParser(java.io.InputStream stream) {
  331.      this(stream, null);
  332.   }
  333.   /** Constructor with InputStream and supplied encoding */
  334.   public PropertyListParser(java.io.InputStream stream, String encoding) {
  335.     try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
  336.     token_source = new PropertyListParserTokenManager(jj_input_stream);
  337.     token = new Token();
  338.     jj_ntk = -1;
  339.     jj_gen = 0;
  340.     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
  341.     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  342.   }

  343.   /** Reinitialise. */
  344.   public void ReInit(java.io.InputStream stream) {
  345.      ReInit(stream, null);
  346.   }
  347.   /** Reinitialise. */
  348.   public void ReInit(java.io.InputStream stream, String encoding) {
  349.     try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
  350.     token_source.ReInit(jj_input_stream);
  351.     token = new Token();
  352.     jj_ntk = -1;
  353.     jj_gen = 0;
  354.     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
  355.     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  356.   }

  357.   /** Constructor. */
  358.   public PropertyListParser(java.io.Reader stream) {
  359.     jj_input_stream = new SimpleCharStream(stream, 1, 1);
  360.     token_source = new PropertyListParserTokenManager(jj_input_stream);
  361.     token = new Token();
  362.     jj_ntk = -1;
  363.     jj_gen = 0;
  364.     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
  365.     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  366.   }

  367.   /** Reinitialise. */
  368.   public void ReInit(java.io.Reader stream) {
  369.     jj_input_stream.ReInit(stream, 1, 1);
  370.     token_source.ReInit(jj_input_stream);
  371.     token = new Token();
  372.     jj_ntk = -1;
  373.     jj_gen = 0;
  374.     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
  375.     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  376.   }

  377.   /** Constructor with generated Token Manager. */
  378.   public PropertyListParser(PropertyListParserTokenManager tm) {
  379.     token_source = tm;
  380.     token = new Token();
  381.     jj_ntk = -1;
  382.     jj_gen = 0;
  383.     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
  384.     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  385.   }

  386.   /** Reinitialise. */
  387.   public void ReInit(PropertyListParserTokenManager tm) {
  388.     token_source = tm;
  389.     token = new Token();
  390.     jj_ntk = -1;
  391.     jj_gen = 0;
  392.     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
  393.     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  394.   }

  395.   private Token jj_consume_token(int kind) throws ParseException {
  396.     Token oldToken;
  397.     if ((oldToken = token).next != null) token = token.next;
  398.     else token = token.next = token_source.getNextToken();
  399.     jj_ntk = -1;
  400.     if (token.kind == kind) {
  401.       jj_gen++;
  402.       if (++jj_gc > 100) {
  403.         jj_gc = 0;
  404.         for (int i = 0; i < jj_2_rtns.length; i++) {
  405.           JJCalls c = jj_2_rtns[i];
  406.           while (c != null) {
  407.             if (c.gen < jj_gen) c.first = null;
  408.             c = c.next;
  409.           }
  410.         }
  411.       }
  412.       return token;
  413.     }
  414.     token = oldToken;
  415.     jj_kind = kind;
  416.     throw generateParseException();
  417.   }

  418.   static private final class LookaheadSuccess extends java.lang.Error { }
  419.   final private LookaheadSuccess jj_ls = new LookaheadSuccess();
  420.   private boolean jj_scan_token(int kind) {
  421.     if (jj_scanpos == jj_lastpos) {
  422.       jj_la--;
  423.       if (jj_scanpos.next == null) {
  424.         jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
  425.       } else {
  426.         jj_lastpos = jj_scanpos = jj_scanpos.next;
  427.       }
  428.     } else {
  429.       jj_scanpos = jj_scanpos.next;
  430.     }
  431.     if (jj_rescan) {
  432.       int i = 0; Token tok = token;
  433.       while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
  434.       if (tok != null) jj_add_error_token(kind, i);
  435.     }
  436.     if (jj_scanpos.kind != kind) return true;
  437.     if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
  438.     return false;
  439.   }


  440. /** Get the next Token. */
  441.   final public Token getNextToken() {
  442.     if (token.next != null) token = token.next;
  443.     else token = token.next = token_source.getNextToken();
  444.     jj_ntk = -1;
  445.     jj_gen++;
  446.     return token;
  447.   }

  448. /** Get the specific Token. */
  449.   final public Token getToken(int index) {
  450.     Token t = token;
  451.     for (int i = 0; i < index; i++) {
  452.       if (t.next != null) t = t.next;
  453.       else t = t.next = token_source.getNextToken();
  454.     }
  455.     return t;
  456.   }

  457.   private int jj_ntk() {
  458.     if ((jj_nt=token.next) == null)
  459.       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
  460.     else
  461.       return (jj_ntk = jj_nt.kind);
  462.   }

  463.   private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
  464.   private int[] jj_expentry;
  465.   private int jj_kind = -1;
  466.   private int[] jj_lasttokens = new int[100];
  467.   private int jj_endpos;

  468.   private void jj_add_error_token(int kind, int pos) {
  469.     if (pos >= 100) return;
  470.     if (pos == jj_endpos + 1) {
  471.       jj_lasttokens[jj_endpos++] = kind;
  472.     } else if (jj_endpos != 0) {
  473.       jj_expentry = new int[jj_endpos];
  474.       for (int i = 0; i < jj_endpos; i++) {
  475.         jj_expentry[i] = jj_lasttokens[i];
  476.       }
  477.       jj_entries_loop: for (java.util.Iterator<?> it = jj_expentries.iterator(); it.hasNext();) {
  478.         int[] oldentry = (int[])(it.next());
  479.         if (oldentry.length == jj_expentry.length) {
  480.           for (int i = 0; i < jj_expentry.length; i++) {
  481.             if (oldentry[i] != jj_expentry[i]) {
  482.               continue jj_entries_loop;
  483.             }
  484.           }
  485.           jj_expentries.add(jj_expentry);
  486.           break jj_entries_loop;
  487.         }
  488.       }
  489.       if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
  490.     }
  491.   }

  492.   /** Generate ParseException. */
  493.   public ParseException generateParseException() {
  494.     jj_expentries.clear();
  495.     boolean[] la1tokens = new boolean[30];
  496.     if (jj_kind >= 0) {
  497.       la1tokens[jj_kind] = true;
  498.       jj_kind = -1;
  499.     }
  500.     for (int i = 0; i < 6; i++) {
  501.       if (jj_la1[i] == jj_gen) {
  502.         for (int j = 0; j < 32; j++) {
  503.           if ((jj_la1_0[i] & (1<<j)) != 0) {
  504.             la1tokens[j] = true;
  505.           }
  506.         }
  507.       }
  508.     }
  509.     for (int i = 0; i < 30; i++) {
  510.       if (la1tokens[i]) {
  511.         jj_expentry = new int[1];
  512.         jj_expentry[0] = i;
  513.         jj_expentries.add(jj_expentry);
  514.       }
  515.     }
  516.     jj_endpos = 0;
  517.     jj_rescan_token();
  518.     jj_add_error_token(0, 0);
  519.     int[][] exptokseq = new int[jj_expentries.size()][];
  520.     for (int i = 0; i < jj_expentries.size(); i++) {
  521.       exptokseq[i] = jj_expentries.get(i);
  522.     }
  523.     return new ParseException(token, exptokseq, tokenImage);
  524.   }

  525.   /** Enable tracing. */
  526.   final public void enable_tracing() {
  527.   }

  528.   /** Disable tracing. */
  529.   final public void disable_tracing() {
  530.   }

  531.   private void jj_rescan_token() {
  532.     jj_rescan = true;
  533.     for (int i = 0; i < 1; i++) {
  534.     try {
  535.       JJCalls p = jj_2_rtns[i];
  536.       do {
  537.         if (p.gen > jj_gen) {
  538.           jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
  539.           switch (i) {
  540.             case 0: jj_3_1(); break;
  541.           }
  542.         }
  543.         p = p.next;
  544.       } while (p != null);
  545.       } catch(LookaheadSuccess ls) { }
  546.     }
  547.     jj_rescan = false;
  548.   }

  549.   private void jj_save(int index, int xla) {
  550.     JJCalls p = jj_2_rtns[index];
  551.     while (p.gen > jj_gen) {
  552.       if (p.next == null) { p = p.next = new JJCalls(); break; }
  553.       p = p.next;
  554.     }
  555.     p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
  556.   }

  557.   static final class JJCalls {
  558.     int gen;
  559.     Token first;
  560.     int arg;
  561.     JJCalls next;
  562.   }

  563. }