Beispiel #1
0
  private static int[][] deserialize(String str) throws IOException {
    StreamTokenizer tok = new StreamTokenizer(new StringReader(str));
    tok.resetSyntax();
    tok.wordChars('0', '9');
    tok.whitespaceChars(' ', ' ');
    tok.parseNumbers();

    tok.nextToken();

    int rows = (int) tok.nval;
    int[][] out = new int[rows][];

    for (int i = 0; i < rows; i++) {
      tok.nextToken();

      int length = (int) tok.nval;
      int[] row = new int[length];
      out[i] = row;

      for (int j = 0; j < length; j++) {
        tok.nextToken();
        row[j] = (int) tok.nval;
      }
    }

    return out;
  }
 /** @tests java.io.StreamTokenizer#parseNumbers() */
 public void test_parseNumbers() throws IOException {
   // SM
   setTest("9.9 678");
   assertTrue("Base behavior failed.", st.nextToken() == StreamTokenizer.TT_NUMBER);
   st.ordinaryChars('0', '9');
   assertEquals("setOrdinary failed.", '6', st.nextToken());
   st.parseNumbers();
   assertTrue("parseNumbers failed.", st.nextToken() == StreamTokenizer.TT_NUMBER);
 }
 /**
  * Constructs a Parser for the given string.
  *
  * @param text The string to be parsed.
  */
 public Parser(String text) {
   Reader reader = new StringReader(text);
   tokenizer = new StreamTokenizer(reader);
   tokenizer.parseNumbers();
   tokenizer.eolIsSignificant(true);
   tokenizer.slashStarComments(true);
   tokenizer.slashSlashComments(true);
   tokenizer.lowerCaseMode(false);
   tokenizer.ordinaryChars(33, 47);
   tokenizer.ordinaryChars(58, 64);
   tokenizer.ordinaryChars(91, 96);
   tokenizer.ordinaryChars(123, 126);
   tokenizer.quoteChar('\"');
   lineNumber = 1;
 }
  public String[] parseTokens(String line) throws IOException {
    List tokens = new ArrayList();

    /*StringTokenizer st = new StringTokenizer(line);
    String token;
    while((token = st.nextToken()) != null) {
        tokens.add(token);
    } */

    StreamTokenizer st = new StreamTokenizer(new StringReader(line));
    st.parseNumbers();
    st.wordChars('_', '_'); // A word can be THIS_IS_A_WORD

    int token = st.nextToken();
    while (token != StreamTokenizer.TT_EOF) {
      String element = null;
      switch (token) {
        case StreamTokenizer.TT_NUMBER:
          element = String.valueOf(st.nval);
          break;
        case StreamTokenizer.TT_WORD:
          element = st.sval;
          break;
        case '"':
        case '\'':
          element = st.sval;
          break;
        case StreamTokenizer.TT_EOL:
          break;
        case StreamTokenizer.TT_EOF:
          break;
        default:
          element = String.valueOf((char) st.ttype);
          break;
      }
      if (element != null) tokens.add(element);
      token = st.nextToken();
    }

    String[] result = new String[tokens.size()];
    for (int index = 0; index < tokens.size(); index++) result[index] = (String) tokens.get(index);
    return result;
  }
Beispiel #5
0
  public static Histogram2D loadFromFile(File file) throws FileNotFoundException, IOException {
    StreamTokenizer st;
    FileReader fr = new FileReader(file);
    st = new StreamTokenizer(fr);
    st.commentChar('#');
    st.parseNumbers();
    int result;

    // doesn't handle errors right now

    double axis1Min, axis1Max, axis2Min, axis2Max;
    int axis1Buckets, axis2Buckets;

    result = st.nextToken();
    axis1Min = st.nval;
    result = st.nextToken();
    axis1Max = st.nval;
    result = st.nextToken();
    axis1Buckets = (int) st.nval;
    result = st.nextToken();
    axis2Min = st.nval;
    result = st.nextToken();
    axis2Max = st.nval;
    result = st.nextToken();
    axis2Buckets = (int) st.nval;

    Histogram2D h =
        new Histogram2D(axis1Min, axis1Max, axis1Buckets, axis2Min, axis2Max, axis2Buckets);
    int num;
    for (int axis1 = 0; axis1 < axis1Buckets; axis1++) {
      for (int axis2 = 0; axis2 < axis2Buckets; axis2++) {
        result = st.nextToken();
        num = (int) st.nval;
        h.setSample(axis1, axis2, num);
      }
    }
    fr.close();
    return h;
  }
  /** @param ribFileReader rib file reader */
  public Rib2Xml(FileReader ribFileReader) {

    /* Configure log4j, read conf out of jar file */
    Class clazz = getClass();
    URL url = clazz.getResource("/conf/log4j.xml");
    if (url == null) {
      /* Try reading via filename */
      DOMConfigurator.configure("../conf/log4j.xml");
      System.err.println("Error: Configuration file for Log4j (log4j.xml) not found, aborting...");
      System.exit(1);
    }
    DOMConfigurator.configure(url);

    /* Create the ribfactory which deal with all the rib elements */
    Config config = Config.instance();
    RibFactory ribFac = new RibFactory(config);

    Vector ribNames = config.getNames();

    StreamTokenizer thTokens = new StreamTokenizer(ribFileReader);
    // thTokens.resetSyntax();
    thTokens.commentChar('#');
    thTokens.eolIsSignificant(false);
    thTokens.parseNumbers();
    thTokens.ordinaryChar('[');
    thTokens.ordinaryChar(']');
    thTokens.quoteChar('"');
    int count = 0;

    String factoryInput = "";

    try {
      while (thTokens.nextToken() != StreamTokenizer.TT_EOF) {
        logger.debug(thTokens.lineno() + ": " + thTokens.sval + ": ttype: " + thTokens.ttype);
        if (thTokens.ttype == StreamTokenizer.TT_NUMBER) {
          logger.debug(thTokens.lineno() + ": " + thTokens.nval);
          factoryInput += " " + String.valueOf(thTokens.nval);
          count++;
        } else if (thTokens.ttype == StreamTokenizer.TT_WORD) {
          if (ribNames.contains(thTokens.sval)) {
            logger.debug(factoryInput);

            // AbstractRib Factory called to add an element to xml document
            logger.debug("Elements: " + count + ": " + factoryInput);
            ribFac.processRibElement(factoryInput);

            factoryInput = thTokens.sval;
          } else {
            factoryInput += " " + thTokens.sval;
          }
          logger.debug(thTokens.lineno() + ": " + thTokens.sval);
          count++;
        } else {
          if (thTokens.ttype != '"') {
            logger.debug(thTokens.lineno() + ": " + (char) thTokens.ttype);
            factoryInput += " " + (char) thTokens.ttype;
            count++;
          } else if (thTokens.sval != null) {
            logger.debug(
                thTokens.lineno()
                    + ": "
                    + (char) thTokens.ttype
                    + thTokens.sval
                    + (char) thTokens.ttype);
            factoryInput += " " + (char) thTokens.ttype + thTokens.sval + (char) thTokens.ttype;
            count++;
          }
        }
      }
    } catch (IOException e) {
      logger.error(e.toString());
    }

    logger.info("Tokens: " + count);

    RibDocument ribDoc = RibDocument.newInstance();
    ribDoc.toFile();
  }
Beispiel #7
0
  public static List<BezierPath> fromPathData(String str) throws IOException {
    LinkedList<BezierPath> paths = new LinkedList<BezierPath>();

    BezierPath path = null;
    Point2D.Double p = new Point2D.Double();
    Point2D.Double c1 = new Point2D.Double();
    Point2D.Double c2 = new Point2D.Double();
    StreamTokenizer tt = new StreamTokenizer(new StringReader(str));
    tt.resetSyntax();
    tt.parseNumbers();
    tt.whitespaceChars(0, ' ');
    tt.whitespaceChars(',', ',');

    char nextCommand = 'M';
    char command = 'M';
    while (tt.nextToken() != StreamTokenizer.TT_EOF) {
      if (tt.ttype > 0) {
        command = (char) tt.ttype;
      } else {
        command = nextCommand;
        tt.pushBack();
      }

      BezierPath.Node node;
      switch (command) {
          // moveto
        case 'M':
          if (path != null) {
            paths.add(path);
          }
          path = new BezierPath();

          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y = tt.nval;
          path.moveTo(p.x, p.y);
          nextCommand = 'L';
          break;
        case 'm':
          if (path != null) {
            paths.add(path);
          }
          path = new BezierPath();

          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x += tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y += tt.nval;
          path.moveTo(p.x, p.y);
          nextCommand = 'l';

          // close path
          break;
        case 'Z':
        case 'z':
          p.x = path.get(0).x[0];
          p.y = path.get(0).y[0];
          path.setClosed(true);

          // lineto
          break;
        case 'L':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y = tt.nval;
          path.lineTo(p.x, p.y);
          nextCommand = 'L';

          break;
        case 'l':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x += tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y += tt.nval;
          path.lineTo(p.x, p.y);
          nextCommand = 'l';

          break;
        case 'H':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x = tt.nval;
          path.lineTo(p.x, p.y);
          nextCommand = 'H';

          break;
        case 'h':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x += tt.nval;
          path.lineTo(p.x, p.y);
          nextCommand = 'h';

          break;
        case 'V':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y = tt.nval;
          path.lineTo(p.x, p.y);
          nextCommand = 'V';

          break;
        case 'v':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y += tt.nval;
          path.lineTo(p.x, p.y);
          nextCommand = 'v';

          // curveto
          break;
        case 'C':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.y = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.y = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y = tt.nval;
          path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y);
          nextCommand = 'C';

          break;
        case 'c':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.x = p.x + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.y = p.y + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.x = p.x + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.y = p.y + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x += tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y += tt.nval;
          path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y);
          nextCommand = 'c';

          break;
        case 'S':
          node = path.get(path.size() - 1);
          c1.x = node.x[0] * 2d - node.x[1];
          c1.y = node.y[0] * 2d - node.y[1];
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.y = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y = tt.nval;
          path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y);
          nextCommand = 'S';

          break;
        case 's':
          node = path.get(path.size() - 1);
          c1.x = node.x[0] * 2d - node.x[1];
          c1.y = node.y[0] * 2d - node.y[1];
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.x = p.x + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c2.y = p.y + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x += tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y += tt.nval;
          path.curveTo(c1.x, c1.y, c2.x, c2.y, p.x, p.y);
          nextCommand = 's';

          // quadto
          break;
        case 'Q':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.y = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y = tt.nval;
          path.quadTo(c1.x, c1.y, p.x, p.y);
          nextCommand = 'Q';

          break;
        case 'q':
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.x = p.x + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          c1.y = p.y + tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x += tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y += tt.nval;
          path.quadTo(c1.x, c1.y, p.x, p.y);
          nextCommand = 'q';

          break;
        case 'T':
          node = path.get(path.size() - 1);
          c1.x = node.x[0] * 2d - node.x[1];
          c1.y = node.y[0] * 2d - node.y[1];
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x = tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y = tt.nval;
          path.quadTo(c1.x, c1.y, p.x, p.y);
          nextCommand = 'T';

          break;
        case 't':
          node = path.get(path.size() - 1);
          c1.x = node.x[0] * 2d - node.x[1];
          c1.y = node.y[0] * 2d - node.y[1];
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.x += tt.nval;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected");
          p.y += tt.nval;
          path.quadTo(c1.x, c1.y, p.x, p.y);
          nextCommand = 's';

          break;
        default:
          throw new IOException("Illegal command: " + command);
      }
    }
    if (path != null) {
      paths.add(path);
    }
    return paths;
  }
Beispiel #8
0
  public static AffineTransform getTransform(String str) throws IOException {
    AffineTransform t = new AffineTransform();

    if (str != null) {

      StreamTokenizer tt = new StreamTokenizer(new StringReader(str));
      tt.resetSyntax();
      tt.wordChars('a', 'z');
      tt.wordChars('A', 'Z');
      tt.wordChars(128 + 32, 255);
      tt.whitespaceChars(0, ' ');
      tt.whitespaceChars(',', ',');
      tt.parseNumbers();

      while (tt.nextToken() != StreamTokenizer.TT_EOF) {
        if (tt.ttype != StreamTokenizer.TT_WORD) {
          throw new IOException("Illegal transform " + str);
        }
        String type = tt.sval;
        if (tt.nextToken() != '(') {
          throw new IOException("'(' not found in transform " + str);
        }
        if (type.equals("matrix")) {
          double[] m = new double[6];
          for (int i = 0; i < 6; i++) {
            if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
              throw new IOException(
                  "Matrix value "
                      + i
                      + " not found in transform "
                      + str
                      + " token:"
                      + tt.ttype
                      + " "
                      + tt.sval);
            }
            if (tt.nextToken() == StreamTokenizer.TT_WORD && tt.sval.startsWith("E")) {
              double mantissa = tt.nval;
              tt.nval = Double.valueOf(tt.nval + tt.sval);
            } else {
              tt.pushBack();
            }
            m[i] = tt.nval;
          }
          t.concatenate(new AffineTransform(m));

        } else if (type.equals("translate")) {
          double tx, ty;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("X-translation value not found in transform " + str);
          }
          tx = tt.nval;
          if (tt.nextToken() == StreamTokenizer.TT_NUMBER) {
            ty = tt.nval;
          } else {
            tt.pushBack();
            ty = 0;
          }
          t.translate(tx, ty);

        } else if (type.equals("scale")) {
          double sx, sy;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("X-scale value not found in transform " + str);
          }
          sx = tt.nval;
          if (tt.nextToken() == StreamTokenizer.TT_NUMBER) {
            sy = tt.nval;
          } else {
            tt.pushBack();
            sy = sx;
          }
          t.scale(sx, sy);

        } else if (type.equals("rotate")) {
          double angle, cx, cy;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("Angle value not found in transform " + str);
          }
          angle = tt.nval;
          if (tt.nextToken() == StreamTokenizer.TT_NUMBER) {
            cx = tt.nval;
            if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
              throw new IOException("Y-center value not found in transform " + str);
            }
            cy = tt.nval;
          } else {
            tt.pushBack();
            cx = cy = 0;
          }
          t.rotate(angle * Math.PI / 180d, cx * Math.PI / 180d, cy * Math.PI / 180d);

        } else if (type.equals("skewX")) {
          double angle;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("Skew angle not found in transform " + str);
          }
          angle = tt.nval;
          t.concatenate(new AffineTransform(1, 0, Math.tan(angle * Math.PI / 180), 1, 0, 0));

        } else if (type.equals("skewY")) {
          double angle;
          if (tt.nextToken() != StreamTokenizer.TT_NUMBER) {
            throw new IOException("Skew angle not found in transform " + str);
          }
          angle = tt.nval;
          t.concatenate(new AffineTransform(1, Math.tan(angle * Math.PI / 180), 0, 1, 0, 0));

        } else {
          throw new IOException("Unknown transform " + type + " in " + str);
        }
        if (tt.nextToken() != ')') {
          throw new IOException("')' not found in transform " + str);
        }
      }
    }
    return t;
  }
Beispiel #9
0
  /**
   * Requests new model inputs from user.
   *
   * <p>Will return -1 if user signals end-of-input ( <code>^d</code>)
   *
   * @param inVec A {@link VectorInfoArrayList} listing inputs and default values
   * @return int -1 to quit, 0 to keep going
   * @throws IOException;
   */
  public int loadInputs(VectorInfoArrayList inVec) throws IOException {
    int tokenType;

    // The following mumbo-jumbo necessary to take stdin and parse it
    FileReader frin = new FileReader(FileDescriptor.in);
    StreamTokenizer st = new StreamTokenizer(frin);

    // Tell parser to look for double-precision numbers and EOLs
    // Note - can't use exponential notation; only 0-9, -, .
    st.parseNumbers();
    st.eolIsSignificant(true);

    // Write header
    System.out.println();
    System.out.println(" Specify input values:");

    // Loop for each input block that might need value
    Iterator<VectorInfo> in = inVec.iterator();
    while (in.hasNext()) {
      boolean blankLine = false;
      VectorInfo inVal = in.next();

      // write name, units, default value
      System.out.print(
          "   " + inVal.getName() + " (" + inVal.getUnits() + ") [" + inVal.getValue() + "]  : ");

      // look for number in input stream; abort on EOF; skip to next on EOL
      do { // look for number or EOL or EOF
        tokenType = st.nextToken();
        // System.out.println("tokenType was " + tokenType);
        if (tokenType == StreamTokenizer.TT_EOF) {
          return -1; // quit
        }
        if (tokenType == StreamTokenizer.TT_EOL) { // skip to next param
          blankLine = true;
          break;
        }
      } while (tokenType != StreamTokenizer.TT_NUMBER); // keep looking until number found

      if (!blankLine) {
        // if not empty line, interpret number and save in block
        //              System.out.println("Input value was " + st.nval);
        try {
          inVal.setValue(st.nval);
          //              System.out.println("setValue called for " + inVal.getName() + " with value
          // of " + st.nval);
        } catch (NumberFormatException e) {
          // take no action - leave value as is
        }

        // look for EOL so we can ignore it
        do {
          tokenType = st.nextToken();
          //              System.out.println("skipping tokenType " + tokenType );
          if (tokenType == StreamTokenizer.TT_EOF) {
            return -1;
          }
        } while (tokenType != StreamTokenizer.TT_EOL);
      }
    }
    return 0; // indicate keep going
  }