1 5 package xdoclet.modules.jdo; 6 7 8 import java.io.File ; 9 import java.text.MessageFormat ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 13 import org.apache.commons.logging.Log; 14 import xjavadoc.*; 15 import xdoclet.XDocletException; 16 import xdoclet.XDocletMessages; 17 import xdoclet.XmlSubTask; 18 import xdoclet.modules.jdo.JdoDocletTask.JdoSpecVersion; 19 import xdoclet.tagshandler.PackageTagsHandler; 20 import xdoclet.template.TemplateException; 21 import xdoclet.util.LogUtil; 22 import xdoclet.util.Translator; 23 24 35 public class JdoXmlMetadataSubTask extends XmlSubTask 36 { 37 private static String DEFAULT_TEMPLATE_FILE = "resources/jdo_xml.xdt"; 38 39 private static String PACKAGE_GENERATED_FILE_NAME = "{0}.jdo"; 40 private static String CLASS_GENERATED_FILE_NAME = "{0}.jdo"; 41 42 private static String JDOXML_PUBLICID_1_0 = "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN"; 43 44 private static String JDOXML_SYSTEMID_1_0 = "http://java.sun.com/dtd/jdo_1_0.dtd"; 45 46 private static String JDOXML_DTD_FILE_NAME_1_0 = "resources/jdo_1_0.dtd"; 47 48 private static String JDOXML_PUBLICID_2_0 = "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN"; 49 50 private static String JDOXML_SYSTEMID_2_0 = "http://java.sun.com/dtd/jdo_2_0.dtd"; 51 52 private static String JDOXML_DTD_FILE_NAME_2_0 = "resources/jdo_2_0.dtd"; 53 54 private String jdoSpec = null; 55 private String generation = "class"; 56 private boolean forceGenerationPerPackage; 57 private String project = "metadata"; 58 59 60 63 public JdoXmlMetadataSubTask() 64 { 65 setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE)); 66 } 67 68 73 public String getJdoSpec() 74 { 75 if (jdoSpec != null) 76 return jdoSpec; 77 78 return (String ) getContext().getConfigParam("jdospec"); 81 } 82 83 public String getGeneration() 84 { 85 return generation; 86 } 87 88 public String getProject() 89 { 90 return project; 91 } 92 93 public void setProject(String project) 94 { 95 this.project = project; 96 } 97 98 public void setGeneration(GenerationOptionTypes value) 99 { 100 generation = value.getValue(); 101 } 102 103 108 public void setJdoSpec(JdoSpecVersion jdoSpec) 109 { 110 Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "setJdoSpec"); 112 113 log.info("new jdoSpec: " + jdoSpec.getValue()); 114 this.jdoSpec = jdoSpec.getValue(); 115 } 116 117 122 public void validateOptions() throws XDocletException 123 { 124 } 128 129 134 public void execute() throws XDocletException 135 { 136 String jdoSpec = getJdoSpec(); 137 Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "execute"); 138 139 log.info("Using jdospec \"" + jdoSpec + "\"."); 140 141 if (JdoSpecVersion.JDO_2_0.equals(jdoSpec)) { 142 setPublicId(JDOXML_PUBLICID_2_0); 143 setSystemId(JDOXML_SYSTEMID_2_0); 144 setDtdURL(getClass().getResource(JDOXML_DTD_FILE_NAME_2_0)); 145 } 146 else { 147 setPublicId(JDOXML_PUBLICID_1_0); 148 setSystemId(JDOXML_SYSTEMID_1_0); 149 setDtdURL(getClass().getResource(JDOXML_DTD_FILE_NAME_1_0)); 150 } 151 152 if (isGenerationPerClass()) 153 setDestinationFile(CLASS_GENERATED_FILE_NAME); 154 else if (isGenerationPerPackage()) 155 setDestinationFile(PACKAGE_GENERATED_FILE_NAME); 156 else 157 setDestinationFile(getProject() + ".jdo"); 158 159 startProcess(); 160 } 161 162 public void startProcess() throws XDocletException 163 { 164 Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "startProcess"); 165 166 if (log.isDebugEnabled()) { 167 log.debug("destDir.toString()=" + getDestDir()); 168 log.debug("getTemplateURL()=" + getTemplateURL()); 169 log.debug("getDestinationfile()=" + getDestinationFile()); 170 log.debug("getOfType()=" + getOfType()); 171 log.debug("getExtent()=" + getExtent()); 172 log.debug("getHavingClassTag()=" + getHavingClassTag()); 173 } 174 175 if (isGenerationPerClass()) { 176 startProcessPerClass(); 177 } 178 else if (isGenerationPerPackage()) { 179 startProcessPerPackage(); 180 } 181 else { 182 startProcessForAll(); 183 } 184 } 185 186 protected boolean isForceGenerationPerPackage() 187 { 188 return forceGenerationPerPackage; 189 } 190 191 195 protected boolean isGenerationPerClass() 196 { 197 return GenerationOptionTypes.CLASS.equals(generation); 198 } 199 200 protected boolean isGenerationPerPackage() 201 { 202 return GenerationOptionTypes.PACKAGE.equals(generation); 203 } 204 205 212 protected String getGeneratedFileName(XPackage pak) throws XDocletException 213 { 214 Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "getGeneratedFileName"); 215 String package_structure = null; 216 217 if (isPrefixWithPackageStructure() == true) { 218 package_structure = PackageTagsHandler.packageNameAsPathFor(pak); 219 220 int lastSlash = package_structure.lastIndexOf('/'); 221 222 if (lastSlash != -1) 223 package_structure = package_structure.substring(0, lastSlash); 224 } 225 226 String packageName = isPackageSubstitutionInheritanceSupported() == true ? package_structure : null; 227 228 String qualifiedName = pak.getName(); 229 int lastDot = qualifiedName.lastIndexOf('.'); 230 String name = qualifiedName.substring(lastDot + 1); 231 232 String destinationFile = MessageFormat.format(getDestinationFile(), new Object []{name}); 233 234 if (log.isDebugEnabled()) { 235 log.debug("pak=" + pak); 236 log.debug("packageName=" + packageName); 237 log.debug("destinationFile=" + destinationFile); 238 } 239 240 return new File (packageName, destinationFile).toString(); 241 } 242 243 protected void setForceGenerationPerPackage(boolean value) 244 { 245 forceGenerationPerPackage = value; 246 } 247 248 protected void startProcessPerPackage() throws XDocletException 249 { 250 Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "startProcessPerPackage"); 251 252 if (log.isDebugEnabled()) { 253 log.debug("Per package."); 254 } 255 256 Collection packages = getXJavaDoc().getSourcePackages(); 257 258 for (Iterator i = packages.iterator(); i.hasNext(); ) { 259 XPackage pakkage = (XPackage) i.next(); 260 261 if (log.isDebugEnabled()) 262 log.debug("Working on " + pakkage); 263 generateForPackage(pakkage); 264 } 265 } 266 267 273 protected void generateForPackage(XPackage pkg) throws XDocletException 274 { 275 Log log = LogUtil.getLog(getClass(), "generateForClass"); 276 String generatedFileName = getGeneratedFileName(pkg); 277 File file = new File (getDestDir().toString(), generatedFileName); 278 279 if (log.isDebugEnabled()) { 280 log.debug("destDir.toString()=" + getDestDir().toString()); 281 log.debug("getGeneratedFileName()=" + generatedFileName); 282 log.debug("file=" + file); 283 } 284 285 if (file.exists()) { 286 log.debug("File exists."); 287 288 290 boolean isGenerationNeeded = true; 293 294 if (!isGenerationNeeded) { 295 return; 296 } 297 } 298 299 file.getParentFile().mkdirs(); 300 301 try { 302 setCurrentPackage(pkg); 303 startEngine(getTemplateURL(), new File (getDestDir(), generatedFileName)); 304 } 305 catch (TemplateException e) { 306 if (e instanceof XDocletException) { 307 throw (XDocletException) e; 308 } 309 else { 310 log.debug("generateForClass()"); 311 throw new XDocletException(e, Translator.getString(XDocletMessages.class, XDocletMessages.RUNNING_FAILED)); 312 } 313 } 314 } 315 316 321 protected void engineStarted() throws XDocletException 322 { 323 String generatedFileName = getDestinationFile(); 324 325 if (isGenerationPerClass()) 326 generatedFileName = getGeneratedFileName(getCurrentClass()); 327 else if (isGenerationPerPackage()) 328 generatedFileName = getGeneratedFileName(getCurrentPackage()); 329 System.out.println(Translator.getString(XDocletMessages.class, XDocletMessages.GENERATING_SOMETHING, new String []{generatedFileName})); 330 } 331 332 336 public static class GenerationOptionTypes extends org.apache.tools.ant.types.EnumeratedAttribute 337 { 338 public final static String CLASS = "class"; 339 public final static String PACKAGE = "package"; 340 public final static String PROJECT = "project"; 341 342 347 public String [] getValues() 348 { 349 return (new String []{CLASS, PACKAGE, PROJECT}); 350 } 351 } 352 } 353 | Popular Tags |