Пример #1
0
  public static Map<String, String> getStyles(String str) throws IOException {
    HashMap<String, String> styles = new HashMap<String, String>();
    if (str == null) return styles;

    StreamTokenizer tt = new StreamTokenizer(new StringReader(str));
    tt.resetSyntax();
    tt.wordChars('!', '9');
    tt.wordChars('<', '~');
    tt.wordChars(128 + 32, 255);
    tt.whitespaceChars(0, ' ');

    while (tt.nextToken() != StreamTokenizer.TT_EOF) {
      if (tt.ttype != ';') {
        String key, value;
        if (tt.ttype != StreamTokenizer.TT_WORD) {
          throw new IOException(
              "Key token expected in " + str + " " + Integer.toHexString(tt.ttype));
        }
        key = tt.sval;
        if (tt.nextToken() != ':') {
          throw new IOException("Colon expected after " + key + " in " + str);
        }
        if (tt.nextToken() != StreamTokenizer.TT_WORD) {
          throw new IOException(
              "Value token expected after " + key + " in " + str + " " + tt.ttype);
        }
        value = tt.sval;
        while (tt.nextToken() == StreamTokenizer.TT_WORD) {
          value += ' ' + tt.sval;
        }
        tt.pushBack();
        styles.put(key, value);
      }
    }

    return styles;
  }
Пример #2
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;
  }
Пример #3
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;
  }
Пример #4
0
  private ArrayList<String> nextRecordLocal() throws IOException {
    if (atEOF) {
      return null;
    }

    ArrayList<String> record = new ArrayList<String>(maxFieldCount);

    StringBuilder fieldValue = null;

    while (true) {
      int token = parser.nextToken();

      if (token == StreamTokenizer.TT_EOF) {
        addField(record, fieldValue);
        atEOF = true;
        break;
      }

      if (token == StreamTokenizer.TT_EOL) {
        addField(record, fieldValue);
        break;
      }

      if (token == separator) {
        addField(record, fieldValue);
        fieldValue = null;
        continue;
      }

      if (token == StreamTokenizer.TT_WORD) {
        if (fieldValue != null) {
          throw new CSVParseException("Unknown error", parser.lineno());
        }

        fieldValue = new StringBuilder(parser.sval);
        continue;
      }

      if (token == '"') {
        if (fieldValue != null) {
          throw new CSVParseException(
              "Found unescaped quote. A value with quote should be within a quote",
              parser.lineno());
        }

        while (true) {
          token = parser.nextToken();

          if (token == StreamTokenizer.TT_EOF) {
            atEOF = true;
            throw new CSVParseException(
                "EOF reached before closing an opened quote", parser.lineno());
          }

          if (token == separator) {
            fieldValue = appendFieldValue(fieldValue, token);
            continue;
          }

          if (token == StreamTokenizer.TT_EOL) {
            fieldValue = appendFieldValue(fieldValue, "\n");
            continue;
          }

          if (token == StreamTokenizer.TT_WORD) {
            fieldValue = appendFieldValue(fieldValue, parser.sval);
            continue;
          }

          if (token == '"') {
            int nextToken = parser.nextToken();

            if (nextToken == '"') {
              // escaped quote
              fieldValue = appendFieldValue(fieldValue, nextToken);
              continue;
            }

            if (nextToken == StreamTokenizer.TT_WORD) {
              throw new CSVParseException(
                  "Not expecting more text after end quote at line " + parser.lineno(),
                  parser.lineno());
            } else {
              parser.pushBack();
              break;
            }
          }
        }
      }
    }

    if (record.size() > maxFieldCount) {
      maxFieldCount = record.size();
    }

    return record;
  }