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