From 597b43bb6fc1d4b5ad9ee13f247a5487fd5d74f8 Mon Sep 17 00:00:00 2001 From: tilman Date: Sun, 14 May 2017 15:10:03 +0200 Subject: [PATCH] almost there... --- .../ajax_getCategoryFolderSubnavi.php | 105 ++++++++++ .../examples/ajax_loadCategoryTree.php | 163 +++++++++++++++ AH-TreeReader/examples/main.xml | 58 ++++++ .../src/de/kompf/javaxml/XPathReader.java | 193 ++++++++++++++++++ .../de/tilman/AHTreeReader/AHTreeReader.java | 144 ++++++++++++- .../src/de/tilman/AHTreeReader/PlainApp.java | 6 +- .../src/de/tilman/AHTreeReader/Tree.java | 16 +- 7 files changed, 672 insertions(+), 13 deletions(-) create mode 100644 AH-TreeReader/examples/ajax_getCategoryFolderSubnavi.php create mode 100644 AH-TreeReader/examples/ajax_loadCategoryTree.php create mode 100644 AH-TreeReader/examples/main.xml create mode 100644 AH-TreeReader/src/de/kompf/javaxml/XPathReader.java diff --git a/AH-TreeReader/examples/ajax_getCategoryFolderSubnavi.php b/AH-TreeReader/examples/ajax_getCategoryFolderSubnavi.php new file mode 100644 index 0000000..1a964b9 --- /dev/null +++ b/AH-TreeReader/examples/ajax_getCategoryFolderSubnavi.php @@ -0,0 +1,105 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/AH-TreeReader/examples/ajax_loadCategoryTree.php b/AH-TreeReader/examples/ajax_loadCategoryTree.php new file mode 100644 index 0000000..d18f53d --- /dev/null +++ b/AH-TreeReader/examples/ajax_loadCategoryTree.php @@ -0,0 +1,163 @@ + + + + + + + + + + + + + + diff --git a/AH-TreeReader/examples/main.xml b/AH-TreeReader/examples/main.xml new file mode 100644 index 0000000..d8d59c5 --- /dev/null +++ b/AH-TreeReader/examples/main.xml @@ -0,0 +1,58 @@ + diff --git a/AH-TreeReader/src/de/kompf/javaxml/XPathReader.java b/AH-TreeReader/src/de/kompf/javaxml/XPathReader.java new file mode 100644 index 0000000..9f00389 --- /dev/null +++ b/AH-TreeReader/src/de/kompf/javaxml/XPathReader.java @@ -0,0 +1,193 @@ +package de.kompf.javaxml; + +import java.io.*; +import java.net.URL; +import java.util.*; + +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.parsers.*; +import javax.xml.xpath.*; + +import org.w3c.dom.*; +import org.xml.sax.SAXException; + +/** + * Sample code how to use to XPath API to extract information from XML data. + * + * @author Kompf + * + */ +public class XPathReader { + + /** + * Evaluate an XML input stream using the given xpath. + * + * @param in The XML input stream. + * @param xpathExpr The xpath expression - must not contain any namespace + * prefixes. + * @param result A collection to append the results to. + */ + void eval(InputStream in, String xpathExpr, Collection result) + throws ParserConfigurationException, SAXException, IOException, + XPathExpressionException { + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + docFactory.setNamespaceAware(false); // important! + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(in); + + XPath xpath = XPathFactory.newInstance().newXPath(); + XPathExpression expr = xpath.compile(xpathExpr); + + NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); + for (int i = 0; i < nodeList.getLength(); ++i) { + Node node = nodeList.item(i); + result.add(node.getNodeValue()); + } + } + + /** + * Evaluate an XML input stream using the given xpath. This implementation is + * namespace aware. + * + * @param in The XML input stream. + * @param xpathExpr The xpath expression - may contain namespace prefixes. + * @param nsCtx The namespace context to resolve the namespace prefixes from + * the xpath expression. + * @param result A collection to append the results to. + */ + void evalNamespaceAware(InputStream in, String xpathExpr, + NamespaceContext nsCtx, Collection result) + throws ParserConfigurationException, SAXException, IOException, + XPathExpressionException { + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + docFactory.setNamespaceAware(true); // important! + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(in); + + XPath xpath = XPathFactory.newInstance().newXPath(); + xpath.setNamespaceContext(nsCtx); + XPathExpression expr = xpath.compile(xpathExpr); + + NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); + for (int i = 0; i < nodeList.getLength(); ++i) { + Node node = nodeList.item(i); + result.add(node.getNodeValue()); + } + } + + /** + * Print the result. + * + * @param result The collection of results. + * @param out The print stream to use. + */ + void printResult(Collection result, PrintStream out) { + // print result + for (String name : result) { + out.println(name); + } + } + + /** + * Read the titles of entries from the Heise news feed. + */ + void readHeiseFeed() throws Exception { + URL heiseFeed = new URL("http://www.heise.de/newsticker/heise-atom.xml"); + InputStream in = heiseFeed.openStream(); + Collection result = new LinkedList(); + eval(in, "//entry/title/text()", result); + printResult(result, System.out); + } + + /** + * Read the titles of entries from the Heise news feed. This is basically the + * same like {@link #readHeiseFeed()} but is aware of the namespace of the + * atom feed. + */ + void readHeiseFeedNamespaceAware() throws Exception { + URL heiseFeed = new URL("http://www.heise.de/newsticker/heise-atom.xml"); + InputStream in = heiseFeed.openStream(); + Collection result = new LinkedList(); + evalNamespaceAware(in, "//ns:entry/ns:title/text()", + new SimpleNamespaceContext("ns", "http://www.w3.org/2005/Atom"), result); + printResult(result, System.out); + } + + /** + * Read the titles of entries from the Twitter public time line. This is + * basically the same like {@link #readHeiseFeed()} but uses another URL. + */ + void readTwitterPublicTimeLine() throws Exception { + URL twitterFeed = new URL( + "http://api.twitter.com/1/statuses/public_timeline.atom"); + InputStream in = twitterFeed.openStream(); + Collection result = new LinkedList(); + eval(in, "//entry/title/text()", result); + printResult(result, System.out); + } + + /** + * Read all all names of 'way' elements from an OSM XML file. The names are + * kept sorted and unique using a TreeSet. + */ + void readOsmWayNames() throws Exception { + // OSM path names + URL osmUrl = new File("md.osm.xml").toURI().toURL(); + InputStream in = osmUrl.openStream(); + Collection result = new TreeSet(); + eval(in, "/osm/way/tag[@k='name']/@v", result); + printResult(result, System.out); + } + + /** + * MAIN. + * + * @param args ignored. + * @throws Exception If an error occurs. + */ + public static void main(String[] args) throws Exception { + XPathReader xPathReader = new XPathReader(); + // Heise News + xPathReader.readHeiseFeed(); + // Heise News namespace aware + //xPathReader.readHeiseFeedNamespaceAware(); + // Twitter public time line + //xPathReader.readTwitterPublicTimeLine(); + // OSM path names + //xPathReader.readOsmWayNames(); + } + + static class SimpleNamespaceContext implements NamespaceContext { + private String prefix; + private String uri; + + public SimpleNamespaceContext(String prefix, String uri) { + this.prefix = prefix; + this.uri = uri; + } + + public String getNamespaceURI(String prefix) { + if (this.prefix.equals(prefix)) { + return uri; + } + return XMLConstants.NULL_NS_URI; + } + + public String getPrefix(String namespaceURI) { + if (uri.equals(namespaceURI)) { + return prefix; + } + return null; + } + + @SuppressWarnings("unchecked") + public Iterator getPrefixes(String namespaceURI) { + List prefixList = new ArrayList(); + if (uri.equals(namespaceURI)) { + prefixList.add(prefix); + } + return prefixList.iterator(); + } + } +} \ No newline at end of file diff --git a/AH-TreeReader/src/de/tilman/AHTreeReader/AHTreeReader.java b/AH-TreeReader/src/de/tilman/AHTreeReader/AHTreeReader.java index f17cbad..143c6dc 100644 --- a/AH-TreeReader/src/de/tilman/AHTreeReader/AHTreeReader.java +++ b/AH-TreeReader/src/de/tilman/AHTreeReader/AHTreeReader.java @@ -1,10 +1,150 @@ package de.tilman.AHTreeReader; +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathFactory; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +import de.tilman.AHTreeReader.Tree.TreeNode; + public class AHTreeReader { + + String cookie = "PHPSESSID=5nccupenl4b7tps9048bge76h3"; + Tree tree = new Tree(); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpHost target = new HttpHost("media-assistant.animalhealth.bayer.com", 443, "https"); + HttpPost httpPost; + HttpEntity httpEntity; + + DocumentBuilderFactory factory; + DocumentBuilder builder; + XPath xpath = XPathFactory.newInstance().newXPath(); + + public AHTreeReader() { + System.out.println("Aloha, funky time"); + + // proxy configuration + /* + HttpHost proxy = new HttpHost("10.185.190.100", 8080, "http"); + RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); + httpPost.setConfig(config); + */ + + try { + factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(false); + builder = factory.newDocumentBuilder(); + + getMainCategories(); + getSubcategories(tree.root); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void getSubcategories(TreeNode node) throws Exception { + for (TreeNode n : node.children) { + + System.out.println("\n----------------"); + System.out.println("Looking for " + n.key + "\n"); + + httpPost = new HttpPost("https://media-assistant.animalhealth.bayer.com/assets/php/ajax_getCategoryFolderSubnavi.php"); + httpPost.setHeader("Cookie", cookie); + + // parent node is transferred as form data o_0 + httpEntity = MultipartEntityBuilder.create().addTextBody("parent", n.key).build(); + httpPost.setEntity(httpEntity); + + CloseableHttpResponse response = httpClient.execute(target, httpPost); + + if (response.getStatusLine().getStatusCode() != 200) { + System.err.println("Error requesting children of " + n.key + ", " + n.value); + break; + } + + String content = EntityUtils.toString(response.getEntity()); + response.close(); + + // dirty optimization: cut out