/** * @see org.newdawn.slick.svg.inkscape.ElementProcessor#process(org.newdawn.slick.svg.Loader, org.w3c.dom.Element, org.newdawn.slick.svg.Diagram, org.newdawn.slick.geom.Transform) */ public void process(Loader loader, Element element, Diagram diagram, Transform transform) throws ParsingException { String ref = element.getAttributeNS("http://www.w3.org/1999/xlink", "href"); String href = Util.getAsReference(ref); Figure referenced = diagram.getFigureByID(href); if (referenced == null) { throw new ParsingException(element, "Unable to locate referenced element: "+href); } Transform local = Util.getTransform(element); Transform trans = local.concatenate(referenced.getTransform()); NonGeometricData data = Util.getNonGeometricData(element); Shape shape = referenced.getShape().transform(trans); data.addAttribute(NonGeometricData.FILL, referenced.getData().getAttribute(NonGeometricData.FILL)); data.addAttribute(NonGeometricData.STROKE, referenced.getData().getAttribute(NonGeometricData.STROKE)); data.addAttribute(NonGeometricData.OPACITY, referenced.getData().getAttribute(NonGeometricData.OPACITY)); data.addAttribute(NonGeometricData.STROKE_WIDTH, referenced.getData().getAttribute(NonGeometricData.STROKE_WIDTH)); Figure figure = new Figure(referenced.getType(), shape, data, trans); diagram.addFigure(figure); }
/** * Process the points in a polygon definition * * @param poly The polygon being built * @param element The XML element being read * @param tokens The tokens representing the path * @return The number of points found * @throws ParsingException Indicates an invalid token in the path */ private static int processPoly(Polygon poly, Element element, StringTokenizer tokens) throws ParsingException { int count = 0; while (tokens.hasMoreTokens()) { String nextToken = tokens.nextToken(); if (nextToken.equals("L")) { continue; } if (nextToken.equals("z")) { break; } if (nextToken.equals("M")) { continue; } if (nextToken.equals("C")) { return 0; } String tokenX = nextToken; String tokenY = tokens.nextToken(); try { float x = Float.parseFloat(tokenX); float y = Float.parseFloat(tokenY); poly.addPoint(x,y); count++; } catch (NumberFormatException e) { throw new ParsingException(element.getAttribute("id"), "Invalid token in points list", e); } } return count; }
/** * Get a floating point attribute that may appear in either the default or * SODIPODI namespace * * @param element The element from which the attribute should be read * @param attr The attribute to be read * @return The value from the given attribute * @throws ParsingException Indicates the value in the attribute was not a float */ static float getFloatAttribute(Element element, String attr) throws ParsingException { String cx = element.getAttribute(attr); if ((cx == null) || (cx.equals(""))) { cx = element.getAttributeNS(SODIPODI, attr); } try { return Float.parseFloat(cx); } catch (NumberFormatException e) { throw new ParsingException(element, "Invalid value for: "+attr, e); } }
/** * Process the points in a polygon definition * * @param poly The polygon being built * @param element The XML element being read * @param tokens The tokens representing the path * @return The number of points found * @throws ParsingException Indicates an invalid token in the path */ private static int processPoly(Polygon poly, Element element, StringTokenizer tokens) throws ParsingException { int count = 0; ArrayList pts = new ArrayList(); boolean moved = false; boolean closed = false; while (tokens.hasMoreTokens()) { String nextToken = tokens.nextToken(); if (nextToken.equals("L")) { continue; } if (nextToken.equals("z")) { closed = true; break; } if (nextToken.equals("M")) { if (!moved) { moved = true; continue; } return 0; } if (nextToken.equals("C")) { return 0; } String tokenX = nextToken; String tokenY = tokens.nextToken(); try { float x = Float.parseFloat(tokenX); float y = Float.parseFloat(tokenY); poly.addPoint(x,y); count++; } catch (NumberFormatException e) { throw new ParsingException(element.getAttribute("id"), "Invalid token in points list", e); } } poly.setClosed(closed); return count; }
/** * Process a document extracting all the elements that the processor is * interested in and producing appropriate diagram components for the * element. * * @param loader The loader/context of the parsing * @param element The element to be processed * @param diagram The diagram to be built * @param transform The transform to apply to all elements at this level * @throws ParsingException Indicates an invalid content to an element */ public void process(Loader loader, Element element, Diagram diagram, Transform transform) throws ParsingException;