1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import java.io.IOException;
26
import nu.xom.Attribute;
27
import nu.xom.Builder;
28
import nu.xom.Document;
29
import nu.xom.Element;
30
import nu.xom.Elements;
32
import nu.xom.ParentNode;
33
import nu.xom.ParsingException;
37
* Demonstrates replacing elements in a document
38
* with different elements that contain the same content.
41
* @author Elliotte Rusty Harold
45
public class ResourceToTable {
47
public final static String XHTML_NAMESPACE
48
= "http://www.w3.org/1999/xhtml";
49
public final static String RDDL_NAMESPACE
50
= "http://www.rddl.org/";
51
public final static String XLINK_NAMESPACE
52
= "http://www.w3.org/1999/xlink";
54
public static void main(String[] args) {
56
if (args.length <= 0) {
57
System.out.println("Usage: java nu.xom.samples.ResourceToTable URL");
62
Builder parser = new Builder();
63
Document doc = parser.build(args[0]);
64
Element root = doc.getRootElement();
65
if (root.getNamespaceURI().equals(XHTML_NAMESPACE)) {
69
System.out.println(args[0] + " does not appear to be an XHTML document");
72
System.out.println(doc.toXML());
74
catch (ParsingException ex) {
75
System.out.println(args[0] + " is not well-formed.");
77
catch (IOException ex) {
79
"Due to an IOException, the parser could not read "
86
public static void convert(Element element) {
88
if (element.getNamespaceURI().equals(RDDL_NAMESPACE)) {
90
Element table = new Element("table", XHTML_NAMESPACE);
94
xlink:title="W3C NOTE XLink2RDF"
95
xlink:role="http://www.w3.org/TR/html4/"
96
xlink:arcrole="http://www.rddl.org/purposes#reference"
97
xlink:href="http://www.w3.org/TR/xlink2rdf/"
100
<a href="http://www.w3.org/TR/xlink2rdf/">
101
W3C Note Harvesting RDF Statements from XLinks
106
will turn into an XHTML table that looks like this:
108
<table id="note-xlink2rdf">
109
<caption>W3C NOTE XLink2RDF</caption>
110
<tr><td>Role: </td><td>http://www.w3.org/TR/html4/</td></tr>
111
<tr><td>Arcrole: </td><td>http://www.rddl.org/purposes#reference</td></tr>
112
<tr><td>Href: </td><td><a href="http://www.w3.org/TR/xlink2rdf/">
113
http://www.w3.org/TR/xlink2rdf/</a></td></tr>
117
<a href="http://www.w3.org/TR/xlink2rdf/">
118
W3C Note Harvesting RDF Statements from XLinks
125
Attribute role = element.getAttribute("role", XLINK_NAMESPACE);
127
Element tr = new Element("tr", XHTML_NAMESPACE);
128
Element td1 = new Element("td", XHTML_NAMESPACE);
130
td1.appendChild("Role: ");
131
Element td2 = new Element("td", XHTML_NAMESPACE);
133
td2.appendChild(role.getValue());
134
table.insertChild(tr, 0);
136
Attribute arcrole = element.getAttribute("arcrole", XLINK_NAMESPACE);
137
if (arcrole != null) {
138
Element tr = new Element("tr", XHTML_NAMESPACE);
139
Element td1 = new Element("td", XHTML_NAMESPACE);
141
td1.appendChild("Arcrole: ");
142
Element td2 = new Element("td", XHTML_NAMESPACE);
144
td2.appendChild(arcrole.getValue());
145
table.insertChild(tr, 0);
147
Attribute href = element.getAttribute("href", XLINK_NAMESPACE);
149
Element tr = new Element("tr", XHTML_NAMESPACE);
150
Element td1 = new Element("td", XHTML_NAMESPACE);
152
td1.appendChild("Href: ");
153
Element td2 = new Element("td", XHTML_NAMESPACE);
155
Element a = new Element("a", XHTML_NAMESPACE);
156
a.appendChild(href.getValue());
158
a.addAttribute(new Attribute("href", href.getValue()));
159
table.insertChild(tr, 0);
161
Attribute title = element.getAttribute("title", XLINK_NAMESPACE);
163
Element caption = new Element("caption", XHTML_NAMESPACE);
164
caption.appendChild(title.getValue());
165
table.insertChild(caption, 0);
169
// Move children into a td
170
Element tr = new Element("tr", XHTML_NAMESPACE);
171
Element td = new Element("td", XHTML_NAMESPACE);
172
td.addAttribute(new Attribute("colspan", "2"));
174
while (element.getChildCount() > 0) {
175
Node child = element.getChild(0);
177
td.appendChild(child);
178
if (child instanceof Element) convert((Element) child);
180
table.appendChild(tr);
182
ParentNode parent = element.getParent();
183
parent.replaceChild(element, table);
187
// Strip out additional namespaces
188
for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) {
189
String prefix = element.getNamespacePrefix(i);
190
element.removeNamespaceDeclaration(prefix);
193
Elements elements = element.getChildElements();
194
for (int i = 0; i < elements.size(); i++) {
195
convert(elements.get(i));
b'\\ No newline at end of file'