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  
18  package org.apache.commons.jxpath.ri.model.dom;
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.w3c.dom.Document;
24  import org.w3c.dom.Node;
25  
26  /**
27   * Test AbstractFactory.
28   */
29  public class TestDOMFactory extends AbstractFactory {
30  
31      private void addDOMElement(final Node parent, final int index, final String tag, final String namespaceURI) {
32          Node child = parent.getFirstChild();
33          int count = 0;
34          while (child != null) {
35              if (child.getNodeName().equals(tag)) {
36                  count++;
37              }
38              child = child.getNextSibling();
39          }
40          // Keep inserting new elements until we have index + 1 of them
41          while (count <= index) {
42              final Document doc = parent.getOwnerDocument();
43              Node newElement;
44              if (namespaceURI == null) {
45                  newElement = doc.createElement(tag);
46              } else {
47                  newElement = doc.createElementNS(namespaceURI, tag);
48              }
49              parent.appendChild(newElement);
50              count++;
51          }
52      }
53  
54      /**
55       * Return <strong>false</strong> if this factory cannot create the requested object.
56       */
57      @Override
58      public boolean createObject(final JXPathContext context, final Pointer pointer, final Object parent, final String name, final int index) {
59          if (name.equals("location") || name.equals("address") || name.equals("street")) {
60              addDOMElement((Node) parent, index, name, null);
61              return true;
62          }
63          if (name.startsWith("price:")) {
64              final String namespaceURI = context.getNamespaceURI("price");
65              addDOMElement((Node) parent, index, name, namespaceURI);
66              return true;
67          }
68          return false;
69      }
70  
71      @Override
72      public boolean declareVariable(final JXPathContext context, final String name) {
73          return false;
74      }
75  }