// 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 org.xml.sax.SAXException; import xaal.objects.AddDataStructure; import xaal.objects.AddStructure; import xaal.objects.Xaal; import xaal.objects.structures.Array; import xaal.objects.structures.DataStructure; import xaal.objects.structures.Edge; import xaal.objects.structures.Index; import xaal.objects.structures.Key; import xaal.objects.structures.Marker; import xaal.objects.structures.Node; import xaal.objects.structures.Structure; import xaal.objects.structures.Tree; import xaal.parser.ParserModule; public class XaalDSParserModule extends ParserModule { public XaalDSParserModule() { super(); } public void startElementArray(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { AddDataStructure ads = getADSObject(); if (ads == null) { //TODO error handling //warning("Element array in illegal position."); return; } Array str = new Array(getXaalObject()); ads.addStructure(str); push(str); //str.setType(DataStructure.ARRAY_TYPE); if (atts.getIndex("size") != -1) { str.setSize(Integer.parseInt(atts.getValue("size"))); } if (atts.getIndex("indexed") != -1) { str.setIndexed(Boolean.parseBoolean(atts.getValue("indexed"))); } if (atts.getIndex("orientation") != -1) { str.setOrientation(atts.getValue("orientation")); } handleStructureAttributes(str, atts); } public void endElementArray(String namespaceURI, String localName, String qName) throws SAXException { getADSObject(); pop(); getADSObject(); } public void startElementGraph(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { AddDataStructure init = getADSObject(); if (init == null) { //TODO error handling //warning("Element array in illegal position."); return; } DataStructure str = new DataStructure(getXaalObject()); init.addStructure(str); push(str); str.setType(DataStructure.GRAPH_TYPE); handleStructureAttributes(str, atts); } public void endElementGraph(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementKey(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { AddStructure s = getASObject(); if (s != null) { Key k = null; if (s instanceof Node) { k = new Key(getXaalObject(), (Node)s); } else { k = new Key(getXaalObject()); } handleStructureAttributes(k, atts); s.addStructure(k); push(k); if (atts.getIndex("value") != -1) k.setValue(atts.getValue("value")); else push(new StringBuffer()); } else { //warning("Element key in illegal position:"); // TODO Error handling push(null); return; } } public void endElementKey(String namespaceURI, String localName, String qName) throws SAXException { if (peek() instanceof StringBuffer) { String str = ((StringBuffer) pop()).toString().trim(); ((Key) pop()).setValue(str); } else pop(); } public void startElementMarker(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { Marker m = new Marker(getXaalObject()); handleStructureAttributes(m, atts); AddDataStructure s = getADSObject(); if (s != null) { s.addStructure(m); push(m); if (atts.getIndex("label") != -1) m.setLabel(atts.getValue("label")); if (atts.getIndex("target") != -1) { m.setTarget(((Xaal) getProperty("xaal-root")).getStructure(atts.getValue("target"))); } } else { //warning("Element key in illegal position:"); // TODO Error handling push(null); return; } } public void endElementMarker(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementIndex(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() instanceof DataStructure) { Index ind = new Index(getXaalObject(), (DataStructure) peek()); handleStructureAttributes(ind, atts); if (atts.getIndex("label") != -1) { ind.setLabel(atts.getValue("label")); } if (atts.getIndex("index") != -1) { ind.setIndex(Integer.parseInt(atts.getValue("index"))); } ((DataStructure) peek()).addNode(ind); push(getProperty(XaalMainParserModule.ADD_DATASTRUCTURE_OBJECT)); setProperty(XaalMainParserModule.ADD_DATASTRUCTURE_OBJECT, ind); push(ind); } else //TODO error handling //warning("Element " + qName + " in illegal position:"); return; } public void endElementIndex(String namespaceURI, String localName, String qName) throws SAXException { pop(); setProperty(XaalMainParserModule.ADD_DATASTRUCTURE_OBJECT, pop()); } public void startElementList(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { AddDataStructure init = getADSObject(); if (init == null) { //TODO error handling //warning("Element array in illegal position."); return; } DataStructure str = new DataStructure(getXaalObject()); init.addStructure(str); push(str); str.setType(DataStructure.LIST_TYPE); handleStructureAttributes(str, atts); } public void endElementList(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementNode(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() instanceof DataStructure) { Node n = new Node(getXaalObject(), (DataStructure) peek()); ((DataStructure) peek()).addNode(n); push(getProperty(XaalMainParserModule.ADD_DATASTRUCTURE_OBJECT)); push(n); setProperty(XaalMainParserModule.ADD_DATASTRUCTURE_OBJECT, n); handleStructureAttributes(n, atts); if (atts.getIndex("label") != -1) { n.setLabel(atts.getValue("label")); } } else { //TODO error handling //warning("Element " + qName + " in illegal position:"); push(null); push(null); return; } } public void endElementNode(String namespaceURI, String localName, String qName) throws SAXException { pop(); setProperty(XaalMainParserModule.ADD_DATASTRUCTURE_OBJECT, pop()); } public void startElementEdge(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() instanceof DataStructure) { Edge ref = new Edge(getXaalObject()); ((DataStructure)peek()).addReference(ref); push(ref); handleStructureAttributes(ref, atts); ref.setFrom(atts.getValue("from")); ref.setTo(atts.getValue("to")); ref.setDirected(Boolean.parseBoolean(atts.getValue("directed"))); if (atts.getIndex("label") != -1) { ref.setLabel(atts.getValue("label")); } } else //TODO error handling //warning("Element " + qName + " in illegal position:"); return; } public void endElementEdge(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementTree(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { AddDataStructure init = getADSObject(); if (init == null) { //TODO error handling //warning("Element array in illegal position."); return; } Tree tree = new Tree(getXaalObject()); init.addStructure(tree); push(tree); if (atts.getIndex("ref") != -1) { tree.setRef(atts.getValue("ref")); // discard = "tree"; return; } handleStructureAttributes(tree, atts); if (atts.getIndex("root") == -1) { //warning("No root found for the tree!"); } else { tree.setRoot(atts.getValue("root")); } } public void endElementTree(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementStructureProperty(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { Object peek = peek(); if (!(peek instanceof Structure)) { //warning("Element property in illegal position:"); // TODO somehow set subelements to be ignored?? return; } String name = atts.getValue("name"); if (atts.getIndex("value") != -1) { String value = atts.getValue("value"); if (name.equals("class")) ((DataStructure) peek).setStrClass(value); else ((Structure) peek).addProperty(name, value); } else { push(name); push(new StringBuffer()); } } public void endElementStructureProperty(String namespaceURI, String localName, String qName) throws SAXException { if (peek() instanceof StringBuffer) { String value = pop().toString(); String name = pop().toString(); Object peek = peek(); if (name.equals("class")) ((DataStructure) peek).setStrClass(value); else ((Structure) peek).addProperty(name, value); } } 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]); } } private void handleStructureAttributes(Structure str, Attributes atts) { if (atts.getIndex("id") != -1) { str.setId(atts.getValue("id")); ((Xaal) getProperty("xaal-root")).addStructure(atts.getValue("id"), str); } if (atts.getIndex("style") != -1) { str.setStyle(((Xaal) getProperty("xaal-root")).getStyle(atts.getValue("style"))); } // TODO Handle ref attribute } private AddDataStructure getADSObject() { Object o = ParserModule.getCurrentObject(); if (o == null) o = getProperty(XaalMainParserModule.ADD_DATASTRUCTURE_OBJECT); if (o == null || !(o instanceof AddDataStructure)) return null; else return (AddDataStructure) o; } private AddStructure getASObject() { Object o = getADSObject(); if (o instanceof AddStructure) return (AddStructure) o; return null; } }