// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package xaal.parser.modules; import java.awt.Color; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import xaal.objects.AddCoordinate; import xaal.objects.AddGraphicalPrimitive; import xaal.objects.Defs; import xaal.objects.StyledObject; import xaal.objects.Xaal; import xaal.objects.graphical.Angle; import xaal.objects.graphical.Arc; import xaal.objects.graphical.Center; import xaal.objects.graphical.Circle; import xaal.objects.graphical.CircleSegment; import xaal.objects.graphical.Coordinate; import xaal.objects.graphical.Ellipse; import xaal.objects.graphical.Font; import xaal.objects.graphical.GraphicalPrimitive; import xaal.objects.graphical.Line; import xaal.objects.graphical.Offset; import xaal.objects.graphical.Point; import xaal.objects.graphical.Polygon; import xaal.objects.graphical.Polyline; import xaal.objects.graphical.Radius; import xaal.objects.graphical.Rectangle; import xaal.objects.graphical.Shape; import xaal.objects.graphical.ShapeDefinition; import xaal.objects.graphical.Square; import xaal.objects.graphical.Style; import xaal.objects.graphical.Text; import xaal.objects.graphical.Triangle; import xaal.parser.ParserModule; /** * @author vkaravir * */ public class XaalGPParserModule extends ParserModule { /** * */ public XaalGPParserModule() { super(); } public void characters(char[] text, int start, int length) { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) peek(); for (int i = start; i < start + length; i++) sb.append(text[i]); } } public void startElementArc(String namespaceURI, String localName, String qName, Attributes atts) { Arc a = new Arc(getXaalObject()); addGraphicalPrimitive(a, atts); push(a); } public void endElementArc(String namespaceURI, String localName, String qName) { pop(); } public void startElementAngle(String namespaceURI, String localName, String qName, Attributes atts) { Angle a = new Angle(); if (atts.getIndex("total") != -1) a.setTotalAngle(Integer.parseInt(atts.getValue("total"))); if (atts.getIndex("start") != -1) a.setStartAngle(Integer.parseInt(atts.getValue("start"))); if (peek() != null && peek() instanceof Arc) { ((Arc)peek()).setAngle(a); } } public void startElementCircle(String namespaceURI, String localName, String qName, Attributes atts) { Circle c = new Circle(getXaalObject()); addGraphicalPrimitive(c, atts); push(c); } public void endElementCircle(String namespaceURI, String localName, String qName) { pop(); } public void startElementCircleSegment(String namespaceURI, String localName, String qName, Attributes atts) { CircleSegment c = new CircleSegment(getXaalObject()); addGraphicalPrimitive(c, atts); push(c); } public void endElementCircleSegment(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementCenter(String namespaceURI, String localName, String qName, Attributes atts) { Center c = new Center(ParserModule.getXaalObject()); if (atts.getIndex("id") != -1) c.setId(atts.getValue("id")); if (atts.getIndex("x") != -1) c.setX(Integer.parseInt(atts.getValue("x").trim())); if (atts.getIndex("y") != -1) c.setY(Integer.parseInt(atts.getValue("y").trim())); if (peek() != null && peek() instanceof Ellipse) { ((Ellipse) peek()).setCenter(c); } else if (peek() != null && peek() instanceof Arc) { ((Arc) peek()).setCenter(c); } else { // TODO error handling } push(c); } public void endElementCenter(String namespaceURI, String localName, String qName) { pop(); } public void startElementRadius(String namespaceURI, String localName, String qName, Attributes atts) { Radius r = new Radius(ParserModule.getXaalObject()); if (atts.getIndex("id") != -1) r.setId(atts.getValue("id")); // if the length attribute is set, ignore x and y if (atts.getIndex("length") != -1) r.setLength(Integer.parseInt(atts.getValue("length").trim())); else { if (atts.getIndex("x") != -1) r.setX(Integer.parseInt(atts.getValue("x").trim())); if (atts.getIndex("y") != -1) r.setY(Integer.parseInt(atts.getValue("y").trim())); } if (peek() != null && peek() instanceof Ellipse) { ((Ellipse) peek()).setRadius(r); } else if (peek() != null && peek() instanceof Arc) { ((Arc) peek()).setRadius(r); } else { // TODO error handling } push(r); } public void endElementRadius(String namespaceURI, String localName, String qName) { pop(); } public void startElementDepth(String namespaceURI, String localName, String qName, Attributes atts) { if (peek() != null && peek() instanceof GraphicalPrimitive) { if (atts.getIndex("value") != -1) ((GraphicalPrimitive) peek()).setDepth(Integer.parseInt(atts .getValue("value"))); } } public void startElementCoordinate(String namespaceURI, String localName, String qName, Attributes atts) { Object top = peek(); if (top == null) top = ParserModule.getProperty(XaalMainParserModule.CURRENT_OBJECT); Coordinate c = new Coordinate(ParserModule.getXaalObject()); if (atts.getIndex("id") != -1) c.setId(atts.getValue("id")); if (atts.getIndex("x") != -1) { try { c.setX(Integer.parseInt(atts.getValue("x").trim())); } catch (NumberFormatException nfe) { // TODO error logging c.setX(0); } } if (atts.getIndex("y") != -1) try { c.setY(Integer.parseInt(atts.getValue("y").trim())); } catch (NumberFormatException nfe) { // TODO error logging c.setX(0); } if (top != null && top instanceof AddCoordinate) { ((AddCoordinate) top).addCoordinate(c); } else { System.out.println("Problem adding coordinate for " + top); } push(c); } public void endElementCoordinate(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementOffset(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { Object top = peek(); if (top == null) top = ParserModule.getProperty(XaalMainParserModule.CURRENT_OBJECT); Coordinate c = (Coordinate) top; Offset o = new Offset(); if (atts.getIndex("x") != -1) { o.setX(Integer.parseInt(atts.getValue("x"))); } if (atts.getIndex("y") != -1) { o.setY(Integer.parseInt(atts.getValue("y"))); } if (atts.getIndex("base-object") != -1) { Xaal x = (Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); o.setBaseObj(x.getXaalObject(atts.getValue("base-object"))); } if (atts.getIndex("anchor") != -1) { o.setAnchor(atts.getValue("anchor")); } c.setOffset(o); } public void endElementOffset(String namespaceURI, String localName, String qName) throws SAXException { } public void startElementStyle(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { Style s = new Style(); Xaal x = (Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); if (atts.getIndex("id") != -1) s.setId(atts.getValue("id")); if (atts.getIndex("uses") != -1) s.setUses(x.getDefs().getStyle(atts.getValue("uses"))); x.getDefs().addStyle(s); push(s); } public void endElementStyle(String namespaceURI, String localName, String qName) throws SAXException { Style s = (Style) pop(); Object o = ParserModule.getCurrentObject(); if (o != null && o instanceof StyledObject) { ((StyledObject) o).setStyle(s); } else if (o != null && o instanceof Defs) { ((Defs) o).addStyle(s); } else { return; } } public void startElementArrow(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Style) { Style s = (Style) peek(); if (atts.getIndex("forward") != -1) s.setForwardArrow(Boolean.parseBoolean(atts.getValue("forward"))); if (atts.getIndex("backward") != -1) s.setBackwardArrow(Boolean.parseBoolean(atts.getValue("backward"))); } } public void startElementColor(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Style) { Style s = (Style) peek(); if (atts.getIndex("name") != -1) s.setColor(atts.getValue("name")); else { int r = 0, g = 0, b = 0; if (atts.getIndex("red") != -1) r = Integer.parseInt(atts.getValue("red")); if (atts.getIndex("green") != -1) g = Integer.parseInt(atts.getValue("green")); if (atts.getIndex("blue") != -1) b = Integer.parseInt(atts.getValue("blue")); s.setColor(new Color(r, g, b)); } } } public void startElementFillColor(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Style) { Style s = (Style) peek(); if (atts.getIndex("name") != -1) s.setFillColor(atts.getValue("name")); else { int r = 0, g = 0, b = 0; if (atts.getIndex("red") != -1) r = Integer.parseInt(atts.getValue("red")); if (atts.getIndex("green") != -1) g = Integer.parseInt(atts.getValue("green")); if (atts.getIndex("blue") != -1) b = Integer.parseInt(atts.getValue("blue")); s.setFillColor(new Color(r, g, b)); } } } public void startElementStroke(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Style) { Style s = (Style) peek(); if (atts.getIndex("type") != -1) s.setStrokeType(atts.getValue("type")); if (atts.getIndex("width") != -1) s.setStrokeWidth(Integer.parseInt(atts.getValue("width"))); } } public void startElementFont(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Style) { Style s = (Style) peek(); Font f = new Font(); if (atts.getIndex("family") != -1) f.setFamily(atts.getValue("family")); if (atts.getIndex("size") != -1) f.setSize(Integer.parseInt(atts.getValue("size"))); if (atts.getIndex("bold") != -1) f.setBold(Boolean.parseBoolean(atts.getValue("bold"))); if (atts.getIndex("italic") != -1) f.setItalic(Boolean.parseBoolean(atts.getValue("italic"))); s.setFont(f); } } public void startElementEllipse(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { Ellipse e = new Ellipse(getXaalObject()); addGraphicalPrimitive(e, atts); push(e); } public void endElementEllipse(String namespaceURI, String localName, String qName) { pop(); } public void startElementLine(String namespaceURI, String localName, String qName, Attributes atts) { Line l = new Line(getXaalObject()); addGraphicalPrimitive(l, atts); push(l); } public void endElementLine(String namespaceURI, String localName, String qName) { pop(); } public void startElementRectangle(String namespaceURI, String localName, String qName, Attributes atts) { Rectangle r = new Rectangle(getXaalObject()); addGraphicalPrimitive(r, atts); push(r); } public void endElementRectangle(String namespaceURI, String localName, String qName) { pop(); } public void startElementRound(String namespaceURI, String localName, String qName, Attributes atts) { if (peek() != null && peek() instanceof Rectangle) { int x = -1; if (atts.getIndex("x") != -1) x = Integer.parseInt(atts.getValue("x")); int y = -1; if (atts.getIndex("y") != -1) y = Integer.parseInt(atts.getValue("y")); if (x == -1 && y != -1) x = y; else if (x != -1 && y == -1) y = x; if (x != -1 && y != -1) ((Rectangle)peek()).setRound(x, y); } } public void startElementPolyline(String namespaceURI, String localName, String qName, Attributes atts) { Polyline p = new Polyline(getXaalObject()); addGraphicalPrimitive(p, atts); push(p); } public void endElementPolyline(String namespaceURI, String localName, String qName) { pop(); } public void startElementClosed(String namespaceURI, String localName, String qName, Attributes atts) { if (peek() != null && peek() instanceof Polyline) { if (atts.getIndex("value") != -1) ((Polyline)peek()).setClosed(Boolean.parseBoolean(atts.getValue("value"))); } } public void startElementPolygon(String namespaceURI, String localName, String qName, Attributes atts) { Polygon p = new Polygon(getXaalObject()); addGraphicalPrimitive(p, atts); push(p); } public void endElementPolygon(String namespaceURI, String localName, String qName) { pop(); } public void startElementText(String namespaceURI, String localName, String qName, Attributes atts) { Text t = new Text(getXaalObject()); addGraphicalPrimitive(t, atts); push(t); } public void endElementText(String namespaceURI, String localName, String qName) { pop(); } public void startElementAlignment(String namespaceURI, String localName, String qName, Attributes atts) { if (peek() != null && peek() instanceof Text) { if (atts.getIndex("value") != -1) ((Text) peek()).setAlignment(atts.getValue("value")); } } public void startElementBoxed(String namespaceURI, String localName, String qName, Attributes atts) { if (peek() != null && peek() instanceof Text) { if (atts.getIndex("value") != -1) ((Text) peek()).setBoxed(Boolean.parseBoolean(atts.getValue("value"))); } } public void startElementContents(String namespaceURI, String localName, String qName, Attributes atts) { if (atts.getIndex("lang") != -1) push(atts.getValue("lang")); else push(""); push(new StringBuffer()); } public void endElementContents(String namespaceURI, String localName, String qName) { if (peek() instanceof StringBuffer) { String str = ((StringBuffer) pop()).toString().trim(); String lang = pop().toString(); if (lang != null && !lang.trim().equals("") && peek() instanceof Text) { ((Text)peek()).addContents(str, lang); } else if (peek() instanceof Text) { ((Text)peek()).setContents(str); } } } public void startElementSquare(String namespaceURI, String localName, String qName, Attributes atts) { Square s = new Square(getXaalObject()); addGraphicalPrimitive(s, atts); push(s); } public void endElementSquare(String namespaceURI, String localName, String qName) { pop(); } public void startElementLength(String namespaceURI, String localName, String qName, Attributes atts) { if (peek() != null && peek() instanceof Square) { if (atts.getIndex("value") != -1) ((Square) peek()).setLength(Integer.parseInt(atts .getValue("value"))); } } public void startElementTriangle(String namespaceURI, String localName, String qName, Attributes atts) { Triangle t = new Triangle(getXaalObject()); addGraphicalPrimitive(t, atts); push(t); } public void endElementTriangle(String namespaceURI, String localName, String qName) { pop(); } public void startElementPoint(String namespaceURI, String localName, String qName, Attributes atts) { Point p = new Point(getXaalObject()); addGraphicalPrimitive(p, atts); push(p); } public void endElementPoint(String namespaceURI, String localName, String qName) { pop(); } public void startElementDefineShape(String namespaceURI, String localName, String qName, Attributes atts) { ShapeDefinition s = new ShapeDefinition(); Xaal x = (Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); if (atts.getIndex("name") != -1) s.setName(atts.getValue("name")); x.getDefs().addShapeDefinition(s); push(s); } public void endElementDefineShape(String namespaceURI, String localName, String qName) { pop(); } public void startElementShape(String namespaceURI, String localName, String qName, Attributes atts) { Shape s = new Shape(getXaalObject()); addGraphicalPrimitive(s, atts); if (atts.getIndex("uses") != -1) { ShapeDefinition sd = ((Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT)).getDefs().getShapeDefinition(atts.getValue("uses")); s.setShapeDefinition(sd); } else { // TODO error logging/handling } push(s); } public void endElementShape(String namespaceURI, String localName, String qName) { pop(); } public void startElementShapeScale(String namespaceURI, String localName, String qName, Attributes atts) { Object o = peek(); if (o == null) o = ParserModule.getCurrentObject(); if (o != null && o instanceof Shape) { double scale = Double.parseDouble(atts.getValue("value")); ((Shape)o).setScale(scale); } else { // TODO error handling } } private AddGraphicalPrimitive getAGPObject() { Object o = peek(); if (o == null) o = ParserModule.getCurrentObject(); if (o == null) o = getProperty(XaalMainParserModule.ADD_GRAPHICALPRIMITIVE_OBJECT); if (o == null || !(o instanceof AddGraphicalPrimitive)) return null; else return (AddGraphicalPrimitive) o; } private void handleGeneralAttributes(GraphicalPrimitive gp, Attributes atts) { if (atts.getIndex("id") != -1) { gp.setId(atts.getValue("id")); Xaal x = (Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); x.addObject(gp.getId(), gp); } if (atts.getIndex("style") != -1) { Style s = ((Xaal) getProperty("xaal-root")).getStyle(atts .getValue("style")); gp.setStyle(s); } if (atts.getIndex("opacity") != -1) { gp.setOpacity(Double.parseDouble(atts.getValue("opacity"))); } if (atts.getIndex("hidden") != -1) { gp.setHidden(Boolean.parseBoolean(atts.getValue("hidden"))); } } protected void addGraphicalPrimitive(GraphicalPrimitive gp, Attributes atts) { AddGraphicalPrimitive agp = getAGPObject(); if (agp != null) { handleGeneralAttributes(gp, atts); agp.addGraphical(gp); } else { // TODO error handling } } }