From 7feb28db1547295ed0541eac9ae766636fd4f562 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Sun, 7 Dec 2014 17:13:10 +0100 Subject: [PATCH] Replacing E-test --- Sda1/Etest/SaxHtmlCreate/Exercise/.gitignore | 5 - Sda1/Etest/SaxHtmlCreate/Exercise/pom.xml | 38 ------- .../hdm_stuttgart/mi/sda1/saxhtml/Driver.java | 15 --- .../mi/sda1/saxhtml/Memo2Html.java | 40 -------- .../mi/sda1/saxhtml/Memo2HtmlHandler.java | 42 -------- Sda1/Etest/SaxHtmlCreate/Solution/.gitignore | 5 - Sda1/Etest/SaxHtmlCreate/Solution/memo.xml | 8 -- Sda1/Etest/SaxHtmlCreate/Solution/pom.xml | 38 ------- .../mi/sda1/saxhtml/Memo2HtmlHandler.java | 88 ----------------- .../mi/sda1/saxhtml/MyErrorHandler.java | 25 ----- Sda1/Etest/SaxHtmlCreate/Solution2/.gitignore | 5 - Sda1/Etest/SaxHtmlCreate/Solution2/memo.xml | 8 -- Sda1/Etest/SaxHtmlCreate/Solution2/pom.xml | 38 ------- .../mi/sda1/saxhtml/Memo2HtmlHandler.java | 82 ---------------- .../mi/sda1/saxhtml/MyErrorHandler.java | 25 ----- .../mi/sda1/saxhtml/package-info.java | 4 - .../SaxMemo2Hhtml_solution/.gitignore | 4 + .../SaxMemo2Hhtml_solution/pom.xml | 98 +++++++++++++++++++ .../mi/sda1/saxhtml/v1}/Driver.java | 6 +- .../mi/sda1/saxhtml/v1}/Memo2Html.java | 13 ++- .../mi/sda1/saxhtml/v1/Memo2HtmlHandler.java | 98 +++++++++++++++++++ .../mi/sda1/saxhtml/v1}/MyErrorHandler.java | 2 +- .../mi/sda1/saxhtml/v1}/package-info.java | 2 +- .../mi/sda1/saxhtml/v2}/Driver.java | 6 +- .../mi/sda1/saxhtml/v2}/Memo2Html.java | 15 ++- .../mi/sda1/saxhtml/v2/Memo2HtmlHandler.java | 96 ++++++++++++++++++ .../mi/sda1/saxhtml/v2}/package-info.java | 2 +- .../src/main/resources/log4j2.xml | 21 ++++ .../src/main/resources}/memo.xml | 0 .../src/main/resources/memo.xml.1.html | 15 +++ .../src/main/resources/memo.xml.2.html | 20 ++++ .../sda1/saxhtml/v1/test/ConversionTest.java | 62 ++++++++++++ .../sda1/saxhtml/v2/test/ConversionTest.java | 66 +++++++++++++ .../de/testing/dom/AssertXpathResult.java | 11 +++ .../de/testing/dom/DomAssert.java | 68 +++++++++++++ 35 files changed, 588 insertions(+), 483 deletions(-) delete mode 100644 Sda1/Etest/SaxHtmlCreate/Exercise/.gitignore delete mode 100644 Sda1/Etest/SaxHtmlCreate/Exercise/pom.xml delete mode 100644 Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java delete mode 100644 Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java delete mode 100644 Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution/.gitignore delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution/memo.xml delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution/pom.xml delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution2/.gitignore delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution2/memo.xml delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution2/pom.xml delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java delete mode 100644 Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/.gitignore create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/pom.xml rename Sda1/Etest/{SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1}/Driver.java (56%) rename Sda1/Etest/{SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1}/Memo2Html.java (83%) create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java rename Sda1/Etest/{SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1}/MyErrorHandler.java (95%) rename Sda1/Etest/{SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1}/package-info.java (54%) rename Sda1/Etest/{SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2}/Driver.java (56%) rename Sda1/Etest/{SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2}/Memo2Html.java (80%) create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2HtmlHandler.java rename Sda1/Etest/{SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2}/package-info.java (54%) create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/log4j2.xml rename Sda1/Etest/{SaxHtmlCreate/Exercise => SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources}/memo.xml (100%) create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.1.html create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.2.html create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/ConversionTest.java create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/AssertXpathResult.java create mode 100644 Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/DomAssert.java diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/.gitignore b/Sda1/Etest/SaxHtmlCreate/Exercise/.gitignore deleted file mode 100644 index f7619e395..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Exercise/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -/bin -/target -/.settings -/.classpath -/.project diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/pom.xml b/Sda1/Etest/SaxHtmlCreate/Exercise/pom.xml deleted file mode 100644 index dd77e0590..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Exercise/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ -<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>de.hdm_stuttgart.mi.sda1.saxhtml</groupId> - <artifactId>saxhtml</artifactId> - <version>1.0</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>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>junit</groupId> - <artifactId>junit</artifactId> - <version>4.11</version> - <scope>test</scope> - </dependency> - </dependencies> -</project> diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java b/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java deleted file mode 100644 index 43de23503..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; - -/** Driver */ -public class Driver { - /** @param argv unused */ - public static void main(String argv[]) { - try{ - Memo2Html xmlStats = new Memo2Html(); - xmlStats.parse("memo.xml"); - - } catch (Exception e){ - e.printStackTrace(System.err); - } - } -} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java b/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java deleted file mode 100644 index 009255e28..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java +++ /dev/null @@ -1,40 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; - -/** Parser, content- and error handler assembly */ -public class Memo2Html { - - private final XMLReader xmlReader; - private final Memo2HtmlHandler eventHandler = new Memo2HtmlHandler(); - private final MyErrorHandler errorHandler = new MyErrorHandler(); - - /** - * @throws SAXException Parsing may fail. - * @throws ParserConfigurationException Unable to instantiate parser. - */ - public Memo2Html() - throws SAXException, ParserConfigurationException{ - final SAXParserFactory saxPf = SAXParserFactory.newInstance(); - final SAXParser saxParser = saxPf.newSAXParser(); - xmlReader = saxParser.getXMLReader(); - xmlReader.setContentHandler(eventHandler); - xmlReader.setErrorHandler(errorHandler); - } - /** Parse an existing memo instance and create HTML output. - * @param uri The resource to be parsed - * @throws IOException Access related problems - * @throws SAXException Parsing may fail - */ - public void parse(final String uri) - throws IOException, SAXException{ - xmlReader.parse(uri); - } -} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java b/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java deleted file mode 100644 index f51da34ee..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java +++ /dev/null @@ -1,42 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; - -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; - -/** - * Turning <memo> documents to html - * - */ -public class Memo2HtmlHandler extends DefaultHandler { - - boolean printElementContent = false; - - @Override public void startDocument() throws SAXException { - System.out.println("Opening document, change me to become a HTML producer"); - } - - @Override - public void startElement(String uri, String localName, String tagName, - Attributes attributes) throws SAXException { - System.out.println("Opening element " + tagName + " , change me to become a HTML producer"); - } - - @Override - public void characters(char[] ch, int start, int length) throws SAXException { - final String elementContent = new String(ch, start, length).trim(); - if (!elementContent.equals("")) { // avoid white space - System.out.println("Charcter content '" + elementContent + "', change me to become a HTML producer"); - } - } - - @Override - public void endElement(String uri, String localName, String tagName) - throws SAXException { - System.out.println("Closing element " + tagName + " , change me to become a HTML producer"); - } - - @Override public void endDocument() throws SAXException { - System.out.println("closing document, change me to become a HTML producer"); - } -} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/.gitignore b/Sda1/Etest/SaxHtmlCreate/Solution/.gitignore deleted file mode 100644 index f7619e395..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -/bin -/target -/.settings -/.classpath -/.project diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/memo.xml b/Sda1/Etest/SaxHtmlCreate/Solution/memo.xml deleted file mode 100644 index bb23c32cb..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution/memo.xml +++ /dev/null @@ -1,8 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<memo> - <from>M. Goik</from> - <to>B. King</to> - <to>A. June</to> - <subject>Best wishes</subject> - <content>Hi all, congratulations to your splendid party</content> -</memo> diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/pom.xml b/Sda1/Etest/SaxHtmlCreate/Solution/pom.xml deleted file mode 100644 index ad9664686..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ -<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>de.hdm_stuttgart.mi.sda1.saxhtml</groupId> - <artifactId>saxhtml_solution</artifactId> - <version>1.0</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>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>junit</groupId> - <artifactId>junit</artifactId> - <version>4.11</version> - <scope>test</scope> - </dependency> - </dependencies> -</project> diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java b/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java deleted file mode 100644 index 057c93ccf..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java +++ /dev/null @@ -1,88 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; - -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; - -/** - * Turning <memo> documents to HTML - * - */ -public class Memo2HtmlHandler extends DefaultHandler { - - boolean printElementContent = false; - - @Override public void startDocument() throws SAXException { - // Create HTML head - print("<html xmlns='http://www.w3.org/1999/xhtml'>\n" - + " <head><title>A Memo</title></head>\n" - + " <body>\n"); // <from> content follows ... - } - - @Override - public void startElement(String uri, String localName, String tagName, - Attributes attributes) throws SAXException { - switch(tagName) { - case "from": - print(" <dl>\n" - + " <dt>Sender:</dt>\n" - + " <dd>"); - printElementContent = true; - break; - - case "to": - print(" <li>"); - printElementContent = true; - break; - - case "subject": - case "content": - case "memo": - break; - - default: - print("unexpected element '" + tagName + "'"); - } - } - - @Override - public void characters(char[] ch, int start, int length) throws SAXException { - if (printElementContent) { - print(new String(ch, start, length).trim()); - } - } - - @Override - public void endElement(String uri, String localName, String tagName) - throws SAXException { - switch(tagName) { - - case "from": - print("</dd>\n" - + " <dt>Recipients:</dt>\n" - + " <dd>\n" - + " <ul>\n"); - printElementContent = false; - break; - - case "to": - print("</li>\n"); - printElementContent = false; - break; - - case "subject": - print(" </ul>\n" // Ending list of recipients - + " </dd>\n" - + " </dl>\n"); - break; - } - } - - @Override public void endDocument() throws SAXException { - print(" </body>\n" - + "</html>\n"); - } - - // Shortcut System.out.print - private void print(final String s) { System.out.print(s);} -} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java b/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java deleted file mode 100644 index 95389354d..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; - -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.ErrorHandler; - -/** Handle parsing errors */ -public class MyErrorHandler implements ErrorHandler { - /** @see ErrorHandler#warning(SAXParseException) */ - public void warning(SAXParseException e) { - System.err.println("[Warning]" + getLocationString(e)); - } - /** @see ErrorHandler#error(SAXParseException) */ - public void error(SAXParseException e) { - System.err.println("[Error]" + getLocationString(e)); - } - /** @see ErrorHandler#fatalError(SAXParseException) */ - public void fatalError(SAXParseException e) throws SAXException{ - System.err.println("[Fatal Error]" + getLocationString(e)); - } - private String getLocationString(SAXParseException e) { - return " line " + e.getLineNumber() + - ", column " + e.getColumnNumber()+ ":" + e.getMessage(); - } -} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/.gitignore b/Sda1/Etest/SaxHtmlCreate/Solution2/.gitignore deleted file mode 100644 index f7619e395..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -/bin -/target -/.settings -/.classpath -/.project diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/memo.xml b/Sda1/Etest/SaxHtmlCreate/Solution2/memo.xml deleted file mode 100644 index bb23c32cb..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/memo.xml +++ /dev/null @@ -1,8 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<memo> - <from>M. Goik</from> - <to>B. King</to> - <to>A. June</to> - <subject>Best wishes</subject> - <content>Hi all, congratulations to your splendid party</content> -</memo> diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/pom.xml b/Sda1/Etest/SaxHtmlCreate/Solution2/pom.xml deleted file mode 100644 index ad9664686..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ -<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>de.hdm_stuttgart.mi.sda1.saxhtml</groupId> - <artifactId>saxhtml_solution</artifactId> - <version>1.0</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>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>junit</groupId> - <artifactId>junit</artifactId> - <version>4.11</version> - <scope>test</scope> - </dependency> - </dependencies> -</project> diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java b/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java deleted file mode 100644 index 71a87c151..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2HtmlHandler.java +++ /dev/null @@ -1,82 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; - -import java.util.List; -import java.util.Vector; - -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; - -/** - * Turning <memo> documents to HTML - * - */ -public class Memo2HtmlHandler extends DefaultHandler { - - String subject, sender; - final List<String> recipients= new Vector<String>(); - final StringBuffer buffer = new StringBuffer(); - - @Override public void startDocument() throws SAXException { - // Clear - recipients.clear(); - - // Create HTML head - print("<html xmlns='http://www.w3.org/1999/xhtml'>\n" - + " <head><title>A Memo</title></head>\n" - + " <body>\n"); // <from> content follows ... - } - - @Override - public void startElement(String uri, String localName, String tagName, - Attributes attributes) throws SAXException { - buffer.setLength(0); - } - - @Override - public void characters(char[] ch, int start, int length) throws SAXException { - buffer.append(ch, start, length); - } - - @Override - public void endElement(String uri, String localName, String tagName) - throws SAXException { - switch(tagName) { - - case "from": - sender = buffer.toString(); - break; - - case "to": - recipients.add(buffer.toString()); - break; - - case "subject": - subject = buffer.toString(); - break; - } - } - - @Override public void endDocument() throws SAXException { - - print( " <h2>Subject:" + subject + "</h2>\n" - + " <dl>\n" - + " <dt>Sender:</dt>\n" - + " <dd>" + sender + "</dd>\n" - + " <dt>Recipients:</dt>\n" - + " <dd>\n" - + " <ul>\n"); - - for(final String recipient: recipients) { - print(" <li>" + recipient + "</li>\n"); - } - print (" </ul>\n" - + " </dd>\n" - + " </dl>\n" - + " </body>\n" - + "</html>\n"); - } - - // Shortcut System.out.print - private void print(final String s) { System.out.print(s);} -} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java b/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java deleted file mode 100644 index 95389354d..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; - -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.ErrorHandler; - -/** Handle parsing errors */ -public class MyErrorHandler implements ErrorHandler { - /** @see ErrorHandler#warning(SAXParseException) */ - public void warning(SAXParseException e) { - System.err.println("[Warning]" + getLocationString(e)); - } - /** @see ErrorHandler#error(SAXParseException) */ - public void error(SAXParseException e) { - System.err.println("[Error]" + getLocationString(e)); - } - /** @see ErrorHandler#fatalError(SAXParseException) */ - public void fatalError(SAXParseException e) throws SAXException{ - System.err.println("[Fatal Error]" + getLocationString(e)); - } - private String getLocationString(SAXParseException e) { - return " line " + e.getLineNumber() + - ", column " + e.getColumnNumber()+ ":" + e.getMessage(); - } -} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java b/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java deleted file mode 100644 index 388d76a62..000000000 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Create HTML output from XML data sources - */ -package de.hdm_stuttgart.mi.sda1.saxhtml; \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/.gitignore b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/.gitignore new file mode 100644 index 000000000..a1c3ab4d0 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/.gitignore @@ -0,0 +1,4 @@ +/target/ +/.settings/ +.classpath +.project diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/pom.xml b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/pom.xml new file mode 100644 index 000000000..d81babfe4 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/pom.xml @@ -0,0 +1,98 @@ +<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>de.hdm_stuttgart.mi.sda1.saxhtml</groupId> + <artifactId>saxmemo2html_solution</artifactId> + <version>1.0</version> + + <packaging>jar</packaging> + + <name>SaxMemo2Hhtml_solution</name> + + <!--Fixme: Add a sensible project related domain here --> + <url>http://www.mi.hdm-stuttgart.de/freedocs</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </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.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>2.1</version> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.1</version> + </dependency> + + </dependencies> + + <build> + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.10.1</version> + <configuration/> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>2.3</version> + <configuration> + <transformers> + <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <manifestEntries> + <Main-Class>de.hdm_stuttgart.de.saxstandard.App</Main-Class> + </manifestEntries> + </transformer> + </transformers> + </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> + </plugin> + + </plugins> + </build> +</project> diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Driver.java similarity index 56% rename from Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Driver.java index 43de23503..78c63536f 100644 --- a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Driver.java @@ -1,12 +1,12 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; +package de.hdm_stuttgart.mi.sda1.saxhtml.v1; /** Driver */ public class Driver { /** @param argv unused */ public static void main(String argv[]) { try{ - Memo2Html xmlStats = new Memo2Html(); - xmlStats.parse("memo.xml"); + Memo2Html memo2html = new Memo2Html(System.out); + memo2html.parse("src/main/resources/memo.xml"); } catch (Exception e){ e.printStackTrace(System.err); diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2Html.java similarity index 83% rename from Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2Html.java index 009255e28..d5e88368e 100644 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2Html.java @@ -1,6 +1,7 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; +package de.hdm_stuttgart.mi.sda1.saxhtml.v1; import java.io.IOException; +import java.io.PrintStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -11,23 +12,27 @@ import org.xml.sax.XMLReader; /** Parser, content- and error handler assembly */ public class Memo2Html { - + private final XMLReader xmlReader; - private final Memo2HtmlHandler eventHandler = new Memo2HtmlHandler(); + private final Memo2HtmlHandler eventHandler; private final MyErrorHandler errorHandler = new MyErrorHandler(); /** * @throws SAXException Parsing may fail. * @throws ParserConfigurationException Unable to instantiate parser. */ - public Memo2Html() + public Memo2Html(final PrintStream out) throws SAXException, ParserConfigurationException{ final SAXParserFactory saxPf = SAXParserFactory.newInstance(); final SAXParser saxParser = saxPf.newSAXParser(); xmlReader = saxParser.getXMLReader(); + + eventHandler = new Memo2HtmlHandler(out); xmlReader.setContentHandler(eventHandler); xmlReader.setErrorHandler(errorHandler); } + + /** Parse an existing memo instance and create HTML output. * @param uri The resource to be parsed * @throws IOException Access related problems diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java new file mode 100644 index 000000000..2b4cd7036 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2HtmlHandler.java @@ -0,0 +1,98 @@ +package de.hdm_stuttgart.mi.sda1.saxhtml.v1; + +import java.io.PrintStream; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +/** + * Turning <memo> documents to HTML + * + */ +public class Memo2HtmlHandler extends DefaultHandler { + + private final PrintStream out; + public Memo2HtmlHandler(final PrintStream out) { + this.out = out; + } + boolean printCharacters = false; + + @Override public void startDocument() throws SAXException { + + out.print("<html>" + + "\n <head>" + + "\n <title>Memo message</title>" + + "\n </head>" + + "\n <body>" + ); + } + + @Override + public void startElement(String uri, String localName, String tagName, + Attributes attributes) throws SAXException { + switch (tagName) { + case "from": + out.print("\n <h2>Message from <strong>"); + printCharacters = true; + break; + + case "to": + printCharacters = true; + out.print(" <li>"); + break; + + case "subject": + printCharacters = true; + out.print(" </ul>" + + "\n <h2>Subject: "); + break; + + case "content": + printCharacters = true; + out.print("\n <p>"); + break; + } + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + if (printCharacters) { + out.print(new String(ch, start, length)); + } + } + + @Override + public void endElement(String uri, String localName, String tagName) + throws SAXException { + printCharacters = false; + switch(tagName) { + + case "from": + out.println("</strong></h2>" + + "\n <h2>Recipients:</h2>" + + "\n <ul>" + ); + break; + + case "to": + out.println("</li>"); + break; + + case "subject": + out.print("</h2>"); + break; + + case "content": + out.print("</p>"); + break; +} + } + + @Override public void endDocument() throws SAXException { + out.println("\n </body>" + + "\n</html>" + ); + out.flush(); + } +} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/MyErrorHandler.java similarity index 95% rename from Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/MyErrorHandler.java index 95389354d..1fce5e72b 100644 --- a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/MyErrorHandler.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/MyErrorHandler.java @@ -1,4 +1,4 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; +package de.hdm_stuttgart.mi.sda1.saxhtml.v1; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/package-info.java similarity index 54% rename from Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/package-info.java index 388d76a62..58d138977 100644 --- a/Sda1/Etest/SaxHtmlCreate/Exercise/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/package-info.java @@ -1,4 +1,4 @@ /** * Create HTML output from XML data sources */ -package de.hdm_stuttgart.mi.sda1.saxhtml; \ No newline at end of file +package de.hdm_stuttgart.mi.sda1.saxhtml.v1; \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Driver.java similarity index 56% rename from Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Driver.java index 43de23503..c36643969 100644 --- a/Sda1/Etest/SaxHtmlCreate/Solution2/src/de/hdm_stuttgart/mi/sda1/saxhtml/Driver.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Driver.java @@ -1,12 +1,12 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; +package de.hdm_stuttgart.mi.sda1.saxhtml.v2; /** Driver */ public class Driver { /** @param argv unused */ public static void main(String argv[]) { try{ - Memo2Html xmlStats = new Memo2Html(); - xmlStats.parse("memo.xml"); + Memo2Html memo2html = new Memo2Html(System.out); + memo2html.parse("src/main/resources/memo.xml"); } catch (Exception e){ e.printStackTrace(System.err); diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2Html.java similarity index 80% rename from Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2Html.java index 009255e28..1f7b1485c 100644 --- a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/Memo2Html.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2Html.java @@ -1,6 +1,7 @@ -package de.hdm_stuttgart.mi.sda1.saxhtml; +package de.hdm_stuttgart.mi.sda1.saxhtml.v2; import java.io.IOException; +import java.io.PrintStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -9,25 +10,31 @@ import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; +import de.hdm_stuttgart.mi.sda1.saxhtml.v1.MyErrorHandler; + /** Parser, content- and error handler assembly */ public class Memo2Html { - + private final XMLReader xmlReader; - private final Memo2HtmlHandler eventHandler = new Memo2HtmlHandler(); + private final Memo2HtmlHandler eventHandler; private final MyErrorHandler errorHandler = new MyErrorHandler(); /** * @throws SAXException Parsing may fail. * @throws ParserConfigurationException Unable to instantiate parser. */ - public Memo2Html() + public Memo2Html(final PrintStream out) throws SAXException, ParserConfigurationException{ final SAXParserFactory saxPf = SAXParserFactory.newInstance(); final SAXParser saxParser = saxPf.newSAXParser(); xmlReader = saxParser.getXMLReader(); + + eventHandler = new Memo2HtmlHandler(out); xmlReader.setContentHandler(eventHandler); xmlReader.setErrorHandler(errorHandler); } + + /** Parse an existing memo instance and create HTML output. * @param uri The resource to be parsed * @throws IOException Access related problems diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2HtmlHandler.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2HtmlHandler.java new file mode 100644 index 000000000..41b5ec5d3 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2HtmlHandler.java @@ -0,0 +1,96 @@ +package de.hdm_stuttgart.mi.sda1.saxhtml.v2; + +import java.io.PrintStream; +import java.util.List; +import java.util.Vector; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +/** + * Turning <memo> documents to HTML + * + */ +public class Memo2HtmlHandler extends DefaultHandler { + + private final PrintStream out; + public Memo2HtmlHandler(final PrintStream out) { + this.out = out; + } + + String from, subject, content; + final List<String> recipients= new Vector<String>(); + final StringBuffer buffer = new StringBuffer(); + + @Override public void startDocument() throws SAXException { + // Clear + recipients.clear(); + } + + @Override + public void startElement(String uri, String localName, String tagName, + Attributes attributes) throws SAXException { + buffer.setLength(0); // Clear string buffer + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + buffer.append(ch, start, length); + } + + @Override + public void endElement(String uri, String localName, String tagName) + throws SAXException { + switch(tagName) { + + case "from": + from = buffer.toString(); + break; + + case "to": + recipients.add(buffer.toString()); + break; + + case "subject": + subject = buffer.toString(); + break; + + case "content": + content = buffer.toString(); + break; +} + } + + @Override public void endDocument() throws SAXException { + // First portion until sender + out.print + ( "<html>\n" // xmlns='http://www.w3.org/1999/xhtml' + + " <head><title>Memo from " + from + "</title></head>\n" + + " <body>\n" + + " <h2>Subject:" + subject + "</h2>\n" + + " <dl>\n" + + " <dt>Sender:</dt>\n" + + " <dd>" + from + "</dd>\n" + + " <dt>Recipients:</dt>\n" + + " <dd>\n" + + " <ul>\n"); + + // Recipients + for(final String recipient: recipients) { + out.print + ( " <li>" + recipient + "</li>\n"); + } + // Tail including reference to sender + out.print + ( " </ul>\n" + + " </dd>\n" + + " </dl>\n" + + " <h2>Subject: " + subject + "</h2>\n" + + " <p>" + content + "</p>\n" + + " <p>End of message from <strong>" + from +"</strong></p>\n" + + " </body>\n" + + "</html>\n"); + out.flush(); + } +} \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/package-info.java similarity index 54% rename from Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/package-info.java index 388d76a62..f3bbf67a2 100644 --- a/Sda1/Etest/SaxHtmlCreate/Solution/src/de/hdm_stuttgart/mi/sda1/saxhtml/package-info.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/package-info.java @@ -1,4 +1,4 @@ /** * Create HTML output from XML data sources */ -package de.hdm_stuttgart.mi.sda1.saxhtml; \ No newline at end of file +package de.hdm_stuttgart.mi.sda1.saxhtml.v2; \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/log4j2.xml b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/log4j2.xml new file mode 100644 index 000000000..ffeb6ae18 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/log4j2.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration> + <Appenders> + <File name="A1" fileName="A1.log" append="false"> + <PatternLayout pattern="%t %-5p %c{2} - %m%n"/> + </File> + <Console name="STDOUT" target="SYSTEM_OUT"> + <PatternLayout pattern="%C{2} (%F:%L) - %m%n"/> + </Console> + </Appenders> + <Loggers> + + <!-- You my want to define class or package level per-logger rules --> + <Logger name="de.hdm_stuttgart.de.saxstandard.App" level="debug"> + <AppenderRef ref="A1"/> + </Logger> + <Root level="debug"> + <AppenderRef ref="STDOUT"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file diff --git a/Sda1/Etest/SaxHtmlCreate/Exercise/memo.xml b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml similarity index 100% rename from Sda1/Etest/SaxHtmlCreate/Exercise/memo.xml rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.1.html b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.1.html new file mode 100644 index 000000000..9157fb5aa --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.1.html @@ -0,0 +1,15 @@ +<html> + <head> + <title>Memo message</title> + </head> + <body> + <h2>Message from <strong>M. Goik</strong></h2> + <h2>Recipients:</h2> + <ul> + <li>B. King</li> + <li>A. June</li> + </ul> + <h2>Subject: Best wishes</h2> + <p>Hi all, congratulations to your splendid party</p> + </body> +</html> diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.2.html b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.2.html new file mode 100644 index 000000000..85659d988 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/resources/memo.xml.2.html @@ -0,0 +1,20 @@ +<html> + <head><title>Memo from M. Goik</title></head> + <body> + <h2>Subject:Best wishes</h2> + <dl> + <dt>Sender:</dt> + <dd>M. Goik</dd> + <dt>Recipients:</dt> + <dd> + <ul> + <li>B. King</li> + <li>A. June</li> + </ul> + </dd> + </dl> + <h2>Subject: Best wishes</h2> + <p>Hi all, congratulations to your splendid party</p> + <p>End of message from <strong>M. Goik</strong></p> + </body> +</html> diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java new file mode 100644 index 000000000..7bfa2b14d --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java @@ -0,0 +1,62 @@ +package de.hdm_stuttgart.de.sda1.saxhtml.v1.test; + +import java.io.IOException; +import java.io.PrintStream; + +import javax.xml.parsers.ParserConfigurationException; + +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXException; + +import de.hdm_stuttgart.de.testing.dom.DomAssert; +import de.hdm_stuttgart.mi.sda1.saxhtml.v1.Memo2Html; + +/** + * Unit testing XML to HTML output + */ +public class ConversionTest { + + public final static String + xmlInputFileName = "src/main/resources/memo.xml", + htmlOutputFileName = xmlInputFileName + ".1.html"; + + static Element htmlRootElement; + + @BeforeClass public static void init() + throws JDOMException, SAXException, ParserConfigurationException, IOException { + + final PrintStream out = new PrintStream(htmlOutputFileName); + final Memo2Html memo2html = new Memo2Html(out); + memo2html.parse(xmlInputFileName); + final SAXBuilder parser = new SAXBuilder(); + htmlRootElement = parser.build(htmlOutputFileName).getRootElement(); + out.close(); + } + + @Test public void testSenderInHeader() { + DomAssert.assertSingleNodeContent("<title>Memo message</title> must be the only child of <head>", htmlRootElement, "head/*", "title", "Memo message"); + } + @Test public void testFrom() { + DomAssert.assertSingleNodeContent("<strong>M. Goik</strong> must appear within first <h2> child of <body>", + htmlRootElement, "body/h2[1]/strong", "strong", "M. Goik"); + } + @Test public void testNumberfRecipients() { + DomAssert.assertNumberOfNodes("Two <li> elements required matching two recipients", htmlRootElement, "body/ul/li", 2); + } + @Test public void testSecondRecipient() { + DomAssert.assertSingleNodeContent("Second recipient must be <li>A. June</li>", + htmlRootElement, "body/ul/li[2]", "li", "A. June"); + } + @Test public void testSubject() { + DomAssert.assertSingleNodeContent("<h2>Subject: Best wishes</h2> must appear as first child of <body>", + htmlRootElement, "body/*[4]", "h2", "Subject: Best wishes"); + } + @Test public void testContent() { + DomAssert.assertSingleNodeContent("Last child of <body> must be content", + htmlRootElement, "body/*[5]", "p", "Hi all, congratulations to your splendid party"); + } +} \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/ConversionTest.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/ConversionTest.java new file mode 100644 index 000000000..c6331557f --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/ConversionTest.java @@ -0,0 +1,66 @@ +package de.hdm_stuttgart.de.sda1.saxhtml.v2.test; + +import java.io.IOException; +import java.io.PrintStream; + +import javax.xml.parsers.ParserConfigurationException; + +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXException; + +import de.hdm_stuttgart.de.testing.dom.DomAssert; +import de.hdm_stuttgart.mi.sda1.saxhtml.v2.Memo2Html; + +/** + * Unit testing XML to HTML output + */ +public class ConversionTest { + + public final static String + xmlInputFileName = "src/main/resources/memo.xml", + htmlOutputFileName = xmlInputFileName + ".2.html"; + + static Element htmlRootElement; + + @BeforeClass public static void init() + throws JDOMException, SAXException, ParserConfigurationException, IOException { + + final PrintStream out = new PrintStream(htmlOutputFileName); + final Memo2Html memo2html = new Memo2Html(out); + memo2html.parse(xmlInputFileName); + final SAXBuilder parser = new SAXBuilder(); + htmlRootElement = parser.build(htmlOutputFileName).getRootElement(); + out.close(); + } + + @Test public void testSenderInHeader() { + DomAssert.assertSingleNodeContent("<title>Memo from M. Goik</title> must be child of <head>", htmlRootElement, "head/title", "Memo from M. Goik"); + } + @Test public void testNumberfRecipients() { + DomAssert.assertNumberOfNodes("There should be two <li> elements matching two recipients", htmlRootElement, "body/dl/dd/ul/li", 2); + } + @Test public void testH1MemoSubjectMessage() { + DomAssert.assertSingleNodeContent("<h2>Subject:Best wishes</h2> must appear as first child of <body>", + htmlRootElement, "body/*[1]", "h2", "Subject:Best wishes"); + } + @Test public void testSenderBeforeRecipientList() { + DomAssert.assertSingleNodeContent("<dd>M. Goik</dd> must appear as second child of the list of recipients", + htmlRootElement, "body/dl/*[2]", "dd", "M. Goik"); + } + @Test public void testMemoContent() { + DomAssert.assertSingleNodeContent("content must appear as second <p> child after recipient list in <body>", + htmlRootElement, "body/dl/following-sibling::*[2]", "p", "Hi all, congratulations to your splendid party"); + } + @Test public void testLastParagraph() { + DomAssert.assertNumberOfNodes("Expecting two <p> children of <body>", + htmlRootElement, "body/*[name(.)='p']", 2); + } + @Test public void testSenderAtFoot() { + DomAssert.assertSingleNodeContent("<strong>M. Goik</strong> must appear inside a <p> immediately following the list of recipients", + htmlRootElement, "body/dl/following-sibling::*[1]", "h2", "Subject: Best wishes"); + } +} \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/AssertXpathResult.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/AssertXpathResult.java new file mode 100644 index 000000000..1a08e5b19 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/AssertXpathResult.java @@ -0,0 +1,11 @@ +package de.hdm_stuttgart.de.testing.dom; + +public class AssertXpathResult extends AssertionError { + + private static final long serialVersionUID = -1369600480719379445L; + + public AssertXpathResult(String msg) { + super(msg); + } + +} diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/DomAssert.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/DomAssert.java new file mode 100644 index 000000000..2e29b3428 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/DomAssert.java @@ -0,0 +1,68 @@ +package de.hdm_stuttgart.de.testing.dom; + +import java.util.List; + +import org.jdom2.Element; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; + +public class DomAssert { + + + final static XPathFactory xpf = XPathFactory.instance(); + + public static void assertNumberOfNodes(final String msg, final Element context, final String xpath, int expectedNodeCount) { + + @SuppressWarnings({ "unchecked", "rawtypes" }) + final XPathExpression<Element> xpathExpr = (XPathExpression<Element>) (XPathExpression) xpf.compile(xpath); + + final List<Element> matchedNodes = xpathExpr.evaluate(context); + + if (expectedNodeCount != matchedNodes.size()) { + throw new AssertXpathResult(msg + "\nExpected " + expectedNodeCount + " node" + (expectedNodeCount == 1? "":"s") + " for xpath '" + xpath + + "' but found " + matchedNodes.size()); + } + } + + public static void assertSingleNodeContent(final String msg, final Element context, final String xpath, String expectedContent) { + + @SuppressWarnings({ "unchecked", "rawtypes" }) + final XPathExpression<Element> xpathExpr = (XPathExpression<Element>) (XPathExpression) xpf.compile(xpath); + + final List<Element> matchedNodes = xpathExpr.evaluate(context); + if (1 == matchedNodes.size()) { + final String content = matchedNodes.get(0).getValue(); + if (!content.equals(expectedContent)) { + throw new AssertXpathResult(msg + "\nExpected content '" + expectedContent + "' for xpath '" + xpath + "' but found '" + content + "'"); + } + } else { + throw new AssertXpathResult("\nExactly one node expected for xpath '" + xpath + "' but found " + matchedNodes.size()); + } + } + + public static void assertSingleNodeContent(final String msg, final Element context, final String xpath, final String expectedTagName, String expectedContent) { + + @SuppressWarnings({ "unchecked", "rawtypes" }) + final XPathExpression<Element> xpathExpr = (XPathExpression<Element>) (XPathExpression) xpf.compile(xpath); + + final List<Element> matchedNodes = xpathExpr.evaluate(context); + if (1 == matchedNodes.size()) { + + final Element element = matchedNodes.get(0); + + final String elementTagName = element.getName(); + if (!expectedTagName.equals(elementTagName)) { + throw new AssertXpathResult(msg + "\nExpected <" + expectedTagName + "> for xpath '" + xpath + "' but found <" + + elementTagName + ">"); + } + final String content = element.getValue(); + if (!expectedContent.equals(content)) { + throw new AssertXpathResult(msg + "\nExpected element content '" + expectedContent + "' for xpath '" + xpath + "' but found '" + + content + "'"); + } + + } else { + throw new AssertXpathResult("Exactly node expected for xpath '" + xpath + "' but found " + matchedNodes.size()); + } + } +} -- GitLab