diff --git a/Doc/Sda1/dom.xml b/Doc/Sda1/dom.xml index 320c444a872665f7d5683d5b82e01fbfc14ae398..6ca960ba1424d595d965e7c069be6a430a675e21 100644 --- a/Doc/Sda1/dom.xml +++ b/Doc/Sda1/dom.xml @@ -1,110 +1,110 @@ - <chapter xml:id="dom" version="5.0" xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xmlns:xi="http://www.w3.org/2001/XInclude" - xmlns:svg="http://www.w3.org/2000/svg" - xmlns:m="http://www.w3.org/1998/Math/MathML" - xmlns:html="http://www.w3.org/1999/xhtml" - xmlns:db="http://docbook.org/ns/docbook"> - <title>The Document Object Model (<acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym>)</title> - - <titleabbrev><acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym></titleabbrev> - - <section xml:id="domBase"> - <title>Language independent specification</title> - - <titleabbrev>Language independence</titleabbrev> - - <para>XML documents allow for automated content processing. We already - discussed the <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> API to access XML - documents by <xref linkend="glo_Java"/> applications. - There are however situations where <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> is not - appropriate:</para> - - <itemizedlist> - <listitem> - <para>The <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> is event - based. XML node elements are passed to handler methods. Sometimes - we want to access neighbouring nodes from a context node in our - handler methods for example a <tag class="starttag">title</tag> - following a <tag class="starttag">chapter</tag> node. <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> does not - offer any support for this. If we need references to neighbouring - nodes we have to create them ourselves during a <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> parsing run. - This is tedious and leads to code being hard to understand.</para> - </listitem> - - <listitem> - <para>Some applications may want to select node sets by <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> - expressions which is completely impossible in a <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> - application.</para> - </listitem> - - <listitem> - <para>We may want to move subtrees within a document itself (for - example exchanging two <tag class="starttag">chapter</tag> nodes) - or even transferring them to a different document.</para> - </listitem> - </itemizedlist> - - <para>The greatest deficiency of the <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> is the fact that - an XML instance is not represented as a tree like structure but as a - succession of events. The <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> allows us to - represent XML document instances as tree like structures and thus - enables navigational operations between nodes.</para> - - <para>In order to achieve language <emphasis>and</emphasis> software - vendor independence the <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> approach uses two - stages:</para> - - <itemizedlist> - <listitem> - <para>The <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> is formulated in - an Interface Definition Language (<abbrev - xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev>)</para> - </listitem> - - <listitem> - <para>In order to use the <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> API by a concrete - programming language a so called <emphasis>language - binding</emphasis> is required. In languages like <xref linkend="glo_Java"/> the - language binding will still be a set of <xref linkend="glo_Java"/> - interfaces. Thus for actually coding an application an - implementation of these interfaces is needed</para> - </listitem> - </itemizedlist> - - <para>So what exactly may an <abbrev - xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> - be? The programming language <xref linkend="glo_Java"/> already allows - pure interface definitions without any implementation. In C++ the same - result can be achieved by so called <emphasis>pure virtual - classes</emphasis>. An <abbrev - xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> - offers extended features to describe such interfaces. For <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> the <productname - xlink:href="http://www.omg.org/gettingstarted/corbafaq.htm">CORBA - 2.2</productname> <abbrev - xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> - had been chosen to describe an XML document programming interface. As - a first example we take an excerpt from the <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym>'s <link - xlink:href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247">Node</link> - interface definition:</para> - - <programlisting language="none">interface Node { +<?xml version="1.0" encoding="UTF-8"?> +<chapter version="5.0" xml:id="dom" xmlns="http://docbook.org/ns/docbook" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:xi="http://www.w3.org/2001/XInclude" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns:m="http://www.w3.org/1998/Math/MathML" + xmlns:html="http://www.w3.org/1999/xhtml" + xmlns:db="http://docbook.org/ns/docbook"> + <title>The Document Object Model (<acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym>)</title> + + <titleabbrev><acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym></titleabbrev> + + <section xml:id="domBase"> + <title>Language independent specification</title> + + <titleabbrev>Language independence</titleabbrev> + + <para>XML documents allow for automated content processing. We already + discussed the <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> API to access XML + documents by <xref linkend="glo_Java"/> applications. There are however + situations where <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> is not + appropriate:</para> + + <itemizedlist> + <listitem> + <para>The <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> is event based. + XML node elements are passed to handler methods. Sometimes we want to + access neighbouring nodes from a context node in our handler methods + for example a <tag class="starttag">title</tag> following a <tag + class="starttag">chapter</tag> node. <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> does not offer + any support for this. If we need references to neighbouring nodes we + have to create them ourselves during a <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> parsing run. This + is tedious and leads to code being hard to understand.</para> + </listitem> + + <listitem> + <para>Some applications may want to select node sets by <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> expressions + which is completely impossible in a <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> + application.</para> + </listitem> + + <listitem> + <para>We may want to move subtrees within a document itself (for + example exchanging two <tag class="starttag">chapter</tag> nodes) or + even transferring them to a different document.</para> + </listitem> + </itemizedlist> + + <para>The greatest deficiency of the <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> is the fact that an + XML instance is not represented as a tree like structure but as a + succession of events. The <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> allows us to represent + XML document instances as tree like structures and thus enables + navigational operations between nodes.</para> + + <para>In order to achieve language <emphasis>and</emphasis> software + vendor independence the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> approach uses two + stages:</para> + + <itemizedlist> + <listitem> + <para>The <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> is + formulated in an Interface Definition Language (<abbrev + xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev>)</para> + </listitem> + + <listitem> + <para>In order to use the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> API by a concrete + programming language a so called <emphasis>language binding</emphasis> + is required. In languages like <xref linkend="glo_Java"/> the language + binding will still be a set of <xref linkend="glo_Java"/> interfaces. + Thus for actually coding an application an implementation of these + interfaces is needed</para> + </listitem> + </itemizedlist> + + <para>So what exactly may an <abbrev + xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> + be? The programming language <xref linkend="glo_Java"/> already allows + pure interface definitions without any implementation. In C++ the same + result can be achieved by so called <emphasis>pure virtual + classes</emphasis>. An <abbrev + xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> + offers extended features to describe such interfaces. For <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> the <productname + xlink:href="http://www.omg.org/gettingstarted/corbafaq.htm">CORBA + 2.2</productname> <abbrev + xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> + had been chosen to describe an XML document programming interface. As a + first example we take an excerpt from the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym>'s <link + xlink:href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247">Node</link> + interface definition:</para> + + <programlisting language="none">interface Node { // NodeType const unsigned short ELEMENT_NODE = 1; const unsigned short ATTRIBUTE_NODE = 2; @@ -126,20 +126,20 @@ raises(DOMException); ...</programlisting> - <para>If we want to implement the <abbrev - xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> - <classname>org.w3c.dom.Node</classname> specification in e.g. <xref linkend="glo_Java"/> a language - binding has to be defined. This means writing <xref linkend="glo_Java"/> code which - closely resembles the <abbrev - xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> - specification. Obviously this task depends on and is restricted by the - constructs being offered by the target programming language. The W3C - <link - xlink:href="http://www.w3.org/TR/DOM-Level-3-Core/java-binding.html">defines</link> - the <xref linkend="glo_Java"/> - <classname>org.w3c.dom.Node</classname> interface by:</para> - - <programlisting language="none">package org.w3c.dom; + <para>If we want to implement the <abbrev + xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> + <classname>org.w3c.dom.Node</classname> specification in e.g. <xref + linkend="glo_Java"/> a language binding has to be defined. This means + writing <xref linkend="glo_Java"/> code which closely resembles the + <abbrev + xlink:href="http://en.wikipedia.org/wiki/Interface_description_language">IDL</abbrev> + specification. Obviously this task depends on and is restricted by the + constructs being offered by the target programming language. The W3C <link + xlink:href="http://www.w3.org/TR/DOM-Level-3-Core/java-binding.html">defines</link> + the <xref linkend="glo_Java"/> <classname>org.w3c.dom.Node</classname> + interface by:</para> + + <programlisting language="none">package org.w3c.dom; public interface Node { public static final short ELEMENT_NODE = 1; // Node Types @@ -160,139 +160,136 @@ public interface Node { ... }</programlisting> - <para>We take - <methodname>org.w3c.dom.Node.getChildNodes()</methodname> as an - example:</para> - - <figure xml:id="domRetrieveChildren"> - <title>Retrieving child nodes of a given context node</title> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/domtree.fig" scale="65"/> - </imageobject> - </mediaobject> - </figure> - - <para>The <classname>org.w3c.dom.Node</classname> interface offers a - set of common operations for objects being part of a XML document. But - a XML document tree contains different types of nodes such as:</para> - - <itemizedlist> - <listitem> - <para>Elements</para> - </listitem> - - <listitem> - <para>Attributes</para> - </listitem> - - <listitem> - <para>Entities</para> - </listitem> - </itemizedlist> - - <para>An XML API may address this issue by offering data types to - represent these different kinds of nodes. The <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> <xref linkend="glo_Java"/> Binding - defines an inheritance hierarchy of interfaces for this - purpose:</para> - - <figure xml:id="domJavaNodeInterfaces"> - <title>Inheritance interface hierarchy in the <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> <xref linkend="glo_Java"/> - binding</title> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/nodeHierarchy.svg"/> - </imageobject> - </mediaobject> - </figure> - - <para>Two commonly used <xref linkend="glo_Java"/> - implementations of these interfaces are:</para> - - <variablelist> - <varlistentry> - <term>Xerces</term> - - <listitem> - <para><orgname - xlink:href="http://xml.apache.org/xerces2-j">Apache Software - foundation</orgname></para> - </listitem> - </varlistentry> - - <varlistentry> - <term>Jaxp</term> - - <listitem> - <para><orgname xlink:href="http://java.sun.com/xml/jaxp">Sun - microsystems</orgname></para> - </listitem> - </varlistentry> - </variablelist> - - <para>Both implementations offer additional interfaces beyond the - <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym>'s - scope.</para> - - <para>Going back to the <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> itself the - specification is divided into <link - xlink:href="http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/introduction.html#DOMArchitecture-h2">modules</link>:</para> - - <figure xml:id="figureDomModules"> - <title><acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> - modules.</title> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Screen/dom-architecture.screen.png"/> - </imageobject> - </mediaobject> - </figure> - </section> - - <section xml:id="domCreate"> - <title>Creating a new document from scratch</title> - - <titleabbrev>New document</titleabbrev> - - <para>If we want to export non-XML content (e.g. from a RDBMS) into - XML we may achieve this by the following recipe:</para> - - <orderedlist> - <listitem> - <para>Create a document builder instance.</para> - </listitem> - - <listitem> - <para>Create an empty <link - xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html">Document</link> - instance.</para> - </listitem> - - <listitem> - <para>Fill in the desired Elements and Attributes.</para> - </listitem> - - <listitem> - <para>Create a serializer.</para> - </listitem> - - <listitem> - <para>Serialize the resulting tree to a stream.</para> - </listitem> - </orderedlist> - - <para>An introductory piece of code illustrates these steps:</para> - - <figure xml:id="simpleDomCreate"> - <title>Creation of a XML document instance from scratch.</title> - - <programlisting language="none">package dom; + <para>We take <methodname>org.w3c.dom.Node.getChildNodes()</methodname> as + an example:</para> + + <figure xml:id="domRetrieveChildren"> + <title>Retrieving child nodes of a given context node</title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/domtree.fig" scale="65"/> + </imageobject> + </mediaobject> + </figure> + + <para>The <classname>org.w3c.dom.Node</classname> interface offers a set + of common operations for objects being part of a XML document. But a XML + document tree contains different types of nodes such as:</para> + + <itemizedlist> + <listitem> + <para>Elements</para> + </listitem> + + <listitem> + <para>Attributes</para> + </listitem> + + <listitem> + <para>Entities</para> + </listitem> + </itemizedlist> + + <para>An XML API may address this issue by offering data types to + represent these different kinds of nodes. The <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> <xref + linkend="glo_Java"/> Binding defines an inheritance hierarchy of + interfaces for this purpose:</para> + + <figure xml:id="domJavaNodeInterfaces"> + <title>Inheritance interface hierarchy in the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> <xref + linkend="glo_Java"/> binding</title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/nodeHierarchy.svg"/> + </imageobject> + </mediaobject> + </figure> + + <para>Two commonly used <xref linkend="glo_Java"/> implementations of + these interfaces are:</para> + + <variablelist> + <varlistentry> + <term>Xerces</term> + + <listitem> + <para><orgname xlink:href="http://xml.apache.org/xerces2-j">Apache + Software foundation</orgname></para> + </listitem> + </varlistentry> + + <varlistentry> + <term>Jaxp</term> + + <listitem> + <para><orgname xlink:href="http://java.sun.com/xml/jaxp">Sun + microsystems</orgname></para> + </listitem> + </varlistentry> + </variablelist> + + <para>Both implementations offer additional interfaces beyond the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym>'s scope.</para> + + <para>Going back to the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> itself the specification + is divided into <link + xlink:href="http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/introduction.html#DOMArchitecture-h2">modules</link>:</para> + + <figure xml:id="figureDomModules"> + <title><acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> + modules.</title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Screen/dom-architecture.screen.png"/> + </imageobject> + </mediaobject> + </figure> + </section> + + <section xml:id="domCreate"> + <title>Creating a new document from scratch</title> + + <titleabbrev>New document</titleabbrev> + + <para>If we want to export non-XML content (e.g. from a RDBMS) into XML we + may achieve this by the following recipe:</para> + + <orderedlist> + <listitem> + <para>Create a document builder instance.</para> + </listitem> + + <listitem> + <para>Create an empty <link + xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html">Document</link> + instance.</para> + </listitem> + + <listitem> + <para>Fill in the desired Elements and Attributes.</para> + </listitem> + + <listitem> + <para>Create a serializer.</para> + </listitem> + + <listitem> + <para>Serialize the resulting tree to a stream.</para> + </listitem> + </orderedlist> + + <para>An introductory piece of code illustrates these steps:</para> + + <figure xml:id="simpleDomCreate"> + <title>Creation of a XML document instance from scratch.</title> + + <programlisting language="none">package dom; ... public class CreateDoc { public static void main(String[] args) throws Exception { @@ -315,45 +312,45 @@ public class CreateDoc { printer.output(titel, System.out);</emphasis> } }</programlisting> - </figure> + </figure> - <para>We get the following result:</para> + <para>We get the following result:</para> - <programlisting language="none"><?xml version="1.0" encoding="UTF-8"?> + <programlisting language="none"><?xml version="1.0" encoding="UTF-8"?> <titel date="23.02.2000">Versuch 1</titel></programlisting> - </section> + </section> - <section xml:id="domCreateExercises"> - <title>Exercises</title> + <section xml:id="domCreateExercises"> + <title>Exercises</title> - <qandaset defaultlabel="qanda" xml:id="createDocModify"> - <title>A sub structured <tag class="starttag">title</tag></title> + <qandaset defaultlabel="qanda" xml:id="createDocModify"> + <title>A sub structured <tag class="starttag">title</tag></title> - <qandadiv> - <qandaentry> - <question> - <label>Creation of an extended XML document instance</label> + <qandadiv> + <qandaentry> + <question> + <label>Creation of an extended XML document instance</label> - <para>In order to run the examples given during the lecture - the <filename - xlink:href="http://www.jdom.org/downloads">jdom2.jar</filename> - library must be added to the <envar>CLASSPATH</envar>.</para> + <para>In order to run the examples given during the lecture the + <filename + xlink:href="http://www.jdom.org/downloads">jdom2.jar</filename> + library must be added to the <envar>CLASSPATH</envar>.</para> - <para>The <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> creating - example given before may be used as a starting point. Extend - the <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> - tree created in <xref linkend="simpleDomCreate"/> to produce - an extended XML document:</para> + <para>The <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> creating example + given before may be used as a starting point. Extend the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> tree created in + <xref linkend="simpleDomCreate"/> to produce an extended XML + document:</para> - <programlisting language="none"><title> + <programlisting language="none"><title> <long>The long version of this title</long> <short>Short version</short> </title></programlisting> - </question> + </question> - <answer> - <programlisting language="none">package dom; + <answer> + <programlisting language="none">package dom; ... public class CreateExtended { /** @@ -381,54 +378,52 @@ public class CreateExtended { printer.output(titel, System.out); } }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="domParse"> - <title>Parsing existing XML documents</title> - - <titleabbrev>Parsing</titleabbrev> - - <para>We already used a <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> to parse an XML - document. Rather than handling <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> events ourselves - these events may be used to construct a <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> representation of our - document. This work is done by an instance of. We use our catalog - example from <xref linkend="simpleCatalog"/> as an introductory - example.</para> - - <para>We already noticed the need for an - <classname>org.xml.sax.ErrorHandler</classname> object during <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> processing. A - <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> Parser - requires a similar type of Object in order to react to parsing errors - in a meaningful way. In principle a <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> parser implementor is - free to choose his implementation but most implementations are based - on top of a <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> parser. For this - reason it was natural to choose a <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> error handling - interface which is similar to a <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> - <classname>org.xml.sax.ErrorHandler</classname>. The following code - serves the needs described before:</para> - - <figure xml:id="domTreeTraversal"> - <title>Accessing a XML Tree purely by <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> methods.</title> - - <programlisting language="none">package dom; + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="domParse"> + <title>Parsing existing XML documents</title> + + <titleabbrev>Parsing</titleabbrev> + + <para>We already used a <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> to parse an XML + document. Rather than handling <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> events ourselves + these events may be used to construct a <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> representation of our + document. This work is done by an instance of. We use our catalog example + from <xref linkend="simpleCatalog"/> as an introductory example.</para> + + <para>We already noticed the need for an + <classname>org.xml.sax.ErrorHandler</classname> object during <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> processing. A + <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> Parser requires + a similar type of Object in order to react to parsing errors in a + meaningful way. In principle a <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> parser implementor is + free to choose his implementation but most implementations are based on + top of a <acronym xlink:href="http://www.saxproject.org">SAX</acronym> + parser. For this reason it was natural to choose a <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> error handling interface + which is similar to a <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> + <classname>org.xml.sax.ErrorHandler</classname>. The following code serves + the needs described before:</para> + + <figure xml:id="domTreeTraversal"> + <title>Accessing a XML Tree purely by <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> methods.</title> + + <programlisting language="none">package dom; ... public class ArticleOrder { <emphasis role="bold"> // Though we are playing DOM here, a <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> parser still + xlink:href="http://www.saxproject.org">SAX</acronym> parser still // assembles our DOM tree.</emphasis> private SAXBuilder builder = new SAXBuilder(); @@ -436,8 +431,7 @@ public class ArticleOrder { <emphasis role="bold">// Though an ErrorHandler is not strictly required it allows // for easierlocalization of XML document errors</emphasis> builder.setErrorHandler(new MySaxErrorHandler(System.out));<co - linkends="domSetSaxErrorHandler-co" - xml:id="domSetSaxErrorHandler"/> + linkends="domSetSaxErrorHandler-co" xml:id="domSetSaxErrorHandler"/> } /** Descending a catalog till its <item> elements. For each product @@ -459,17 +453,17 @@ public class ArticleOrder { + ", order number: " + item.getAttributeValue("orderNo")); } ...</programlisting> - <para>Note <coref linkend="domSetSaxErrorHandler" - xml:id="domSetSaxErrorHandler-co"/>: This is our standard <acronym - xlink:href="http://www.saxproject.org">SAX</acronym> error handler - implementing the <classname>org.xml.sax.ErrorHandler</classname> - interface.</para> - </figure> + <para>Note <coref linkend="domSetSaxErrorHandler" + xml:id="domSetSaxErrorHandler-co"/>: This is our standard <acronym + xlink:href="http://www.saxproject.org">SAX</acronym> error handler + implementing the <classname>org.xml.sax.ErrorHandler</classname> + interface.</para> + </figure> - <para>Executing this method needs a driver instance providing an input - XML filename:</para> + <para>Executing this method needs a driver instance providing an input XML + filename:</para> - <programlisting language="none">package dom; + <programlisting language="none">package dom; ... public class ArticleOrderDriver { public static void main(String[] argv) throws Exception { @@ -478,33 +472,33 @@ public class ArticleOrderDriver { } }</programlisting> - <para>This yields:</para> + <para>This yields:</para> - <programlisting language="none">Article: Swinging headset, order number: 3218 + <programlisting language="none">Article: Swinging headset, order number: 3218 Article: 200W Stereo Amplifier, order number: 9921</programlisting> - <para>To illustrate the internal processes we take a look at the - sequence diagram:</para> + <para>To illustrate the internal processes we take a look at the sequence + diagram:</para> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/sequenceDomParser.svg"/> - </imageobject> - </mediaobject> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/sequenceDomParser.svg"/> + </imageobject> + </mediaobject> - <qandaset defaultlabel="qanda" xml:id="exercise_domHtmlSimple"> - <title>Creating HTML output</title> + <qandaset defaultlabel="qanda" xml:id="exercise_domHtmlSimple"> + <title>Creating HTML output</title> - <qandadiv> - <qandaentry> - <question> - <label>Simple HTML output</label> + <qandadiv> + <qandaentry> + <question> + <label>Simple HTML output</label> - <para>Instead exporting simple text output in <xref - linkend="domTreeTraversal"/> we may also create HTML pages - like:</para> + <para>Instead exporting simple text output in <xref + linkend="domTreeTraversal"/> we may also create HTML pages + like:</para> - <programlisting language="none"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> + <programlisting language="none"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Available articles</title> @@ -518,37 +512,36 @@ Article: 200W Stereo Amplifier, order number: 9921</programlisting> </tr> <tr> <td align="left"><emphasis role="bold">Swinging headset</emphasis></td><td><emphasis - role="bold">3218</emphasis></td> + role="bold">3218</emphasis></td> </tr> <tr> <td align="left"><emphasis role="bold">200W Stereo Amplifier</emphasis></td><td><emphasis - role="bold">9921</emphasis></td> + role="bold">9921</emphasis></td> </tr> </tbody> </table> </body> </html></programlisting> - <para>Instead of simply writing - <code>...println(<html>\n\t<head>...)</code> - statements you are expected to code a more sophisticated - solution. We may combine<xref linkend="createDocModify"/> and - <xref linkend="createDocModify"/>. The idea is reading the XML - catalog instance as a <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> as before. - Then construct a <emphasis>second</emphasis> <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> tree for the - desired HTML output and fill in the article information from - the first <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> tree - accordingly.</para> - </question> - - <answer> - <para>We introduce a class - <classname>solve.dom.HtmlTree</classname>:</para> - - <programlisting language="none">package solve.dom; + <para>Instead of simply writing + <code>...println(<html>\n\t<head>...)</code> + statements you are expected to code a more sophisticated solution. + We may combine<xref linkend="createDocModify"/> and <xref + linkend="createDocModify"/>. The idea is reading the XML catalog + instance as a <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> as before. Then + construct a <emphasis>second</emphasis> <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> tree for the + desired HTML output and fill in the article information from the + first <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> + tree accordingly.</para> + </question> + + <answer> + <para>We introduce a class + <classname>solve.dom.HtmlTree</classname>:</para> + + <programlisting language="none">package solve.dom; ... package solve.dom; @@ -573,16 +566,16 @@ public class HtmlTree { public HtmlTree(final String titleText, final String[] tableHeaderFields) { <co - linkends="programlisting_catalog2html_htmlskel_co" - xml:id="programlisting_catalog2html_htmlskel"/> + linkends="programlisting_catalog2html_htmlskel_co" + xml:id="programlisting_catalog2html_htmlskel"/> DocType doctype = new DocType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"); final Element htmlRoot = new Element("html"); <co - linkends="programlisting_catalog2html_tablehead_co" - xml:id="programlisting_catalog2html_tablehead"/> + linkends="programlisting_catalog2html_tablehead_co" + xml:id="programlisting_catalog2html_tablehead"/> htmlOutput = new Document(htmlRoot); htmlOutput.setDocType(doctype); @@ -610,8 +603,8 @@ public class HtmlTree { } public void appendItem(final String itemName, final String orderNo) {<co - linkends="programlisting_catalog2html_insertproduct_co" - xml:id="programlisting_catalog2html_insertproduct"/> + linkends="programlisting_catalog2html_insertproduct_co" + xml:id="programlisting_catalog2html_insertproduct"/> final Element tr = new Element("tr"); tableBody.addContent(tr); tr.addContent(new Element("td").addContent(new Text(itemName))); @@ -641,12 +634,12 @@ public class HtmlTree { </programlisting> - <calloutlist> - <callout arearefs="programlisting_catalog2html_htmlskel" - xml:id="programlisting_catalog2html_htmlskel_co"> - <para>A basic HTML skeleton is is being created:</para> + <calloutlist> + <callout arearefs="programlisting_catalog2html_htmlskel" + xml:id="programlisting_catalog2html_htmlskel_co"> + <para>A basic HTML skeleton is is being created:</para> - <programlisting language="none"><?xml version="1.0" encoding="UTF-8"?> + <programlisting language="none"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> @@ -662,16 +655,16 @@ public class HtmlTree { </body> </html></programlisting> - <para>The table containing the product's data is empty at - this point and thus invalid.</para> - </callout> + <para>The table containing the product's data is empty at this + point and thus invalid.</para> + </callout> - <callout arearefs="programlisting_catalog2html_tablehead" - xml:id="programlisting_catalog2html_tablehead_co"> - <para>The table's header is appended but the actual data - from our two products is still missing:</para> + <callout arearefs="programlisting_catalog2html_tablehead" + xml:id="programlisting_catalog2html_tablehead_co"> + <para>The table's header is appended but the actual data from + our two products is still missing:</para> - <programlisting language="none">... <h1>Available articles</h1> + <programlisting language="none">... <h1>Available articles</h1> <table> <tbody> <tr> @@ -680,16 +673,16 @@ public class HtmlTree { <emphasis role="bold"></tr></emphasis><!-- Data to be appended after this row in next step --> <emphasis role="bold"></tbody></emphasis> </table> ...</programlisting> - </callout> + </callout> - <callout arearefs="programlisting_catalog2html_insertproduct" - xml:id="programlisting_catalog2html_insertproduct_co"> - <para>Calling - <methodname>solve.dom.HtmlTree.appendItem(String,String)</methodname> - once per product completes the creation of our HTML DOM - tree:</para> + <callout arearefs="programlisting_catalog2html_insertproduct" + xml:id="programlisting_catalog2html_insertproduct_co"> + <para>Calling + <methodname>solve.dom.HtmlTree.appendItem(String,String)</methodname> + once per product completes the creation of our HTML DOM + tree:</para> - <programlisting language="none">... </tr> + <programlisting language="none">... </tr> <tr> <td>Swinging headset</td> <td>3218</td> @@ -699,13 +692,13 @@ public class HtmlTree { <td>9921</td> </tr> </tbody> ...</programlisting> - </callout> - </calloutlist> + </callout> + </calloutlist> - <para>The class <classname>solve.dom.Article2Html</classname> - reads the catalog data:</para> + <para>The class <classname>solve.dom.Article2Html</classname> + reads the catalog data:</para> - <programlisting language="none">package solve.dom; + <programlisting language="none">package solve.dom; ... public class Article2Html { @@ -717,8 +710,8 @@ public class Article2Html { builder.setErrorHandler(new MySaxErrorHandler(System.out)); htmlResult = new HtmlTree("Available articles", new String[] { <co - linkends="programlisting_catalog2html_glue_createhtmldom_co" - xml:id="programlisting_catalog2html_glue_createhtmldom"/> + linkends="programlisting_catalog2html_glue_createhtmldom_co" + xml:id="programlisting_catalog2html_glue_createhtmldom"/> "Article Description", "Order Number" }); } @@ -737,122 +730,120 @@ public class Article2Html { builder.build(filename).getRootElement().getChildren(); for (final Element item : items) { <co - linkends="programlisting_catalog2html_glue_prodloop_co" - xml:id="programlisting_catalog2html_glue_prodloop"/> + linkends="programlisting_catalog2html_glue_prodloop_co" + xml:id="programlisting_catalog2html_glue_prodloop"/> htmlResult.appendItem(item.getText(), item.getAttributeValue("orderNo")); <co - linkends="programlisting_catalog2html_glue_insertprod_co" - xml:id="programlisting_catalog2html_glue_insertprod"/> + linkends="programlisting_catalog2html_glue_insertprod_co" + xml:id="programlisting_catalog2html_glue_insertprod"/> } htmlResult.serialize(out); <co - linkends="programlisting_catalog2html_glue_serialize_co" - xml:id="programlisting_catalog2html_glue_serialize"/> + linkends="programlisting_catalog2html_glue_serialize_co" + xml:id="programlisting_catalog2html_glue_serialize"/> } }</programlisting> - <calloutlist> - <callout arearefs="programlisting_catalog2html_glue_createhtmldom" - xml:id="programlisting_catalog2html_glue_createhtmldom_co"> - <para>Create an instance holding a HTML <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> with a - table header containing the strings <emphasis>Article - Description</emphasis> and <emphasis>Order - Number</emphasis>.</para> - </callout> - - <callout arearefs="programlisting_catalog2html_glue_prodloop" - xml:id="programlisting_catalog2html_glue_prodloop_co"> - <para>Iterate over all product nodes.</para> - </callout> - - <callout arearefs="programlisting_catalog2html_glue_insertprod" - xml:id="programlisting_catalog2html_glue_insertprod_co"> - <para>Insert the product's name an order number into the - HTML <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym>.</para> - </callout> - - <callout arearefs="programlisting_catalog2html_glue_serialize" - xml:id="programlisting_catalog2html_glue_serialize_co"> - <para>Serialize the completed HTML <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> tree to - the output stream.</para> - </callout> - </calloutlist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="domJavaScript"> - <title>Using <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> - with HTML/Javascript</title> - - <para>Due to script language support in a variety of browsers we may - also use the <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> - to implement client side event handling. As an example we <link - xlink:href="Sda1/Ref/src/tablesort.html">demonstrate</link> how a HTML - table can be made sortable by clicking on a header's column. The - example code along with the code description can be found at <uri - xlink:href="http://www.kryogenix.org/code/browser/sorttable">http://www.kryogenix.org/code/browser/sorttable</uri>.</para> - - <para>Quite remarkably there are only few ingredients required to - enrich an ordinary static HTML table with this functionality:</para> + <calloutlist> + <callout arearefs="programlisting_catalog2html_glue_createhtmldom" + xml:id="programlisting_catalog2html_glue_createhtmldom_co"> + <para>Create an instance holding a HTML <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> with a table + header containing the strings <emphasis>Article + Description</emphasis> and <emphasis>Order + Number</emphasis>.</para> + </callout> + + <callout arearefs="programlisting_catalog2html_glue_prodloop" + xml:id="programlisting_catalog2html_glue_prodloop_co"> + <para>Iterate over all product nodes.</para> + </callout> + + <callout arearefs="programlisting_catalog2html_glue_insertprod" + xml:id="programlisting_catalog2html_glue_insertprod_co"> + <para>Insert the product's name an order number into the HTML + <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym>.</para> + </callout> + + <callout arearefs="programlisting_catalog2html_glue_serialize" + xml:id="programlisting_catalog2html_glue_serialize_co"> + <para>Serialize the completed HTML <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> tree to the + output stream.</para> + </callout> + </calloutlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="domJavaScript"> + <title>Using <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> + with HTML/Javascript</title> + + <para>Due to script language support in a variety of browsers we may also + use the <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> to + implement client side event handling. As an example we <link + xlink:href="Sda1/Ref/src/tablesort.html">demonstrate</link> how a HTML + table can be made sortable by clicking on a header's column. The example + code along with the code description can be found at <uri + xlink:href="http://www.kryogenix.org/code/browser/sorttable">http://www.kryogenix.org/code/browser/sorttable</uri>.</para> + + <para>Quite remarkably there are only few ingredients required to enrich + an ordinary static HTML table with this functionality:</para> + + <itemizedlist> + <listitem> + <para>An external Javascript library has to be included via + <code><script type="text/javascript" + src="sorttable.js"></code></para> + </listitem> + + <listitem> + <para>Each sortable HTML table needs:</para> <itemizedlist> <listitem> - <para>An external Javascript library has to be included via - <code><script type="text/javascript" - src="sorttable.js"></code></para> + <para>A unique <code>id</code> attribute</para> </listitem> <listitem> - <para>Each sortable HTML table needs:</para> - - <itemizedlist> - <listitem> - <para>A unique <code>id</code> attribute</para> - </listitem> - - <listitem> - <para>A <code>class="sortable"</code> attribute</para> - </listitem> - </itemizedlist> + <para>A <code>class="sortable"</code> attribute</para> </listitem> </itemizedlist> - </section> - - <section xml:id="domXpath"> - <title>Using <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym></title> - - <para><xref linkend="domTreeTraversal"/> demonstrated the possibility - to traverse trees solely by using <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> Method calls. Though - this approach is possible it will in general not lead to stable - applications. Real world examples are often based on large XML - documents with complex hierarchical structures. Thus using this rather - primitive approach will foster deeply nested method calls being - necessary to access desired node sets. In addition changing the - conceptional schema will require rewriting large code - portions..</para> - - <para>As we already know from <abbrev - xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> transformations - <code>Xpath</code> allows to address node sets inside a XML tree. The - role of <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> can be - compared to SQL queries when working with relational databases. - <acronym xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> may - also be used within <xref linkend="glo_Java"/> code. As a - first example we show an image filename extracting application - operating on XHTML documents. The following example contains three - <tag class="starttag">img</tag> elements:</para> - - <figure xml:id="htmlGallery"> - <title>A HTML document containing <code>IMG</code> tags.</title> - - <programlisting language="none"><?xml version="1.0"?> + </listitem> + </itemizedlist> + </section> + + <section xml:id="domXpath"> + <title>Using <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym></title> + + <para><xref linkend="domTreeTraversal"/> demonstrated the possibility to + traverse trees solely by using <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> Method calls. Though this + approach is possible it will in general not lead to stable applications. + Real world examples are often based on large XML documents with complex + hierarchical structures. Thus using this rather primitive approach will + foster deeply nested method calls being necessary to access desired node + sets. In addition changing the conceptional schema will require rewriting + large code portions..</para> + + <para>As we already know from <abbrev + xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> transformations + <code>Xpath</code> allows to address node sets inside a XML tree. The role + of <acronym xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> can be + compared to SQL queries when working with relational databases. <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> may also be used + within <xref linkend="glo_Java"/> code. As a first example we show an + image filename extracting application operating on XHTML documents. The + following example contains three <tag class="starttag">img</tag> + elements:</para> + + <figure xml:id="htmlGallery"> + <title>A HTML document containing <code>IMG</code> tags.</title> + + <programlisting language="none"><?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> @@ -877,21 +868,20 @@ public class Article2Html { </body> </html> </programlisting> - </figure> + </figure> - <para>A given HTML document may contain <tag - class="starttag">img</tag> elements at <emphasis>arbitrary</emphasis> - positions. It is sometimes desirable to check for existence and - accessibility of such external objects being necessary for the page's - correct rendering. A simple XSL script will do first part the job - namely extracting the <tag class="starttag">img</tag> elements:</para> + <para>A given HTML document may contain <tag class="starttag">img</tag> + elements at <emphasis>arbitrary</emphasis> positions. It is sometimes + desirable to check for existence and accessibility of such external + objects being necessary for the page's correct rendering. A simple XSL + script will do first part the job namely extracting the <tag + class="starttag">img</tag> elements:</para> - <figure xml:id="gallery2imagelist"> - <title>A <abbrev - xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> script for - image name extraction.</title> + <figure xml:id="gallery2imagelist"> + <title>A <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> + script for image name extraction.</title> - <programlisting language="none"><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + <programlisting language="none"><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml"> <xsl:output method="text"/> @@ -903,38 +893,37 @@ public class Article2Html { </xsl:template> </xsl:stylesheet></programlisting> - </figure> - - <para>Note the necessity for <code>html</code> namespace inclusion - into the <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> expression in - <code><xsl:for-each select="//html:img"></code>. A simple - <code>select="//img"></code> results in an empty node set. - Executing the <abbrev - xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> script yields a - list of image filenames being contained in the HTML page i.e. - <code>inline.gif one.gif two.gif</code>.</para> - - <para>Now we want to write a <xref linkend="glo_Java"/> application - which allows to check whether these referenced image files do exist - and have sufficient permissions to be accessed. A simple approach may - pipe the <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> - output to our application which then executes the readability checks. - Instead we want to incorporate the <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> based search - into the application. Ignoring Namespaces and trying to resemble the - <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> actions - as closely as possible our application will have to search for <link - xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Element.html">Element</link> - Nodes by the <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> expression - <code>//html:img</code>:</para> - - <figure xml:id="domFindImages"> - <title>Extracting <tag class="emptytag">img</tag> element image - references from a HTML document.</title> - - <programlisting language="none">package dom.xpath; + </figure> + + <para>Note the necessity for <code>html</code> namespace inclusion into + the <acronym xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> + expression in <code><xsl:for-each select="//html:img"></code>. A + simple <code>select="//img"></code> results in an empty node set. + Executing the <abbrev + xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> script yields a list + of image filenames being contained in the HTML page i.e. <code>inline.gif + one.gif two.gif</code>.</para> + + <para>Now we want to write a <xref linkend="glo_Java"/> application which + allows to check whether these referenced image files do exist and have + sufficient permissions to be accessed. A simple approach may pipe the + <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> output to + our application which then executes the readability checks. Instead we + want to incorporate the <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> based search into + the application. Ignoring Namespaces and trying to resemble the <abbrev + xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> actions as closely + as possible our application will have to search for <link + xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Element.html">Element</link> + Nodes by the <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> expression + <code>//html:img</code>:</para> + + <figure xml:id="domFindImages"> + <title>Extracting <tag class="emptytag">img</tag> element image + references from a HTML document.</title> + + <programlisting language="none">package dom.xpath; ... public class DomXpath { private final SAXBuilder builder = new SAXBuilder(); @@ -945,117 +934,148 @@ public class DomXpath { public void process(final String xhtmlFilename) throws JDOMException, IOException { final Document htmlInput = builder.build(xhtmlFilename);<co - linkends="programlisting_java_searchimg_parse_co" - xml:id="programlisting_java_searchimg_parse"/> + linkends="programlisting_java_searchimg_parse_co" + xml:id="programlisting_java_searchimg_parse"/> final XPathExpression<Object> xpath = XPathFactory.instance().compile( "//img" ); <co - linkends="programlisting_java_searchimg_pf_co" - xml:id="programlisting_java_searchimg_pf"/> <co - linkends="programlisting_java_searchimg_newxpath_co" - xml:id="programlisting_java_searchimg_newxpath"/> + linkends="programlisting_java_searchimg_pf_co" + xml:id="programlisting_java_searchimg_pf"/> <co + linkends="programlisting_java_searchimg_newxpath_co" + xml:id="programlisting_java_searchimg_newxpath"/> final List<Object> images = xpath.evaluate(htmlInput);<co - linkends="programlisting_java_searchimg_execquery_co" - xml:id="programlisting_java_searchimg_execquery"/> + linkends="programlisting_java_searchimg_execquery_co" + xml:id="programlisting_java_searchimg_execquery"/> for (Object o: images) { <co - linkends="programlisting_java_searchimg_loop_co" - xml:id="programlisting_java_searchimg_loop"/> + linkends="programlisting_java_searchimg_loop_co" + xml:id="programlisting_java_searchimg_loop"/> final Element image = (Element ) o;<co - linkends="programlisting_java_searchimg_cast_co" - xml:id="programlisting_java_searchimg_cast"/> + linkends="programlisting_java_searchimg_cast_co" + xml:id="programlisting_java_searchimg_cast"/> System.out.print(image.getAttribute("src") + " "); } } }</programlisting> - <caption> - <para>This application searches for <tag - class="emptytag">img</tag> elements and shows their - <code>src</code> attribute value.</para> - </caption> - </figure> - - <calloutlist> - <callout arearefs="programlisting_java_searchimg_parse" - xml:id="programlisting_java_searchimg_parse_co"> - <para>Parse a XHTML document instance into a DOM tree.</para> - </callout> - - <callout arearefs="programlisting_java_searchimg_pf" - xml:id="programlisting_java_searchimg_pf_co"> - <para>Create a <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> - factory.</para> - </callout> - - <callout arearefs="programlisting_java_searchimg_newxpath" - xml:id="programlisting_java_searchimg_newxpath_co"> - <para>Create a <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> query - instance. This may be used to search for a set of nodes starting - from a context node.</para> - </callout> - - <callout arearefs="programlisting_java_searchimg_execquery" - xml:id="programlisting_java_searchimg_execquery_co"> - <para>Using the document's root node as the context node we search - for <tag class="starttag">img</tag> elements appearing at - arbitrary positions in our document.</para> - </callout> - - <callout arearefs="programlisting_java_searchimg_loop" - xml:id="programlisting_java_searchimg_loop_co"> - <para>We iterate over the retrieved list of images.</para> - </callout> - - <callout arearefs="programlisting_java_searchimg_cast" - xml:id="programlisting_java_searchimg_cast_co"> - <para>Casting to the correct type.</para> - </callout> - </calloutlist> - - <para>The result is a list of image filename references:</para> - - <programlisting language="none">inline.gif one.gif http://www.hdm-stuttgart.de/favicon.ico </programlisting> - - <qandaset defaultlabel="qanda" xml:id="quandaentry_CastAlwaysLegal"> - <title>Legal casting?</title> - - <qandadiv> - <qandaentry> - <question> - <para>Why is the cast in <coref - linkend="programlisting_java_searchimg_cast"/> in <xref - linkend="domFindImages"/> guaranteed to never cause a - <classname>java.lang.ClassCastException</classname>?</para> - </question> - - <answer> - <para>The <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> - <code>//img</code> expression is guaranteed to return only - <tag class="starttag">img</tag> elements. Thus within our - <xref linkend="glo_Java"/> - context we are sure to find only - <classname>org.jdom2.Element</classname> instances.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - - <qandaset defaultlabel="qanda" xml:id="exercise_htmlImageVerify"> - <title>Verification of referenced images readability</title> - - <qandadiv> - <qandaentry> - <question> - <para>We want to extend the example given in <xref - linkend="domFindImages"/> by testing the existence and - checking for readability of referenced images. The following - HTML document contains <quote>dead</quote> image - references:</para> - - <programlisting language="none" - xml:id="domCheckImageAccessibility"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + <caption> + <para>This application searches for <tag class="emptytag">img</tag> + elements and shows their <code>src</code> attribute value.</para> + </caption> + </figure> + + <calloutlist> + <callout arearefs="programlisting_java_searchimg_parse" + xml:id="programlisting_java_searchimg_parse_co"> + <para>Parse a XHTML document instance into a DOM tree.</para> + </callout> + + <callout arearefs="programlisting_java_searchimg_pf" + xml:id="programlisting_java_searchimg_pf_co"> + <para>Create a <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> + factory.</para> + </callout> + + <callout arearefs="programlisting_java_searchimg_newxpath" + xml:id="programlisting_java_searchimg_newxpath_co"> + <para>Create a <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> query + instance. This may be used to search for a set of nodes starting from + a context node.</para> + </callout> + + <callout arearefs="programlisting_java_searchimg_execquery" + xml:id="programlisting_java_searchimg_execquery_co"> + <para>Using the document's root node as the context node we search for + <tag class="starttag">img</tag> elements appearing at arbitrary + positions in our document.</para> + </callout> + + <callout arearefs="programlisting_java_searchimg_loop" + xml:id="programlisting_java_searchimg_loop_co"> + <para>We iterate over the retrieved list of images.</para> + </callout> + + <callout arearefs="programlisting_java_searchimg_cast" + xml:id="programlisting_java_searchimg_cast_co"> + <para>Casting to the correct type.</para> + </callout> + </calloutlist> + + <para>The result is a list of image filename references:</para> + + <programlisting language="none">inline.gif one.gif http://www.hdm-stuttgart.de/favicon.ico </programlisting> + + <section xml:id="sda1SectCastAlwaysLegal"> + <title>Legal casting?</title> + + <qandaset defaultlabel="qanda" xml:id="sda1QandaCastAlwaysLegal"> + <qandadiv> + <qandaentry> + <question> + <para>Why is the cast in <coref + linkend="programlisting_java_searchimg_cast"/> in <xref + linkend="domFindImages"/> guaranteed to never cause a + <classname>java.lang.ClassCastException</classname>?</para> + </question> + + <answer> + <para>The <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> + expression <code>//img</code> is guaranteed to return only <tag + class="starttag">img</tag> elements in contrast to e.g. + <code>//text()</code>. Thus within our <xref + linkend="glo_Java"/> context we are sure to find only + <classname>org.jdom2.Element</classname> instances.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sda1SectAvoidCast"> + <title>Avoiding casts in XPath results</title> + + <qandaset defaultlabel="qanda" xml:id="sda1QandaAvoidCast"> + <qandadiv> + <qandaentry> + <question> + <para>Casts in XPath expression related queries are just ugly. + We may get rid of this problem using Filter instances.</para> + + <para>Re-write <xref linkend="domFindImages"/> avoiding casts + completely.</para> + + <tip> + <para>Use the <methodname + xlink:href="http://www.jdom.org/docs/apidocs/org/jdom2/xpath/XPathFactory.html#compile(java.lang.String,%20org.jdom2.filter.Filter)">compile(...)</methodname> + method.</para> + </tip> + </question> + + <answer> + <annotation role="make"> + <para role="eclipse">Sda1/NoCast</para> + </annotation> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sda1SectVerifyImgReadable"> + <title>Verification of referenced images readability</title> + + <qandaset defaultlabel="qanda" xml:id="sda1QandaVerifyImgReadable"> + <qandadiv> + <qandaentry> + <question> + <para>We want to extend the example given in <xref + linkend="domFindImages"/> by testing the existence and checking + for readability of referenced images. The following HTML + document contains <quote>dead</quote> image references:</para> + + <programlisting language="none" + xml:id="domCheckImageAccessibility"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> ... <body> @@ -1078,33 +1098,32 @@ public class DomXpath { </body> </html></programlisting> - <para>Write an application which checks for readability of - <abbrev - xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev> - image references to <emphasis>external</emphasis> Servers - starting either with <code>http://</code> or - <code>ftp://</code> ignoring other protocol types. Internal - image references referring to the <quote>current</quote> - server typically look like <code><img - src="/images/test.gif"</code>. So in order to distinguish - these two types of references we may use the XSL built in - function <link - xlink:href="http://www.cafeconleche.org/books/bible2/chapters/ch17.html">starts-with()</link> - testing for the <code>http</code> or <code>ftp</code> protocol - definition part of an <abbrev - xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev>. - A possible output for the example being given is:</para> - - <programlisting language="none">Received 'sun.awt.image.URLImageSource' from + <para>Write an application which checks for readability of + <abbrev + xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev> + image references to <emphasis>external</emphasis> Servers + starting either with <code>http://</code> or <code>ftp://</code> + ignoring other protocol types. Internal image references + referring to the <quote>current</quote> server typically look + like <code><img src="/images/test.gif"</code>. So in order to + distinguish these two types of references we may use the XSL + built in function <link + xlink:href="http://www.cafeconleche.org/books/bible2/chapters/ch17.html">starts-with()</link> + testing for the <code>http</code> or <code>ftp</code> protocol + definition part of an <abbrev + xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev>. A + possible output for the example being given is:</para> + + <programlisting language="none">Received 'sun.awt.image.URLImageSource' from http://www.hdm-stuttgart.de/bilder_navigation/laptop.gif Unable to open 'http://www.hdm-stuttgart.de/rotfl.gif'</programlisting> - <para>The following code snippet shows a helpful class method - to check for both correctness of <abbrev - xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev>'s - and accessibility of referenced objects:</para> + <para>The following code snippet shows a helpful class method to + check for both correctness of <abbrev + xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev>'s + and accessibility of referenced objects:</para> - <programlisting language="none">package dom.xpath; + <programlisting language="none">package dom.xpath; ... public class CheckUrl { public static void checkReadability(final String urlRef) { @@ -1127,30 +1146,29 @@ public class CheckUrl { } } }</programlisting> - </question> - - <answer> - <para>We are interested in the set of images within a given - HTML document containing an <link - xlink:href="http://www.w3.org/Addressing">URL</link> reference - starting either with <code>http://</code> or - <code>ftp://</code>. This is achieved by the following - <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> - expression:</para> - - <programlisting language="none">//html:img[starts-with(@src, 'http://') or starts-with(@src, 'ftp://')]</programlisting> - - <para>The application only needs to pass the corresponding - <abbrev - xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev>'s - to the method <link - xlink:href="domCheckUrlObjectExistence">CheckUrl.checkReadability()</link>. - The rest of the code is identical to the <link - linkend="domFindImages">introductory example</link>:</para> - - <informalfigure xml:id="solutionFintExtImgRef"> - <programlisting language="none">package dom.xpath; + </question> + + <answer> + <para>We are interested in the set of images within a given HTML + document containing an <link + xlink:href="http://www.w3.org/Addressing">URL</link> reference + starting either with <code>http://</code> or + <code>ftp://</code>. This is achieved by the following <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> + expression:</para> + + <programlisting language="none">//html:img[starts-with(@src, 'http://') or starts-with(@src, 'ftp://')]</programlisting> + + <para>The application only needs to pass the corresponding + <abbrev + xlink:href="http://www.ietf.org/rfc/rfc1738.txt">URL</abbrev>'s + to the method <link + xlink:href="domCheckUrlObjectExistence">CheckUrl.checkReadability()</link>. + The rest of the code is identical to the <link + linkend="domFindImages">introductory example</link>:</para> + + <informalfigure xml:id="solutionFintExtImgRef"> + <programlisting language="none">package dom.xpath; ... public class CheckExtImage { private final SAXBuilder builder = new SAXBuilder(); @@ -1171,31 +1189,30 @@ public class CheckExtImage { } } }</programlisting> - </informalfigure> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="domXsl"> - <title><acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> and - <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev></title> - - <para><xref linkend="glo_Java"/> - based <xref linkend="glo_XML"/> - applications may use XSL style sheets for processing. A <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> tree may for example - be transformed into another tree. The package <link - xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/package-frame.html">javax.xml.transform</link> - provides interfaces and classes for this purpose. We consider the - following product catalog example:</para> - - <figure xml:id="climbingCatalog"> - <title>A simplified <xref linkend="glo_XML"/> product - catalog</title> - - <programlisting language="none"><catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + </informalfigure> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + </section> + + <section xml:id="domXsl"> + <title><acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> and + <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev></title> + + <para><xref linkend="glo_Java"/> based <xref linkend="glo_XML"/> + applications may use XSL style sheets for processing. A <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> tree may for example be + transformed into another tree. The package <link + xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/package-frame.html">javax.xml.transform</link> + provides interfaces and classes for this purpose. We consider the + following product catalog example:</para> + + <figure xml:id="climbingCatalog"> + <title>A simplified <xref linkend="glo_XML"/> product catalog</title> + + <programlisting language="none"><catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="catalog.xsd"> <title>Outdoor products</title> <introduction> @@ -1219,10 +1236,10 @@ public class CheckExtImage { </product> </catalog></programlisting> - <para>A corresponding schema file <filename>catalog.xsd</filename> - is straightforward:</para> + <para>A corresponding schema file <filename>catalog.xsd</filename> is + straightforward:</para> - <programlisting language="none"><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" + <programlisting language="none"><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" vc:minVersion="1.0" vc:maxVersion="1.1"> @@ -1267,18 +1284,17 @@ public class CheckExtImage { </xs:schema> </programlisting> - </figure> + </figure> - <para>A <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> - style sheet may be used to transform this document into the HTML - Format:</para> + <para>A <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> + style sheet may be used to transform this document into the HTML + Format:</para> - <figure xml:id="catalog2html"> - <title>A <abbrev - xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> style sheet - for catalog transformation to HTML.</title> + <figure xml:id="catalog2html"> + <title>A <abbrev xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> + style sheet for catalog transformation to HTML.</title> - <programlisting language="none"><?xml version="1.0" encoding="utf-8"?> + <programlisting language="none"><?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns="http://www.w3.org/1999/xhtml"> @@ -1305,21 +1321,20 @@ public class CheckExtImage { </xsl:if> </xsl:template> </xsl:stylesheet></programlisting> - </figure> + </figure> - <para>As a preparation for <xref linkend="exercise_catalogRdbms"/> we - now demonstrate the usage of <abbrev - xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> within a - <xref linkend="glo_Java"/> application. - This is done by a <link - xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer</link> - instance:</para> + <para>As a preparation for <xref linkend="exercise_catalogRdbms"/> we now + demonstrate the usage of <abbrev + xlink:href="http://www.w3.org/Style/XSL">XSL</abbrev> within a <xref + linkend="glo_Java"/> application. This is done by a <link + xlink:href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer</link> + instance:</para> - <figure xml:id="xml2xml"> - <title>Transforming an XML document instance to HTML by a XSL style - sheet.</title> + <figure xml:id="xml2xml"> + <title>Transforming an XML document instance to HTML by a XSL style + sheet.</title> - <programlisting language="none">package dom.xsl; + <programlisting language="none">package dom.xsl; ... public class Xml2Html { private final SAXBuilder builder = new SAXBuilder(); @@ -1345,15 +1360,15 @@ public class Xml2Html { } }</programlisting> - </figure> + </figure> - <para>A corresponding driver file is needed to invoke a - transformation:</para> + <para>A corresponding driver file is needed to invoke a + transformation:</para> - <figure xml:id="xml2xmlDriver"> - <title>A driver class for the xml2xml transformer.</title> + <figure xml:id="xml2xmlDriver"> + <title>A driver class for the xml2xml transformer.</title> - <programlisting language="none">package dom.xsl; + <programlisting language="none">package dom.xsl; ... public class Xml2HtmlDriver { ... @@ -1374,22 +1389,22 @@ public class Xml2HtmlDriver { } } }</programlisting> - </figure> + </figure> - <qandaset defaultlabel="qanda" xml:id="exercise_catalogRdbms"> - <title>HTML from XML and relational data</title> + <qandaset defaultlabel="qanda" xml:id="exercise_catalogRdbms"> + <title>HTML from XML and relational data</title> - <qandadiv> - <qandaentry> - <question> - <label>Catalogs and RDBMS</label> + <qandadiv> + <qandaentry> + <question> + <label>Catalogs and RDBMS</label> - <para>We want to extend the transformation being described - before in <xref linkend="xml2xml"/> by reading price - information from a RDBMS. Consider the following schema and - <code>INSERT</code>s:</para> + <para>We want to extend the transformation being described before + in <xref linkend="xml2xml"/> by reading price information from a + RDBMS. Consider the following schema and + <code>INSERT</code>s:</para> - <programlisting language="none">CREATE TABLE Product( + <programlisting language="none">CREATE TABLE Product( orderNo CHAR(10) ,price NUMERIC(10,2) ); @@ -1397,49 +1412,48 @@ public class Xml2HtmlDriver { INSERT INTO Product VALUES('x-223', 330.20); INSERT INTO Product VALUES('w-124', 110.40);</programlisting> - <para>Adding prices may be implemented the following - way:</para> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/xml2html.fig"/> - </imageobject> - </mediaobject> - - <para>You may implement this by following these steps:</para> - - <orderedlist> - <listitem> - <para>You may reuse class - <classname>sax.rdbms.RdbmsAccess</classname> from <xref - linkend="saxRdbms"/>.</para> - </listitem> - - <listitem> - <para>Use the previous class to modify <xref - linkend="xml2xml"/> by introducing a new method - <code>addPrices(final Document catalog)</code> which adds - prices to the <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> tree - accordingly. The insertion points may be reached by an - <acronym - xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> - expression.</para> - </listitem> - </orderedlist> - </question> - - <answer> - <para>The additional functionality on top of <xref - linkend="xml2xml"/> is represented by a method - <methodname>dom.xsl.XmlRdbms2Html.addPrices()</methodname>. - This method modifies the <acronym - xlink:href="http://www.w3.org/DOM">DOM</acronym> input tree - prior to applying the XSL. Prices are being inserting based on - data received from an RDBMS via <trademark - xlink:href="http://electronics.zibb.com/trademark/jdbc/29545026">JDBC</trademark>:</para> - - <programlisting language="none">package dom.xsl; + <para>Adding prices may be implemented the following way:</para> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/xml2html.fig"/> + </imageobject> + </mediaobject> + + <para>You may implement this by following these steps:</para> + + <orderedlist> + <listitem> + <para>You may reuse class + <classname>sax.rdbms.RdbmsAccess</classname> from <xref + linkend="saxRdbms"/>.</para> + </listitem> + + <listitem> + <para>Use the previous class to modify <xref + linkend="xml2xml"/> by introducing a new method + <code>addPrices(final Document catalog)</code> which adds + prices to the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> tree + accordingly. The insertion points may be reached by an + <acronym + xlink:href="http://www.w3.org/TR/xpath">XPath</acronym> + expression.</para> + </listitem> + </orderedlist> + </question> + + <answer> + <para>The additional functionality on top of <xref + linkend="xml2xml"/> is represented by a method + <methodname>dom.xsl.XmlRdbms2Html.addPrices()</methodname>. This + method modifies the <acronym + xlink:href="http://www.w3.org/DOM">DOM</acronym> input tree prior + to applying the XSL. Prices are being inserting based on data + received from an RDBMS via <trademark + xlink:href="http://electronics.zibb.com/trademark/jdbc/29545026">JDBC</trademark>:</para> + + <programlisting language="none">package dom.xsl; ... public class XmlRdbms2Html { private final SAXBuilder builder = new SAXBuilder(); @@ -1502,10 +1516,10 @@ public class XmlRdbms2Html { } }</programlisting> - <para>The method <code>addPrices(...)</code> utilizes our - RDBMS access class:</para> + <para>The method <code>addPrices(...)</code> utilizes our RDBMS + access class:</para> - <programlisting language="none">package dom.xsl; + <programlisting language="none">package dom.xsl; ... public class DbAccess { public void connect(final String jdbcUrl, @@ -1534,11 +1548,11 @@ public class DbAccess { ... }</programlisting> - <para>Of course the connection details should be moved to a - configuration file.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - </chapter> + <para>Of course the connection details should be moved to a + configuration file.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> +</chapter> diff --git a/P/Sda1/ImageSearch/pom.xml b/P/Sda1/ImageSearch/pom.xml index daa8beddab5521354f5bdd534d56786c07f0c6da..4e008b5742f5d0acf36832b7aa55eb6c179f2d6b 100644 --- a/P/Sda1/ImageSearch/pom.xml +++ b/P/Sda1/ImageSearch/pom.xml @@ -15,23 +15,7 @@ <version>0.8</version> <packaging>jar</packaging> - <name>saxerrorhandler</name> + <name>Imgsearch</name> <url>http://www.mi.hdm-stuttgart.de/freedocs</url> - <dependencies> - - <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> - </project> diff --git a/P/Sda1/NoCast/.gitignore b/P/Sda1/NoCast/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..27306f265821aa9c2c06a66091534c947c7cfab9 --- /dev/null +++ b/P/Sda1/NoCast/.gitignore @@ -0,0 +1,5 @@ +A1.log +/target/ +/.settings/ +.classpath +.project diff --git a/P/Sda1/NoCast/pom.xml b/P/Sda1/NoCast/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..2caab7339bc36aef3d8d83c43cb1c54cb119a5ee --- /dev/null +++ b/P/Sda1/NoCast/pom.xml @@ -0,0 +1,37 @@ +<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>nocast</artifactId> + <version>0.8</version> + <packaging>jar</packaging> + + <name>NoCast</name> + <url>http://www.mi.hdm-stuttgart.de/freedocs</url> + + <dependencies> + + <dependency> + <groupId>org.jdom</groupId> + <artifactId>jdom2</artifactId> + <version>2.0.6</version> + </dependency> + + <dependency> + <groupId>jaxen</groupId> + <artifactId>jaxen</artifactId> + <version>1.1.4</version> + </dependency> + + </dependencies> + +</project> diff --git a/P/Sda1/NoCast/src/main/java/de/hdm_stuttgart/mi/sda1/nocast/DomXpath.java b/P/Sda1/NoCast/src/main/java/de/hdm_stuttgart/mi/sda1/nocast/DomXpath.java new file mode 100644 index 0000000000000000000000000000000000000000..d5f8346184a81df0571601b948e4aaca4f8adb35 --- /dev/null +++ b/P/Sda1/NoCast/src/main/java/de/hdm_stuttgart/mi/sda1/nocast/DomXpath.java @@ -0,0 +1,44 @@ +package de.hdm_stuttgart.mi.sda1.nocast; + +import java.io.IOException; +import java.util.List; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.filter.ElementFilter; +import org.jdom2.input.SAXBuilder; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; + +/** + * How to use XPath expressions to search for <img> + * elements in XHTML document instances. + * + */ +public class DomXpath { + private final SAXBuilder builder = new SAXBuilder(); + + /** + * + */ + public DomXpath() {} + /** + * Read an XHTML document and search for images. The image filenames will be + * written to standard out. + * + * @param xhtmlFilename The XHTML's filename to be examined. + * @throws JDOMException General parsing fault + * @throws IOException file access problem + */ + public void process(final String xhtmlFilename) throws JDOMException, IOException { + + final Document htmlInput = builder.build(xhtmlFilename); + final XPathExpression<Element> xpath = XPathFactory.instance().compile("//img", new ElementFilter()); + final List<Element> images = xpath.evaluate(htmlInput); + + for (final Element image: images) { + System.out.print(image.getAttributeValue("src") + " "); + } + } +} \ No newline at end of file diff --git a/P/Sda1/NoCast/src/main/java/de/hdm_stuttgart/mi/sda1/nocast/driver/DomXpathDriver.java b/P/Sda1/NoCast/src/main/java/de/hdm_stuttgart/mi/sda1/nocast/driver/DomXpathDriver.java new file mode 100644 index 0000000000000000000000000000000000000000..83e4fbd378e2207e5b88955f4f289c7634a27e37 --- /dev/null +++ b/P/Sda1/NoCast/src/main/java/de/hdm_stuttgart/mi/sda1/nocast/driver/DomXpathDriver.java @@ -0,0 +1,22 @@ +package de.hdm_stuttgart.mi.sda1.nocast.driver; + +import de.hdm_stuttgart.mi.sda1.nocast.DomXpath; + +/** + * Driver to test XPath based searching of <img> elements + * within a given HTML document. + * +*/ +public class DomXpathDriver { + + /** + * @param argv Unused command arguments + * @throws Exception File access problem or corrupt content. + */ + public static void main(String[] argv) throws Exception { + final DomXpath ao = new DomXpath(); + // The following HTML document is being searched + // for <img> elements. + ao.process("src/main/resources/gallery.html"); + } +} \ No newline at end of file diff --git a/P/Sda1/NoCast/src/main/resources/gallery.html b/P/Sda1/NoCast/src/main/resources/gallery.html new file mode 100644 index 0000000000000000000000000000000000000000..600f3ab6b58f97117a00155c2aeef1b6e6a76a4d --- /dev/null +++ b/P/Sda1/NoCast/src/main/resources/gallery.html @@ -0,0 +1,26 @@ +<?xml version="1.0"?> +<!-- + <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + --> +<html> + <head> + <title>Picture gallery</title> + </head> + <body> + <h1>Picture gallery</h1> + <p>Images may appear inline:<img src="inline.gif" alt="none"/></p> + <table> + <tbody> + <tr> + <td>Number one:</td> + <td><img src="one.gif" alt="none"/></td> + </tr> + <tr> + <td>Number two:</td> + <td><img src="http://www.hdm-stuttgart.de/favicon.ico" alt="none"/></td> + </tr> + </tbody> + </table> + </body> +</html> diff --git a/P/pom.xml b/P/pom.xml index 1d98ef163be51e6277977b7a4c755292ac82d48f..d3194bc73344d9b49c59e243a92e2570a04181da 100644 --- a/P/pom.xml +++ b/P/pom.xml @@ -107,6 +107,8 @@ <module>Sda1/catalog2sql</module> <module>Sda1/catalog2rdbms</module> + <module>Sda1/NoCast</module> + <module>Sda1/rdbms2catalog</module> <module>Sda1/xmlstatistics</module>