diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Driver.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Driver.java index 78c63536f62100da8b355be84104441586fb39b1..1a3cd8f294bc311b7d3c83a64c606af9f74bab85 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Driver.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Driver.java @@ -5,7 +5,7 @@ public class Driver { /** @param argv unused */ public static void main(String argv[]) { try{ - Memo2Html memo2html = new Memo2Html(System.out); + Memo2Html memo2html = new Memo2Html(new Memo2HtmlHandler()); memo2html.parse("src/main/resources/memo.xml"); } catch (Exception e){ diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2Html.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2Html.java index 1e0d749a6160fafa783964291af32b75df1cc8e9..399857954518056580f40c9754f041ed70423957 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2Html.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v1/Memo2Html.java @@ -7,14 +7,14 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; /** Parser, content- and error handler assembly */ -public class Memo2Html { +public class Memo2Html { private final XMLReader xmlReader; - private final Memo2HtmlHandler eventHandler; private final MyErrorHandler errorHandler = new MyErrorHandler(); /** @@ -24,14 +24,13 @@ public class Memo2Html { * @throws ParserConfigurationException * Unable to instantiate parser. */ - public Memo2Html(final PrintStream out) throws SAXException, + public Memo2Html(final ContentHandler handler) throws SAXException, ParserConfigurationException { final SAXParserFactory saxPf = SAXParserFactory.newInstance(); final SAXParser saxParser = saxPf.newSAXParser(); xmlReader = saxParser.getXMLReader(); - eventHandler = new Memo2HtmlHandler(out); - xmlReader.setContentHandler(eventHandler); + xmlReader.setContentHandler(handler); xmlReader.setErrorHandler(errorHandler); } 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 index 69226b80b448b30ecba8ac78bbb46fb41fa6b4da..580fcfbc50618ac9d3d6531122b35ac83df52d80 100644 --- 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 @@ -6,19 +6,21 @@ import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; +import de.hdm_stuttgart.mi.sda1.saxhtml.tools.ContentRedirect; + /** * Turning <memo> documents to HTML * */ -public class Memo2HtmlHandler extends DefaultHandler { +public class Memo2HtmlHandler extends DefaultHandler implements ContentRedirect { - private final PrintStream out; + private PrintStream out; /** * @param out */ - public Memo2HtmlHandler(final PrintStream out) { - this.out = out; + public Memo2HtmlHandler() { + out = System.out; } boolean printCharacters = false; @@ -94,4 +96,10 @@ public class Memo2HtmlHandler extends DefaultHandler { out.println("\n </body>" + "\n</html>"); out.flush(); } + + @Override + public void setOutputStream(final PrintStream out) { + this.out = out; + + } } \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Driver.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Driver.java index c36643969519caddd555da67d283e0761d40e5ba..99616e0b9355e6a616dbb4de9449938da16b8165 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Driver.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Driver.java @@ -1,11 +1,13 @@ package de.hdm_stuttgart.mi.sda1.saxhtml.v2; +import de.hdm_stuttgart.mi.sda1.saxhtml.v1.Memo2HtmlHandler; + /** Driver */ public class Driver { /** @param argv unused */ public static void main(String argv[]) { try{ - Memo2Html memo2html = new Memo2Html(System.out); + Memo2Html memo2html = new Memo2Html(new Memo2HtmlHandler()); memo2html.parse("src/main/resources/memo.xml"); } catch (Exception e){ diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2Html.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2Html.java index 5d0dcf1baaa40ac3aa9c942d84bac252575cdae0..5b98ba9ca48adbc80b1401ac69b171ba1819af77 100644 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2Html.java +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/main/java/de/hdm_stuttgart/mi/sda1/saxhtml/v2/Memo2Html.java @@ -7,6 +7,7 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; @@ -16,7 +17,6 @@ import de.hdm_stuttgart.mi.sda1.saxhtml.v1.MyErrorHandler; public class Memo2Html { private final XMLReader xmlReader; - private final Memo2HtmlHandler eventHandler; private final MyErrorHandler errorHandler = new MyErrorHandler(); /** @@ -26,14 +26,13 @@ public class Memo2Html { * @throws ParserConfigurationException * Unable to instantiate parser. */ - public Memo2Html(final PrintStream out) throws SAXException, + public Memo2Html(final ContentHandler handler) throws SAXException, ParserConfigurationException { final SAXParserFactory saxPf = SAXParserFactory.newInstance(); final SAXParser saxParser = saxPf.newSAXParser(); xmlReader = saxParser.getXMLReader(); - eventHandler = new Memo2HtmlHandler(out); - xmlReader.setContentHandler(eventHandler); + xmlReader.setContentHandler(handler); xmlReader.setErrorHandler(errorHandler); } 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 index d71670297b411473671674173ec405480da7e25a..dcdf36a95e1b69425a63d6eb3164a651a193ed6b 100644 --- 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 @@ -8,20 +8,22 @@ import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; +import de.hdm_stuttgart.mi.sda1.saxhtml.tools.ContentRedirect; + /** * Turning <memo> documents to HTML * */ -public class Memo2HtmlHandler extends DefaultHandler { +public class Memo2HtmlHandler extends DefaultHandler implements ContentRedirect { - private final PrintStream out; + private PrintStream out; /** * @param out * Write output to stream */ - public Memo2HtmlHandler(final PrintStream out) { - this.out = out; + public Memo2HtmlHandler() { + out = System.out; } String from, subject, content; @@ -94,4 +96,9 @@ public class Memo2HtmlHandler extends DefaultHandler { + from + "</strong></p>\n" + " </body>\n" + "</html>\n"); out.flush(); } + + @Override + public void setOutputStream(PrintStream out) { + this.out = out; + } } \ No newline at end of file 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 index 9157fb5aa12ce92600a3548e8121ddc82d121284..85659d988aad844c3283745f8a4f183099b21b79 100644 --- 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 @@ -1,15 +1,20 @@ <html> - <head> - <title>Memo message</title> - </head> + <head><title>Memo from M. Goik</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> + <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/TestSimpleSaxTransform.java similarity index 59% rename from Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java index c4b3c305d4899888ac084bc0c46716e0ecfd0c66..f8bbf56bc58fcd24d629d5bd2c7edaea288648ba 100644 --- 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/TestSimpleSaxTransform.java @@ -1,41 +1,24 @@ 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.Assert; import org.junit.Test; -import org.xml.sax.SAXException; +import de.hdm_stuttgart.de.testing.dom.ConversionTest; import de.hdm_stuttgart.de.testing.dom.DomAssert; -import de.hdm_stuttgart.mi.sda1.saxhtml.v1.Memo2Html; +import de.hdm_stuttgart.mi.sda1.saxhtml.v1.Memo2HtmlHandler; /** * Unit testing XML to HTML output */ @SuppressWarnings("javadoc") -public class ConversionTest { +public class TestSimpleSaxTransform extends ConversionTest { - final static String - xmlInputFileName = "src/main/resources/memo.xml", - htmlOutputFileName = xmlInputFileName + ".1.html"; - - static Element htmlRootElement; + public TestSimpleSaxTransform() { + super("src/main/resources/memo.xml", new Memo2HtmlHandler(), ".1.html"); + } - @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 checkWellFormedness() { + Assert.assertNull("Unable to parse generated file:" + errorInitString, errorInitString); } @Test public void testSenderInHeader() { 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 deleted file mode 100644 index 4c0467f29b17d2785a96b158e4a69accc7ea8fc3..0000000000000000000000000000000000000000 --- a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/ConversionTest.java +++ /dev/null @@ -1,73 +0,0 @@ -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 - */ -@SuppressWarnings("javadoc") -public class ConversionTest { - - final static String - xmlInputFileName = "src/main/resources/memo.xml", - htmlOutputFileName = xmlInputFileName + ".2.html"; - - static Element htmlRootElement; - - /** - * @throws JDOMException - * @throws SAXException - * @throws ParserConfigurationException - * @throws IOException - */ -@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/Saxmemo2html/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/TestComplexSaxTransform.java similarity index 74% rename from Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/ConversionTest.java rename to Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java index b7ed831060d56f30a52bd7814c6b56ee6014aafb..adda24bb8196ec57d5ddab8624cf19936b7cfd79 100644 --- a/Sda1/Etest/SaxMemo2Html/Saxmemo2html/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/TestComplexSaxTransform.java @@ -12,38 +12,21 @@ import org.junit.BeforeClass; import org.junit.Test; import org.xml.sax.SAXException; +import de.hdm_stuttgart.de.testing.dom.ConversionTest; import de.hdm_stuttgart.de.testing.dom.DomAssert; import de.hdm_stuttgart.mi.sda1.saxhtml.v2.Memo2Html; +import de.hdm_stuttgart.mi.sda1.saxhtml.v2.Memo2HtmlHandler; /** * Turning <memo> documents to HTML as being shown in sample document memo.xml.2.sample.html */ @SuppressWarnings("javadoc") -public class ConversionTest { - - final static String - xmlInputFileName = "src/main/resources/memo.xml", - htmlOutputFileName = xmlInputFileName + ".2.html"; - - static Element htmlRootElement; - - /** - * @throws JDOMException - * @throws SAXException - * @throws ParserConfigurationException - * @throws IOException - */ - @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(); - } +public class TestComplexSaxTransform extends ConversionTest { + + public TestComplexSaxTransform() { + super("src/main/resources/memo.xml", new Memo2HtmlHandler(), ".2.html"); + } @Test public void testSenderInHeader() { DomAssert.assertSingleNodeContent("<title>Memo from M. Goik</title> must be child of <head>", htmlRootElement, "head/title", "Memo from M. Goik"); diff --git a/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/ConversionTest.java b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/ConversionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d26e81018bdda4e0057efc005925da5c375324ec --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/SaxMemo2Hhtml_solution/src/test/java/de/hdm_stuttgart/de/testing/dom/ConversionTest.java @@ -0,0 +1,75 @@ +package de.hdm_stuttgart.de.testing.dom; + +import java.io.FileNotFoundException; +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.ContentHandler; +import org.xml.sax.SAXException; + +import de.hdm_stuttgart.de.testing.dom.DomAssert; +import de.hdm_stuttgart.mi.sda1.saxhtml.tools.ContentRedirect; +import de.hdm_stuttgart.mi.sda1.saxhtml.v1.Memo2Html; + +/** + * Turning <memo> documents to HTML as being shown in sample document memo.xml.2.sample.html + + */ +@SuppressWarnings("javadoc") +public abstract class ConversionTest { + + public final String xmlInputFileName, htmlOutputFileName; + final ContentRedirect saxHandler; + protected String errorInitString = null; + + protected Element htmlRootElement; + + protected ConversionTest(final String xmlInputFileName, final ContentRedirect saxHandler, final String outputExtension) { + + this.saxHandler = saxHandler; + + this.xmlInputFileName = xmlInputFileName; + this.htmlOutputFileName = xmlInputFileName + outputExtension; + + final PrintStream out; + + try { + out = new PrintStream(htmlOutputFileName); + } catch (FileNotFoundException e1) { + errorInitString = "Unable to open file '" + htmlOutputFileName + "' for writing"; + return; + } + saxHandler.setOutputStream(out); + final Memo2Html memo2html; + try { + memo2html = new Memo2Html(saxHandler); + } catch (SAXException | ParserConfigurationException e2) { + e2.printStackTrace(); + return; + } + try { + memo2html.parse(xmlInputFileName); + } catch (IOException e1) { + errorInitString = "Unable parse file '" + xmlInputFileName + "'"; + return; + } catch (SAXException e) { + e.printStackTrace(); + } + final SAXBuilder parser = new SAXBuilder(); + + try { + htmlRootElement = parser.build(htmlOutputFileName).getRootElement(); + } catch (JDOMException | IOException e1) { + errorInitString = "Unable to parse file '" + htmlOutputFileName + "', see stack trace for further information"; + e1.printStackTrace(); + } + out.close(); + } +} \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java b/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java similarity index 59% rename from Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java rename to Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java index c4b3c305d4899888ac084bc0c46716e0ecfd0c66..939fa29a1d4a1b948a89cdeac2fbb7ce49bccdad 100644 --- a/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/ConversionTest.java +++ b/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v1/test/TestSimpleSaxTransform.java @@ -1,41 +1,23 @@ 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.Assert; import org.junit.Test; -import org.xml.sax.SAXException; +import de.hdm_stuttgart.de.testing.dom.ConversionTest; import de.hdm_stuttgart.de.testing.dom.DomAssert; -import de.hdm_stuttgart.mi.sda1.saxhtml.v1.Memo2Html; /** * Unit testing XML to HTML output */ @SuppressWarnings("javadoc") -public class ConversionTest { +public class TestSimpleSaxTransform extends ConversionTest { - final static String - xmlInputFileName = "src/main/resources/memo.xml", - htmlOutputFileName = xmlInputFileName + ".1.html"; - - static Element htmlRootElement; + public TestSimpleSaxTransform() { + super("src/main/resources/memo.xml", ".1.html"); + } - @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 checkWellFormedness() { + Assert.assertNull("Unable to parse generated file:" + errorInitString, errorInitString); } @Test public void testSenderInHeader() { diff --git a/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java b/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java new file mode 100644 index 0000000000000000000000000000000000000000..d6f7e525ae38ac63b27328956e68a8a6d2b1d182 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/sda1/saxhtml/v2/test/TestComplexSaxTransform.java @@ -0,0 +1,56 @@ +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.ConversionTest; +import de.hdm_stuttgart.de.testing.dom.DomAssert; +import de.hdm_stuttgart.mi.sda1.saxhtml.v2.Memo2Html; + +/** + * Turning <memo> documents to HTML as being shown in sample document memo.xml.2.sample.html + + */ +@SuppressWarnings("javadoc") +public class TestComplexSaxTransform extends ConversionTest { + + public TestComplexSaxTransform() { + super("src/main/resources/memo.xml", ".2.html"); + } + + @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/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/testing/dom/ConversionTest.java b/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/testing/dom/ConversionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4886a72e7934c1094e5df52798e7c54ff28c8be8 --- /dev/null +++ b/Sda1/Etest/SaxMemo2Html/Saxmemo2html/src/test/java/de/hdm_stuttgart/de/testing/dom/ConversionTest.java @@ -0,0 +1,80 @@ +package de.hdm_stuttgart.de.testing.dom; + +import java.io.FileNotFoundException; +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; + +/** + * Turning <memo> documents to HTML as being shown in sample document memo.xml.2.sample.html + + */ +@SuppressWarnings("javadoc") +public abstract class ConversionTest { + + protected ConversionTest(final String xmlInputFileName, final String extension) { + this.xmlInputFileName = xmlInputFileName; + this.htmlOutputFileName = xmlInputFileName + extension; + } + + public final String xmlInputFileName, htmlOutputFileName; // To be statically initialized in derived class + protected String errorInitString = null; + + protected Element htmlRootElement; + + /** + * @throws JDOMException + * @throws SAXException + * @throws ParserConfigurationException + * @throws IOException + */ + public void init() { + + PrintStream out; + + try { + System.err.println("of=" + htmlOutputFileName); + out = new PrintStream(htmlOutputFileName); + } catch (FileNotFoundException e1) { + errorInitString = "Unable to open file '" + htmlOutputFileName + "' for writing"; + return; + } + Memo2Html memo2html; + try { + memo2html = new Memo2Html(out); + } catch (SAXException | ParserConfigurationException e2) { + // TODO Auto-generated catch block + e2.printStackTrace(); + return; + } + try { + memo2html.parse(xmlInputFileName); + } catch (IOException e1) { + errorInitString = "Unable parse file '" + xmlInputFileName + "'"; + return; + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + final SAXBuilder parser = new SAXBuilder(); + + try { + htmlRootElement = parser.build(htmlOutputFileName).getRootElement(); + } catch (JDOMException | IOException e1) { + errorInitString = "Unable to parse file '" + htmlOutputFileName + "', see stack trace for further information"; + e1.printStackTrace(); + } + out.close(); + } +} \ No newline at end of file diff --git a/Sda1/Etest/SaxMemo2Html/exercise1.xhtml b/Sda1/Etest/SaxMemo2Html/exercise1.xhtml index 0bb111955f8f70a806d7755516f83a3b537bac3e..2f98a36e193c272fa18835d99cbe1ac9995aa2bd 100644 --- a/Sda1/Etest/SaxMemo2Html/exercise1.xhtml +++ b/Sda1/Etest/SaxMemo2Html/exercise1.xhtml @@ -33,7 +33,7 @@ <li><p><strong>src/main/resources/memo.xml.1.sample.html</strong></p><p>The intended output corresponding to be generated from </p></li> - <li><p><strong>de.hdm_stuttgart.de.sda1.saxhtml.v1.test.ConversionTest</strong></p><p>This + <li><p><strong>de.hdm_stuttgart.de.sda1.saxhtml.v1.test.TestSimpleSaxTransform</strong></p><p>This Junit class executes your handler and writes the corresponding output to src/main/resources/memo.xml.1.html. The latter file then becomes subject to a series of XPath expression tests checking for @@ -63,9 +63,10 @@ <h2>Subject: Best wishes</h2> <p>Hi all, congratulations to your splendid party</p> </body> -</html></pre><p> Watch your output being generated by executing - <code>de.hdm_stuttgart.mi.sda1.saxhtml.v1.Driver</code>. When you are - satisfied with your result execute the Junit test +</html></pre><p>Your SAX handler shall allow for processing of multiple + XML input documents in sequence. </p><p>Watch your output being generated by + executing <code>de.hdm_stuttgart.mi.sda1.saxhtml.v1.Driver</code>. When you + are satisfied with your result execute the Junit test <code>de.hdm_stuttgart.de.sda1.saxhtml.v1.test.ConversionTest</code>. When you are finished export your Maven project as a compressed .zip archive and upload it.</p><h2>ToDo, Part 2</h2><p>This second exercise deals with Java @@ -73,8 +74,8 @@ names. So you might want to close <strong>all</strong> file tabs in your current Eclipse project to avoid confusing yourself with files from the first part of this exercise.</p><p>Your project contains a second set of - files intended to create more sophisticated output as being contained in - <code>memo.xml.2.sample.html</code>:</p><pre><html> + files intended to create more sophisticated output. A sample result is being + contained in <code>memo.xml.2.sample.html</code>:</p><pre><html> <head><title>Memo from M. Goik</title></head> <body> <h2>Subject:Best wishes</h2> @@ -95,13 +96,14 @@ </body> </html></pre><p>Complete the implementation of <code>de.hdm_stuttgart.mi.sda1.saxhtml.v2.Memo2HtmlHandler</code>. - Corresponding Driver and Junit classes are being supplied by + Corresponding Driver and Junit classes are being supplied as <code>de.hdm_stuttgart.mi.sda1.saxhtml.v2.Driver</code> and - <code>de.hdm_stuttgart.de.sda1.saxhtml.v2.test.ConversionTest</code>.</p><p>When + <code>de.hdm_stuttgart.de.sda1.saxhtml.v2.test.TestComplexSaxTransform</code>.</p><p>When you are finished zip your project again and upload it. Only the last .zip file will become subject to marking.</p><h3>Hint:</h3><p>In this second part - the order of content from your XML input is not being preserved. Furthermore - some input (sender's name) has to be duplicated. To deal with these + the order of content from your XML input is not being preserved. The subject + for example is to be shown before both sender and recipients. Furthermore + some input (sender's name) has to be shown multiple times. To tackle these challenges you may first collect all relevant input during the SAX parsing process and completely defer HTML output generation to the <a href="http://docs.oracle.com/javase/8/docs/api/org/xml/sax/ContentHandler.html#endDocument--">endDocument()</a> diff --git a/Sda1/Etest/SaxMemo2Html/exercise2.xhtml b/Sda1/Etest/SaxMemo2Html/exercise2.xhtml deleted file mode 100644 index 392b6ec3baadab87969c6ded9981f55ac68d4507..0000000000000000000000000000000000000000 --- a/Sda1/Etest/SaxMemo2Html/exercise2.xhtml +++ /dev/null @@ -1,90 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE html> -<html xmlns="http://www.w3.org/1999/xhtml"> - <head> - <title>Exercise 2</title> - </head> - - <body><h2>Objective</h2><p>Extension of the <code>Book1/book2html.xsl</code> - stylesheet from the previous exercise to accomplish additional - features.</p><h2>Preparations</h2><p>Reuse your current project from the - previous exercise. It contains a second sub folder<code> Book2</code> - offering similar files as before. Configure a second transformation scenario - named book2ToHtml accordingly.</p><h2>Description</h2><p>The current - <code><code>Book2/work.xml</code></code> and <code>Book2/book.xsd</code> - files differ from the previous exercise by:</p><ul> - <li>Allowing <strong>optional</strong> <code>id</code> attributes of - type <code>xs:ID</code> for <code><book></code>, - <code><chapter></code> and <code><para></code> elements</li> - - <li><p>Allowing para elements to be composed of mixed content involving - two new elements:</p><ol> - <li><emphasis> elements to indicate important text - sections.</li> - - <li><code><link linkend="xyz"></code> elements allowing for - document internal links being implemented by <code>xs:ID</code> / - <code>xs:IDREF</code> pairs.</li> - </ol></li> - </ul><pre style="font-family:monospace;"><book ... xsi:noNamespaceSchemaLocation="book.xsd"> - <title>Beginning XSL</title> - <chapter id="firstChapter"> - <title>Basic structure</title> - <para>An XSLT consists of a stylesheet declaration and a set of templates.</para> - <para>Do <emphasis>not forget</emphasis>: template elements must not be nested!</para> - </chapter> - <chapter> - <title>Important elements</title> - <para id="someText">Some more text.</para> - <para>The <link linkend="firstChapter">first chapter</link> explains basic structures.</para> - </chapter> -</book></pre><p>We want to transform instances like the above into HTML - by means of <code>Book2/book2html.xsl</code>. The intended result is being - shown in file <code>Book2/work.reference.html</code>:</p><pre - style="font-family:monospace;"><html> - <head> - <title>Beginning XSL</title> - </head> - <body id="top"> - <h1>Beginning XSL</h1> - <h2 id="firstChapter">Basic structure</h2> - <p>An XSLT consists of a stylesheet declaration and a set of templates.</p> - <p>Do <em>not forget</em>: template elements must not be nested!</p> - <h2>Important elements</h2> - <p id="someText">Some more text.</p> - <p>The <a href="#firstChapter">first chapter</a> explains basic structures.</p> - </body> -</html></pre><p>The following conditions shall be met:</p><ul> - <li><p>Implementing <code>id</code> attributes:</p><ul> - <li><code><book id='xyz'></code> becomes <code><body - id='xyz'></code></li> - - <li><code><chapter id='xyz'></code> becomes <code><h2 - id='xyz'></code></li> - - <li><code><para id='xyz'></code> becomes <code><p - id='xyz'></code></li> - </ul></li> - - <li><p><code>xs:IDREF</code> attributes shall be represented by local - HTML references as shown above in <code><a - href="#firstChapter"></code>.</p></li> - </ul><h2>ToDo</h2><p>Complete the implementation of - <code>Book2/book2html.xsl</code>. You may want to start from your XSL result - of the previous exercise by copying relevant content from - <code>Book1/book2html.xsl</code>.</p><p>Open the unit test file - <code>Book2/bookLinkMixedTest.xspec</code> (which imports all tests from the - previous exercise file <code>Book1/bookBasicTest.xspec</code> as well) and - hit <code>Apply transformation scenario(s) Xspec for XSLT</code> in the - Eclipse tool bar. This allows to continuously check your progress and - reminds you about possible errors. When you are finished upload your final - <code>Book2/book2html.xsl</code> result.</p><h2>Hints:</h2><ul> - <li>Use <code><xsl:attributes></code> to supply <code>id</code> - values.</li> - - <li>Optional <code>id</code> values may be handled either by - <code><xsl:if></code> statements or by <xsl:apply-templates - select='...|@id'> having a corresponding <code><xsl:template - match='@id'></code> counterpart.</li> - </ul></body> -</html>