home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / WIN95 / IAVAZIP.EXE / DATA.Z / Parser.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-08-18  |  2.8 KB  |  198 lines

  1. package com.sfs.vrml;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.StreamTokenizer;
  6. import java.util.Vector;
  7.  
  8. public class Parser {
  9.    private StreamTokenizer _st;
  10.  
  11.    public Object parse() throws IOException, SyntaxError {
  12.       return this.parseNode();
  13.    }
  14.  
  15.    private Object parseRotation() throws IOException, SyntaxError {
  16.       double var1 = (double)50.0F;
  17.       double var3 = (double)0.0F;
  18.       double var5 = (double)0.0F;
  19.       double var7 = (Math.PI / 2D);
  20.  
  21.       while(true) {
  22.          this._st.nextToken();
  23.          if (this._st.ttype == 125) {
  24.             double var9 = Math.sqrt(var1 * var1 + var3 * var3 + var5 * var5);
  25.             var1 /= var9;
  26.             var3 /= var9;
  27.             var5 /= var9;
  28.             return new Rotation((double)0.0F, (double)0.0F, (double)0.0F, var1, var3, var5, var7);
  29.          }
  30.  
  31.          if (this._st.ttype != -3) {
  32.             throw new SyntaxError("missing field name");
  33.          }
  34.  
  35.          if (!this._st.sval.equals("rotation")) {
  36.             throw new SyntaxError("invalid field name: " + this._st.sval);
  37.          }
  38.  
  39.          this._st.nextToken();
  40.          if (this._st.ttype != -2) {
  41.             throw new SyntaxError("missing field value");
  42.          }
  43.  
  44.          var1 = this._st.nval;
  45.          this._st.nextToken();
  46.          if (this._st.ttype != -2) {
  47.             throw new SyntaxError("missing field value");
  48.          }
  49.  
  50.          var3 = this._st.nval;
  51.          this._st.nextToken();
  52.          if (this._st.ttype != -2) {
  53.             throw new SyntaxError("missing field value");
  54.          }
  55.  
  56.          var5 = this._st.nval;
  57.          this._st.nextToken();
  58.          if (this._st.ttype != -2) {
  59.             throw new SyntaxError("missing field value");
  60.          }
  61.  
  62.          var7 = this._st.nval;
  63.       }
  64.    }
  65.  
  66.    private Object parseSeparator() throws IOException, SyntaxError {
  67.       Vector var1 = new Vector();
  68.  
  69.       while(true) {
  70.          this._st.nextToken();
  71.          if (this._st.ttype == 125) {
  72.             return var1;
  73.          }
  74.  
  75.          this._st.pushBack();
  76.          var1.addElement(this.parseNode());
  77.       }
  78.    }
  79.  
  80.    public Parser(InputStream var1) {
  81.       this._st = new StreamTokenizer(var1);
  82.    }
  83.  
  84.    private Object parseTranslation() throws IOException, SyntaxError {
  85.       double var1 = (double)50.0F;
  86.       double var3 = (double)0.0F;
  87.       double var5 = (double)0.0F;
  88.  
  89.       while(true) {
  90.          this._st.nextToken();
  91.          if (this._st.ttype == 125) {
  92.             return new Translation(var1, var3, var5);
  93.          }
  94.  
  95.          if (this._st.ttype != -3) {
  96.             throw new SyntaxError("missing field name");
  97.          }
  98.  
  99.          if (!this._st.sval.equals("translation")) {
  100.             throw new SyntaxError("invalid field name: " + this._st.sval);
  101.          }
  102.  
  103.          this._st.nextToken();
  104.          if (this._st.ttype != -2) {
  105.             throw new SyntaxError("missing field value");
  106.          }
  107.  
  108.          var1 = this._st.nval;
  109.          this._st.nextToken();
  110.          if (this._st.ttype != -2) {
  111.             throw new SyntaxError("missing field value");
  112.          }
  113.  
  114.          var3 = this._st.nval;
  115.          this._st.nextToken();
  116.          if (this._st.ttype != -2) {
  117.             throw new SyntaxError("missing field value");
  118.          }
  119.  
  120.          var5 = this._st.nval;
  121.       }
  122.    }
  123.  
  124.    private Object parseNode() throws IOException, SyntaxError {
  125.       this._st.nextToken();
  126.       if (this._st.ttype != -3) {
  127.          throw new SyntaxError("missing node type");
  128.       } else {
  129.          String var1 = this._st.sval;
  130.          this._st.nextToken();
  131.          if (this._st.ttype != 123) {
  132.             throw new SyntaxError("'{' expected");
  133.          } else if (var1.equals("Separator")) {
  134.             return this.parseSeparator();
  135.          } else if (var1.equals("Triangle")) {
  136.             return this.parseTriangle();
  137.          } else if (var1.equals("Translation")) {
  138.             return this.parseTranslation();
  139.          } else if (var1.equals("Rotation")) {
  140.             return this.parseRotation();
  141.          } else {
  142.             throw new SyntaxError("invalid node type: " + var1);
  143.          }
  144.       }
  145.    }
  146.  
  147.    private Object parseTriangle() throws IOException, SyntaxError {
  148.       Coordinates var1 = new Coordinates((double)0.0F, (double)0.0F, (double)0.0F);
  149.       Coordinates var2 = new Coordinates((double)50.0F, (double)0.0F, (double)0.0F);
  150.       Coordinates var3 = new Coordinates((double)50.0F, (double)0.0F, (double)50.0F);
  151.  
  152.       while(true) {
  153.          this._st.nextToken();
  154.          if (this._st.ttype == 125) {
  155.             return new TriangleShape(var1, var2, var3);
  156.          }
  157.  
  158.          if (this._st.ttype != -3) {
  159.             throw new SyntaxError("missing field name");
  160.          }
  161.  
  162.          String var4 = this._st.sval;
  163.          double var5 = (double)0.0F;
  164.          double var7 = (double)0.0F;
  165.          double var9 = (double)0.0F;
  166.          this._st.nextToken();
  167.          if (this._st.ttype != -2) {
  168.             throw new SyntaxError("missing field value");
  169.          }
  170.  
  171.          var5 = this._st.nval;
  172.          this._st.nextToken();
  173.          if (this._st.ttype != -2) {
  174.             throw new SyntaxError("missing field value");
  175.          }
  176.  
  177.          var7 = this._st.nval;
  178.          this._st.nextToken();
  179.          if (this._st.ttype != -2) {
  180.             throw new SyntaxError("missing field value");
  181.          }
  182.  
  183.          var9 = this._st.nval;
  184.          if (var4.equals("a")) {
  185.             var1 = new Coordinates(var5, var7, var9);
  186.          } else if (var4.equals("b")) {
  187.             var2 = new Coordinates(var5, var7, var9);
  188.          } else {
  189.             if (!var4.equals("c")) {
  190.                throw new SyntaxError("invalid field name: " + var4);
  191.             }
  192.  
  193.             var3 = new Coordinates(var5, var7, var9);
  194.          }
  195.       }
  196.    }
  197. }
  198.