1 16 17 package de.gulden.util.javasource; 18 19 import de.gulden.util.xml.XMLToolbox; 20 import de.gulden.util.javasource.jjt.Node; 21 import javax.xml.parsers.*; 22 import org.w3c.dom.*; 23 import java.io.*; 24 import java.util.*; 25 26 32 public class Package extends SourceObject implements PackageMember { 33 34 38 41 public Vector myClass; 42 43 46 public Vector children; 47 48 51 public Package father; 52 53 54 58 61 public Package() { 62 myClass=new Vector(); 63 children=new Vector(); 64 name=""; } 66 67 68 72 75 public void setName(String qualifiedName) { 76 super.setName(qualifiedName); 77 initEmptyFathers(); } 79 80 public void add(PackageMember p) { 81 p.addToPackage(this); 82 } 83 84 91 public Class findClass(String qualifiedName) { 92 String selfName = this.getName(); 93 boolean base = (selfName.equals("")); 94 String q; 95 if (!base) { 96 if (qualifiedName.startsWith(selfName+".")) { 97 q = qualifiedName.substring(selfName.length()+1); 98 } else { 99 return null; 100 } 101 } else { 102 q = qualifiedName; 103 } 104 String firstPart; 106 int dot = q.indexOf('.'); 107 if (dot!=-1) { 108 firstPart = q.substring(0, dot); 109 String find; 110 if (!base) { 111 find = selfName + "." + firstPart; 112 } else { 113 find = firstPart; 114 } 115 NamedIterator it=getInnerPackages(); 118 Package p=(Package )it.find(find); 119 if (p!=null) { 120 return p.findClass(qualifiedName); } 122 it=getClasses(); 124 Class c=(Class )it.find(find); 125 if (c != null) { 126 ClassInner ci = c.findInnerClass(qualifiedName); 127 return ci; 128 } else { 129 return null; 130 } 131 } else { 132 NamedIterator it=getClasses(); 134 Class c=(Class )it.find(qualifiedName); 135 return c; 136 } 137 } 138 139 146 public Package findPackage(String qualifiedName) { 147 String selfName = this.getName(); 148 boolean base = (selfName.equals("")); 149 if (selfName.equals(qualifiedName)) { 150 return this; 151 } 152 String q; 153 if (!base) { 154 if (qualifiedName.startsWith(selfName+".")) { 155 q = qualifiedName.substring(selfName.length()+1); 156 } else { 157 return null; 158 } 159 } else { 160 q = qualifiedName; 161 } 162 String find; 164 String firstPart; 165 int dot = q.indexOf('.'); 166 if (dot!=-1) { 167 firstPart = q.substring(0, dot); 168 if (!base) { 169 find = selfName + "." + firstPart; 170 } else { 171 find = firstPart; 172 } 173 } else { 175 find = qualifiedName; 177 } 178 NamedIterator it=getInnerPackages(); 179 Package p=(Package )it.find(find); 180 if ((p!=null)&&(find != qualifiedName)) { p = p.findPackage(qualifiedName); } 183 return p; 184 } 185 186 189 public NamedIterator getClasses() { 190 NamedIterator it=new NamedIterator(myClass); 191 return it; 192 } 193 194 198 public NamedIterator getAllClasses() { 199 Vector v=new Vector(); 200 for (NamedIterator it=getClasses();it.hasMore();) { 201 v.addElement(it.next()); 202 } 203 for (NamedIterator it=getInnerPackages();it.hasMore();) { 204 Package p=(Package )it.next(); 205 for (NamedIterator it2=p.getAllClasses();it2.hasMore();) { 206 v.addElement(it2.next()); 207 } 208 } 209 return new NamedIterator(v); 210 } 211 212 215 public NamedIterator getInnerPackages() { 216 return new NamedIterator(children); 217 } 218 219 222 public Package getParentPackage() { 223 return getPackage(); 224 } 225 226 229 public void addToPackage(Package p) { 230 if (this.getName().startsWith(p.getName())) { 232 if (p.getName().equals(getName())) { 234 NamedIterator classes=getClasses(); 236 while (classes.hasMore()) { 237 p.add((Class )classes.next()); 238 } 239 NamedIterator packages=getInnerPackages(); 241 while (packages.hasMore()) { 242 Package inner=(Package )packages.next(); 243 p.add(inner); 244 } 245 } 246 else if (p.getName().equals(getParentPackage().getName())) { 248 NamedIterator it=p.getInnerPackages(); 249 Package inner=(Package )it.find(getName()); if (inner!=null) { 251 inner.add(this); } 253 else { 254 p.registerPackage(this); } 256 } 257 else { 258 p.add(getParentPackage()); 260 } 261 } 262 else { 263 throw new IllegalArgumentException ("cannot add package '"+getName()+"' into package '"+p.getName()+"'"); 264 } 265 } 266 267 273 public Element buildXML(Document d) { 274 Element e; 275 if (!isBasePackage()) { 276 e=super.buildXML(d); 277 } else { 278 e=d.getDocumentElement(); 279 } 280 NamedIterator it=getClasses(); 281 while (it.hasMore()) { 282 Class c=(Class )it.next(); 283 e.appendChild(c.buildXML(d)); 284 } 285 it=getInnerPackages(); 286 while (it.hasMore()) { 287 Package p=(Package )it.next(); 288 e.appendChild(p.buildXML(d)); 289 } 290 return e; 291 } 292 293 299 public void initFromXML(Element element) throws IOException { 300 String tagname=element.getTagName(); 302 if (tagname.equals("package")) { 303 super.initFromXML(element); 304 } else if (tagname.equals("xjava")) { name=""; 306 } else { 307 throw new IOException("illegal tag name "+tagname+" expected 'xjava' or 'package'"); 308 } 309 310 initEmptyFathers(); 311 312 myClass.removeAllElements(); 314 315 NodeList nl=XMLToolbox.getChildren(element,"class"); 317 for (int i=0;i<nl.getLength();i++) { 318 Class cl=new Class (); 319 cl.setPackage(this); 320 cl.initFromXML((Element)nl.item(i)); 321 } 322 323 nl=XMLToolbox.getChildren(element,"interface"); 325 for (int i=0;i<nl.getLength();i++) { 326 Class cl=new Class (); 327 cl.setPackage(this); 328 cl.setInterface(true); 329 cl.initFromXML((Element)nl.item(i)); 330 } 331 332 children.removeAllElements(); 334 nl=XMLToolbox.getChildren(element,"package"); 335 for (int i=0;i<nl.getLength();i++) { 336 Package pa=new Package (); 337 pa.initFromXML((Element)nl.item(i)); 338 registerPackage(pa); 339 } 340 } 341 342 346 public boolean isBasePackage() { 347 return getName().equals(""); 348 } 349 350 353 public Package getBasePackage() { 354 if (isBasePackage()) { 355 return this; 356 } 357 else { 358 return getPackage().getBasePackage(); 359 } 360 } 361 362 365 protected void initEmptyFathers() { 366 if (!isBasePackage()) { 367 String fatherName=getParentPackageName(getName()); 368 Package f=new Package (); 369 f.setName(fatherName); 370 f.registerPackage(this); 371 f.initEmptyFathers(); 372 myPackage=f; 373 } 374 else { 375 myPackage=null; 376 } 377 } 378 379 382 void registerClass(Class c) { 383 if (!vectorContainsReference(myClass,c)) { 384 myClass.addElement(c); 385 } 386 } 387 388 391 void registerPackage(Package p) { 392 children.addElement(p); 393 } 394 395 398 void initFromAST(Node node) { 399 super.initFromAST(node); 401 initEmptyFathers(); 402 } 403 404 405 409 public static boolean isSourcePackage(Package base, String name) { 410 return (base.findPackage(name) != null); 411 } 412 413 417 static String getParentPackageName(String n) { 418 int pos=n.lastIndexOf('.'); 419 if (pos!=-1) { 420 return n.substring(0,pos); 421 } 422 else { 423 return ""; 424 } 425 } 426 427 432 static boolean vectorContainsReference(Vector v, Object o) { 433 for (Enumeration e=v.elements();e.hasMoreElements();) { 434 if (e.nextElement()==o) { 435 return true; 436 } 437 } 438 return false; 439 } 440 441 } | Popular Tags |