HERE IS THE SVG FILE:

import java.awt.Shape;
import java.io.File;
import java.io.IOException;

import org.apache.batik.bridge.*;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.ext.awt.geom.PathLength;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.ShapeNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.svg.*;
import org.w3c.dom.*;

public class RenderableSVG {

    public static void main(String[] args) {
        SVGDocument doc = null;
        String rtFileName = "C:\\Users\\Craig\\Google
Drive\\eclipse-workspace2\\Find Length Of Shape By ID\\drawing.svg";

        try {
            String urlString = new
File(rtFileName).getAbsoluteFile().toURL().toString();

            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            doc = (SVGDocument) f.createDocument(urlString);
        } catch (IOException ex) {
            System.out.println("SVG file could not opened");
        }

        UserAgent userAgent=new UserAgentAdapter();
        DocumentLoader loader=new DocumentLoader(userAgent);
        BridgeContext ctx=new BridgeContext(userAgent,loader);
        System.out.println(ctx);

        Element el = doc.getElementById("ABC");
        System.out.println(el);

        GraphicsNode gn = ctx.getGraphicsNode(el);
        System.out.println(gn);

        Shape shape = ((ShapeNode)gn).getShape();
        PathLength pathLength = new PathLength(shape);
        System.out.println(pathLength.lengthOfPath());
     }
}

HERE IS THE SVG FILE:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/";
   xmlns:cc="http://creativecommons.org/ns#";
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
   xmlns:svg="http://www.w3.org/2000/svg";
   xmlns="http://www.w3.org/2000/svg";
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd";
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape";
   style="fill-opacity:1; color-rendering:auto; color-interpolation:auto;
stroke:black; text-rendering:auto; stroke-linecap:square;
stroke-miterlimit:10; stroke-opacity:1; shape-rendering:auto; fill:black;
stroke-dasharray:none; font-weight:normal; stroke-width:1;
font-family:'Dialog'; font-style:normal; stroke-linejoin:miter;
font-size:12; stroke-dashoffset:0; image-rendering:auto;"
   width="200"
   height="200"
   id="svg2"
   version="1.1"
   inkscape:version="0.48.4 r9939"
   sodipodi:docname="drawing.svg">
  <metadata
     id="metadata13">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage"; />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <sodipodi:namedview
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1"
     objecttolerance="10"
     gridtolerance="10"
     guidetolerance="10"
     inkscape:pageopacity="0"
     inkscape:pageshadow="2"
     inkscape:window-width="640"
     inkscape:window-height="480"
     id="namedview11"
     showgrid="false"
     inkscape:zoom="1.18"
     inkscape:cx="100"
     inkscape:cy="100"
     inkscape:window-x="0"
     inkscape:window-y="0"
     inkscape:window-maximized="0"
     inkscape:current-layer="svg2" />
  <!--Generated by the Batik Graphics2D SVG Generator-->
  <defs
     id="genericDefs" />
  <g
     id="g5">
    <g
       id="g7">
      <rect
         x="0"
         width="100"
         height="100"
         y="0"
         style="stroke:none"
         id="ABC" />
    </g>
  </g>
</svg>

HERE IS WHAT THE PROGRAM PRINTS OUT:

org.apache.batik.bridge.BridgeContext@65ab90e2
org.apache.batik.dom.svg.SVGOMRectElement@62b51993
null
Exception in thread "main" java.lang.NullPointerException
    at RenderableSVG.main(RenderableSVG.java:41)

WHY IS THE VARIABLE gn SET TO null?

SHOULDN'T gn BE SET TO THE OBJECT CORRESPONDING TO THE ELEMENT WITH ID SET
TO "ABC"?

THE LENGTH OF THE RECTANGLE PATH SHOULD BE 400.

Reply via email to