KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > config > DDCommon


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.j2ee.deployment.config;
22
23 import javax.enterprise.deploy.model.*;
24 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
25 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
26 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
27 import org.netbeans.modules.schema2beans.*;
28 import org.openide.ErrorManager;
29 import java.io.Writer JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.util.*;
32
33 abstract public class DDCommon implements DDBean {
34
35     StandardDDImpl container;
36     DDCommon parent = null;
37     final BaseBean bean;
38     final String JavaDoc xpath;
39     final String JavaDoc dtdname;
40     final ModuleDeploymentSupport support;
41     final Set childBeans = new HashSet();
42     
43     // must know about its STandardDDImpl to pass to ConfigBeanStorage
44
DDCommon(DDCommon copy) {
45         this(copy.parent,copy.bean,copy.support,copy.xpath);
46     }
47     
48     DDCommon(DDCommon parent, BaseBean bean, ModuleDeploymentSupport support, String JavaDoc dtdname) {
49         this.parent = parent;
50         this.bean = bean;
51         this.dtdname = dtdname;
52         // TODO fix the DDBeanRoot
53
this.xpath = ((parent == null) ? "" : parent.xpath) + "/" + dtdname; // NOI18N
54
this.support = support;
55         if(parent != null) parent.addChild(this);
56     }
57     
58     void addChild(DDCommon bean) {
59         childBeans.add(bean);
60     }
61     
62     void removeChild(DDCommon bean) {
63         childBeans.remove(bean);
64     }
65     
66     // find the DDBean which is a child of this DDBean that has the
67
// given corresponding BaseBean. If there is no such DDBean, return
68
// null
69
DDCommon findChild(BaseBean bean) {
70         for(Iterator i = childBeans.iterator();i.hasNext(); ) {
71             DDCommon child = (DDCommon) i.next();
72             if(child.bean == bean) return child;
73         }
74         return null;
75     }
76     
77     final public String JavaDoc getXpath() {
78         return xpath;
79     }
80     
81     final public DDBeanRoot getRoot() {
82         DDCommon root = this;
83         while(root.parent != null) root = root.parent;
84         return (DDRoot) support.getBean(root.bean);
85     }
86     
87     final public DDBean[] getChildBean(String JavaDoc xpath) {
88         return getChildrenImpl(xpath);
89     }
90     
91     public String JavaDoc getText() {
92         Writer JavaDoc w = new StringWriter JavaDoc();
93         try {
94             bean.writeNode(w);
95         } catch (Exception JavaDoc e) {
96         }
97         return w.toString();
98     }
99     
100     final public String JavaDoc[] getText(String JavaDoc xpath) {
101         StandardDDImpl[] dds = getChildrenImpl(xpath);
102         if(dds == null) return null;
103         String JavaDoc[] ret = new String JavaDoc[dds.length];
104         for(int i = 0; i < dds.length; i++)
105             ret[i] = dds[i].proxy.getText();
106         return ret;
107     }
108     
109     final private StandardDDImpl[] getChildrenImpl(String JavaDoc xpath) {
110         // System.out.println("Starting search with " + xpath);
111
xpath = ModuleDeploymentSupport.normalizePath(xpath);
112         // System.out.println("Now " + xpath);
113
DDCommon searchRoot = this;
114         if(xpath == null || xpath.equals("") || xpath.equals(".")) { // NOI18N
115
return new StandardDDImpl[] { container };
116         }
117         if(xpath.startsWith("/")) { // NOI18N
118
searchRoot = ((DDRoot)getRoot()).proxy;
119             xpath = xpath.substring(xpath.indexOf("/") + 1); // NOI18N
120
}
121         else if (xpath.equals("..")) {
122             if(parent == null) return null;
123             else return new StandardDDImpl[] { parent.container };
124         }
125         else while (xpath.startsWith("../") && searchRoot != null) {
126             searchRoot = searchRoot.parent;
127             xpath = xpath.substring(3);
128         }
129
130         Collection ret = searchRoot.search(xpath,true);
131
132         StandardDDImpl[] arr = new StandardDDImpl[ret.size()];
133         ret.toArray(arr);
134         return arr;
135     }
136     
137     Collection search(String JavaDoc xpath,boolean addCurrent) {
138         
139         Collection ret = new LinkedList();
140         
141         int index = xpath.indexOf("/"); // NOI18N
142
String JavaDoc fragment = index < 0 ? xpath : xpath.substring(0,index);
143         
144         if(isProxy()) {
145             // find all children manually
146
BeanProp prop = bean.beanProp(fragment);
147             if(prop != null) {
148                 String JavaDoc remainder = index < 0 ? "" : xpath.substring(index); // NOI18N
149
if(prop.isIndexed()) {
150                     Object JavaDoc[] values = prop.getValues();
151                     for(int i = 0; i < values.length; i++) {
152                         DDCommon ddc = prop.isBean() ? support.getBean((BaseBean) values[i]).proxy
153                         : support.getBean(prop,i).proxy;
154                         ret.addAll(ddc.search(remainder,true));
155                     }
156                 } else {
157                     DDCommon ddc = prop.isBean() ? support.getBean(prop.getBean()).proxy
158                     : support.getBean(prop,-1).proxy;
159                     ret.addAll(ddc.search(remainder,true));
160                 }
161             }
162         } else if(addCurrent) {
163             DDParser parser = new DDParser(bean,xpath);
164             
165             while(parser.hasNext()) {
166                 Object JavaDoc current = parser.next();
167                 DDParser.DDLocation location = parser.getLocation();
168                 if(location.isNode()) {
169                     BaseBean currentBean = (BaseBean) current;
170                     ret.add(support.getBean(currentBean));
171                 }
172                 else {
173                     ret.add(support.getBean(
174                     location.getRoot().getProperty(location.getName()),
175                     location.getIndex()));
176                 }
177             }
178         }
179         
180         if(index < 0) return ret;
181         
182         // PENDING optimization - keep a semaphore recording whether
183
// or not there are any children proxies, if not you can
184
// skip this loop
185
for(Iterator i = childBeans.iterator(); i.hasNext() ; ) {
186             DDCommon ddc = (DDCommon) i.next();
187             if(ddc.dtdname.equals(fragment))
188                 ret.addAll(ddc.search(xpath.substring(index),false));
189         }
190         
191         return ret;
192         
193     }
194     
195     boolean isProxy() {
196         return false;
197     }
198     
199     public void addXpathListener(String JavaDoc xpath,XpathListener listener) {
200         support.addXpathListener(this,xpath,listener);
201     }
202     
203     public void removeXpathListener(String JavaDoc xpath,XpathListener listener) {
204         support.removeXpathListener(this,xpath,listener);
205     }
206     
207     public int hashCode() {
208         return bean.hashCode();
209     }
210     
211     /* Must be overridden in subclasses, and super.equals(o) must be
212        part of the computation. */

213     public boolean equals(Object JavaDoc o) {
214         if(o instanceof DDCommon)
215             return ((DDCommon)o).bean == bean;
216         return false;
217     }
218     
219     public static String JavaDoc getRelativePath(String JavaDoc child, String JavaDoc parent) {
220         String JavaDoc relPath = child.substring(parent.length());
221         if (relPath.startsWith("/"))
222             relPath = relPath.substring(1); // NOI18N
223
return relPath;
224     }
225     
226     public String JavaDoc[] getAttributeNames() {
227         return null;
228     }
229     
230     public String JavaDoc getAttributeValue(String JavaDoc name) {
231         return null;
232     }
233     
234     public String JavaDoc getId() {
235         return null;
236     }
237     
238     public J2eeModuleProvider getModuleProvider() {
239         return support.getProvider();
240     }
241 }
242
243
Popular Tags