示例#1
19
  /**
   * Looks up a href within our universe. If the href refers to a document that is not loaded, it
   * will be loaded. The URL #target will then be checked against the SVG diagram's index and the
   * coresponding element returned. If there is no coresponding index, null is returned.
   */
  public SVGElement getElement(URI path, boolean loadIfAbsent) {
    try {
      // Strip fragment from URI
      URI xmlBase = new URI(path.getScheme(), path.getSchemeSpecificPart(), null);

      SVGDiagram dia = (SVGDiagram) loadedDocs.get(xmlBase);
      if (dia == null && loadIfAbsent) {
        // System.err.println("SVGUnivserse: " + xmlBase.toString());
        // javax.swing.JOptionPane.showMessageDialog(null, xmlBase.toString());
        URL url = xmlBase.toURL();

        loadSVG(url, false);
        dia = (SVGDiagram) loadedDocs.get(xmlBase);
        if (dia == null) {
          return null;
        }
      }

      String fragment = path.getFragment();
      return fragment == null ? dia.getRoot() : dia.getElement(fragment);
    } catch (Exception e) {
      Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, "Could not parse path " + path, e);
      return null;
    }
  }
示例#2
0
  private void loadURL(URL url) {
    boolean verbose = cmCheck_verbose.isSelected();

    universe = new SVGUniverse();
    universe.setVerbose(verbose);
    SVGDiagram diagram = null;

    if (!CheckBoxMenuItem_anonInputStream.isSelected()) {
      // Load from a disk with a valid URL
      URI uri = universe.loadSVG(url);

      if (verbose) System.err.println(uri.toString());

      diagram = universe.getDiagram(uri);
    } else {
      // Load from a stream with no particular valid URL
      try {
        InputStream is = url.openStream();
        URI uri = universe.loadSVG(is, "defaultName");

        if (verbose) System.err.println(uri.toString());

        diagram = universe.getDiagram(uri);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    svgDisplayPanel.setDiagram(diagram);
    repaint();
  }
示例#3
0
  /**
   * Returns the diagram that has been loaded from this root. If diagram is not already loaded,
   * returns null.
   */
  public SVGDiagram getDiagram(URI xmlBase, boolean loadIfAbsent) {
    if (xmlBase == null) {
      return null;
    }

    SVGDiagram dia = (SVGDiagram) loadedDocs.get(xmlBase);
    if (dia != null || !loadIfAbsent) {
      return dia;
    }

    // Load missing diagram
    try {
      URL url;
      if ("jar".equals(xmlBase.getScheme())
          && xmlBase.getPath() != null
          && !xmlBase.getPath().contains("!/")) {
        // Workaround for resources stored in jars loaded by Webstart.
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6753651
        url = SVGUniverse.class.getResource("xmlBase.getPath()");
      } else {
        url = xmlBase.toURL();
      }

      loadSVG(url, false);
      dia = (SVGDiagram) loadedDocs.get(xmlBase);
      return dia;
    } catch (Exception e) {
      Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, "Could not parse", e);
    }

    return null;
  }
示例#4
0
  private void loadURL(URL url) {
    boolean verbose = cmCheck_verbose.isSelected();

    //                SVGUniverse universe = new SVGUniverse();
    SVGUniverse universe = SVGCache.getSVGUniverse();
    SVGDiagram diagram = null;
    URI uri;

    if (!CheckBoxMenuItem_anonInputStream.isSelected()) {
      // Load from a disk with a valid URL
      uri = universe.loadSVG(url);

      if (verbose) System.err.println("Loading document " + uri.toString());

      diagram = universe.getDiagram(uri);
    } else {
      // Load from a stream with no particular valid URL
      try {
        InputStream is = url.openStream();
        uri = universe.loadSVG(is, "defaultName");

        if (verbose) System.err.println("Loading document " + uri.toString());
      } catch (Exception e) {
        e.printStackTrace();
        return;
      }
    }
    /*
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(bs);
    os.writeObject(universe);
    os.close();

    ByteArrayInputStream bin = new ByteArrayInputStream(bs.toByteArray());
    ObjectInputStream is = new ObjectInputStream(bin);
    universe = (SVGUniverse)is.readObject();
    is.close();
    */

    diagram = universe.getDiagram(uri);

    svgDisplayPanel.setDiagram(diagram);
    repaint();
  }
示例#5
0
 /**
  * @param pathToSVGFile e.g. "/tracks/monaco/track.svg"
  * @param width int declaring required width
  * @param height int declaring required height
  * @return the successfully rendered BufferedImage, null otherwise
  */
 public static BufferedImage getSVGImage(
     final String pathToSVGFile, final int width, final int height) {
   final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   try {
     final SVGUniverse uni = new SVGUniverse();
     final URI svgURI = uni.loadSVG(ImageLoader.class.getResource(pathToSVGFile));
     paintImage((Graphics2D) image.getGraphics(), new Dimension(width, height), uni, svgURI);
   } catch (final Exception e) {
     e.printStackTrace();
     return null;
   }
   return createCompatibleImage(image, true);
 }