1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.model.jdom;
18
19 import java.util.List;
20
21 import org.apache.commons.jxpath.AbstractFactory;
22 import org.apache.commons.jxpath.JXPathContext;
23 import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
24 import org.apache.commons.jxpath.xml.DocumentContainer;
25
26 import org.jdom.Attribute;
27 import org.jdom.CDATA;
28 import org.jdom.Document;
29 import org.jdom.Element;
30 import org.jdom.Text;
31
32
33
34
35
36
37
38 public class JDOMModelTest extends XMLModelTestCase {
39
40 protected String getModel() {
41 return DocumentContainer.MODEL_JDOM;
42 }
43
44 public void testGetNode() {
45 assertXPathNodeType(context, "/", Document.class);
46 assertXPathNodeType(context, "/vendor/location", Element.class);
47 assertXPathNodeType(context, "//location/@name", Attribute.class);
48 assertXPathNodeType(context, "//vendor", Element.class); //bugzilla #38586
49 }
50
51 public void testGetElementDescendantOrSelf() {
52 JXPathContext childContext = context.getRelativeContext(context.getPointer("/vendor"));
53 assertTrue(childContext.getContextBean() instanceof Element);
54 assertXPathNodeType(childContext, "//vendor", Element.class);
55 }
56
57 public void testID() {
58
59 }
60
61 protected AbstractFactory getAbstractFactory() {
62 return new TestJDOMFactory();
63 }
64
65 protected String getXMLSignature(
66 Object node,
67 boolean elements,
68 boolean attributes,
69 boolean text,
70 boolean pi)
71 {
72 StringBuffer buffer = new StringBuffer();
73 appendXMLSignature(buffer, node, elements, attributes, text, pi);
74 return buffer.toString();
75 }
76
77 private void appendXMLSignature(
78 StringBuffer buffer,
79 Object object,
80 boolean elements,
81 boolean attributes,
82 boolean text,
83 boolean pi)
84 {
85 if (object instanceof Document) {
86 buffer.append("<D>");
87 appendXMLSignature(
88 buffer,
89 ((Document) object).getContent(),
90 elements,
91 attributes,
92 text,
93 pi);
94 buffer.append("</D");
95 }
96 else if (object instanceof Element) {
97 String tag = elements ? ((Element) object).getName() : "E";
98 buffer.append("<");
99 buffer.append(tag);
100 buffer.append(">");
101 appendXMLSignature(
102 buffer,
103 ((Element) object).getContent(),
104 elements,
105 attributes,
106 text,
107 pi);
108 buffer.append("</");
109 buffer.append(tag);
110 buffer.append(">");
111 }
112 else if (object instanceof Text || object instanceof CDATA) {
113 if (text) {
114 String string = ((Text) object).getText();
115 string = string.replace('\n', '=');
116 buffer.append(string);
117 }
118 }
119 }
120
121 private void appendXMLSignature(
122 StringBuffer buffer,
123 List children,
124 boolean elements,
125 boolean attributes,
126 boolean text,
127 boolean pi)
128 {
129 for (int i = 0; i < children.size(); i++) {
130 appendXMLSignature(
131 buffer,
132 children.get(i),
133 elements,
134 attributes,
135 text,
136 pi);
137 }
138 }
139 }