1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 /***
26 * Test AbstractFactory.
27 *
28 * @author Dmitri Plotnikov
29 * @version $Revision: 1.6 $ $Date: 2004/06/29 22:58:17 $
30 */
31 public class TestJDOMFactory extends AbstractFactory {
32
33 /***
34 * Create a new instance and put it in the collection on the parent object.
35 * Return <b>false</b> if this factory cannot create the requested object.
36 */
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
71 while (count <= index) {
72
73
74 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 }