// 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 org.xml.sax.Attributes; import xaal.objects.AddGraphicalPrimitive; import xaal.objects.AddStructure; import xaal.objects.Xaal; import xaal.objects.animation.AddAnimationOperation; import xaal.objects.animation.AnimationOperation; import xaal.objects.animation.GraphicalOperation; import xaal.objects.animation.graphical.ChangePropertyOperation; import xaal.objects.animation.graphical.ChangeStyleOperation; import xaal.objects.animation.graphical.HideOperation; import xaal.objects.animation.graphical.MoveOperation; import xaal.objects.animation.graphical.RotateOperation; import xaal.objects.animation.graphical.ScaleOperation; import xaal.objects.animation.graphical.ShowOperation; import xaal.objects.graphical.GraphicalPrimitive; import xaal.objects.structures.Structure; import xaal.parser.ParserModule; public class XaalGPAnimParserModule extends ParserModule { public XaalGPAnimParserModule() { super(); } public void startElementHide(String namespaceURI, String localName, String qName, Attributes atts) { AddAnimationOperation aao = getAAOObject(); if (aao != null) { HideOperation ho = new HideOperation(getXaalObject()); handleAnimationAttributes(ho, atts); aao.addAnimationOperation(ho); if (atts.getIndex("type") != -1) ho.setType(atts.getValue("type")); push(ho); } else { // TODO error handling push(null); } } public void endElementHide(String namespaceURI, String localName, String qName) { pop(); } public void startElementShow(String namespaceURI, String localName, String qName, Attributes atts) { AddAnimationOperation aao = getAAOObject(); if (aao != null) { ShowOperation so = new ShowOperation(getXaalObject()); handleAnimationAttributes(so, atts); aao.addAnimationOperation(so); if (atts.getIndex("type") != -1) so.setType(atts.getValue("type")); push(so); } else { // TODO error handling push(null); } } public void endElementShow(String namespaceURI, String localName, String qName) { pop(); } public void startElementScale(String namespaceURI, String localName, String qName, Attributes atts) { AddAnimationOperation aao = getAAOObject(); if (aao != null) { ScaleOperation so = new ScaleOperation(getXaalObject()); handleAnimationAttributes(so, atts); aao.addAnimationOperation(so); if (atts.getIndex("scale") != -1) so.setScale(atts.getValue("scale")); push(so); } else { // TODO error handling push(null); } } public void endElementScale(String namespaceURI, String localName, String qName) { pop(); } public void startElementRotate(String namespaceURI, String localName, String qName, Attributes atts) { AddAnimationOperation aao = getAAOObject(); if (aao != null) { RotateOperation ro = new RotateOperation(getXaalObject()); handleAnimationAttributes(ro, atts); aao.addAnimationOperation(ro); if (atts.getIndex("type") != -1) ro.setType(atts.getValue("type")); if (atts.getIndex("degree") != -1) ro.setDegree(atts.getValue("degree")); push(ro); } else { // TODO error handling push(null); } } public void endElementRotate(String namespaceURI, String localName, String qName) { pop(); } public void startElementChangeStyle(String namespaceURI, String localName, String qName, Attributes atts) { AddAnimationOperation aao = getAAOObject(); if (aao != null) { ChangeStyleOperation cso = new ChangeStyleOperation(getXaalObject()); handleAnimationAttributes(cso, atts); aao.addAnimationOperation(cso); push(cso); } else { // TODO error handling push(null); } } public void endElementChangeStyle(String namespaceURI, String localName, String qName) { pop(); } public void startElementChangeProperty(String namespaceURI, String localName, String qName, Attributes atts) { AddAnimationOperation aao = getAAOObject(); if (aao != null) { ChangePropertyOperation cpo = new ChangePropertyOperation(getXaalObject()); handleAnimationAttributes(cpo, atts); aao.addAnimationOperation(cpo); push(cpo); } else { // TODO error handling push(null); } } public void endElementChangeProperty(String namespaceURI, String localName, String qName) { pop(); } public void startElementMove(String namespaceURI, String localName, String qName, Attributes atts) { AddAnimationOperation aao = getAAOObject(); if (aao != null) { MoveOperation mo = new MoveOperation(getXaalObject()); handleAnimationAttributes(mo, atts); if (atts.getIndex("type") != -1) mo.setType(atts.getValue("type")); aao.addAnimationOperation(mo); push(mo); } else { // TODO error handling push(null); } } public void endElementMove(String namespaceURI, String localName, String qName) { pop(); } public void startElementObjectRef(String namespaceURI, String localName, String qName, Attributes atts) { AnimationOperation top = getGOObject(); if (top != null) { String id = atts.getValue("id"); Xaal x = (Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Object o = x.getObject(id); if (o != null) { if (top instanceof AddStructure && o instanceof Structure) { ((AddStructure)top).addStructure((Structure) o); } else if (top instanceof AddGraphicalPrimitive && o instanceof GraphicalPrimitive) { ((AddGraphicalPrimitive)top).addGraphical((GraphicalPrimitive) o); } else if (top instanceof GraphicalOperation) { ((GraphicalOperation)top).addObject(o); } else { System.err.println("failed to parse object-ref " + id); } if (atts.getIndex("node") != -1) { top.setObjectData(o, atts.getValue("node").trim()); } } else { // TODO error reporting System.err.println("failed to parse object-ref " + id); } } else { System.out.println("reisil men!"); // TODO error handling } } public void startElementProperty(String namespaceURI, String localName, String qName, Attributes atts) { Object o = peek(); if (o == null) ParserModule.getCurrentObject(); if (o != null && o instanceof ChangePropertyOperation) { ChangePropertyOperation cpo = (ChangePropertyOperation) o; cpo.setPropertyType(atts.getValue("type")); cpo.setPropertyValue(atts.getValue("value")); } else { // TODO error handling } } public void startElementKeypoints(String namespaceURI, String localName, String qName, Attributes atts) { Object o = peek(); if (o == null) ParserModule.getCurrentObject(); if (o != null && o instanceof MoveOperation) push(((MoveOperation) o).getKeypoints()); else push(null); } public void endElementKeypoints(String namespaceURI, String localName, String qName) { pop(); } public void startElementAlongObject(String namespaceURI, String localName, String qName, Attributes atts) { Object o = peek(); if (o == null) ParserModule.getCurrentObject(); if (o != null && o instanceof MoveOperation) { String id = atts.getValue("id"); GraphicalPrimitive gp = (GraphicalPrimitive) ((Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT)).getObject(id); if (gp != null) ((MoveOperation) o).setAlongObject(gp); } else { // TODO error handling/logging } } /** * Sets the attributes of the given AnimationOperation if they exists in the * given set of attributes. * * @param oper * The AnimationOperation to add the attributes. * @param atts * The set of attributes. */ private void handleAnimationAttributes(AnimationOperation oper, Attributes atts) { if (atts.getIndex("id") != -1) oper.setId(atts.getValue("id")); } private AddAnimationOperation getAAOObject() { Object o = peek(); if (o == null) o = ParserModule.getCurrentObject(); if (o == null) o = getProperty(XaalMainParserModule.ADD_ANIMATIONOPERATION_OBJECT); if (o == null || !(o instanceof AddAnimationOperation)) return null; else return (AddAnimationOperation) o; } private AnimationOperation getGOObject() { Object o = peek(); if (o == null) o = ParserModule.getCurrentObject(); if (o == null || !(o instanceof AnimationOperation)) return null; else return (AnimationOperation)o; } }