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.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 91 92 public class RDDLToTable extends NodeFactory { 93 94 public final static String RDDL_NAMESPACE 95 = "http://www.rddl.org/"; 96 public final static String XHTML_NAMESPACE 97 = "http://www.w3.org/1999/xhtml"; 98 public final static String XLINK_NAMESPACE 99 = "http://www.w3.org/1999/xlink"; 100 101 102 public static void main(String [] 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 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 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 rootElementName, 200 String publicID, String 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 |