KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > RDDLToTable


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
25
26 import nu.xom.Attribute;
27 import nu.xom.Builder;
28 import nu.xom.DocType;
29 import nu.xom.Document;
30 import nu.xom.Element;
31 import nu.xom.Node;
32 import nu.xom.NodeFactory;
33 import nu.xom.Nodes;
34 import nu.xom.ParsingException;
35 import nu.xom.Serializer;
36
37 /**
38  * <p>
39  * Demonstrates a custom <code>NodeFactory</code> that converts
40  * <code>rddl:resource</code> elements to XHTML tables.
41  * This is inspired by Example 8-11 in
42  * <cite>Processing XML with Java</cite>.
43  * In brief, it demonstrates that major modifications
44  * may have to take place in <code>endElement</code> but can still
45  * be effectively streamed.
46  * </p>
47  *
48  * <p>
49  * <code>rddl:resource</code> elements are replaced by a simple table.
50  * The various attributes of the resource are mapped to different
51  * parts of the table. In particular, a <code>rddl:resource</code>
52  * like this:
53  * </p>
54  *
55  * <pre><code>&lt;rddl:resource
56  * id="note-xlink2rdf"
57  * xlink:title="W3C NOTE XLink2RDF"
58  * xlink:role="http://www.w3.org/TR/html4/"
59  * xlink:arcrole="http://www.rddl.org/purposes#reference"
60  * xlink:href="http://www.w3.org/TR/xlink2rdf/"
61  * >
62  * &lt;li>
63  * &lt;a HREF="http://www.w3.org/TR/xlink2rdf/">
64  * W3C Note Harvesting RDF Statements from XLinks
65  * &lt;/a>
66  * &lt;/li>
67  *&lt;/rddl:resource></code></pre>
68  *
69  * <p>will turn into an XHTML table that looks like this:</p>
70  *
71  *<pre><code>&lt;table id="note-xlink2rdf">
72  * &lt;caption>W3C NOTE XLink2RDF&lt;/caption>
73  * &lt;tr>&lt;td>Role: &lt;/td>&lt;td>http://www.w3.org/TR/html4/&lt;/td>&lt;/tr>
74  * &lt;tr>&lt;td>Arcrole: &lt;/td>&lt;td>http://www.rddl.org/purposes#reference&lt;/td>&lt;/tr>
75  * &lt;tr>&lt;td>Href: &lt;/td>&lt;td>&lt;a HREF="http://www.w3.org/TR/xlink2rdf/">
76  * http://www.w3.org/TR/xlink2rdf/&lt;/a>&lt;/td>&lt;/tr>
77  * &lt;tr>
78  * &lt;td colspan="2">
79  * &lt;li>
80  * &lt;a HREF="http://www.w3.org/TR/xlink2rdf/">
81  * W3C Note Harvesting RDF Statements from XLinks
82  * &lt;/a>
83  * &lt;/li>
84  * &lt;/td>
85  * &lt;/tr>
86  *&lt;/table></code></pre>
87  *
88  * @author Elliotte Rusty Harold
89  * @version 1.0
90  */

91
92 public class RDDLToTable extends NodeFactory {
93
94     public final static String JavaDoc RDDL_NAMESPACE
95       = "http://www.rddl.org/";
96     public final static String JavaDoc XHTML_NAMESPACE
97       = "http://www.w3.org/1999/xhtml";
98     public final static String JavaDoc XLINK_NAMESPACE
99       = "http://www.w3.org/1999/xlink";
100
101
102     public static void main(String JavaDoc[] args) {
103
104         if (args.length <= 0) {
105           System.out.println("Usage: java nu.xom.samples.RDDLToTable URL");
106           return;
107         }
108         
109         try {
110             Builder parser = new Builder(new RDDLToTable());
111             Document doc = parser.build(args[0]);
112             Serializer out = new Serializer(System.out);
113             out.write(doc);
114         }
115         catch (ParsingException ex) {
116           System.out.println(args[0] + " is not well-formed.");
117           System.out.println(ex.getMessage());
118         }
119         catch (IOException JavaDoc ex) {
120           System.out.println(
121            "Due to an IOException, the parser could not read "
122            + args[0]
123           );
124         }
125         
126     }
127     
128     public Nodes finishMakingElement(Element element) {
129         
130         element.removeNamespaceDeclaration("rddl");
131         element.removeNamespaceDeclaration("xlink");
132         Element result = element;
133         if (RDDL_NAMESPACE.equals(element.getNamespaceURI())
134           && "resource".equals(element.getLocalName())) {
135             Element table = new Element("table", XHTML_NAMESPACE);
136             // move the content
137
Element tr = new Element("tr", XHTML_NAMESPACE);
138             Element td = new Element("td", XHTML_NAMESPACE);
139             td.addAttribute(new Attribute("colspan", "2"));
140             tr.appendChild(td);
141             while (element.getChildCount() > 0) {
142                 Node child = element.removeChild(0);
143                 td.appendChild(child);
144             }
145             table.appendChild(tr);
146             
147             Attribute href = element.getAttribute("role", XLINK_NAMESPACE);
148             if (href != null) {
149                 element.removeAttribute(href);
150                 Element trhref = new Element("tr", XHTML_NAMESPACE);
151                 Element tdhref1 = new Element("td", XHTML_NAMESPACE);
152                 Element tdhref2 = new Element("td", XHTML_NAMESPACE);
153                 tdhref1.appendChild("href: ");
154                 tdhref2.appendChild(href.getValue());
155                 trhref.appendChild(tdhref1);
156                 trhref.appendChild(tdhref2);
157                 table.insertChild(trhref, 0);
158             }
159             
160             Attribute arcrole = element.getAttribute("role", XLINK_NAMESPACE);
161             if (arcrole != null) {
162                 element.removeAttribute(arcrole);
163                 Element trarcrole = new Element("tr", XHTML_NAMESPACE);
164                 Element tdarcrole1 = new Element("td", XHTML_NAMESPACE);
165                 Element tdarcrole2 = new Element("td", XHTML_NAMESPACE);
166                 tdarcrole1.appendChild("arcrole: ");
167                 tdarcrole2.appendChild(arcrole.getValue());
168                 trarcrole.appendChild(tdarcrole1);
169                 trarcrole.appendChild(tdarcrole2);
170                 table.insertChild(trarcrole, 0);
171             }
172
173
174             Attribute role = element.getAttribute("role", XLINK_NAMESPACE);
175             if (role != null) {
176                 element.removeAttribute(role);
177                 Element trrole = new Element("tr", XHTML_NAMESPACE);
178                 Element tdrole1 = new Element("td", XHTML_NAMESPACE);
179                 Element tdrole2 = new Element("td", XHTML_NAMESPACE);
180                 tdrole1.appendChild("role: ");
181                 tdrole2.appendChild(role.getValue());
182                 trrole.appendChild(tdrole1);
183                 trrole.appendChild(tdrole2);
184                 table.insertChild(trrole, 0);
185             }
186                        
187             Attribute id = element.getAttribute("id");
188             if (id != null) {
189                 element.removeAttribute(id);
190                 Element caption = new Element("caption", XHTML_NAMESPACE);
191                 caption.appendChild(id.getValue());
192                 table.insertChild(caption, 0);
193             }
194             result = table;
195         }
196         return new Nodes(result);
197     }
198
199     public Nodes makeDocType(String JavaDoc rootElementName,
200       String JavaDoc publicID, String JavaDoc systemID) {
201         return new Nodes(new DocType("html",
202           "PUBLIC \"-//W3C//DTD XHTML Basic 1.0//EN\"",
203           "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"));
204     }
205
206 }
207
Popular Tags