1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // Keep inserting new elements until we have index + 1 of them
71          while (count <= index) {
72              // In a real factory we would need to do the right thing with
73              // the namespace prefix.
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  }