From 729c9bbc144ea4aeac2410af2824ecadddb8fa5b Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Fri, 5 Jun 2015 21:57:38 +0200 Subject: [PATCH] Activated img alignment example --- Doc/Sda1/sax.xml | 49 +++++++++++ P/Sda1/Alignimg/.gitignore | 1 + P/Sda1/Alignimg/pom.xml | 83 +++---------------- .../hdm_stuttgart/mi/sda1/alignimg/App.java | 67 ++++++++------- .../mi/sda1/alignimg/handler/Img.java | 51 ++++++++++++ .../handler/SearchAlignContentHandler.java | 8 +- .../src/main/resources/testdata.xhtml | 5 +- .../src/test/java/alignimg/TestImgSearch.java | 51 ++++++++++++ P/pom.xml | 1 + 9 files changed, 211 insertions(+), 105 deletions(-) create mode 100644 P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/Img.java create mode 100644 P/Sda1/Alignimg/src/test/java/alignimg/TestImgSearch.java diff --git a/Doc/Sda1/sax.xml b/Doc/Sda1/sax.xml index d6293ef57..e44afcaeb 100644 --- a/Doc/Sda1/sax.xml +++ b/Doc/Sda1/sax.xml @@ -1080,6 +1080,55 @@ public class MemoViewHandler extends DefaultHandler { </qandaset> </section> + <section xml:id="sda1SectImgAlign"> + <title>Filtering <tag class="emptytag">img</tag> elements.</title> + + <qandaset defaultlabel="qanda" xml:id="sda1QandaImgAlign"> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following <xref linkend="glo_XHTML"/> + document instance example:</para> + + <programlisting language="none"><html xmlns='http://www.w3.org/1999/xhtml'> + <head> + <title>A simple image</title> + </head> + <body> + <img src='a.gif' align='top'/> + + <p>Some inline image without alignment <img src="b.gif"/></p> + <p>Some inline image with alignment <img src="c.gif" align="bottom"/></p> + </body> +</html></programlisting> + + <para>This instance contains three <tag + class="emptytag">img</tag> elements. Two of them have an old + style <property>align</property> property. Modern HTML versions + prohibit this usage in favour of CSS <code><img + style="vertical-align: text-top;" /></code>.</para> + + <para>Write an application which produces the following list of + non-conforming images:</para> + + <programlisting language="none">Found image element 'a.gif' having attribute align='top' +Found image element 'c.gif' having attribute align='bottom' +</programlisting> + + <para>Write your application in a testable fashion and provide + unit test(s).</para> + </question> + + <answer> + <annotation role="make"> + <para role="eclipse">Sda1/Alignimg</para> + </annotation> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + <section xml:id="sda1SectFilterImg"> <title>Filtering <tag class="emptytag">img</tag> elements.</title> diff --git a/P/Sda1/Alignimg/.gitignore b/P/Sda1/Alignimg/.gitignore index a1c3ab4d0..963f79fe9 100644 --- a/P/Sda1/Alignimg/.gitignore +++ b/P/Sda1/Alignimg/.gitignore @@ -2,3 +2,4 @@ /.settings/ .classpath .project +A1.log \ No newline at end of file diff --git a/P/Sda1/Alignimg/pom.xml b/P/Sda1/Alignimg/pom.xml index 7676700cf..cdd1d2b3a 100644 --- a/P/Sda1/Alignimg/pom.xml +++ b/P/Sda1/Alignimg/pom.xml @@ -1,84 +1,21 @@ <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> + <parent> + <groupId>de.hdm-stuttgart.mi</groupId> + <artifactId>lecturenotes-pom</artifactId> + <version>1.0</version> + + <relativePath>../../pom.xml</relativePath> + </parent> + <groupId>de.hdm-stuttgart.mi.sda1</groupId> <artifactId>alignimg</artifactId> <version>0.8</version> <packaging>jar</packaging> - <name>alignimg</name> - - <!--Fixme: Add a sensible project related domain here --> - <url>http://somedomain.org</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.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> + <name>Alignimg</name> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-javadoc-plugin</artifactId> - <version>2.10.1</version> - <configuration/> - </plugin> + <url>http://www.mi.hdm-stuttgart.de/freedocs</url> - <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.mi.sda1.alignimg.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/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/App.java b/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/App.java index 0fec57d67..90438db1f 100644 --- a/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/App.java +++ b/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/App.java @@ -11,42 +11,49 @@ import org.apache.logging.log4j.Logger; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; +import de.hdm_stuttgart.mi.sda1.alignimg.handler.Img; import de.hdm_stuttgart.mi.sda1.alignimg.handler.SearchAlignContentHandler; import de.hdm_stuttgart.mi.sda1.alignimg.handler.SaxErrorHandler; - /** * A simple SAX parser demo * */ public class App { - private static final Logger log = LogManager.getLogger(App.class); - - /** - * @param args Unused - * @throws SAXException - * @throws ParserConfigurationException - * @throws IOException - */ - public static void main( String[] args ) throws ParserConfigurationException, SAXException, IOException { - - log.info("Creating SAX parser factory"); - final SAXParserFactory saxPf = SAXParserFactory.newInstance(); - - log.info("Creating SAX parser"); - final SAXParser saxParser = saxPf.newSAXParser(); - final XMLReader xmlReader = saxParser.getXMLReader(); - - log.info("Enabling namespaces and namespace prefix processing"); - xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); - xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); - - log.info("Registering content- and error handler instances"); - xmlReader.setContentHandler(new SearchAlignContentHandler()); - xmlReader.setErrorHandler(new SaxErrorHandler()); - - final String xmlDocumentInstanceFilename = "src/main/resources/testdata.xhtml"; - log.info("Start parsing file '" + xmlDocumentInstanceFilename + "'"); - xmlReader.parse(xmlDocumentInstanceFilename); - } + private static final Logger log = LogManager.getLogger(App.class); + + /** + * @param args + * Unused + * @throws SAXException + * @throws ParserConfigurationException + * @throws IOException + */ + public static void main(String[] args) throws ParserConfigurationException, + SAXException, IOException { + + log.info("Creating SAX parser factory"); + final SAXParserFactory saxPf = SAXParserFactory.newInstance(); + + log.info("Creating SAX parser"); + final SAXParser saxParser = saxPf.newSAXParser(); + final XMLReader xmlReader = saxParser.getXMLReader(); + + log.info("Enabling namespaces and namespace prefix processing"); + xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); + xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); + + log.info("Registering content- and error handler instances"); + final SearchAlignContentHandler searchAlignContentHandler = new SearchAlignContentHandler(); + xmlReader.setContentHandler(searchAlignContentHandler); + xmlReader.setErrorHandler(new SaxErrorHandler()); + + final String xmlDocumentInstanceFilename = "src/main/resources/testdata.xhtml"; + log.info("Start parsing file '" + xmlDocumentInstanceFilename + "'"); + xmlReader.parse(xmlDocumentInstanceFilename); + + for (final Img img : searchAlignContentHandler.alignImages) { + System.out.println("Found " + img); + } + } } diff --git a/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/Img.java b/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/Img.java new file mode 100644 index 000000000..13a244472 --- /dev/null +++ b/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/Img.java @@ -0,0 +1,51 @@ +package de.hdm_stuttgart.mi.sda1.alignimg.handler; + +public class Img { + public final String imgUrl, alignmentValue; + + public Img(final String imgUrl, final String alignmentValue) { + this.imgUrl = imgUrl; + this.alignmentValue = alignmentValue; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((alignmentValue == null) ? 0 : alignmentValue.hashCode()); + result = prime * result + ((imgUrl == null) ? 0 : imgUrl.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj instanceof Img) { + final Img other = (Img) obj; + if (alignmentValue == null) { + if (other.alignmentValue != null) + return false; + } else if (!alignmentValue.equals(other.alignmentValue)) + return false; + if (imgUrl == null) { + if (other.imgUrl != null) + return false; + } else if (!imgUrl.equals(other.imgUrl)) { + return false; + } + return true; + } else { + return false; + } + } + + @Override + public String toString() { + return "image element '" + imgUrl + + "' having attribute align='" + alignmentValue + "'"; + } + + +} \ No newline at end of file diff --git a/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/SearchAlignContentHandler.java b/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/SearchAlignContentHandler.java index 94d816b80..661661dcd 100644 --- a/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/SearchAlignContentHandler.java +++ b/P/Sda1/Alignimg/src/main/java/de/hdm_stuttgart/mi/sda1/alignimg/handler/SearchAlignContentHandler.java @@ -1,5 +1,8 @@ package de.hdm_stuttgart.mi.sda1.alignimg.handler; +import java.util.List; +import java.util.Vector; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.xml.sax.Attributes; @@ -9,6 +12,8 @@ import org.xml.sax.SAXException; public class SearchAlignContentHandler implements ContentHandler { private static final Logger log = LogManager.getLogger(SearchAlignContentHandler.class); + + public final List<Img> alignImages = new Vector<Img>(); Locator locator = null; @@ -20,6 +25,7 @@ public class SearchAlignContentHandler implements ContentHandler { @Override public void startDocument() throws SAXException { log.info(displayWithLocation("Opening document")); + alignImages.clear(); } @Override @@ -45,7 +51,7 @@ public class SearchAlignContentHandler implements ContentHandler { if (qName.equals("img")) { final String alignValue = atts.getValue("align"); if (null != alignValue) { - System.out.println("Found image '" + atts.getValue("src") + "' having attribute align='" + alignValue + "'"); + alignImages.add(new Img(atts.getValue("src"), alignValue)); } } diff --git a/P/Sda1/Alignimg/src/main/resources/testdata.xhtml b/P/Sda1/Alignimg/src/main/resources/testdata.xhtml index 787720cd1..7e3a22b9b 100644 --- a/P/Sda1/Alignimg/src/main/resources/testdata.xhtml +++ b/P/Sda1/Alignimg/src/main/resources/testdata.xhtml @@ -3,6 +3,9 @@ <title>A simple image</title> </head> <body> - <img src="q.gif" align='top'/> + <img src='a.gif' align='top'/> + + <p>Some inline image without alignment <img src="b.gif"/></p> + <p>Some inline image with alignment <img src="c.gif" align="bottom"/></p> </body> </html> \ No newline at end of file diff --git a/P/Sda1/Alignimg/src/test/java/alignimg/TestImgSearch.java b/P/Sda1/Alignimg/src/test/java/alignimg/TestImgSearch.java new file mode 100644 index 000000000..6540a4eeb --- /dev/null +++ b/P/Sda1/Alignimg/src/test/java/alignimg/TestImgSearch.java @@ -0,0 +1,51 @@ +package alignimg; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +import de.hdm_stuttgart.mi.sda1.alignimg.handler.Img; +import de.hdm_stuttgart.mi.sda1.alignimg.handler.SaxErrorHandler; +import de.hdm_stuttgart.mi.sda1.alignimg.handler.SearchAlignContentHandler; + +public class TestImgSearch { + + @Test + public void searchAlignImageElements() throws ParserConfigurationException, + SAXException, IOException { + + final SAXParserFactory saxPf = SAXParserFactory.newInstance(); + + final SAXParser saxParser = saxPf.newSAXParser(); + final XMLReader xmlReader = saxParser.getXMLReader(); + + xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); + xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); + + final SearchAlignContentHandler searchAlignContentHandler = new SearchAlignContentHandler(); + xmlReader.setContentHandler(searchAlignContentHandler); + xmlReader.setErrorHandler(new SaxErrorHandler()); + + final String xmlDocumentInstanceFilename = "src/main/resources/testdata.xhtml"; + xmlReader.parse(xmlDocumentInstanceFilename); + + final Img[] expectedAlignImages = new Img[]{ + new Img("a.gif", "top") + ,new Img("c.gif", "bottom") + }; + // Image count correct? + Assert.assertEquals(expectedAlignImages.length, searchAlignContentHandler.alignImages.size()); + + // Images in list correct? + for (int i = 0; i < expectedAlignImages.length; i++) { + Assert.assertEquals(expectedAlignImages[i], searchAlignContentHandler.alignImages.get(i)); + } + } +} \ No newline at end of file diff --git a/P/pom.xml b/P/pom.xml index ed8e2754a..23714480c 100644 --- a/P/pom.xml +++ b/P/pom.xml @@ -106,6 +106,7 @@ <!-- Structured data and applications 1 --> + <module>Sda1/Alignimg</module> <module>Sda1/ImageSearch</module> <module>Sda1/saxerrorhandler</module> <module>Sda1/catalog2sql</module> -- GitLab