org.w3c.dom.svg.SVGDocument#getRootElement ( )源码实例Demo

下面列出了org.w3c.dom.svg.SVGDocument#getRootElement ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Knowage-Server   文件: SVGMapMerger.java
/**
 * Merge map.
 *
 * @param srcMap
 *            the src map
 * @param dstMap
 *            the dst map
 * @param srcId
 *            the src id
 * @param dstId
 *            the dst id
 */
public static void mergeMap(SVGDocument srcMap, SVGDocument dstMap, String srcId, String dstId) {
	SVGElement srcMapRoot;
	Element srcElement;
	Element dstElement;

	srcMapRoot = srcMap.getRootElement();
	srcElement = (srcId == null ? srcMapRoot : srcMap.getElementById(srcId));

	dstElement = dstMap.getElementById(dstId);

	NodeList nodeList = srcElement.getChildNodes();
	for (int i = 0; i < nodeList.getLength(); i++) {
		Node node = nodeList.item(i);
		Node importedNode = dstMap.importNode(node, true);
		dstElement.appendChild(importedNode);
	}
}
 
源代码2 项目: hop   文件: SvgCache.java
public synchronized static SvgCacheEntry loadSvg( SvgFile svgFile ) throws HopException {

    SvgCacheEntry cacheEntry = findSvg( svgFile.getFilename() );
    if (cacheEntry!=null) {
      return cacheEntry;
    }

    try {
      String parser = XMLResourceDescriptor.getXMLParserClassName();
      SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory( parser );
      InputStream svgStream = svgFile.getClassLoader().getResourceAsStream( svgFile.getFilename() );

      if ( svgStream == null ) {
        throw new HopException( "Unable to find file '" + svgFile.getFilename() + "'" );
      }
      SVGDocument svgDocument = factory.createSVGDocument( svgFile.getFilename(), svgStream );

      Element elSVG = svgDocument.getRootElement();
      String widthString = elSVG.getAttribute( "width" );
      String heightString = elSVG.getAttribute( "height" );
      double width = Const.toDouble( widthString.replace( "px", "" ), -1 );
      double height = Const.toDouble( heightString.replace( "px", "" ), -1 );
      if ( width < 0 || height < 0 ) {
        throw new HopException( "Unable to find valid width or height in SVG document " + svgFile.getFilename() );
      }
      cacheEntry = new SvgCacheEntry( svgFile.getFilename(), svgDocument, (int)Math.round(width), (int)Math.round(height) );
      getInstance().fileDocumentMap.put( svgFile.getFilename(), cacheEntry );
      return cacheEntry;
    } catch ( Exception e ) {
      throw new HopException( "Error loading SVG file " + svgFile.getFilename(), e );
    }
  }
 
/**
 * Extract the viewbox of the input SVG
 * @return
 * @throws MalformedURLException
 * @throws IOException
 */
private Rectangle extractSVGBounds(QualifiedSVGResource svg) throws IOException {
    // check <svg> attributes first : x, y, width, height
    SVGDocument svgDocument = getSVGDocument(svg);
    SVGSVGElement svgElement = svgDocument.getRootElement();
    if (svgElement.getAttributeNode("width") != null && svgElement.getAttribute("height") != null) {

        UserAgent userAgent = new DensityAwareUserAgent(svg.getDensity().getDpi());
        UnitProcessor.Context context = org.apache.batik.bridge.UnitProcessor.createContext(
                new BridgeContext(userAgent), svgElement);

        float width = svgLengthInPixels(svgElement.getWidth().getBaseVal(), context);
        float height = svgLengthInPixels(svgElement.getHeight().getBaseVal(), context);
        float x = 0;
        float y = 0;
        // check x and y attributes
        if (svgElement.getX() != null && svgElement.getX().getBaseVal() != null) {
            x = svgLengthInPixels(svgElement.getX().getBaseVal(), context);
        }
        if (svgElement.getY() != null && svgElement.getY().getBaseVal() != null) {
            y = svgLengthInPixels(svgElement.getY().getBaseVal(), context);
        }

        return new Rectangle((int) floor(x), (int) floor(y), (int) ceil(width), (int) ceil(height));
    }

    // use computed bounds
    log.warn("Take time to fix desired width and height attributes of the root <svg> node for this file... " +
            "ROI will be computed by magic using Batik " + boundsType.name() + " bounds");
    return boundsType.getBounds(getGraphicsNode(svgDocument, svg.getDensity().getDpi()));
}