View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests JXPath with JDOM
34   *
35   * @author Dmitri Plotnikov
36   * @version $Revision: 694353 $ $Date: 2008-09-11 19:04:57 +0200 (Do, 11 Sep 2008) $
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          // id() is not supported by JDOM
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 }