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.jdom;
19  
20  import java.util.List;
21  
22  import org.apache.commons.jxpath.AbstractFactory;
23  import org.apache.commons.jxpath.JXPathContext;
24  import org.apache.commons.jxpath.Pointer;
25  import org.jdom.Element;
26  
27  /**
28   * Test AbstractFactory.
29   */
30  public class TestJDOMFactory extends AbstractFactory {
31  
32      private void addJDOMElement(final Element parent, final int index, String tag, final String namespaceURI) {
33          final List children = parent.getContent();
34          int count = 0;
35          for (final Object child : children) {
36              if (child instanceof Element && ((Element) child).getQualifiedName().equals(tag)) {
37                  count++;
38              }
39          }
40          // Keep inserting new elements until we have index + 1 of them
41          while (count <= index) {
42              // In a real factory we would need to do the right thing with
43              // the namespace prefix.
44              Element newElement;
45              if (namespaceURI != null) {
46                  final String prefix = tag.substring(0, tag.indexOf(':'));
47                  tag = tag.substring(tag.indexOf(':') + 1);
48                  newElement = new Element(tag, prefix, namespaceURI);
49              } else {
50                  newElement = new Element(tag);
51              }
52              parent.addContent(newElement);
53              count++;
54          }
55      }
56  
57      /**
58       * Create a new instance and put it in the collection on the parent object. Return <strong>false</strong> if this factory cannot create the requested
59       * object.
60       */
61      @Override
62      public boolean createObject(final JXPathContext context, final Pointer pointer, final Object parent, final String name, final int index) {
63          if (name.equals("location") || name.equals("address") || name.equals("street")) {
64              addJDOMElement((Element) parent, index, name, null);
65              return true;
66          }
67          if (name.startsWith("price:")) {
68              final String namespaceURI = context.getNamespaceURI("price");
69              addJDOMElement((Element) parent, index, name, namespaceURI);
70              return true;
71          }
72          return false;
73      }
74  
75      @Override
76      public boolean declareVariable(final JXPathContext context, final String name) {
77          return false;
78      }
79  }