1 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 ; 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 ; 30 import java.io.StringWriter ; 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 xpath; 39 final String dtdname; 40 final ModuleDDSupport support; 41 final Set configBeans = new HashSet(); 42 final Set childBeans = new HashSet(); 43 44 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 dtdname) { 51 this.parent = parent; 52 this.bean = bean; 53 this.dtdname = dtdname; 54 this.xpath = ((parent == null) ? "" : parent.xpath) + "/" + dtdname; 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 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 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 xpath) { 95 return getChildrenImpl(xpath); 96 } 97 98 public String getText() { 99 Writer w = new StringWriter (); 100 try { 101 bean.writeNode(w); 102 } catch (Exception e) { 103 } 104 return w.toString(); 105 } 106 107 final public String [] getText(String xpath) { 108 StandardDDImpl[] dds = getChildrenImpl(xpath); 109 if(dds == null) { 110 return null; 111 } 112 String [] ret = new String [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 xpath) { 120 xpath = ModuleDDSupport.normalizePath(xpath); 122 DDCommon searchRoot = this; 124 if(xpath == null || xpath.equals("") || xpath.equals(".")) return new StandardDDImpl[] { container }; 126 if(xpath.startsWith("/")) { searchRoot = ((DDRoot)getRoot()).proxy; 128 xpath = xpath.substring(xpath.indexOf("/") + 1); } 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 xpath,boolean addCurrent) { 145 146 Collection ret = new LinkedList(); 147 148 int index = xpath.indexOf("/"); String fragment = index < 0 ? xpath : xpath.substring(0,index); 150 151 if(isProxy()) { 152 BeanProp prop = bean.beanProp(fragment); 154 if(prop != null) { 155 String remainder = index < 0 ? "" : xpath.substring(index); if(prop.isIndexed()) { 157 Object [] 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 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 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 xpath,XpathListener listener) { 207 support.addXpathListener(this,xpath,listener); 208 } 209 210 public void removeXpathListener(String xpath,XpathListener listener) { 211 support.removeXpathListener(this,xpath,listener); 212 } 213 214 public int hashCode() { 215 return bean.hashCode(); 216 } 217 218 220 public boolean equals(Object o) { 221 if(o instanceof DDCommon) 222 return ((DDCommon)o).bean == bean; 223 return false; 224 } 225 226 void fireEvent(XpathEvent xe) { 227 if(xe.isChangeEvent()) { 229 notifyChange(xe); 230 } else { 231 String eventXpath = xe.getBean().getXpath(); 234 String 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 e) { 242 ErrorManager.getDefault().log(ErrorManager.WARNING, e.getMessage()); 244 } 245 } 246 } 247 if(parent != null) { 248 parent.fireEvent(xe); 249 } 250 } 251 public static String getRelativePath(String child, String parent) { 252 String relPath = child.substring(parent.length()); 253 if (relPath.startsWith("/")) 254 relPath = relPath.substring(1); 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 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 [] getAttributeNames() { 280 return null; 281 } 282 283 public String getAttributeValue(String name) { 284 return null; 285 } 286 287 public String getId() { 288 return null; 289 } 290 291 public J2eeModuleProvider getModuleProvider() { 292 return support.getProvider(); 293 } 294 } 295 296 | Popular Tags |