コード例 #1
0
  public static void main(String[] args) throws Exception {
    // Create an SVG document.
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);

    // Create a converter for this document.
    SVGGraphics2D g = new SVGGraphics2D(doc);

    // Do some drawing.
    Shape circle = new Ellipse2D.Double(0, 0, 50, 50);
    g.setPaint(Color.red);
    g.fill(circle);
    g.translate(60, 0);
    g.setPaint(Color.green);
    g.fill(circle);
    g.translate(60, 0);
    g.setPaint(Color.blue);
    g.fill(circle);
    g.setSVGCanvasSize(new Dimension(180, 50));

    // Populate the document root with the generated SVG content.
    Element root = doc.getDocumentElement();
    g.getRoot(root);

    Writer out = new OutputStreamWriter(System.out, "UTF-8");
    g.stream(out, true);

    // Display the document.
    JSVGCanvas canvas = new JSVGCanvas();
    JFrame f = new JFrame();
    f.getContentPane().add(canvas);
    canvas.setSVGDocument(doc);
    f.pack();
    f.setVisible(true);
  }
コード例 #2
0
ファイル: SVGAccuracyTest.java プロジェクト: Squeegee/batik
  /**
   * This method will only throw exceptions if some aspect of the test's internal operation fails.
   */
  public TestReport runImpl() throws Exception {
    DefaultTestReport report = new DefaultTestReport(this);

    SVGGraphics2D g2d = buildSVGGraphics2D();
    g2d.setSVGCanvasSize(CANVAS_SIZE);

    //
    // Generate SVG content
    //
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8");
    try {
      painter.paint(g2d);
      configureSVGGraphics2D(g2d);
      g2d.stream(osw);
      osw.flush();
      bos.flush();
      bos.close();
    } catch (Exception e) {
      StringWriter trace = new StringWriter();
      e.printStackTrace(new PrintWriter(trace));
      report.setErrorCode(ERROR_CANNOT_GENERATE_SVG);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_CANNOT_GENERATE_SVG,
                    new String[] {
                      painter == null ? "null" : painter.getClass().getName(),
                      e.getClass().getName(),
                      e.getMessage(),
                      trace.toString()
                    }))
          });
      report.setPassed(false);
      return report;
    }

    //
    // Compare with reference SVG
    //
    InputStream refStream = null;
    try {
      refStream = new BufferedInputStream(refURL.openStream());
    } catch (Exception e) {
      report.setErrorCode(ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE,
                    new Object[] {
                      refURL != null ? refURL.toExternalForm() : "null", e.getMessage()
                    }))
          });
      report.setPassed(false);
      save(bos.toByteArray());
      return report;
    }

    InputStream newStream = new ByteArrayInputStream(bos.toByteArray());

    boolean accurate = true;
    String refLine = null;
    String newLine = null;
    int ln = 1;

    try {
      // accurate = compare(refStream, newStream);
      BufferedReader refReader = new BufferedReader(new InputStreamReader(refStream));
      BufferedReader newReader = new BufferedReader(new InputStreamReader(newStream));
      while ((refLine = refReader.readLine()) != null) {
        newLine = newReader.readLine();
        if (newLine == null || !refLine.equals(newLine)) {
          accurate = false;
          break;
        }
        ln++;
      }

      if (accurate) {
        // need to make sure newLine is null as well
        newLine = newReader.readLine();
        if (newLine != null) {
          accurate = false;
        }
      }

    } catch (IOException e) {
      report.setErrorCode(ERROR_ERROR_WHILE_COMPARING_FILES);
      report.setDescription(
          new TestReport.Entry[] {
            new TestReport.Entry(
                Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                Messages.formatMessage(
                    ERROR_ERROR_WHILE_COMPARING_FILES,
                    new Object[] {refURL.toExternalForm(), e.getMessage()}))
          });
      report.setPassed(false);
      save(bos.toByteArray());
      return report;
    }

    if (!accurate) {
      save(bos.toByteArray());
      int cn = computeColumnNumber(refLine, newLine);
      String expectedChar = "eol";
      if (cn >= 0 && refLine != null && refLine.length() > cn) {
        expectedChar = (new Character(refLine.charAt(cn))).toString();
      }
      String foundChar = "null";
      if (cn >= 0 && newLine != null && newLine.length() > cn) {
        foundChar = (new Character(newLine.charAt(cn))).toString();
      }

      if (expectedChar.equals(" ")) {
        expectedChar = "' '";
      }
      if (foundChar.equals(" ")) {
        foundChar = "' '";
      }

      report.setErrorCode(ERROR_GENERATED_SVG_INACCURATE);
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_LINE_NUMBER, null), new Integer(ln));
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_NUMBER, null), new Integer(cn));
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_EXPECTED_VALUE, null), expectedChar);
      report.addDescriptionEntry(
          Messages.formatMessage(ENTRY_KEY_COLUMN_FOUND_VALUE, null), foundChar);
      report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_REFERENCE_LINE, null), refLine);
      report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_NEW_LINE, null), newLine);
      report.setPassed(false);
    } else {
      report.setPassed(true);
    }

    return report;
  }