1 package spoon.reflect.factory; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.StringTokenizer ; 9 import java.util.TreeMap ; 10 11 import spoon.reflect.Factory; 12 import spoon.reflect.declaration.CtPackage; 13 import spoon.reflect.declaration.CtType; 14 import spoon.reflect.reference.CtPackageReference; 15 16 19 public class PackageFactory extends SubFactory implements Serializable { 20 private static final long serialVersionUID = 1L; 21 22 25 private Map <String , CtPackage> packages = new TreeMap <String , CtPackage>(); 26 27 33 public PackageFactory(Factory factory) { 34 super(factory); 35 } 36 37 40 public CtPackageReference createReference(CtPackage pack) { 41 return createReference(pack.getQualifiedName()); 42 } 43 44 52 public CtPackageReference createReference(Package pack) { 53 return createReference(pack.getName()); 54 } 55 56 62 public CtPackageReference createReference(String name) { 63 if (name.equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) 64 return null; 65 CtPackageReference ref = factory.Core().createPackageReference(); 66 ref.setSimpleName(name); 67 return ref; 68 } 69 70 79 public CtPackage create(CtPackage parent, String simpleName) { 80 return getOrCreate(parent.toString() + CtPackage.PACKAGE_SEPARATOR 81 + simpleName); 82 } 83 84 90 public CtPackage getOrCreate(String qualifiedName) { 91 StringTokenizer token = new StringTokenizer (qualifiedName, 92 CtPackage.PACKAGE_SEPARATOR); 93 CtPackage last = null; 94 95 if (token.hasMoreElements()) { 96 String name = token.nextToken(); 97 if (name.length() < 0) 98 name = CtPackage.TOP_LEVEL_PACKAGE_NAME; 99 if (packages.containsKey(name)) { 100 last = packages.get(name); 101 } else { 102 last = factory.Core().createPackage(); 103 last.setSimpleName(name); 104 register(last); 105 } 106 } 107 108 while (token.hasMoreElements()) { 109 String name = token.nextToken(); 110 CtPackage next = last.getPackage(name); 111 if (next == null) { 112 next = factory.Core().createPackage(); 113 next.setSimpleName(name); 114 last.getPackages().add(next); 115 next.setParent(last); 116 } 117 last = next; 118 } 119 120 return last; 121 } 122 123 130 public CtPackage get(String qualifiedName) { 131 if (qualifiedName.contains(CtType.INNERTTYPE_SEPARATOR)) { 132 throw new RuntimeException ("Invalide package name " + qualifiedName); 133 } 134 StringTokenizer token = new StringTokenizer (qualifiedName, 135 CtPackage.PACKAGE_SEPARATOR); 136 CtPackage current = null; 137 if (token.hasMoreElements()) { 138 current = packages.get(token.nextElement()); 139 while (token.hasMoreElements() && current != null) 140 current = current.getPackage(token.nextToken()); 141 } 142 return current; 143 } 144 145 149 public Collection <CtPackage> getAll() { 150 Collection <CtPackage> packs = new ArrayList <CtPackage>(); 151 for (CtPackage pack : packages.values()) { 152 packs.addAll(getSubPackageList(pack)); 153 } 154 return packs; 155 } 156 157 160 public Collection <CtPackage> getAllRoots() { 161 return packages.values(); 162 } 163 164 private List <CtPackage> getSubPackageList(CtPackage pack) { 165 List <CtPackage> packs = new ArrayList <CtPackage>(); 166 packs.add(pack); 167 for (CtPackage p : pack.getPackages()) { 168 packs.addAll(getSubPackageList(p)); 169 } 170 return packs; 171 } 172 173 176 public void register(CtPackage pck) { 177 if (packages.containsKey(pck.getQualifiedName())) { 178 throw new RuntimeException ("package " + pck.getQualifiedName() 179 + " already created"); 180 } 181 packages.put(pck.getQualifiedName(), pck); 182 } 183 184 } | Popular Tags |