// 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.net.MalformedURLException; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import xaal.objects.Xaal; import xaal.objects.metadata.Application; import xaal.objects.metadata.Author; import xaal.objects.metadata.Metadata; import xaal.parser.ParserModule; public class XaalMetadataParserModule extends ParserModule { public XaalMetadataParserModule() { 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 startElementMetadata(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { Metadata md = new Metadata(); ((Xaal) getProperty("xaal-root")).setMetadata(md); push(md); } public void endElementMetadata(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementAnimationInfo(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { } public void startElementTitle(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Metadata) push(new StringBuffer()); } public void endElementTitle(String namespaceURI, String localName, String qName) { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); ((Metadata) peek()).setTitle(sb.toString().trim()); } else { // TODO error handling of not well-formed title element } } public void startElementDesc(String namespaceURI, String localName, String qName, Attributes atts) { if (peek() != null && peek() instanceof Metadata) push(new StringBuffer()); } public void endElementDesc(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); ((Metadata) peek()).setDesc(sb.toString().trim()); } else { // TODO error handling of not well-formed title element } } public void startElementSubject(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Metadata) push(new StringBuffer()); } public void endElementSubject(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); ((Metadata) peek()).setSubject(sb.toString().trim()); } else { // TODO error handling of not well-formed title element } } public void startElementKeyword(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Metadata) push(new StringBuffer()); } public void endElementKeyword(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); ((Metadata) peek()).addKeyword(sb.toString().trim()); } else { // TODO error handling of not well-formed title element } } public void startElementAuthor(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Metadata) { Author a = new Author(); ((Metadata) peek()).addAuthor(a); push(a); } else { // TODO error handling of not well-formed title element push(null); } } public void endElementAuthor(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementFirstname(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Author) push(new StringBuffer()); } public void endElementFirstname(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); if (peek() instanceof Author) ((Author) peek()).setFirstName(sb.toString().trim()); } } public void startElementLastname(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Author) push(new StringBuffer()); } public void endElementLastname(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); if (peek() instanceof Author) ((Author) peek()).setLastName(sb.toString().trim()); } } public void startElementAffiliation(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Author) push(new StringBuffer()); } public void endElementAffiliation(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); if (peek() instanceof Author) ((Author) peek()).setAffiliation(sb.toString().trim()); } } public void startElementEmail(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Author) push(new StringBuffer()); } public void endElementEmail(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); if (peek() instanceof Author) ((Author) peek()).setEmail(sb.toString().trim()); } } public void startElementApplication(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Metadata) { Application a = new Application(); ((Metadata) peek()).addApplication(a); push(a); } else { // TODO error handling of not well-formed title element push(null); } } public void endElementApplication(String namespaceURI, String localName, String qName) throws SAXException { pop(); } public void startElementName(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Application) push(new StringBuffer()); } public void endElementName(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); ((Application) peek()).setName(sb.toString().trim()); } } public void startElementVersion(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Application) push(new StringBuffer()); } public void endElementVersion(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); ((Application) peek()).setVersion(sb.toString().trim()); } } public void startElementHomepage(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (peek() != null && peek() instanceof Application) push(new StringBuffer()); } public void endElementHomepage(String namespaceURI, String localName, String qName) throws SAXException { if (peek() != null && peek() instanceof StringBuffer) { StringBuffer sb = (StringBuffer) pop(); try { ((Application) peek()).setHomepage(sb.toString().trim()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }