KickJava   Java API By Example, From Geeks To Geeks.

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

220     public boolean equals(Object JavaDoc o) {
221         if(o instanceof DDCommon)
222             return ((DDCommon)o).bean == bean;
223         return false;
224     }
225     
226     void fireEvent(XpathEvent xe) {
227         // System.out.println("Got event " + xe + " at " + this);
228
if(xe.isChangeEvent()) {
229             notifyChange(xe);
230         } else {
231             // xpath is a prefix of eventXpath
232
// System.out.println("Xpath is " + xpath);
233
String JavaDoc eventXpath = xe.getBean().getXpath();
234             // System.out.println("eventXpath is " + eventXpath);
235
// take away xpath
236
String JavaDoc relPath = getRelativePath(eventXpath, xpath);
237             ConfigBeanStorage[] confBeans = getConfigBeans();
238             for (int i = 0; i < confBeans.length; i++) {
239                 try {
240                     confBeans[i].fireEvent(relPath,xe);
241                 } catch (ConfigurationException JavaDoc e) {
242                     // PENDING need to do something better here with the CE?
243
ErrorManager.getDefault().log(ErrorManager.WARNING, e.getMessage());
244                 }
245             }
246         }
247         if(parent != null) {
248             parent.fireEvent(xe);
249         }
250     }
251     public static String JavaDoc getRelativePath(String JavaDoc child, String JavaDoc parent) {
252         String JavaDoc relPath = child.substring(parent.length());
253         if (relPath.startsWith("/"))
254             relPath = relPath.substring(1); // NOI18N
255
return relPath;
256     }
257     void notifyChange(XpathEvent event) {
258         ConfigBeanStorage[] confBeans = getConfigBeans();
259         for (int i = 0; i < confBeans.length; i++) {
260             confBeans[i].bean.notifyDDChange(event);
261         }
262     }
263     
264     // PENDING move the fireCustomizerListeners to this class as well.
265
public void addConfigBean(ConfigBeanStorage cbs) {
266         configBeans.add(cbs);
267     }
268     
269     public void removeConfigBean(ConfigBeanStorage cbs) {
270         configBeans.remove(cbs);
271     }
272     
273     public ConfigBeanStorage[] getConfigBeans() {
274         ConfigBeanStorage[] ret = new ConfigBeanStorage[configBeans.size()];
275         configBeans.toArray(ret);
276         return ret;
277     }
278     
279     public String JavaDoc[] getAttributeNames() {
280         return null;
281     }
282     
283     public String JavaDoc getAttributeValue(String JavaDoc name) {
284         return null;
285     }
286     
287     public String JavaDoc getId() {
288         return null;
289     }
290     
291     public J2eeModuleProvider getModuleProvider() {
292         return support.getProvider();
293     }
294 }
295
296
Popular Tags