Java tutorial
/* * Copyright 2009 Martin Palma * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.unibz.inf.xmlssd.metadator.xml; import gnu.xml.XMLPrinter; import gnu.xquery.lang.XQuery; import it.unibz.inf.xmlssd.metadator.helpers.Exporter; import it.unibz.inf.xmlssd.metadator.helpers.UIHelper; import it.unibz.inf.xmlssd.metadator.sqlite.SQLiteManager; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; /** * Class for processing XML. * @author Martin Palma <martin@palma.bz> * */ public class XMLProcessor { private SQLiteManager backend; /** * Constructs the object. */ public XMLProcessor() { backend = new SQLiteManager(); } /** * Method which queries the data and returns the corresponding result. * @param query * @return * @throws Throwable */ public String get(String query) throws Throwable { // Creating a temporal snapshot of the stored data. File tmpFile = File.createTempFile("metadator", ".xml"); // Modifing the query and adding the doc('file.xml') query = query.replaceAll("%%doc%%", "doc('" + tmpFile.getAbsolutePath() + "')"); // Write XML for processing the XQuery/XPath Exporter exporter = new Exporter(tmpFile); exporter.export(); StringWriter aStringWriter = new StringWriter(); XMLPrinter aXMLWriter = new XMLPrinter(aStringWriter); XQuery aXQuery = new XQuery(); aXQuery.eval(query, (Writer) aXMLWriter); aXMLWriter.flush(); aStringWriter.flush(); String result = aStringWriter.getBuffer().toString(); // Remove tmp file tmpFile.delete(); return (result.equals("")) ? "No result!" : result; } /** * Method which returns only a simple list of photos for displaying it in the * deletion dialog. * @return * @throws IOException */ public String getListOfPhotos() throws IOException { Document document = DocumentHelper.createDocument(); Element root = document.addElement("metadator"); List list = backend.get().selectNodes("//metadator/photo"); for (Iterator i = list.iterator(); i.hasNext();) { Element element = (Element) i.next(); Node author = element.selectSingleNode("//author"); Node title = element.selectSingleNode("//title"); root.addElement("photo").addAttribute("id", element.attributeValue("id")) .addAttribute("author", author.getStringValue()) .addAttribute("filename", element.attributeValue("filename")) .addAttribute("title", title.getStringValue()); } return UIHelper.prettyPrintString(document.asXML()); } }