view src/de/mpiwg/anteater/xml/impl/JDOMParser.java @ 0:036535fcd179

anteater
author jdamerow
date Fri, 14 Sep 2012 10:30:43 +0200
parents
children
line wrap: on
line source

package de.mpiwg.anteater.xml.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.Text;
import org.jdom2.filter.ElementFilter;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

import de.mpiwg.anteater.xml.IXMLParser;

public class JDOMParser implements IXMLParser {

	private Document doc;

	public JDOMParser(String pathOrContent, boolean loadFromFile) {
		if (loadFromFile) {
			init(pathOrContent);
		} else {
			try {
				SAXBuilder builder = new SAXBuilder();
				doc = builder.build(new StringReader(pathOrContent));
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public JDOMParser(InputStream stream) {
		init(stream);
	}

	public JDOMParser() {
		doc = new Document();
	}

	protected void init(String filepath) {
		SAXBuilder builder = new SAXBuilder();
		File file = new File(filepath);
		if (file.exists()) {
			try {
				doc = builder.build(file);
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	protected void init(InputStream stream) {
		SAXBuilder builder = new SAXBuilder();
		try {
			doc = builder.build(stream);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public Element getRoot() {
		return doc.getRootElement();
	}
	
	@Override
	public List<Element> executeXPath(String xpath, String defaultNSPrefix) {
		if (doc == null)
			return null;

		XPathFactory factory = XPathFactory.instance();
		// get namespaces but remove empty prefix to prevent xpath exception
		List<Namespace> namespaces = doc.getRootElement()
				.getNamespacesInScope();
		List<Namespace> finalNamespaces = new ArrayList<Namespace>();
		for (Namespace ns : namespaces) {
			if (!ns.getPrefix().isEmpty())
				finalNamespaces.add(ns);
			else if (!ns.getURI().trim().isEmpty() && defaultNSPrefix != null && !defaultNSPrefix.trim().isEmpty()) {
				finalNamespaces.add(Namespace.getNamespace(defaultNSPrefix, ns.getURI()));
			}
		}
		List<Element> results;
		// evaluate xpath
		XPathExpression<Element> expression = factory.compile(xpath,
				new ElementFilter(), null, finalNamespaces);
		results = expression.evaluate(doc.getRootElement());

		return results;
	}

	protected String stripText(Element node) {
		StringBuffer text = new StringBuffer();
		stripTextRecursivie(node, text);
		return text.toString().trim();
	}
	
	protected void stripTextRecursivie(Element node, StringBuffer text) {
		for (Content child : node.getContent()) {
			if (child instanceof Text) {
				text.append(((Text) child).getText() != null ? ((Text) child)
						.getTextNormalize() + " " : "");
			} else if (child instanceof Element) {
				stripTextRecursivie((Element) child, text);
			}
		}
	}

	public boolean save(String filepath, boolean prettyprint) {
		XMLOutputter out = null;
		if (prettyprint)
			out = new XMLOutputter(Format.getPrettyFormat());
		else
			out = new XMLOutputter();
		//FileWriter writer;
		FileOutputStream stream;
		try {
			stream = new FileOutputStream(new File(filepath));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		}
		OutputStreamWriter osWriter = new OutputStreamWriter(stream, Charset.forName("UTF-8"));
		try {
			//writer = new FileWriter(filepath);
			out.output(doc, osWriter);
			osWriter.flush();
			osWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}