diff --git a/ws/eclipse/Sda1PreExam/.classpath b/ws/eclipse/Sda1PreExam/.classpath index fb565a588d8a709e8aeac7a50320c6f25af0122f..b3e7b1aad7c50277dd16d71108cd34fc85986219 100644 --- a/ws/eclipse/Sda1PreExam/.classpath +++ b/ws/eclipse/Sda1PreExam/.classpath @@ -1,6 +1,20 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="src" path="src"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> - <classpathentry kind="output" path="bin"/> + <classpathentry kind="src" output="target/classes" path="src"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="output" path="target/classes"/> </classpath> diff --git a/ws/eclipse/Sda1PreExam/.gitignore b/ws/eclipse/Sda1PreExam/.gitignore index 5e56e040ec0902e58df8573adaec65c5da6e9304..934e0e06ffa0a2aeedbe1341d321549336719cc0 100644 --- a/ws/eclipse/Sda1PreExam/.gitignore +++ b/ws/eclipse/Sda1PreExam/.gitignore @@ -1 +1,2 @@ /bin +/target diff --git a/ws/eclipse/Sda1PreExam/.project b/ws/eclipse/Sda1PreExam/.project index bd460ad411fc962cf3e0f608ab31e919297ce476..a4a688ab11aa816c3d100d3c487cddb406f00455 100644 --- a/ws/eclipse/Sda1PreExam/.project +++ b/ws/eclipse/Sda1PreExam/.project @@ -10,8 +10,14 @@ <arguments> </arguments> </buildCommand> + <buildCommand> + <name>org.eclipse.m2e.core.maven2Builder</name> + <arguments> + </arguments> + </buildCommand> </buildSpec> <natures> + <nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription> diff --git a/ws/eclipse/Sda1PreExam/pom.xml b/ws/eclipse/Sda1PreExam/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..9ae2f0dde17362e0d4ecc6f2d9308d43f8ad8464 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/pom.xml @@ -0,0 +1,54 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>Sda1PreExam</groupId> + <artifactId>Sda1PreExam</artifactId> + <version>0.0.1-SNAPSHOT</version> + <build> + <sourceDirectory>src</sourceDirectory> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <source>1.7</source> + <target>1.7</target> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>5.1.23</version> + </dependency> + + <dependency> + <groupId>org.jdom</groupId> + <artifactId>jdom2</artifactId> + <version>2.0.5</version> + </dependency> + <dependency> + <groupId>jaxen</groupId> + <artifactId>jaxen</artifactId> + <version>1.1.4</version> + </dependency> + + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>6.8</version> + </dependency> + <dependency> + <groupId>commons-codec</groupId> + <artifactId>commons-codec</artifactId> + <version>1.7</version> + </dependency> + <dependency> + <groupId>xml-resolver</groupId> + <artifactId>xml-resolver</artifactId> + <version>1.2</version> + </dependency> + </dependencies> + +</project> \ No newline at end of file diff --git a/ws/eclipse/Sda1PreExam/src/dom/table/Driver.java b/ws/eclipse/Sda1PreExam/src/dom/table/Driver.java new file mode 100644 index 0000000000000000000000000000000000000000..7273f9b4b380c761f5e2043ca2125f4aaa71c732 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table/Driver.java @@ -0,0 +1,20 @@ +package dom.table; + +/** + * Driver to test XPath based searching of <img> elements + * within a given HTML document. + * +*/ +public class Driver { + + /** + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception { + final TablePresentation ao = new TablePresentation(); + // The following HTML document is being searched + // for <img> elements. + ao.process("src/dom/table/persons.xml"); + } +} \ No newline at end of file diff --git a/ws/eclipse/Sda1PreExam/src/dom/table/MySaxErrorHandler.java b/ws/eclipse/Sda1PreExam/src/dom/table/MySaxErrorHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..ff30d5df3f98a44fb85a69b811ddd363a53453a7 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table/MySaxErrorHandler.java @@ -0,0 +1,42 @@ +package dom.table; + +import java.io.PrintStream; + +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * + * @author goik + */ +public class MySaxErrorHandler implements ErrorHandler { + + private PrintStream out; //The error handler's output goes here + + private String getParseExceptionInfo //Returns a string describing + (SAXParseException ex) { //parse exception details. + return "Error '" + ex.getMessage() + "' at line " + + ex.getLineNumber() + ", column " + + ex.getColumnNumber(); + } + + /** + * @param out + */ + public MySaxErrorHandler(final PrintStream out) { + this.out = out; + } + @Override + public void warning(SAXParseException exception) throws SAXException { + out.print("Warning:" + getParseExceptionInfo(exception)); + } + @Override + public void error(SAXParseException exception) throws SAXException { + out.print("Error:" + getParseExceptionInfo(exception)); + } + @Override + public void fatalError(SAXParseException exception) throws SAXException { + out.print("Fatal error:" + getParseExceptionInfo(exception)); + } +} \ No newline at end of file diff --git a/ws/eclipse/Sda1PreExam/src/dom/table/ReorderTableData.java b/ws/eclipse/Sda1PreExam/src/dom/table/ReorderTableData.java new file mode 100644 index 0000000000000000000000000000000000000000..77c484569200b8ceeac816f7515274c6fedb0f26 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table/ReorderTableData.java @@ -0,0 +1,47 @@ +package dom.table; + +import java.io.IOException; +import java.util.List; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; + + +/** + * How to use XPath expressions to search for <img> + * elements in XHTML document instances. + * + * @author goik + */ +public class ReorderTableData { + private final SAXBuilder builder = new SAXBuilder(); + + /** + * + */ + public ReorderTableData() { + builder.setErrorHandler(new MySaxErrorHandler(System.err)); + } + /** + * Read an XHTML document and search for images. The image filenames will be + * written to standard out. + * @param xhtmlFilename The XHTML's filename to be examined. + * @throws JDOMException + * @throws IOException + */ + public void process(final String xhtmlFilename) throws JDOMException, IOException { + + final Document htmlInput = builder.build(xhtmlFilename); + final XPathExpression<Object> xpath = XPathFactory.instance().compile("//img"); + final List<Object> images = xpath.evaluate(htmlInput); + + for (Object o: images) { + final Element image = (Element ) o; + System.out.print(image.getAttributeValue("src") + " "); + } + } +} \ No newline at end of file diff --git a/ws/eclipse/Sda1PreExam/src/dom/table/TablePresentation.java b/ws/eclipse/Sda1PreExam/src/dom/table/TablePresentation.java new file mode 100644 index 0000000000000000000000000000000000000000..0af8df95b9c16a60e7e2b0cf09b544d0a224c79f --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table/TablePresentation.java @@ -0,0 +1,52 @@ +package dom.table; + +import java.io.IOException; +import java.util.List; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +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; + +public class TablePresentation { + private final SAXBuilder builder = new SAXBuilder(); + + public TablePresentation() { + builder.setErrorHandler(new MySaxErrorHandler(System.err)); + } + + public void process(final String xmlFilename) throws JDOMException, + IOException { + + // Create HTML <table><tr><th>Name</th><th>Birthday</th></tr></table> + final Element table = new Element("table"); + final Element tableHeader = new Element("tr"); + table.addContent(tableHeader); + tableHeader.addContent(new Element("th").addContent("Name")); + tableHeader.addContent(new Element("th").addContent("Birthday")); + + // Read addressbook from XML input file + final Document addressbook = builder.build(xmlFilename); + // Search for <person> elements + @SuppressWarnings("unchecked") + final XPathExpression<Element> xpath = (XPathExpression<Element>) (Object) XPathFactory + .instance().compile("/addressbook/person"); + final List<Element> persons = xpath.evaluate(addressbook); + // Create a <tr> for each <person> element + for (Element person : persons) { + final Element tr = new Element("tr"); + table.addContent(tr); + tr.addContent(new Element("td").addContent(person.getChild("name").getText())); + tr.addContent(new Element("td").addContent(person.getChild("birthday").getText())); + } + final Format outFormat = Format.getPrettyFormat(); + + // Serialize to standard output + final XMLOutputter printer = new XMLOutputter(outFormat); + printer.output(table, System.out); + } +} \ No newline at end of file diff --git a/ws/eclipse/Sda1PreExam/src/dom/table/persons.xml b/ws/eclipse/Sda1PreExam/src/dom/table/persons.xml new file mode 100644 index 0000000000000000000000000000000000000000..19416e3290526545b5ca29ba64388555e85b7984 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table/persons.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<addressbook> + <person> + <name>Scott Geoffrey</name> + <birthday>1942-07-22</birthday> + </person> + <person> + <name>Julia Duncan</name> + <birthday>2001-11-07</birthday> + </person> + <person> + <name>Jim Smith</name> + <birthday>1981-04-13</birthday> + </person> +</addressbook> \ No newline at end of file