User: rinkrank
  Date: 02/05/23 18:04:45

  Modified:    core/src/xdoclet Tag: MODULE_REFACTORING_BRANCH
                        DocletTask.java
  Log:
  improved module loading mechanism
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.39.2.18 +55 -83    xdoclet/core/src/xdoclet/DocletTask.java
  
  Index: DocletTask.java
  ===================================================================
  RCS file: /cvsroot/xdoclet/xdoclet/core/src/xdoclet/DocletTask.java,v
  retrieving revision 1.39.2.17
  retrieving revision 1.39.2.18
  diff -u -w -r1.39.2.17 -r1.39.2.18
  --- DocletTask.java   20 May 2002 17:13:15 -0000      1.39.2.17
  +++ DocletTask.java   24 May 2002 01:04:45 -0000      1.39.2.18
  @@ -45,9 +45,13 @@
   {
       // ant will replace the tag with the version propperty specified in build.xml
       public final static String XDOCLET_VERSION = "@VERSION@";
  -    private static Map subtaskMap;
   
       protected transient DocletContext context = null;
  +
  +    /**
  +     * logical name (java.lang.String) -> subtask (xdoclet.SubTask or a subclass of 
it)
  +     */
  +    private final Map subtaskMap = new HashMap();
       private List    modules = null;
       private File    destDir;
       private File    mergeDir;
  @@ -58,76 +62,65 @@
       private List    subTasks = new ArrayList();
       private List    configParams = new ArrayList();
   
  -    public DocletTask() throws BuildException
  -    {
  +    static {
           try {
  -            if (subtaskMap == null) {
  -                String classpath = ((AntClassLoader) 
getClass().getClassLoader()).getClasspath();
  +            String classpath = ((AntClassLoader) 
DocletTask.class.getClassLoader()).getClasspath();
   
                   System.setProperty("xdoclet.class.path", classpath);
  -
  -                List modules = ModuleFinder.findModules(true);
  -
  -                subtaskMap = new HashMap();
  -                registerModules(modules);
  -            }
           }
           catch (ClassCastException e) {
  -            throw new BuildException("xdoclet.jar should not be on the system 
classpath when starting Ant. It should be on a path passed to the taskdef.");
  +            throw new BuildException("xdoclet.jar should not be on the system 
classpath when starting Ant. It should be on a path passed to the <taskdef> using 
classpath or classpathref.");
           }
       }
   
  -    private static void registerModules(List modules) throws BuildException
  +    public DocletTask() throws BuildException
       {
  -        Log log = LogUtil.getLog(DocletTask.class, "registerModules");
  +        Log log = LogUtil.getLog(getClass(), "DocletTask");
   
  +        // Register subtasks that apply to us (they do if they in xdoclet.xml have 
declared us as parent)
  +        List modules = ModuleFinder.findModules();
           Iterator i = modules.iterator();
   
           while (i.hasNext()) {
               XDocletModule module = (XDocletModule) i.next();
  +            List subTaskDefinitions = module.getSubTaskDefinitions();
  +            Iterator j = subTaskDefinitions.iterator();
   
  -            log.debug("Registering module " + module);
  -
  -            // Register tag handlers defined in the module
  -            List tagHandlerDefinitions = module.getTagHandlerDefinitions();
  -            Iterator k = tagHandlerDefinitions.iterator();
  -
  -            while (k.hasNext()) {
  -                TagHandlerDefinition thd = (TagHandlerDefinition) k.next();
  -
  -                log.debug("Registering tag handler " + thd.namespace);
  +            while (j.hasNext()) {
  +                SubTaskDefinition subTaskDefinition = (SubTaskDefinition) j.next();
   
                   try {
  -                    TemplateTagHandler handler = (TemplateTagHandler) 
Class.forName(thd.className).newInstance();
  -                    // get ANOTHER instance for template parser. So it would not 
overwrite
  -                    // template engine settings.
  +                    Class parentTaskClass = 
Class.forName(subTaskDefinition.parentTaskClass);
   
  -                    TemplateTagHandler handlerForParser = (TemplateTagHandler) 
Class.forName(thd.className).newInstance();
  +                    if (parentTaskClass.isAssignableFrom(getClass())) {
  +                        if (subtaskMap.containsKey(subTaskDefinition.name)) {
  +                            String conflictingSubTaskClassName = 
subtaskMap.get(subTaskDefinition.name).getClass().getName();
  +
  +                            if 
(!subTaskDefinition.implementationClass.equals(conflictingSubTaskClassName)) {
  +                                // duplicate subtask definition, and it's not the 
same classname (which occurs
  +                                // if a module is twice or more on classpath - 
which is OK)
  +                                throw new BuildException("Ambiguous subtask 
definition for logical name " + subTaskDefinition.name + ":" + 
conflictingSubTaskClassName + " and " + subTaskDefinition.implementationClass);
  +                            }
  +                        }
   
  -                    log.debug("Add tagHandler " + thd.namespace + " (" + 
thd.className + ')');
  -                    
TemplateEngine.getEngineInstance().setTagHandlerFor(thd.namespace, handler);
  -                    // add it also to template parser, or it will barf
  -                    // while ettempting to parse files...
  -                    
TemplateParser.getParserInstance().setTagHandlerFor(thd.namespace, handlerForParser);
  +                        SubTask subTask = (SubTask) 
Class.forName(subTaskDefinition.implementationClass).newInstance();
   
  +                        log.debug("Registering SubTask " + subTaskDefinition.name + 
" (" + subTaskDefinition.implementationClass + ") to DocletTask " + 
getClass().getName());
  +                        subtaskMap.put(subTaskDefinition.name, subTask);
                   }
  -                catch (TemplateException e) {
  -                    throw new BuildException("Couldn't register " + thd.className + 
" to namespace " + thd.namespace + ':' + e.getMessage(), e);
                   }
  -                catch (Exception e) {
  -                    throw new BuildException("Couldn't instantiate " + 
thd.className + " taghandler " + e.getMessage(), e);
  +                catch (ClassNotFoundException e) {
  +                    throw new BuildException("Couldn't find class: " + 
e.getMessage(), e);
                   }
  +                catch (InstantiationException e) {
  +                    throw new BuildException("Couldn't instantiate class: " + 
e.getMessage(), e);
  +                }
  +                catch (IllegalAccessException e) {
  +                    throw new BuildException("Couldn't invoke constructor: " + 
e.getMessage(), e);
  +                }
  +                catch (ClassCastException e) {
  +                    throw new BuildException("Couldn't cast to " + 
SubTask.class.getName(), e);
               }
  -
  -            // Register subtasks that apply to us (they do if they in xdoclet.xml 
have declared us as parent)
  -            List subTaskDefinitions = module.getSubTaskDefinitions();
  -            Iterator j = subTaskDefinitions.iterator();
  -
  -            while (j.hasNext()) {
  -                SubTaskDefinition std = (SubTaskDefinition) j.next();
  -
  -                log.debug("Add SubTask " + std.name + " (" + 
std.implementationClass + ')');
  -                subtaskMap.put(std.name, std);
               }
           }
       }
  @@ -279,37 +272,16 @@
   
       public Object createDynamicElement(String name) throws BuildException
       {
  -        try {
  -            SubTaskDefinition subTaskDefinition = (SubTaskDefinition) 
subtaskMap.get(name);
  +        SubTask subTask = (SubTask) subtaskMap.get(name);
   
  -            if (subTaskDefinition == null) {
  -                throw new BuildException("Can't create a " + name + " element here. 
The subtask names are case sensitive.");
  +        if (subTask == null) {
  +            throw new BuildException("Can't create a " + name + " element under " + 
getTaskName() + ". Make sure the jar file containing the " +
  +                "corresponding subtask class is on the classpath specified in the 
<taskdef> that defined " + getTaskName() + ".");
               }
   
  -            Class parentTaskClass = 
Class.forName(subTaskDefinition.parentTaskClass);
  -
  -            if (!parentTaskClass.isAssignableFrom(getClass())) {
  -                throw new BuildException("Can't create a " + name + " element here. 
See manual about how to specify XDoclet modules.");
  -            }
  -
  -            SubTask subTask = (SubTask) 
Class.forName(subTaskDefinition.implementationClass).newInstance();
  -
               subTasks.add(subTask);
               return subTask;
           }
  -        catch (ClassNotFoundException e) {
  -            throw new BuildException("Couldn't find class: " + e.getMessage(), e);
  -        }
  -        catch (InstantiationException e) {
  -            throw new BuildException("Couldn't instantiate class: " + 
e.getMessage(), e);
  -        }
  -        catch (IllegalAccessException e) {
  -            throw new BuildException("Couldn't invoke constructor: " + 
e.getMessage(), e);
  -        }
  -        catch (ClassCastException e) {
  -            throw new BuildException("Couldn't cast to " + SubTask.class.getName(), 
e);
  -        }
  -    }
   
       /**
        * Describe the method
  @@ -333,13 +305,13 @@
                   subTasks.add(subtask);
               }
               catch (ClassNotFoundException e) {
  -                throw new BuildException("Couldn't find class: " + e.getMessage(), 
e);
  +                throw new BuildException("Couldn't find class: " + e.getMessage(), 
e, location);
               }
               catch (InstantiationException e) {
  -                throw new BuildException("Couldn't instantiate class: " + 
e.getMessage(), e);
  +                throw new BuildException("Couldn't instantiate class: " + 
e.getMessage(), e, location);
               }
               catch (IllegalAccessException e) {
  -                throw new BuildException("Couldn't invoke constructor: " + 
e.getMessage(), e);
  +                throw new BuildException("Couldn't invoke constructor: " + 
e.getMessage(), e, location);
               }
           }
       }
  @@ -410,7 +382,7 @@
           try {
               new XDocletMain().start();
           }
  -        catch (Exception e) {
  +        catch (XDocletException e) {
               throw new BuildException("XDoclet failed", e, location);
           }
       }
  
  
  

_______________________________________________________________

Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm

_______________________________________________
Xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to