Esempio n. 1
0
 /** Returns the XML parser to use from the TranscodingGetHints(). */
 public final String getXMLParserClassName() {
   if (SpriteTranscoder.this.hints.containsKey(KEY_XML_PARSER_CLASSNAME)) {
     return (String) SpriteTranscoder.this.hints.get(KEY_XML_PARSER_CLASSNAME);
   } else {
     return XMLResourceDescriptor.getXMLParserClassName();
   }
 }
Esempio n. 2
0
 /** Creates the <code>TranscoderInput</code>. */
 protected TranscoderInput createTranscoderInput() {
   try {
     String parser = XMLResourceDescriptor.getXMLParserClassName();
     SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
     Document doc = f.createDocument(resolveURL(inputURI).toString());
     return new TranscoderInput(doc);
   } catch (IOException ex) {
     throw new IllegalArgumentException(inputURI);
   }
 }
Esempio n. 3
0
  public Document(InputStream stream) throws IOException {
    f = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
    doc = f.createSVGDocument(null, stream);

    // Boot the CSS engine
    userAgent = new UserAgentAdapter();
    loader = new DocumentLoader(userAgent);
    ctx = new BridgeContext(userAgent, loader);
    ctx.setDynamicState(BridgeContext.DYNAMIC);
    builder = new GVTBuilder();
    rootGN = builder.build(ctx, doc);

    svgRoot = doc.getRootElement();
    vcss = (ViewCSS) doc.getDocumentElement();
  }
Esempio n. 4
0
public class Constant {
  public static String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  static String parser = XMLResourceDescriptor.getXMLParserClassName();
  public static SAXSVGDocumentFactory saxFactory = new SAXSVGDocumentFactory(parser);
  protected static int svgDimension = 48;

  public CanvasMediator mediator;
  public StateManager stateManager = new StateManager((CanvasImpl) this);
  public JPanel guiPanel;

  public PointMatrix matrix = new PointMatrix();
  protected JSVGCanvas canvas = new JSVGCanvas();

  protected static DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  public SVGDocument domFactory;
}
public class SVGColorExtract {
  static final String parser = XMLResourceDescriptor.getXMLParserClassName();

  static final SAXSVGDocumentFactory svgDocFactory = new SAXSVGDocumentFactory(parser);

  public static void main(String[] args) {
    try {
      FileInputStream f = new FileInputStream(args[0]);

      SVGDocument doc = (SVGDocument) svgDocFactory.createDocument(null, f);

      BridgeContext bc = new BridgeContext(new UserAgentAdapter());
      GVTBuilder gvtb = new GVTBuilder();

      GraphicsNode gn = gvtb.build(bc, doc);
      GVTTreeWalker tw = new GVTTreeWalker(gn);
      gn = tw.nextGraphicsNode();

      System.out.print("new Color[] { ");

      Graphics2D g2 =
          GraphicsEnvironment.getLocalGraphicsEnvironment()
              .createGraphics(new BufferedImage(32, 32, BufferedImage.TYPE_3BYTE_BGR));

      while (true) {
        // System.out.println("node " + i);
        gn = tw.nextGraphicsNode();
        if (gn == null) {
          break;
        }

        gn.paint(g2);
        Color c = g2.getColor();
        System.out.print(
            "new Color(" + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() + "), ");
      }

      System.out.println("}");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 private void loadDocument() {
   transcoder = null;
   failedToLoadDocument = true;
   if (uri == null) {
     return;
   }
   String parser = XMLResourceDescriptor.getXMLParserClassName();
   SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
   try {
     Document document;
     if (documentsMap.containsKey(uri)) document = documentsMap.get(uri);
     else {
       document = factory.createDocument(uri);
       documentsMap.put(uri, document);
     }
     transcoder = new SimpleImageTranscoder(document);
     failedToLoadDocument = false;
   } catch (IOException e) {
     Activator.logError("Error loading SVG file", e);
   }
 }
  public Icon getIcon(Feature feature, Expression url, String format, int size) throws Exception {
    // check we do support the declared format
    if (format == null || !formats.contains(format.toLowerCase())) return null;

    // grab the url
    URL svgfile = url.evaluate(feature, URL.class);
    if (svgfile == null)
      throw new IllegalArgumentException(
          "The specified expression could not be turned into an URL");

    // turn the svg into a document and cache results
    RenderableSVG svg = glyphCache.get(svgfile);
    if (svg == null) {
      String parser = XMLResourceDescriptor.getXMLParserClassName();
      SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
      Document doc = f.createDocument(svgfile.toString());
      svg = new RenderableSVG(doc);
      glyphCache.put(svgfile, svg);
    }

    return new SVGIcon(svg, size);
  }
Esempio n. 8
0
  private static void drawSvgToGraphics2D(Svg svg, Graphics2D g2, Dimension size)
      throws IOException {
    // Copied (and modified) from http://stackoverflow.com/a/12502943

    String parser = XMLResourceDescriptor.getXMLParserClassName();
    SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
    UserAgent userAgent = new UserAgentAdapter();
    DocumentLoader loader = new DocumentLoader(userAgent);
    BridgeContext ctx = new BridgeContext(userAgent, loader);
    ctx.setDynamicState(BridgeContext.DYNAMIC);
    GVTBuilder builder = new GVTBuilder();

    StringReader svgReader = new StringReader(svg.toString());
    SVGDocument parsedSvgDocument = factory.createSVGDocument(null, svgReader);
    GraphicsNode chartGfx = builder.build(ctx, parsedSvgDocument);
    Dimension actualSize = svg.getSize();
    double scaleWidth = 1.0 * size.width / actualSize.width;
    double scaleHeight = 1.0 * size.height / actualSize.height;
    chartGfx.setTransform(AffineTransform.getScaleInstance(scaleWidth, scaleHeight));

    chartGfx.paint(g2);
  }
  /**
   * Transcodes the specified XML input in the specified output. All <code>TranscoderException
   * </code> exceptions not catched previously are tagged as fatal errors (ie. call the <code>
   * fatalError</code> method of the <code>ErrorHandler</code>).
   *
   * @param input the XML input to transcode
   * @param output the ouput where to transcode
   * @exception TranscoderException if an error occured while transcoding
   */
  public void transcode(TranscoderInput input, TranscoderOutput output) throws TranscoderException {

    Document document = null;
    String uri = input.getURI();
    if (input.getDocument() != null) {
      document = input.getDocument();
    } else {
      String parserClassname = (String) hints.get(KEY_XML_PARSER_CLASSNAME);
      String namespaceURI = (String) hints.get(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI);
      String documentElement = (String) hints.get(KEY_DOCUMENT_ELEMENT);
      DOMImplementation domImpl = (DOMImplementation) hints.get(KEY_DOM_IMPLEMENTATION);

      if (parserClassname == null) {
        parserClassname = XMLResourceDescriptor.getXMLParserClassName();
      }
      if (domImpl == null) {
        handler.fatalError(
            new TranscoderException("Unspecified transcoding hints: KEY_DOM_IMPLEMENTATION"));
        return;
      }
      if (namespaceURI == null) {
        handler.fatalError(
            new TranscoderException(
                "Unspecified transcoding hints: KEY_DOCUMENT_ELEMENT_NAMESPACE_URI"));
        return;
      }
      if (documentElement == null) {
        handler.fatalError(
            new TranscoderException("Unspecified transcoding hints: KEY_DOCUMENT_ELEMENT"));
        return;
      }
      // parse the XML document
      DocumentFactory f = createDocumentFactory(domImpl, parserClassname);
      Object xmlParserValidating = hints.get(KEY_XML_PARSER_VALIDATING);
      boolean validating =
          xmlParserValidating != null && ((Boolean) xmlParserValidating).booleanValue();
      f.setValidating(validating);
      try {
        if (input.getInputStream() != null) {
          document =
              f.createDocument(
                  namespaceURI, documentElement, input.getURI(), input.getInputStream());
        } else if (input.getReader() != null) {
          document =
              f.createDocument(namespaceURI, documentElement, input.getURI(), input.getReader());
        } else if (input.getXMLReader() != null) {
          document =
              f.createDocument(namespaceURI, documentElement, input.getURI(), input.getXMLReader());
        } else if (uri != null) {
          document = f.createDocument(namespaceURI, documentElement, uri);
        }
      } catch (DOMException ex) {
        handler.fatalError(new TranscoderException(ex));
      } catch (IOException ex) {
        handler.fatalError(new TranscoderException(ex));
      }
    }
    // call the dedicated transcode method
    if (document != null) {
      try {
        transcode(document, uri, output);
      } catch (TranscoderException ex) {
        // at this time, all TranscoderExceptions are fatal errors
        handler.fatalError(ex);
        return;
      }
    }
  }
 /**
  * Creates a preference manager.
  *
  * @param prefFileName the name of the preference file.
  * @param defaults where to get defaults value if the value is not specified in the file.
  */
 public XMLPreferenceManager(String prefFileName, Map defaults) {
   this(prefFileName, defaults, XMLResourceDescriptor.getXMLParserClassName());
 }
 /**
  * Creates a preference manager.
  *
  * @param prefFileName the name of the preference file.
  */
 public XMLPreferenceManager(String prefFileName) {
   this(prefFileName, null, XMLResourceDescriptor.getXMLParserClassName());
 }