1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.io.FileNotFoundException ; 23 import java.io.PrintWriter ; 24 import java.util.Properties ; 25 import java.util.Vector ; 26 27 import org.apache.lenya.xml.xpointer.XPointer; 28 import org.apache.log4j.Category; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 34 public class XPointerFactory { 35 static Category log = Category.getInstance(XPointerFactory.class); 36 XPointer xpointer = null; 37 38 41 public XPointerFactory() { 42 Properties properties = new Properties (); 43 String propertiesFileName = "conf.properties"; 44 45 try { 46 properties.load(XPointerFactory.class.getResourceAsStream(propertiesFileName)); 47 } catch (Exception e) { 48 log.fatal(": Failed to load properties from resource: " + propertiesFileName); 49 } 50 51 String xpointerName = properties.getProperty("XPointer"); 52 53 if (xpointerName == null) { 54 log.fatal(": No XPointer specified in " + propertiesFileName); 55 } 56 57 try { 58 Class xpointerClass = Class.forName(xpointerName); 59 xpointer = (XPointer) xpointerClass.newInstance(); 60 } catch (Exception e) { 61 log.fatal(": " + e); 62 } 63 } 64 65 70 public static void main(String [] args) { 71 XPointerFactory xpf = new XPointerFactory(); 72 DOMParserFactory dpf = new DOMParserFactory(); 73 74 if (args.length != 2) { 75 System.err.println("Usage: java " + xpf.getClass().getName() + 76 " example.xml \"/Example/People/Person[1]/Name\""); 77 78 return; 79 } 80 81 Document document = null; 82 83 try { 84 document = dpf.getDocument(args[0]); 85 } catch (FileNotFoundException e) { 86 System.err.println("No such file or directory: " + e.getMessage()); 87 } catch (Exception e) { 88 System.err.println(e.getMessage()); 89 } 90 91 String xpath = args[1]; 92 93 try { 94 Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath + ")"); 95 String [] values = xpf.getNodeValues(nodes); 96 97 for (int i = 0; i < nodes.size(); i++) { 98 System.out.println(((Node ) nodes.elementAt(i)).getNodeName() + ": " + values[i]); 99 } 100 } catch (Exception e) { 101 System.err.println(xpf.getClass().getName() + ".main(): " + e); 102 } 103 104 Document doc = xpf.employees(); 105 106 try { 107 Vector nodes = xpf.select(doc.getDocumentElement(), "xpointer(/Employees/Employee[2])"); 108 String [] values = xpf.getNodeValues(nodes); 109 110 for (int i = 0; i < nodes.size(); i++) { 111 System.out.println(((Node ) nodes.elementAt(i)).getNodeName() + ": " + values[i]); 112 } 113 114 Element leviElement = (Element ) nodes.elementAt(0); 115 leviElement.appendChild(dpf.newTextNode(doc, " Brucker")); 116 } catch (Exception e) { 117 System.err.println(xpf.getClass().getName() + ".main(): " + e); 118 } 119 120 new DOMWriter(new PrintWriter (System.out)).print(doc); 121 System.out.println(""); 122 } 123 124 131 public void parse(String reference, Vector xpaths, Vector namespaces) throws MalformedXPointerException { 132 tokenize(reference, xpaths, namespaces); 133 } 134 135 145 public Vector select(Node node, String reference) throws Exception { 146 Vector xpaths = new Vector (); 147 Vector namespaces = new Vector (); 148 parse(reference, xpaths, namespaces); 149 150 Vector nodes = new Vector (); 151 152 for (int i = 0; i < xpaths.size(); i++) { 153 Vector n = xpointer.select(node, (String ) xpaths.elementAt(i), namespaces); 154 155 for (int j = 0; j < n.size(); j++) { 156 nodes.addElement(n.elementAt(j)); 157 } 158 } 159 160 return nodes; 161 } 162 163 173 public Node selectAt(Node node, String reference, int i) throws Exception { 174 return (Node )select(node, reference).elementAt(i); 175 } 176 177 185 public void tokenize(String xpointer, Vector xpaths, Vector namespaces) throws MalformedXPointerException { 186 if ((xpointer.indexOf("xpointer(") == 0) && (xpointer.charAt(xpointer.length() - 1) == ')')) { 187 188 String substring = xpointer.substring(9, xpointer.length()); 189 int i = substring.indexOf(")"); 190 191 if (i >= 0) { 192 log.debug("XPath: " + substring.substring(0, i)); 193 xpaths.addElement(substring.substring(0, i)); 194 tokenize(substring.substring(i + 1, substring.length()), xpaths, namespaces); 195 } else { 196 xpaths.addElement(substring.substring(0, substring.length() - 1)); 197 return; 198 } 199 } else if ((xpointer.indexOf("xmlns(") == 0) && (xpointer.charAt(xpointer.length() - 1) == ')')) { 200 String substring = xpointer.substring(6, xpointer.length()); 201 int i = substring.indexOf(")"); 202 203 if (i >= 0) { 204 log.debug("Namespace: " + substring.substring(0, i)); 205 namespaces.addElement(substring.substring(0, i)); 206 tokenize(substring.substring(i + 1, substring.length()), xpaths, namespaces); 207 } else { 208 xpaths.addElement(substring.substring(0, substring.length() - 1)); 209 return; 210 } 211 } else if (xpointer.equals("")) { 212 return; 213 } else { 214 throw new MalformedXPointerException(xpointer); 215 } 216 } 217 218 227 public String [] getNodeValues(Vector nodes) throws Exception { 228 String [] values = new String [nodes.size()]; 229 230 for (int i = 0; i < values.length; i++) { 231 Node node = (Node ) nodes.elementAt(i); 232 short type = node.getNodeType(); 233 234 switch (type) { 235 case Node.ELEMENT_NODE: { 236 values[i] = getElementValue((Element ) node); 237 238 break; 239 } 240 241 case Node.ATTRIBUTE_NODE: { 242 values[i] = node.getNodeValue(); 243 244 break; 245 } 246 247 default: 248 values[i] = ""; 249 throw new Exception ("Neither ELEMENT nor ATTRIBUTE: " + type); 250 } 251 } 252 253 return values; 254 } 255 256 263 public String getElementValue(Element element) { 264 String value = ""; 265 NodeList nl = element.getChildNodes(); 266 267 for (int k = 0; k < nl.getLength(); k++) { 268 short nodeType = nl.item(k).getNodeType(); 269 270 if (nodeType == Node.TEXT_NODE) { 271 value = value + nl.item(k).getNodeValue(); 272 } else if (nodeType == Node.ELEMENT_NODE) { 273 value = value + getElementValue((Element ) nl.item(k)); 274 } else { 275 System.err.println("EXCEPTION: " + this.getClass().getName() + 276 ".getElementValue(): No TEXT_NODE"); 277 } 278 } 279 280 return value; 281 } 282 283 289 public void getElementValue(Element element, Vector text) { 290 NodeList nl = element.getChildNodes(); 291 292 for (int k = 0; k < nl.getLength(); k++) { 293 short nodeType = nl.item(k).getNodeType(); 294 295 if (nodeType == Node.TEXT_NODE) { 296 text.addElement(nl.item(k).getNodeValue()); 297 } else if (nodeType == Node.ELEMENT_NODE) { 298 getElementValue((Element ) nl.item(k), text); 299 } else { 300 System.err.println("EXCEPTION: " + this.getClass().getName() + 301 ".getElementValue(): No TEXT_NODE"); 302 } 303 } 304 } 305 306 311 public Document employees() { 312 DOMParserFactory dpf = new DOMParserFactory(); 313 Document doc = dpf.getDocument(); 314 Element michi = dpf.newElementNode(doc, "Employee"); 315 michi.setAttribute("Id", "0"); 316 michi.appendChild(dpf.newTextNode(doc, "Michi")); 317 318 Element levi = dpf.newElementNode(doc, "Employee"); 319 levi.setAttribute("Id", "1"); 320 levi.appendChild(dpf.newTextNode(doc, "Levi")); 321 322 Element employees = dpf.newElementNode(doc, "Employees"); 323 employees.appendChild(dpf.newTextNode(doc, "\n")); 324 employees.appendChild(michi); 325 employees.appendChild(dpf.newTextNode(doc, "\n")); 326 employees.appendChild(levi); 327 employees.appendChild(dpf.newTextNode(doc, "\n")); 328 doc.appendChild(employees); 329 330 return doc; 331 } 332 } 333 | Popular Tags |