1 16 package org.apache.commons.jxpath.ri.model.jdom; 17 18 import java.util.List ; 19 20 import org.apache.commons.jxpath.AbstractFactory; 21 import org.apache.commons.jxpath.JXPathContext; 22 import org.apache.commons.jxpath.Pointer; 23 import org.jdom.Element; 24 25 31 public class TestJDOMFactory extends AbstractFactory { 32 33 37 public boolean createObject( 38 JXPathContext context, 39 Pointer pointer, 40 Object parent, 41 String name, 42 int index) 43 { 44 if (name.equals("location") 45 || name.equals("address") 46 || name.equals("street")) { 47 addJDOMElement((Element) parent, index, name, null); 48 return true; 49 } 50 if (name.startsWith("price:")) { 51 String namespaceURI = context.getNamespaceURI("price"); 52 addJDOMElement((Element) parent, index, name, namespaceURI); 53 return true; 54 } 55 56 return false; 57 } 58 59 private void addJDOMElement(Element parent, int index, String tag, String namespaceURI) { 60 List children = parent.getContent(); 61 int count = 0; 62 for (int i = 0; i < children.size(); i++) { 63 Object child = children.get(i); 64 if (child instanceof Element 65 && ((Element) child).getQualifiedName().equals(tag)) { 66 count++; 67 } 68 } 69 70 while (count <= index) { 72 Element newElement; 75 if (namespaceURI != null) { 76 String prefix = tag.substring(0, tag.indexOf(':')); 77 tag = tag.substring(tag.indexOf(':') + 1); 78 newElement = new Element(tag, prefix, namespaceURI); 79 } 80 else { 81 newElement = new Element(tag); 82 } 83 parent.addContent(newElement); 84 count++; 85 } 86 } 87 88 public boolean declareVariable(JXPathContext context, String name) { 89 return false; 90 } 91 } 92 | Popular Tags |