1 5 package xdoclet.tagshandler; 6 7 import java.io.Serializable ; 8 import java.util.*; 9 10 import xjavadoc.*; 11 12 import xdoclet.DocletContext; 13 import xdoclet.XDocletException; 14 import xdoclet.XDocletMessages; 15 import xdoclet.template.TemplateException; 16 import xdoclet.util.Translator; 17 18 26 public class PackageTagsHandler extends AbstractProgramElementTagsHandler 27 { 28 35 public static String getPackageNameFor(XPackage pak, boolean withSubstitution) 36 { 37 return getPackageNameFor(pak.getName(), withSubstitution); 38 } 39 40 46 public static String getPackageNameFor(String packageName) 47 { 48 return getPackageNameFor(packageName, true); 49 } 50 51 59 public static String getPackageNameFor(String packageName, boolean withSubstitution) 60 { 61 ArrayList packageSubstitutions = getPackageSubstitutions(DocletContext.getInstance().getActiveSubTask().getSubTaskName()); 62 63 if (packageSubstitutions == null || !withSubstitution) { 64 return packageName; 65 } 66 67 for (int i = 0; i < packageSubstitutions.size(); i++) { 68 PackageSubstitution ps = (PackageSubstitution) packageSubstitutions.get(i); 69 StringTokenizer st = new StringTokenizer(ps.getPackages(), ",", false); 70 71 if (ps.getUseFirst() == false) { 72 while (st.hasMoreTokens()) { 73 String packages = st.nextToken(); 74 String suffix = "." + packages; 75 76 if (packageName.endsWith(suffix)) { 77 if (ps.getSubstituteWith() == null || ps.getSubstituteWith().length() == 0) { 78 packageName = packageName.substring(0, packageName.length() - suffix.length()); 79 } 80 else { 81 packageName = packageName.substring(0, packageName.length() - 82 suffix.length()) + '.' + ps.getSubstituteWith(); 83 } 84 break; 85 } 86 } 87 } 88 else { 89 packageName = replaceInline(packageName, ps.getPackages(), ps.getSubstituteWith()); 90 } 91 } 92 93 return packageName; 94 } 95 96 102 public static ArrayList getPackageSubstitutions(String subtaskName) 103 { 104 ArrayList packageSubstitutions = null; 107 boolean supportsPackageSubstitutionInheritance = true; 108 109 Boolean supports = ((Boolean ) DocletContext.getInstance().getConfigParam(subtaskName + ".packageSubstitutionInheritanceSupported")); 110 111 if (supports != null) { 112 supportsPackageSubstitutionInheritance = supports.booleanValue(); 113 } 114 115 packageSubstitutions = (ArrayList) DocletContext.getInstance().getConfigParam(subtaskName + ".packageSubstitutions"); 116 117 if (supportsPackageSubstitutionInheritance && (packageSubstitutions == null || packageSubstitutions.isEmpty())) { 119 packageSubstitutions = (ArrayList) DocletContext.getInstance().getConfigParam("packageSubstitutions"); 120 } 121 122 return packageSubstitutions; 123 } 124 125 132 public static String packageNameAsPathFor(XPackage pak) 133 { 134 return getPackageNameFor(pak, true).replace('.', '/'); 135 } 136 137 144 public static String packageNameAsPathWithoutSubstitutionFor(XPackage pak) 145 { 146 return getPackageNameFor(pak, false).replace('.', '/'); 147 } 148 149 156 public static String packageNameAsPathFor(String qualifiedName) 157 { 158 String qName = qualifiedName; 159 160 ArrayList pss = getPackageSubstitutions(DocletContext.getInstance().getActiveSubTask().getSubTaskName()); 161 162 PackageSubstitution ps; 163 164 for (int i = 0; i < pss.size(); i++) { 165 ps = (PackageSubstitution) pss.get(i); 166 if (ps.getUseFirst() == true) { 167 qName = replaceInline(qName, ps.getPackages(), ps.getSubstituteWith()); 168 } 169 } 170 171 return qName.replace('.', '/'); 172 } 173 174 183 public static String replaceInline(String original, String oldOne, String newOne) 184 { 185 int index = original.indexOf(oldOne); 186 187 if (index > -1) 188 return original.substring(0, index) + newOne + original.substring(index + oldOne.length()); 189 else 190 return original; 191 } 192 193 202 public String packageName() throws XDocletException 203 { 204 if (getCurrentPackage() != null) { 205 return getCurrentPackage().getName(); 207 } 208 else { 209 return getCurrentClass().getContainingPackage().getName(); 210 } 211 } 212 213 220 public void packageOf(String template) throws XDocletException 221 { 222 getEngine().print(getPackageNameFrom(template)); 223 } 224 225 233 public void packageDeclarationOf(String template) throws XDocletException 234 { 235 String packageName = getPackageNameFrom(template); 236 237 if (packageName != null && packageName.length() > 0) { 238 getEngine().print("package " + packageName + ";"); 239 } 240 } 241 242 258 public void forAllPackages(String template, Properties attributes) throws XDocletException 259 { 260 Collection classes = getXJavaDoc().getSourceClasses(); 261 SortedSet packages = new TreeSet(); 262 263 for (Iterator i = classes.iterator(); i.hasNext(); ) { 264 XClass clazz = (XClass) i.next(); 265 266 packages.add(clazz.getContainingPackage()); 267 } 268 269 XPackage currentPackage = null; 270 271 for (Iterator packageIterator = packages.iterator(); packageIterator.hasNext(); ) { 272 currentPackage = (XPackage) packageIterator.next(); 273 setCurrentPackage(currentPackage); 274 generate(template); 275 } 276 setCurrentPackage(null); 279 } 280 281 288 public String packageNameAsPath() throws XDocletException 289 { 290 return packageNameAsPathFor(packageName()); 291 } 292 293 300 private String getPackageNameFrom(String template) throws XDocletException 301 { 302 try { 303 String fullClassName = getEngine().outputOf(template); 304 int pos = fullClassName.lastIndexOf('.'); 305 306 if (pos < 0) { 307 return ""; 308 } 309 else { 310 return getPackageNameFor(fullClassName.substring(0, pos), true); 311 } 312 } 313 catch (TemplateException ex) { 314 throw new XDocletException(ex, Translator.getString(XDocletMessages.class, XDocletMessages.METHOD_FAILED, new String []{"packageOf"})); 315 } 316 317 } 318 319 351 public static class PackageSubstitution implements Serializable 352 { 353 private String packages = null; 354 private String substituteWith = null; 355 private boolean useFirst = false; 356 357 362 public String getPackages() 363 { 364 return packages; 365 } 366 367 372 public String getSubstituteWith() 373 { 374 return substituteWith; 375 } 376 377 383 public boolean getUseFirst() 384 { 385 return this.useFirst; 386 } 387 388 393 public void setPackages(String packages) 394 { 395 this.packages = packages; 396 } 397 398 403 public void setSubstituteWith(String substituteWith) 404 { 405 this.substituteWith = substituteWith; 406 } 407 408 413 public void setUseFirst(boolean first) 414 { 415 this.useFirst = first; 416 417 } 418 } 419 } 420 | Popular Tags |