1 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 ; 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 ModuleDeploymentSupport support; 41 final Set childBeans = new HashSet(); 42 43 DDCommon(DDCommon copy) { 45 this(copy.parent,copy.bean,copy.support,copy.xpath); 46 } 47 48 DDCommon(DDCommon parent, BaseBean bean, ModuleDeploymentSupport support, String dtdname) { 49 this.parent = parent; 50 this.bean = bean; 51 this.dtdname = dtdname; 52 this.xpath = ((parent == null) ? "" : parent.xpath) + "/" + dtdname; 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 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 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 xpath) { 88 return getChildrenImpl(xpath); 89 } 90 91 public String getText() { 92 Writer w = new StringWriter (); 93 try { 94 bean.writeNode(w); 95 } catch (Exception e) { 96 } 97 return w.toString(); 98 } 99 100 final public String [] getText(String xpath) { 101 StandardDDImpl[] dds = getChildrenImpl(xpath); 102 if(dds == null) return null; 103 String [] ret = new String [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 xpath) { 110 xpath = ModuleDeploymentSupport.normalizePath(xpath); 112 DDCommon searchRoot = this; 114 if(xpath == null || xpath.equals("") || xpath.equals(".")) { return new StandardDDImpl[] { container }; 116 } 117 if(xpath.startsWith("/")) { searchRoot = ((DDRoot)getRoot()).proxy; 119 xpath = xpath.substring(xpath.indexOf("/") + 1); } 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 xpath,boolean addCurrent) { 138 139 Collection ret = new LinkedList(); 140 141 int index = xpath.indexOf("/"); String fragment = index < 0 ? xpath : xpath.substring(0,index); 143 144 if(isProxy()) { 145 BeanProp prop = bean.beanProp(fragment); 147 if(prop != null) { 148 String remainder = index < 0 ? "" : xpath.substring(index); if(prop.isIndexed()) { 150 Object [] 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 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 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 xpath,XpathListener listener) { 200 support.addXpathListener(this,xpath,listener); 201 } 202 203 public void removeXpathListener(String xpath,XpathListener listener) { 204 support.removeXpathListener(this,xpath,listener); 205 } 206 207 public int hashCode() { 208 return bean.hashCode(); 209 } 210 211 213 public boolean equals(Object o) { 214 if(o instanceof DDCommon) 215 return ((DDCommon)o).bean == bean; 216 return false; 217 } 218 219 public static String getRelativePath(String child, String parent) { 220 String relPath = child.substring(parent.length()); 221 if (relPath.startsWith("/")) 222 relPath = relPath.substring(1); return relPath; 224 } 225 226 public String [] getAttributeNames() { 227 return null; 228 } 229 230 public String getAttributeValue(String name) { 231 return null; 232 } 233 234 public String getId() { 235 return null; 236 } 237 238 public J2eeModuleProvider getModuleProvider() { 239 return support.getProvider(); 240 } 241 } 242 243 | Popular Tags |