// Path: source/java/src/org/lucee/extension/pdf/util/XMLUtil.java
// public class XMLUtil {
// 
// 	public static final short UNDEFINED_NODE = -1;
// 	private static TransformerFactory transformerFactory;
// 
// 	public static Element getRootElement(Node node) {
// 		Document doc = null;
// 		if (node instanceof Document) doc = (Document) node;
// 		else doc = node.getOwnerDocument();
// 		return doc.getDocumentElement();
// 	}
// 
// 	public static synchronized Element getChildWithName(String name, Element el) {
// 		Element[] children = getChildElementsAsArray(el);
// 		for (int i = 0; i < children.length; i++) {
// 			if (name.equalsIgnoreCase(children[i].getNodeName())) return children[i];
// 		}
// 		return null;
// 	}
// 
// 	public static Element[] getChildElementsAsArray(Node node) {
// 		ArrayList<Node> nodeList = getChildNodes(node, Node.ELEMENT_NODE, null);
// 		return nodeList.toArray(new Element[nodeList.size()]);
// 	}
// 
// 	public static synchronized ArrayList<Node> getChildNodes(Node node, short type, String filter) {
// 		ArrayList<Node> rtn = new ArrayList<Node>();
// 		NodeList nodes = node.getChildNodes();
// 		int len = nodes.getLength();
// 		Node n;
// 		for (int i = 0; i < len; i++) {
// 			try {
// 				n = nodes.item(i);
// 				if (n != null && (type == UNDEFINED_NODE || n.getNodeType() == type)) {
// 					if (filter == null || filter.equals(n.getLocalName())) rtn.add(n);
// 				}
// 			}
// 			catch (Exception t) {
// 			}
// 		}
// 		return rtn;
// 	}
// 
// 	public static Document getDocument(Node node) {
// 		if (node instanceof Document) return (Document) node;
// 		return node.getOwnerDocument();
// 	}
// 
// 	public static final Document parseHTML(InputSource xml) throws SAXException, IOException {
// 		XMLReader reader = new Parser();
// 		reader.setFeature(Parser.namespacesFeature, true);
// 		reader.setFeature(Parser.namespacePrefixesFeature, true);
// 		try {
// 			Transformer transformer = getTransformerFactory().newTransformer();
// 
// 			DOMResult result = new DOMResult();
// 			transformer.transform(new SAXSource(reader, xml), result);
// 			return getDocument(result.getNode());
// 		}
// 		catch (Exception e) {
// 			throw new SAXException(e);
// 		}
// 	}
// 
// 	public static TransformerFactory getTransformerFactory() {
// 		if (transformerFactory == null) {
// 			try {
// 				Class<?> clazz = CFMLEngineFactory.getInstance().getClassUtil().loadClass("lucee.runtime.text.xml.XMLUtil");
// 				transformerFactory = (TransformerFactory) clazz.getMethod("getTransformerFactory", new Class[0]).invoke(null, new Object[0]);
// 			}
// 			catch (Exception e) {
// 				e.printStackTrace();
// 				transformerFactory = TransformerFactory.newInstance();
// 			}
// 		}
// 		return transformerFactory;
// 	}
// 
// 	// toString(node, omitXMLDecl, indent, publicId, systemId, encoding);
// 	public static String toString(Node node, boolean omitXMLDecl, boolean indent, String publicId, String systemId, String encoding) throws PageException {
// 		// FUTURE use interface from loader
// 		try {
// 			Class<?> clazz = CFMLEngineFactory.getInstance().getClassUtil().loadClass("lucee.runtime.text.xml.XMLCaster");
// 			Method method = clazz.getMethod("toString", new Class[] { Node.class, boolean.class, boolean.class, String.class, String.class, String.class });
// 			return (String) method.invoke(null, new Object[] { node, omitXMLDecl, indent, publicId, systemId, encoding });
// 		}
// 		catch (Exception e) {
// 			throw CFMLEngineFactory.getInstance().getCastUtil().toPageException(e);
// 		}
// 	}
// 
// 	public static InputSource toInputSource(String str) {
// 		return new InputSource(new StringReader(str));
// 	}
// }


import java.net.MalformedURLException;
import java.net.URL;
import org.lucee.extension.pdf.util.XMLUtil;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* Copyright (c) 2016, Lucee Assosication Switzerland
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either 
* version 2.1 of the License, or (at your option) any later version.
* 
* This library 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
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public 
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
* 
**/
package org.lucee.extension.pdf.pd4ml;



/**
 * Transform a HTML String, set all relative Pathes inside HTML File to absolute
 *
 */
public final class URLResolver {

	private Tag[] tags = new Tag[] { new Tag("a", "href"), new Tag("link", "href"), new Tag("form", "action"), new Tag("applet", "code"), new Tag("script", "src"),
			new Tag("body", "background"), new Tag("frame", "src"), new Tag("bgsound", "src"), new Tag("img", "src"),

			new Tag("embed", new String[] { "src", "pluginspace" }), new Tag("object", new String[] { "data", "classid", "codebase", "usemap" })

	};

	public void transform(Node node, URL url) throws MalformedURLException {
		Element el;
		if (node.getNodeType() == Node.DOCUMENT_NODE) {
			transform(XMLUtil.getRootElement(node), url);
		}
		else if (node.getNodeType() == Node.ELEMENT_NODE) {
// Path: source/java/src/org/lucee/extension/pdf/util/XMLUtil.java
// public class XMLUtil {
// 
// 	public static final short UNDEFINED_NODE = -1;
// 	private static TransformerFactory transformerFactory;
// 
// 	public static Element getRootElement(Node node) {
// 		Document doc = null;
// 		if (node instanceof Document) doc = (Document) node;
// 		else doc = node.getOwnerDocument();
// 		return doc.getDocumentElement();
// 	}
// 
// 	public static synchronized Element getChildWithName(String name, Element el) {
// 		Element[] children = getChildElementsAsArray(el);
// 		for (int i = 0; i < children.length; i++) {
// 			if (name.equalsIgnoreCase(children[i].getNodeName())) return children[i];
// 		}
// 		return null;
// 	}
// 
// 	public static Element[] getChildElementsAsArray(Node node) {
// 		ArrayList<Node> nodeList = getChildNodes(node, Node.ELEMENT_NODE, null);
// 		return nodeList.toArray(new Element[nodeList.size()]);
// 	}
// 
// 	public static synchronized ArrayList<Node> getChildNodes(Node node, short type, String filter) {
// 		ArrayList<Node> rtn = new ArrayList<Node>();
// 		NodeList nodes = node.getChildNodes();
// 		int len = nodes.getLength();
// 		Node n;
// 		for (int i = 0; i < len; i++) {
// 			try {
// 				n = nodes.item(i);
// 				if (n != null && (type == UNDEFINED_NODE || n.getNodeType() == type)) {
// 					if (filter == null || filter.equals(n.getLocalName())) rtn.add(n);
// 				}
// 			}
// 			catch (Exception t) {
// 			}
// 		}
// 		return rtn;
// 	}
// 
// 	public static Document getDocument(Node node) {
// 		if (node instanceof Document) return (Document) node;
// 		return node.getOwnerDocument();
// 	}
// 
// 	public static final Document parseHTML(InputSource xml) throws SAXException, IOException {
// 		XMLReader reader = new Parser();
// 		reader.setFeature(Parser.namespacesFeature, true);
// 		reader.setFeature(Parser.namespacePrefixesFeature, true);
// 		try {
// 			Transformer transformer = getTransformerFactory().newTransformer();
// 
// 			DOMResult result = new DOMResult();
// 			transformer.transform(new SAXSource(reader, xml), result);
// 			return getDocument(result.getNode());
// 		}
// 		catch (Exception e) {
// 			throw new SAXException(e);
// 		}
// 	}
// 
// 	public static TransformerFactory getTransformerFactory() {
// 		if (transformerFactory == null) {
// 			try {
// 				Class<?> clazz = CFMLEngineFactory.getInstance().getClassUtil().loadClass("lucee.runtime.text.xml.XMLUtil");
// 				transformerFactory = (TransformerFactory) clazz.getMethod("getTransformerFactory", new Class[0]).invoke(null, new Object[0]);
// 			}
// 			catch (Exception e) {
// 				e.printStackTrace();
// 				transformerFactory = TransformerFactory.newInstance();
// 			}
// 		}
// 		return transformerFactory;
// 	}
// 
// 	// toString(node, omitXMLDecl, indent, publicId, systemId, encoding);
// 	public static String toString(Node node, boolean omitXMLDecl, boolean indent, String publicId, String systemId, String encoding) throws PageException {
// 		// FUTURE use interface from loader
// 		try {
// 			Class<?> clazz = CFMLEngineFactory.getInstance().getClassUtil().loadClass("lucee.runtime.text.xml.XMLCaster");
// 			Method method = clazz.getMethod("toString", new Class[] { Node.class, boolean.class, boolean.class, String.class, String.class, String.class });
// 			return (String) method.invoke(null, new Object[] { node, omitXMLDecl, indent, publicId, systemId, encoding });
// 		}
// 		catch (Exception e) {
// 			throw CFMLEngineFactory.getInstance().getCastUtil().toPageException(e);
// 		}
// 	}
// 
// 	public static InputSource toInputSource(String str) {
// 		return new InputSource(new StringReader(str));
// 	}
// }


// Path: source/java/src/org/lucee/extension/pdf/pd4ml/URLResolver.java
import java.net.MalformedURLException;
import java.net.URL;
import org.lucee.extension.pdf.util.XMLUtil;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
*
* Copyright (c) 2016, Lucee Assosication Switzerland
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either 
* version 2.1 of the License, or (at your option) any later version.
* 
* This library 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
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public 
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
* 
**/
package org.lucee.extension.pdf.pd4ml;



/**
 * Transform a HTML String, set all relative Pathes inside HTML File to absolute
 *
 */
public final class URLResolver {

	private Tag[] tags = new Tag[] { new Tag("a", "href"), new Tag("link", "href"), new Tag("form", "action"), new Tag("applet", "code"), new Tag("script", "src"),
			new Tag("body", "background"), new Tag("frame", "src"), new Tag("bgsound", "src"), new Tag("img", "src"),

			new Tag("embed", new String[] { "src", "pluginspace" }), new Tag("object", new String[] { "data", "classid", "codebase", "usemap" })

	};

	public void transform(Node node, URL url) throws MalformedURLException {
		Element el;
		if (node.getNodeType() == Node.DOCUMENT_NODE) {
			transform(XMLUtil.getRootElement(node), url);
		}
		else if (node.getNodeType() == Node.ELEMENT_NODE) {
			el = (Element) node;