KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > tools > DDBeanImpl


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.geronimo.deployment.tools;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.enterprise.deploy.model.DDBean JavaDoc;
27 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
28 import javax.enterprise.deploy.model.XpathListener JavaDoc;
29
30 import org.apache.xmlbeans.XmlCursor;
31
32 /**
33  *
34  *
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class DDBeanImpl implements DDBean JavaDoc {
38     protected final DDBeanRoot JavaDoc root;
39     protected final String JavaDoc xpath;
40     protected final Map JavaDoc children;
41     protected final String JavaDoc content;
42     protected final Map JavaDoc attributeMap;
43     protected final DDBean JavaDoc parent;
44
45     public DDBeanImpl(DDBeanRoot JavaDoc root, DDBean JavaDoc parent, String JavaDoc xpath, XmlCursor c) {
46         this.root = root;
47         this.parent = parent;
48         this.xpath = xpath;
49         this.children = new HashMap JavaDoc();
50         this.attributeMap = new HashMap JavaDoc();
51         content = c.getTextValue();
52         c.push();
53         if (c.toFirstAttribute()) {
54             do {
55                 attributeMap.put(c.getName().getLocalPart(), c.getTextValue());
56             } while (c.toNextAttribute());
57         }
58         c.pop();
59         c.push();
60         if (c.toFirstChild()) {
61             do {
62                 String JavaDoc name = c.getName().getLocalPart();
63                 List JavaDoc nodes = (List JavaDoc) children.get(name);
64                 if (nodes == null) {
65                     nodes = new ArrayList JavaDoc();
66                     children.put(name, nodes);
67                 }
68                 nodes.add(new DDBeanImpl(root, this, xpath + "/" + name, c));
69             } while (c.toNextSibling());
70         }
71         c.pop();
72     }
73
74     DDBeanImpl(DDBeanImpl source, String JavaDoc xpath) {
75         this.xpath = xpath;
76         this.root = source.root;
77         this.parent = source.parent;
78         this.children = source.children;
79         this.content = source.content;
80         this.attributeMap = source.attributeMap;
81     }
82
83     public DDBeanRoot JavaDoc getRoot() {
84         return root;
85     }
86
87     public String JavaDoc getXpath() {
88         return xpath;
89     }
90
91     public String JavaDoc getText() {
92         return content;
93     }
94
95     public String JavaDoc getId() {
96         return getAttributeValue("id");
97     }
98
99     public String JavaDoc getAttributeValue(String JavaDoc attrName) {
100         String JavaDoc value = (String JavaDoc) attributeMap.get(attrName);
101         if (value == null || value.length() == 0) {
102             return null;
103         }
104         return value;
105     }
106
107     public String JavaDoc[] getText(String JavaDoc xpath) {
108         DDBean JavaDoc[] beans = getChildBean(xpath);
109         if (beans == null) {
110             return null;
111         }
112
113         String JavaDoc[] text = new String JavaDoc[beans.length];
114         for (int i = 0; i < beans.length; i++) {
115             text[i] = beans[i].getText();
116         }
117         return text;
118     }
119
120     public DDBean JavaDoc[] getChildBean(String JavaDoc xpath) {
121         if (xpath.startsWith("/")) {
122             return getRoot().getChildBean(xpath.substring(1));
123         } else if(xpath.equals(".")) {
124             return new DDBean JavaDoc[]{this};
125         } else if(xpath.startsWith("./")) {
126             return getChildBean(xpath.substring(2));
127         } else if(xpath.startsWith("..")) {
128             if(xpath.length() == 2) {
129                 return new DDBean JavaDoc[]{parent};
130             } else {
131                 return parent.getChildBean(xpath.substring(3));
132             }
133         }
134         int index = xpath.indexOf('/');
135         if (index == -1) {
136             List JavaDoc beans = (List JavaDoc) children.get(xpath);
137             if (beans == null) {
138                 return null;
139             }
140             DDBean JavaDoc[] newDDBeans = (DDBean JavaDoc[]) beans.toArray(new DDBean JavaDoc[beans.size()]);
141             for (int i = 0; i < newDDBeans.length; i++) {
142                 newDDBeans[i] = new DDBeanImpl((DDBeanImpl) newDDBeans[i], xpath);
143             }
144             return newDDBeans;
145         } else {
146             List JavaDoc childBeans = (List JavaDoc) children.get(xpath.substring(0, index));
147             if (childBeans == null) {
148                 return null;
149             }
150             String JavaDoc path = xpath.substring(index + 1);
151             List JavaDoc beans = new ArrayList JavaDoc();
152             for (Iterator JavaDoc i = childBeans.iterator(); i.hasNext();) {
153                 DDBean JavaDoc bean = (DDBean JavaDoc) i.next();
154                 DDBean JavaDoc[] childs = bean.getChildBean(path);
155                 if (childs != null) {
156                     for (int j = 0; j < childs.length; j++) {
157                         beans.add(new DDBeanImpl((DDBeanImpl) childs[j], xpath));
158                     }
159                 }
160             }
161             return beans.size() > 0 ? (DDBean JavaDoc[]) beans.toArray(new DDBean JavaDoc[beans.size()]) : null;
162         }
163     }
164
165     public String JavaDoc[] getAttributeNames() {
166         return (String JavaDoc[]) attributeMap.keySet().toArray(new String JavaDoc[attributeMap.size()]);
167     }
168
169     public void addXpathListener(String JavaDoc xpath, XpathListener JavaDoc xpl) {
170     }
171
172     public void removeXpathListener(String JavaDoc xpath, XpathListener JavaDoc xpl) {
173     }
174
175     public boolean equals(Object JavaDoc other) {
176         if (other.getClass() != DDBeanImpl.class) {
177             return false;
178         }
179         DDBeanImpl otherdd = (DDBeanImpl) other;
180         return xpath.equals(otherdd.xpath)
181                 && children.equals(otherdd.children)
182                 && attributeMap.equals(otherdd.attributeMap)
183                 && root.equals(otherdd.root);
184     }
185
186     public int hashCode() {
187         return xpath.hashCode() ^ attributeMap.hashCode() ^ root.hashCode();
188     }
189 }
190
Popular Tags