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