~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/ResourceToTable.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2002, 2003 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;
 
25
 
 
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;
 
31
import nu.xom.Node;
 
32
import nu.xom.ParentNode;
 
33
import nu.xom.ParsingException;
 
34
 
 
35
/**
 
36
 * <p>
 
37
 *   Demonstrates replacing elements in a document
 
38
 *   with different elements that contain the same content.
 
39
 * </p>
 
40
 * 
 
41
 * @author Elliotte Rusty Harold
 
42
 * @version 1.0
 
43
 *
 
44
 */
 
45
public class ResourceToTable {
 
46
 
 
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";
 
53
 
 
54
    public static void main(String[] args) {
 
55
 
 
56
        if (args.length <= 0) {
 
57
          System.out.println("Usage: java nu.xom.samples.ResourceToTable URL");
 
58
          return;
 
59
        }
 
60
        
 
61
        try {
 
62
            Builder parser = new Builder();
 
63
            Document doc = parser.build(args[0]);
 
64
            Element root = doc.getRootElement();
 
65
            if (root.getNamespaceURI().equals(XHTML_NAMESPACE)) {
 
66
                convert(root);
 
67
            }
 
68
            else {
 
69
                System.out.println(args[0] + " does not appear to be an XHTML document");
 
70
                return;   
 
71
            }
 
72
            System.out.println(doc.toXML());
 
73
        }
 
74
        catch (ParsingException ex) {
 
75
          System.out.println(args[0] + " is not well-formed.");
 
76
        }
 
77
        catch (IOException ex) { 
 
78
          System.out.println(
 
79
           "Due to an IOException, the parser could not read " 
 
80
           + args[0]
 
81
          ); 
 
82
        }      
 
83
        
 
84
    }
 
85
    
 
86
    public static void convert(Element element) {
 
87
        
 
88
       if (element.getNamespaceURI().equals(RDDL_NAMESPACE)) {
 
89
            
 
90
            Element table = new Element("table", XHTML_NAMESPACE);
 
91
            
 
92
/* <rddl:resource
 
93
        id="note-xlink2rdf"
 
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/"
 
98
        >
 
99
      <li>
 
100
        <a href="http://www.w3.org/TR/xlink2rdf/">
 
101
          W3C Note Harvesting RDF Statements from XLinks
 
102
        </a>
 
103
      </li>
 
104
</rddl:resource>
 
105
 
 
106
will turn into an XHTML table that looks like this:
 
107
 
 
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>
 
114
  <tr>
 
115
    <td colspan="2">
 
116
      <li>
 
117
        <a href="http://www.w3.org/TR/xlink2rdf/">
 
118
          W3C Note Harvesting RDF Statements from XLinks
 
119
        </a>
 
120
      </li>
 
121
    </td>
 
122
  </tr>
 
123
</table> */
 
124
 
 
125
            Attribute role = element.getAttribute("role", XLINK_NAMESPACE);            
 
126
            if (role != null) {
 
127
                Element tr = new Element("tr", XHTML_NAMESPACE);
 
128
                Element td1 = new Element("td", XHTML_NAMESPACE);
 
129
                tr.appendChild(td1);
 
130
                td1.appendChild("Role: ");
 
131
                Element td2 = new Element("td", XHTML_NAMESPACE);
 
132
                tr.appendChild(td2);
 
133
                td2.appendChild(role.getValue());
 
134
                table.insertChild(tr, 0);
 
135
            }    
 
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);
 
140
                tr.appendChild(td1);
 
141
                td1.appendChild("Arcrole: ");
 
142
                Element td2 = new Element("td", XHTML_NAMESPACE);
 
143
                tr.appendChild(td2);
 
144
                td2.appendChild(arcrole.getValue());
 
145
                table.insertChild(tr, 0);
 
146
            }    
 
147
            Attribute href = element.getAttribute("href", XLINK_NAMESPACE);            
 
148
            if (href != null) {
 
149
                Element tr = new Element("tr", XHTML_NAMESPACE);
 
150
                Element td1 = new Element("td", XHTML_NAMESPACE);
 
151
                tr.appendChild(td1);
 
152
                td1.appendChild("Href: ");
 
153
                Element td2 = new Element("td", XHTML_NAMESPACE);
 
154
                tr.appendChild(td2);
 
155
                Element a = new Element("a", XHTML_NAMESPACE);
 
156
                a.appendChild(href.getValue());
 
157
                td2.appendChild(a);
 
158
                a.addAttribute(new Attribute("href", href.getValue()));
 
159
                table.insertChild(tr, 0);
 
160
            }    
 
161
            Attribute title = element.getAttribute("title", XLINK_NAMESPACE);            
 
162
            if (title != null) {
 
163
                Element caption = new Element("caption", XHTML_NAMESPACE);
 
164
                caption.appendChild(title.getValue());
 
165
                table.insertChild(caption, 0);
 
166
            }    
 
167
              
 
168
              
 
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"));
 
173
            tr.appendChild(td);
 
174
            while (element.getChildCount() > 0) {
 
175
                Node child = element.getChild(0);
 
176
                child.detach();
 
177
                td.appendChild(child);
 
178
                if (child instanceof Element) convert((Element) child);
 
179
            }        
 
180
            table.appendChild(tr);  
 
181
            
 
182
            ParentNode parent = element.getParent();
 
183
            parent.replaceChild(element, table);
 
184
                
 
185
        }
 
186
        else {
 
187
            // Strip out additional namespaces
 
188
            for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) {
 
189
                String prefix = element.getNamespacePrefix(i);
 
190
                element.removeNamespaceDeclaration(prefix);  
 
191
            }
 
192
 
 
193
            Elements elements = element.getChildElements();      
 
194
            for (int i = 0; i < elements.size(); i++) {
 
195
              convert(elements.get(i));
 
196
            }
 
197
                
 
198
        }            
 
199
        
 
200
    } 
 
201
    
 
202
}
 
 
b'\\ No newline at end of file'