1 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 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 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 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 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 } | Popular Tags |