KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jxpath > ri > model > dom > TestDOMFactory


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.dom;
17
18 import org.apache.commons.jxpath.AbstractFactory;
19 import org.apache.commons.jxpath.JXPathContext;
20 import org.apache.commons.jxpath.Pointer;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 /**
25  * Test AbstractFactory.
26  *
27  * @author Dmitri Plotnikov
28  * @version $Revision: 1.6 $ $Date: 2004/06/29 22:58:17 $
29  */

30 public class TestDOMFactory extends AbstractFactory {
31
32     /**
33      * Return <b>false</b> if this factory cannot create the requested object.
34      */

35     public boolean createObject(
36         JXPathContext context,
37         Pointer pointer,
38         Object JavaDoc parent,
39         String JavaDoc name,
40         int index)
41     {
42         if (name.equals("location")
43             || name.equals("address")
44             || name.equals("street")) {
45             addDOMElement((Node JavaDoc) parent, index, name, null);
46             return true;
47         }
48         if (name.startsWith("price:")) {
49             String JavaDoc namespaceURI = context.getNamespaceURI("price");
50             addDOMElement((Node JavaDoc) parent, index, name, namespaceURI);
51             return true;
52         }
53         return false;
54     }
55
56     private void addDOMElement(Node JavaDoc parent, int index, String JavaDoc tag, String JavaDoc namespaceURI) {
57         Node JavaDoc child = parent.getFirstChild();
58         int count = 0;
59         while (child != null) {
60             if (child.getNodeName().equals(tag)) {
61                 count++;
62             }
63             child = child.getNextSibling();
64         }
65
66         // Keep inserting new elements until we have index + 1 of them
67
while (count <= index) {
68             Document JavaDoc doc = parent.getOwnerDocument();
69             Node JavaDoc newElement;
70             if (namespaceURI == null) {
71                 newElement = doc.createElement(tag);
72             }
73             else {
74                 newElement = doc.createElementNS(namespaceURI, tag);
75             }
76        
77             parent.appendChild(newElement);
78             count++;
79         }
80     }
81
82     public boolean declareVariable(JXPathContext context, String JavaDoc name) {
83         return false;
84     }
85 }
86
Popular Tags