Java Code Examples for org.w3c.dom.svg.SVGDocument#getRootElement()

The following examples show how to use org.w3c.dom.svg.SVGDocument#getRootElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SVGMapMerger.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example 2
Source File: SvgCache.java    From hop with Apache License 2.0 5 votes vote down vote up
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 );
    }
  }
 
Example 3
Source File: QualifiedSVGResourceFactory.java    From androidsvgdrawable-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
}