home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / javax / swing / text / html / CSSParser.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  6.9 KB  |  842 lines

  1. package javax.swing.text.html;
  2.  
  3. import java.io.IOException;
  4. import java.io.Reader;
  5.  
  6. class CSSParser {
  7.    private static final int IDENTIFIER = 1;
  8.    private static final int BRACKET_OPEN = 2;
  9.    private static final int BRACKET_CLOSE = 3;
  10.    private static final int BRACE_OPEN = 4;
  11.    private static final int BRACE_CLOSE = 5;
  12.    private static final int PAREN_OPEN = 6;
  13.    private static final int PAREN_CLOSE = 7;
  14.    private static final int END = -1;
  15.    private static final char[] charMapping = new char[]{'\u0000', '\u0000', '[', ']', '{', '}', '(', ')', '\u0000'};
  16.    private boolean didPushChar;
  17.    private int pushedChar;
  18.    private StringBuffer unitBuffer = new StringBuffer();
  19.    private int[] unitStack = new int[2];
  20.    private int stackCount;
  21.    private Reader reader;
  22.    private boolean encounteredRuleSet;
  23.    private CSSParserCallback callback;
  24.    private char[] tokenBuffer = new char[80];
  25.    private int tokenBufferLength;
  26.    private boolean readWS;
  27.  
  28.    void parse(Reader var1, CSSParserCallback var2, boolean var3) throws IOException {
  29.       this.callback = var2;
  30.       this.stackCount = this.tokenBufferLength = 0;
  31.       this.reader = var1;
  32.       this.encounteredRuleSet = false;
  33.  
  34.       try {
  35.          if (var3) {
  36.             this.parseDeclarationBlock();
  37.          } else {
  38.             while(this.getNextStatement()) {
  39.             }
  40.          }
  41.       } finally {
  42.          Object var9 = null;
  43.          Object var8 = null;
  44.       }
  45.  
  46.    }
  47.  
  48.    private boolean getNextStatement() throws IOException {
  49.       this.unitBuffer.setLength(0);
  50.       int var1 = this.nextToken('\u0000');
  51.       switch (var1) {
  52.          case -1:
  53.             return false;
  54.          case 0:
  55.          default:
  56.             return true;
  57.          case 1:
  58.             if (this.tokenBufferLength > 0) {
  59.                if (this.tokenBuffer[0] == '@') {
  60.                   this.parseAtRule();
  61.                } else {
  62.                   this.encounteredRuleSet = true;
  63.                   this.parseRuleSet();
  64.                }
  65.             }
  66.  
  67.             return true;
  68.          case 2:
  69.          case 4:
  70.          case 6:
  71.             this.parseTillClosed(var1);
  72.             return true;
  73.          case 3:
  74.          case 5:
  75.          case 7:
  76.             throw new RuntimeException("Unexpected top level block close");
  77.       }
  78.    }
  79.  
  80.    private void parseAtRule() throws IOException {
  81.       boolean var1 = false;
  82.       boolean var2 = this.tokenBufferLength == 7 && this.tokenBuffer[0] == '@' && this.tokenBuffer[1] == 'i' && this.tokenBuffer[2] == 'm' && this.tokenBuffer[3] == 'p' && this.tokenBuffer[4] == 'o' && this.tokenBuffer[5] == 'r' && this.tokenBuffer[6] == 't';
  83.       this.unitBuffer.setLength(0);
  84.  
  85.       while(!var1) {
  86.          int var3 = this.nextToken(';');
  87.          switch (var3) {
  88.             case -1:
  89.                var1 = true;
  90.             case 0:
  91.             default:
  92.                break;
  93.             case 1:
  94.                if (this.tokenBufferLength > 0 && this.tokenBuffer[this.tokenBufferLength - 1] == ';') {
  95.                   --this.tokenBufferLength;
  96.                   var1 = true;
  97.                }
  98.  
  99.                if (this.tokenBufferLength > 0) {
  100.                   if (this.unitBuffer.length() > 0 && this.readWS) {
  101.                      this.unitBuffer.append(' ');
  102.                   }
  103.  
  104.                   this.unitBuffer.append(this.tokenBuffer, 0, this.tokenBufferLength);
  105.                }
  106.                break;
  107.             case 2:
  108.             case 6:
  109.                this.unitBuffer.append(charMapping[var3]);
  110.                this.parseTillClosed(var3);
  111.                break;
  112.             case 3:
  113.             case 5:
  114.             case 7:
  115.                throw new RuntimeException("Unexpected close in @ rule");
  116.             case 4:
  117.                if (this.unitBuffer.length() > 0 && this.readWS) {
  118.                   this.unitBuffer.append(' ');
  119.                }
  120.  
  121.                this.unitBuffer.append(charMapping[var3]);
  122.                this.parseTillClosed(var3);
  123.                var1 = true;
  124.                int var4 = this.readWS();
  125.                if (var4 != -1 && var4 != 59) {
  126.                   this.pushChar(var4);
  127.                }
  128.          }
  129.       }
  130.  
  131.       if (var2 && !this.encounteredRuleSet) {
  132.          this.callback.handleImport(this.unitBuffer.toString());
  133.       }
  134.  
  135.    }
  136.  
  137.    private void parseRuleSet() throws IOException {
  138.       if (this.parseSelectors()) {
  139.          this.callback.startRule();
  140.          this.parseDeclarationBlock();
  141.          this.callback.endRule();
  142.       }
  143.  
  144.    }
  145.  
  146.    private boolean parseSelectors() throws IOException {
  147.       if (this.tokenBufferLength > 0) {
  148.          this.callback.handleSelector(new String(this.tokenBuffer, 0, this.tokenBufferLength));
  149.       }
  150.  
  151.       this.unitBuffer.setLength(0);
  152.  
  153.       while(true) {
  154.          int var1;
  155.          while((var1 = this.nextToken('\u0000')) == 1) {
  156.             if (this.tokenBufferLength > 0) {
  157.                this.callback.handleSelector(new String(this.tokenBuffer, 0, this.tokenBufferLength));
  158.             }
  159.          }
  160.  
  161.          switch (var1) {
  162.             case -1:
  163.                return false;
  164.             case 0:
  165.             case 1:
  166.             default:
  167.                break;
  168.             case 2:
  169.             case 6:
  170.                this.parseTillClosed(var1);
  171.                this.unitBuffer.setLength(0);
  172.                break;
  173.             case 3:
  174.             case 5:
  175.             case 7:
  176.                throw new RuntimeException("Unexpected block close in selector");
  177.             case 4:
  178.                return true;
  179.          }
  180.       }
  181.    }
  182.  
  183.    private void parseDeclarationBlock() throws IOException {
  184.       while(true) {
  185.          int var1 = this.parseDeclaration();
  186.          switch (var1) {
  187.             case -1:
  188.             case 5:
  189.                return;
  190.             case 0:
  191.             case 1:
  192.             case 2:
  193.             case 4:
  194.             case 6:
  195.             default:
  196.                break;
  197.             case 3:
  198.             case 7:
  199.                throw new RuntimeException("Unexpected close in declaration block");
  200.          }
  201.       }
  202.    }
  203.  
  204.    private int parseDeclaration() throws IOException {
  205.       int var1;
  206.       if ((var1 = this.parseIdentifiers(':', false)) != 1) {
  207.          return var1;
  208.       } else {
  209.          for(int var2 = this.unitBuffer.length() - 1; var2 >= 0; --var2) {
  210.             this.unitBuffer.setCharAt(var2, Character.toLowerCase(this.unitBuffer.charAt(var2)));
  211.          }
  212.  
  213.          this.callback.handleProperty(this.unitBuffer.toString());
  214.          var1 = this.parseIdentifiers(';', true);
  215.          this.callback.handleValue(this.unitBuffer.toString());
  216.          return var1;
  217.       }
  218.    }
  219.  
  220.    private int parseIdentifiers(char var1, boolean var2) throws IOException {
  221.       this.unitBuffer.setLength(0);
  222.  
  223.       while(true) {
  224.          int var3 = this.nextToken(var1);
  225.          switch (var3) {
  226.             case -1:
  227.             case 3:
  228.             case 5:
  229.             case 7:
  230.                return var3;
  231.             case 0:
  232.             default:
  233.                break;
  234.             case 1:
  235.                if (this.tokenBufferLength > 0) {
  236.                   if (this.tokenBuffer[this.tokenBufferLength - 1] == var1) {
  237.                      if (--this.tokenBufferLength > 0) {
  238.                         if (this.readWS && this.unitBuffer.length() > 0) {
  239.                            this.unitBuffer.append(' ');
  240.                         }
  241.  
  242.                         this.unitBuffer.append(this.tokenBuffer, 0, this.tokenBufferLength);
  243.                      }
  244.  
  245.                      return 1;
  246.                   }
  247.  
  248.                   if (this.readWS && this.unitBuffer.length() > 0) {
  249.                      this.unitBuffer.append(' ');
  250.                   }
  251.  
  252.                   this.unitBuffer.append(this.tokenBuffer, 0, this.tokenBufferLength);
  253.                }
  254.                break;
  255.             case 2:
  256.             case 4:
  257.             case 6:
  258.                int var4 = this.unitBuffer.length();
  259.                if (var2) {
  260.                   this.unitBuffer.append(charMapping[var3]);
  261.                }
  262.  
  263.                this.parseTillClosed(var3);
  264.                if (!var2) {
  265.                   this.unitBuffer.setLength(var4);
  266.                }
  267.          }
  268.       }
  269.    }
  270.  
  271.    private void parseTillClosed(int var1) throws IOException {
  272.       boolean var3 = false;
  273.       this.startBlock(var1);
  274.  
  275.       while(!var3) {
  276.          int var2 = this.nextToken('\u0000');
  277.          switch (var2) {
  278.             case -1:
  279.                throw new RuntimeException("Unclosed block");
  280.             case 0:
  281.             default:
  282.                break;
  283.             case 1:
  284.                if (this.unitBuffer.length() > 0 && this.readWS) {
  285.                   this.unitBuffer.append(' ');
  286.                }
  287.  
  288.                if (this.tokenBufferLength > 0) {
  289.                   this.unitBuffer.append(this.tokenBuffer, 0, this.tokenBufferLength);
  290.                }
  291.                break;
  292.             case 2:
  293.             case 4:
  294.             case 6:
  295.                if (this.unitBuffer.length() > 0 && this.readWS) {
  296.                   this.unitBuffer.append(' ');
  297.                }
  298.  
  299.                this.unitBuffer.append(charMapping[var2]);
  300.                this.startBlock(var2);
  301.                break;
  302.             case 3:
  303.             case 5:
  304.             case 7:
  305.                if (this.unitBuffer.length() > 0 && this.readWS) {
  306.                   this.unitBuffer.append(' ');
  307.                }
  308.  
  309.                this.unitBuffer.append(charMapping[var2]);
  310.                this.endBlock(var2);
  311.                if (!this.inBlock()) {
  312.                   var3 = true;
  313.                }
  314.          }
  315.       }
  316.  
  317.    }
  318.  
  319.    private int nextToken(char var1) throws IOException {
  320.       this.readWS = false;
  321.       int var2 = this.readWS();
  322.       switch (var2) {
  323.          case -1:
  324.             return -1;
  325.          case 34:
  326.             this.readTill('"');
  327.             if (this.tokenBufferLength > 0) {
  328.                --this.tokenBufferLength;
  329.             }
  330.  
  331.             return 1;
  332.          case 39:
  333.             this.readTill('\'');
  334.             if (this.tokenBufferLength > 0) {
  335.                --this.tokenBufferLength;
  336.             }
  337.  
  338.             return 1;
  339.          case 40:
  340.             return 6;
  341.          case 41:
  342.             return 7;
  343.          case 91:
  344.             return 2;
  345.          case 93:
  346.             return 3;
  347.          case 123:
  348.             return 4;
  349.          case 125:
  350.             return 5;
  351.          default:
  352.             this.pushChar(var2);
  353.             this.getIdentifier(var1);
  354.             return 1;
  355.       }
  356.    }
  357.  
  358.    private boolean getIdentifier(char var1) throws IOException {
  359.       boolean var2 = false;
  360.       boolean var3 = false;
  361.       int var4 = 0;
  362.       int var5 = 0;
  363.       char var7 = var1;
  364.       int var9 = 0;
  365.       this.tokenBufferLength = 0;
  366.  
  367.       while(!var3) {
  368.          int var6 = this.readChar();
  369.          byte var8;
  370.          switch (var6) {
  371.             case -1:
  372.                var3 = true;
  373.                var8 = 0;
  374.                break;
  375.             case 0:
  376.             case 1:
  377.             case 2:
  378.             case 3:
  379.             case 4:
  380.             case 5:
  381.             case 6:
  382.             case 7:
  383.             case 8:
  384.             case 11:
  385.             case 12:
  386.             case 14:
  387.             case 15:
  388.             case 16:
  389.             case 17:
  390.             case 18:
  391.             case 19:
  392.             case 20:
  393.             case 21:
  394.             case 22:
  395.             case 23:
  396.             case 24:
  397.             case 25:
  398.             case 26:
  399.             case 27:
  400.             case 28:
  401.             case 29:
  402.             case 30:
  403.             case 31:
  404.             case 33:
  405.             case 35:
  406.             case 36:
  407.             case 37:
  408.             case 38:
  409.             case 42:
  410.             case 43:
  411.             case 44:
  412.             case 45:
  413.             case 46:
  414.             case 58:
  415.             case 59:
  416.             case 60:
  417.             case 61:
  418.             case 62:
  419.             case 63:
  420.             case 64:
  421.             case 71:
  422.             case 72:
  423.             case 73:
  424.             case 74:
  425.             case 75:
  426.             case 76:
  427.             case 77:
  428.             case 78:
  429.             case 79:
  430.             case 80:
  431.             case 81:
  432.             case 82:
  433.             case 83:
  434.             case 84:
  435.             case 85:
  436.             case 86:
  437.             case 87:
  438.             case 88:
  439.             case 89:
  440.             case 90:
  441.             case 94:
  442.             case 95:
  443.             case 96:
  444.             case 103:
  445.             case 104:
  446.             case 105:
  447.             case 106:
  448.             case 107:
  449.             case 108:
  450.             case 109:
  451.             case 110:
  452.             case 111:
  453.             case 112:
  454.             case 113:
  455.             case 114:
  456.             case 115:
  457.             case 116:
  458.             case 117:
  459.             case 118:
  460.             case 119:
  461.             case 120:
  462.             case 121:
  463.             case 122:
  464.             case 124:
  465.             default:
  466.                var8 = 0;
  467.                break;
  468.             case 9:
  469.             case 10:
  470.             case 13:
  471.             case 32:
  472.             case 34:
  473.             case 39:
  474.             case 40:
  475.             case 41:
  476.             case 91:
  477.             case 93:
  478.             case 123:
  479.             case 125:
  480.                var8 = 3;
  481.                break;
  482.             case 47:
  483.                var8 = 4;
  484.                break;
  485.             case 48:
  486.             case 49:
  487.             case 50:
  488.             case 51:
  489.             case 52:
  490.             case 53:
  491.             case 54:
  492.             case 55:
  493.             case 56:
  494.             case 57:
  495.                var8 = 2;
  496.                var9 = var6 - 48;
  497.                break;
  498.             case 65:
  499.             case 66:
  500.             case 67:
  501.             case 68:
  502.             case 69:
  503.             case 70:
  504.                var8 = 2;
  505.                var9 = var6 - 65 + 10;
  506.                break;
  507.             case 92:
  508.                var8 = 1;
  509.                break;
  510.             case 97:
  511.             case 98:
  512.             case 99:
  513.             case 100:
  514.             case 101:
  515.             case 102:
  516.                var8 = 2;
  517.                var9 = var6 - 97 + 10;
  518.          }
  519.  
  520.          if (var2) {
  521.             if (var8 == 2) {
  522.                var5 = var5 * 16 + var9;
  523.                ++var4;
  524.                if (var4 == 4) {
  525.                   var2 = false;
  526.                   this.append((char)var5);
  527.                }
  528.             } else {
  529.                var2 = false;
  530.                if (var4 > 0) {
  531.                   this.append((char)var5);
  532.                   this.pushChar(var6);
  533.                } else if (!var3) {
  534.                   this.append((char)var6);
  535.                }
  536.             }
  537.          } else if (!var3) {
  538.             if (var8 == 1) {
  539.                var2 = true;
  540.                var4 = 0;
  541.                var5 = 0;
  542.             } else if (var8 == 3) {
  543.                var3 = true;
  544.                this.pushChar(var6);
  545.             } else if (var8 == 4) {
  546.                var6 = this.readChar();
  547.                if (var6 == 42) {
  548.                   var3 = true;
  549.                   this.readComment();
  550.                   this.readWS = true;
  551.                } else {
  552.                   this.append('/');
  553.                   if (var6 == -1) {
  554.                      var3 = true;
  555.                   } else {
  556.                      this.pushChar(var6);
  557.                   }
  558.                }
  559.             } else {
  560.                this.append((char)var6);
  561.                if (var6 == var7) {
  562.                   var3 = true;
  563.                }
  564.             }
  565.          }
  566.       }
  567.  
  568.       return this.tokenBufferLength > 0;
  569.    }
  570.  
  571.    private void readTill(char var1) throws IOException {
  572.       boolean var2 = false;
  573.       int var3 = 0;
  574.       int var4 = 0;
  575.       boolean var6 = false;
  576.       char var7 = var1;
  577.       int var9 = 0;
  578.       this.tokenBufferLength = 0;
  579.  
  580.       while(!var6) {
  581.          int var5 = this.readChar();
  582.          byte var8;
  583.          switch (var5) {
  584.             case -1:
  585.                throw new RuntimeException("Unclosed " + var1);
  586.             case 0:
  587.             case 1:
  588.             case 2:
  589.             case 3:
  590.             case 4:
  591.             case 5:
  592.             case 6:
  593.             case 7:
  594.             case 8:
  595.             case 9:
  596.             case 10:
  597.             case 11:
  598.             case 12:
  599.             case 13:
  600.             case 14:
  601.             case 15:
  602.             case 16:
  603.             case 17:
  604.             case 18:
  605.             case 19:
  606.             case 20:
  607.             case 21:
  608.             case 22:
  609.             case 23:
  610.             case 24:
  611.             case 25:
  612.             case 26:
  613.             case 27:
  614.             case 28:
  615.             case 29:
  616.             case 30:
  617.             case 31:
  618.             case 32:
  619.             case 33:
  620.             case 34:
  621.             case 35:
  622.             case 36:
  623.             case 37:
  624.             case 38:
  625.             case 39:
  626.             case 40:
  627.             case 41:
  628.             case 42:
  629.             case 43:
  630.             case 44:
  631.             case 45:
  632.             case 46:
  633.             case 47:
  634.             case 58:
  635.             case 59:
  636.             case 60:
  637.             case 61:
  638.             case 62:
  639.             case 63:
  640.             case 64:
  641.             case 71:
  642.             case 72:
  643.             case 73:
  644.             case 74:
  645.             case 75:
  646.             case 76:
  647.             case 77:
  648.             case 78:
  649.             case 79:
  650.             case 80:
  651.             case 81:
  652.             case 82:
  653.             case 83:
  654.             case 84:
  655.             case 85:
  656.             case 86:
  657.             case 87:
  658.             case 88:
  659.             case 89:
  660.             case 90:
  661.             case 91:
  662.             case 93:
  663.             case 94:
  664.             case 95:
  665.             case 96:
  666.             default:
  667.                var8 = 0;
  668.                break;
  669.             case 48:
  670.             case 49:
  671.             case 50:
  672.             case 51:
  673.             case 52:
  674.             case 53:
  675.             case 54:
  676.             case 55:
  677.             case 56:
  678.             case 57:
  679.                var8 = 2;
  680.                var9 = var5 - 48;
  681.                break;
  682.             case 65:
  683.             case 66:
  684.             case 67:
  685.             case 68:
  686.             case 69:
  687.             case 70:
  688.                var8 = 2;
  689.                var9 = var5 - 65 + 10;
  690.                break;
  691.             case 92:
  692.                var8 = 1;
  693.                break;
  694.             case 97:
  695.             case 98:
  696.             case 99:
  697.             case 100:
  698.             case 101:
  699.             case 102:
  700.                var8 = 2;
  701.                var9 = var5 - 97 + 10;
  702.          }
  703.  
  704.          if (var2) {
  705.             if (var8 == 2) {
  706.                var4 = var4 * 16 + var9;
  707.                ++var3;
  708.                if (var3 == 4) {
  709.                   var2 = false;
  710.                   this.append((char)var4);
  711.                }
  712.             } else if (var3 > 0) {
  713.                this.append((char)var4);
  714.                if (var8 == 1) {
  715.                   var2 = true;
  716.                   var3 = 0;
  717.                   var4 = 0;
  718.                } else {
  719.                   if (var5 == var7) {
  720.                      var6 = true;
  721.                   }
  722.  
  723.                   this.append((char)var5);
  724.                   var2 = false;
  725.                }
  726.             } else {
  727.                this.append((char)var5);
  728.                var2 = false;
  729.             }
  730.          } else if (var8 == 1) {
  731.             var2 = true;
  732.             var3 = 0;
  733.             var4 = 0;
  734.          } else {
  735.             if (var5 == var7) {
  736.                var6 = true;
  737.             }
  738.  
  739.             this.append((char)var5);
  740.          }
  741.       }
  742.  
  743.    }
  744.  
  745.    private void append(char var1) {
  746.       if (this.tokenBufferLength == this.tokenBuffer.length) {
  747.          char[] var2 = new char[this.tokenBuffer.length * 2];
  748.          System.arraycopy(this.tokenBuffer, 0, var2, 0, this.tokenBuffer.length);
  749.          this.tokenBuffer = var2;
  750.       }
  751.  
  752.       this.tokenBuffer[this.tokenBufferLength++] = var1;
  753.    }
  754.  
  755.    private void readComment() throws IOException {
  756.       while(true) {
  757.          int var1 = this.readChar();
  758.          switch (var1) {
  759.             case -1:
  760.                throw new RuntimeException("Unclosed comment");
  761.             case 42:
  762.                var1 = this.readChar();
  763.                if (var1 == 47) {
  764.                   return;
  765.                }
  766.  
  767.                if (var1 == -1) {
  768.                   throw new RuntimeException("Unclosed comment");
  769.                }
  770.  
  771.                this.pushChar(var1);
  772.          }
  773.       }
  774.    }
  775.  
  776.    private void startBlock(int var1) {
  777.       if (this.stackCount == this.unitStack.length) {
  778.          int[] var2 = new int[this.stackCount * 2];
  779.          System.arraycopy(this.unitStack, 0, var2, 0, this.stackCount);
  780.          this.unitStack = var2;
  781.       }
  782.  
  783.       this.unitStack[this.stackCount++] = var1;
  784.    }
  785.  
  786.    private void endBlock(int var1) {
  787.       byte var2;
  788.       switch (var1) {
  789.          case 3:
  790.             var2 = 2;
  791.             break;
  792.          case 4:
  793.          case 6:
  794.          default:
  795.             var2 = -1;
  796.             break;
  797.          case 5:
  798.             var2 = 4;
  799.             break;
  800.          case 7:
  801.             var2 = 6;
  802.       }
  803.  
  804.       if (this.stackCount > 0 && this.unitStack[this.stackCount - 1] == var2) {
  805.          --this.stackCount;
  806.       } else {
  807.          throw new RuntimeException("Unmatched block");
  808.       }
  809.    }
  810.  
  811.    private boolean inBlock() {
  812.       return this.stackCount > 0;
  813.    }
  814.  
  815.    private int readWS() throws IOException {
  816.       int var1;
  817.       while((var1 = this.readChar()) != -1 && Character.isWhitespace((char)var1)) {
  818.          this.readWS = true;
  819.       }
  820.  
  821.       return var1;
  822.    }
  823.  
  824.    private int readChar() throws IOException {
  825.       if (this.didPushChar) {
  826.          this.didPushChar = false;
  827.          return this.pushedChar;
  828.       } else {
  829.          return this.reader.read();
  830.       }
  831.    }
  832.  
  833.    private void pushChar(int var1) {
  834.       if (this.didPushChar) {
  835.          throw new RuntimeException("Can not handle look ahead of more than one character");
  836.       } else {
  837.          this.didPushChar = true;
  838.          this.pushedChar = var1;
  839.       }
  840.    }
  841. }
  842.