1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.model.dom;
18
19 import org.apache.commons.jxpath.AbstractFactory;
20 import org.apache.commons.jxpath.JXPathContext;
21 import org.apache.commons.jxpath.Pointer;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Node;
24
25
26
27
28
29
30
31 public class TestDOMFactory extends AbstractFactory {
32
33
34
35
36 public boolean createObject(
37 JXPathContext context,
38 Pointer pointer,
39 Object parent,
40 String name,
41 int index)
42 {
43 if (name.equals("location")
44 || name.equals("address")
45 || name.equals("street")) {
46 addDOMElement((Node) parent, index, name, null);
47 return true;
48 }
49 if (name.startsWith("price:")) {
50 String namespaceURI = context.getNamespaceURI("price");
51 addDOMElement((Node) parent, index, name, namespaceURI);
52 return true;
53 }
54 return false;
55 }
56
57 private void addDOMElement(Node parent, int index, String tag, String namespaceURI) {
58 Node child = parent.getFirstChild();
59 int count = 0;
60 while (child != null) {
61 if (child.getNodeName().equals(tag)) {
62 count++;
63 }
64 child = child.getNextSibling();
65 }
66
67
68 while (count <= index) {
69 Document doc = parent.getOwnerDocument();
70 Node newElement;
71 if (namespaceURI == null) {
72 newElement = doc.createElement(tag);
73 }
74 else {
75 newElement = doc.createElementNS(namespaceURI, tag);
76 }
77
78 parent.appendChild(newElement);
79 count++;
80 }
81 }
82
83 public boolean declareVariable(JXPathContext context, String name) {
84 return false;
85 }
86 }