diff --git a/ws/eclipse/Sda1PreExam/src/dom/table/ReorderTableData.java b/ws/eclipse/Sda1PreExam/src/dom/table/ReorderTableData.java deleted file mode 100644 index 77c484569200b8ceeac816f7515274c6fedb0f26..0000000000000000000000000000000000000000 --- a/ws/eclipse/Sda1PreExam/src/dom/table/ReorderTableData.java +++ /dev/null @@ -1,47 +0,0 @@ -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/table1/Driver.java b/ws/eclipse/Sda1PreExam/src/dom/table1/Driver.java new file mode 100644 index 0000000000000000000000000000000000000000..ae358f89708f2b4312b3525ed5a67c4790612343 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table1/Driver.java @@ -0,0 +1,20 @@ +package dom.table1; + +/** + * 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/table1/TablePresentation.java b/ws/eclipse/Sda1PreExam/src/dom/table1/TablePresentation.java new file mode 100644 index 0000000000000000000000000000000000000000..358db98ce53cc338dc034b3207bed8b13bbb1177 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table1/TablePresentation.java @@ -0,0 +1,60 @@ +package dom.table1; + +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; + +import dom.table.MySaxErrorHandler; + +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 + boolean setBackground = true; + for (Element person : persons) { + final Element tr = new Element("tr"); + if (setBackground) { + tr.setAttribute("style", "background-color: #FFFF88;"); + setBackground = false; + } else { + setBackground = true; + } + 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/table2/Driver.java b/ws/eclipse/Sda1PreExam/src/dom/table2/Driver.java new file mode 100644 index 0000000000000000000000000000000000000000..0d844fd891a58f35eb0cb2a61abf1629510cfbe8 --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table2/Driver.java @@ -0,0 +1,20 @@ +package dom.table2; + +/** + * 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/table2/TablePresentation.java b/ws/eclipse/Sda1PreExam/src/dom/table2/TablePresentation.java new file mode 100644 index 0000000000000000000000000000000000000000..ab9b4e297201983ea25c17b1c51081e2c8e0aa5c --- /dev/null +++ b/ws/eclipse/Sda1PreExam/src/dom/table2/TablePresentation.java @@ -0,0 +1,57 @@ +package dom.table2; + +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; + +import dom.table.MySaxErrorHandler; + +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 { + + // Read addressbook from XML input file + final Document addressbook = builder.build(xmlFilename); + + // Create HTML <table/> + final Element table = new Element("table"); + + // 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 the name row + final Element nameRow = new Element("tr"); + table.addContent(nameRow); + nameRow.addContent(new Element("th").addContent("Name")); + + // Create the name birthday row + final Element birthdayRow = new Element("tr"); + table.addContent(birthdayRow); + birthdayRow.addContent(new Element("th").addContent("Birthday")); + + for (Element person : persons) { + nameRow.addContent(new Element("td").addContent(person.getChild("name").getText())); + birthdayRow.addContent(new Element("td").addContent(person.getChild("birthday").getText())); + } + + final Format outFormat = Format.getPrettyFormat(); + final XMLOutputter printer = new XMLOutputter(outFormat); + printer.output(table, System.out); + } +} \ No newline at end of file