out-xpath: support namespace prefixes defined in the input document
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 17 Sep 2014 21:31:18 +0200 (2014-09-17)
changeset 101fd83cd102325
parent 100 b50e77d23d13
child 102 a7f7b9094cc3
out-xpath: support namespace prefixes defined in the input document
java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/DocumentNamespaceContext.java
java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/OneItemIterator.java
java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/DocumentNamespaceContext.java	Wed Sep 17 21:31:18 2014 +0200
     1.3 @@ -0,0 +1,59 @@
     1.4 +/**
     1.5 + * Alt2XML
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.alt2xml.out.xpath;
    1.22 +
    1.23 +import java.util.Iterator;
    1.24 +import javax.xml.XMLConstants;
    1.25 +import javax.xml.namespace.NamespaceContext;
    1.26 +import org.w3c.dom.Document;
    1.27 +import org.w3c.dom.Node;
    1.28 +
    1.29 +/**
    1.30 + *
    1.31 + * @author Ing. František Kučera (frantovo.cz)
    1.32 + */
    1.33 +public class DocumentNamespaceContext implements NamespaceContext {
    1.34 +
    1.35 +	private final Node document;
    1.36 +
    1.37 +	public DocumentNamespaceContext(Node document) {
    1.38 +		this.document = document;
    1.39 +	}
    1.40 +
    1.41 +	@Override
    1.42 +	public String getNamespaceURI(String prefix) {
    1.43 +		if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
    1.44 +			return document.lookupNamespaceURI(null);
    1.45 +		} else {
    1.46 +			return document.lookupNamespaceURI(prefix);
    1.47 +		}
    1.48 +	}
    1.49 +
    1.50 +	@Override
    1.51 +	public String getPrefix(String xmlns) {
    1.52 +		return document.lookupPrefix(xmlns);
    1.53 +	}
    1.54 +
    1.55 +	/**
    1.56 +	 * TODO: support multiple prefixes
    1.57 +	 */
    1.58 +	@Override
    1.59 +	public Iterator getPrefixes(String xmlns) {
    1.60 +		return new OneItemIterator(getPrefix(xmlns));
    1.61 +	}
    1.62 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/OneItemIterator.java	Wed Sep 17 21:31:18 2014 +0200
     2.3 @@ -0,0 +1,49 @@
     2.4 +/**
     2.5 + * Alt2XML
     2.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package cz.frantovo.alt2xml.out.xpath;
    2.22 +
    2.23 +import java.util.Iterator;
    2.24 +
    2.25 +/**
    2.26 + *
    2.27 + * @author Ing. František Kučera (frantovo.cz)
    2.28 + */
    2.29 +public class OneItemIterator<T> implements Iterator<T> {
    2.30 +
    2.31 +	private final T item;
    2.32 +	private boolean unused = true;
    2.33 +
    2.34 +	public OneItemIterator(T item) {
    2.35 +		this.item = item;
    2.36 +	}
    2.37 +
    2.38 +	@Override
    2.39 +	public boolean hasNext() {
    2.40 +		return unused;
    2.41 +	}
    2.42 +
    2.43 +	@Override
    2.44 +	public T next() {
    2.45 +		return item;
    2.46 +	}
    2.47 +
    2.48 +	@Override
    2.49 +	public void remove() {
    2.50 +		throw new UnsupportedOperationException("remove not supported");
    2.51 +	}
    2.52 +}
     3.1 --- a/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Wed Sep 17 21:06:56 2014 +0200
     3.2 +++ b/java/alt2xml-out-xpath/src/cz/frantovo/alt2xml/out/xpath/XPathAction.java	Wed Sep 17 21:31:18 2014 +0200
     3.3 @@ -31,6 +31,7 @@
     3.4  import javax.xml.xpath.XPathExpressionException;
     3.5  import javax.xml.xpath.XPathFactory;
     3.6  import javax.xml.xpath.XPathVariableResolver;
     3.7 +import org.w3c.dom.Node;
     3.8  
     3.9  /**
    3.10   *
    3.11 @@ -47,7 +48,6 @@
    3.12  	private final XPathFactory xpathFactory;
    3.13  	private final XPath xpath;
    3.14  	private final String expressionString;
    3.15 -	private final XPathExpression xpathExpression;
    3.16  
    3.17  	public XPathAction(ActionContext actionContext) throws OutputActionException {
    3.18  		super(actionContext);
    3.19 @@ -79,12 +79,6 @@
    3.20  			xpath.setXPathVariableResolver(variableResolver);
    3.21  
    3.22  			expressionString = actionData.get(0);
    3.23 -			try {
    3.24 -				xpathExpression = xpath.compile(expressionString);
    3.25 -			} catch (XPathExpressionException e) {
    3.26 -				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
    3.27 -			}
    3.28 -
    3.29  		}
    3.30  	}
    3.31  
    3.32 @@ -147,8 +141,19 @@
    3.33  
    3.34  	@Override
    3.35  	public void run(DOMResult domResult) throws OutputActionException {
    3.36 +		XPathExpression xpathExpression = null;
    3.37  		try {
    3.38 -			String result = xpathExpression.evaluate(domResult.getNode());
    3.39 +			Node document = domResult.getNode();
    3.40 +
    3.41 +			xpath.setNamespaceContext(new DocumentNamespaceContext(document));
    3.42 +
    3.43 +			try {
    3.44 +				xpathExpression = xpath.compile(expressionString);
    3.45 +			} catch (XPathExpressionException e) {
    3.46 +				throw new OutputActionException("Unable to compile XPath: " + expressionString, e);
    3.47 +			}
    3.48 +
    3.49 +			String result = xpathExpression.evaluate(document);
    3.50  			try (PrintWriter out = new PrintWriter(getActionContext().getOutputStream())) {
    3.51  				out.print(result);
    3.52  				if (lineBreak) {