KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > DocInfo


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: DocInfo.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.lazydom;
25
26 import java.util.HashMap JavaDoc;
27
28 import org.enhydra.xml.dom.SimpleDOMTraversal;
29 import org.enhydra.xml.driver.TestException;
30 import org.w3c.dom.Attr JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33
34 /**
35  * Information collected on a test document. Use for various tests to find
36  * their way into the DOM.
37  */

38 class DocInfo {
39     /**
40      * name/value pair to element node id entry.
41      */

42     private class AttrElementId {
43         /* the mapping */
44         public final String JavaDoc attrName;
45         public final String JavaDoc attrValue;
46         public final int elementNodeId;
47
48         /** Constructor */
49         public AttrElementId(String JavaDoc name,
50                              String JavaDoc value,
51                              int id) {
52             attrName = name;
53             attrValue = value;
54             elementNodeId = id;
55         }
56         
57         /** Equality test on name and value */
58         public boolean equals(Object JavaDoc obj) {
59             if (obj instanceof AttrElementId) {
60                 AttrElementId entry2 = (AttrElementId)obj;
61                 return attrName.equals(entry2.attrName)
62                     && attrValue.equals(entry2.attrValue);
63             } else {
64                 return false;
65             }
66         }
67
68         /** Hash code on name and value */
69         public int hashCode() {
70             return attrName.hashCode() + attrValue.hashCode();
71         }
72     }
73
74     /**
75      * Table of attribute name/value pairs to node id.
76      */

77     private HashMap JavaDoc fAttrElementIdMap = new HashMap JavaDoc();
78
79     /**
80      * Constructor. Collect data from the template document.
81      */

82     public DocInfo(LazyDocument document) {
83
84         // Traverse template tree, collecting infomation
85
SimpleDOMTraversal traverser
86             = new SimpleDOMTraversal(new SimpleDOMTraversal.Handler() {
87                     public void handleNode(Node JavaDoc node) {
88                         if (node instanceof Attr JavaDoc) {
89                             addAttrElementId((LazyAttr)node);
90                         }
91                     }
92                 });
93         traverser.traverse(document.getTemplateDocument());
94     }
95     
96     /**
97      * Add an attribute to the attrElementIdMap.
98      */

99     private void addAttrElementId(LazyAttr attr) {
100         LazyElement element = (LazyElement)attr.getOwnerElement();
101         AttrElementId attrElementId = new AttrElementId(attr.getNodeName(),
102                                                         attr.getNodeValue(),
103                                                         element.getNodeId());
104         if (!fAttrElementIdMap.containsKey(attrElementId)) {
105             fAttrElementIdMap.put(attrElementId, attrElementId);
106         }
107     }
108
109     /**
110      * Convert a attibute name/value to the containing element's node id.
111      */

112     public int getElementId(String JavaDoc attrName,
113                             String JavaDoc attrValue) {
114         // Make a key
115
AttrElementId key = new AttrElementId(attrName, attrValue, 0);
116         AttrElementId entry = (AttrElementId)fAttrElementIdMap.get(key);
117         if (entry == null) {
118             throw new TestException("Attribute/value pair not found: "
119                                     + attrName + "/" + attrValue);
120         }
121         return entry.elementNodeId;
122     }
123
124     /**
125      * Find the first Element in a lazy tree with an attribute with the
126      * given name and value. Uses the proto tree and dom converter to
127      * find the node by id.
128      */

129     public Element JavaDoc getByAttrValue(LazyDocument lazyDoc,
130                                   String JavaDoc attrName,
131                                   String JavaDoc attrValue) {
132         
133         return (Element JavaDoc)lazyDoc.getNodeById(getElementId(attrName, attrValue));
134     }
135 }
136
Popular Tags