Update of 
/cvsroot/xdoclet/xdoclet/modules/hibernate/src/xdoclet/modules/hibernate
In directory 
sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3952/modules/hibernate/src/xdoclet/modules/hibernate

Modified Files:
        HibernateSubTask.java HibernateTagsHandler.java 
Log Message:
XDT-1196: support for specifying hibernate mapping files

Index: HibernateSubTask.java
===================================================================
RCS file: 
/cvsroot/xdoclet/xdoclet/modules/hibernate/src/xdoclet/modules/hibernate/HibernateSubTask.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -r1.15 -r1.16
*** HibernateSubTask.java       8 Aug 2005 15:02:28 -0000       1.15
--- HibernateSubTask.java       11 Apr 2006 15:01:12 -0000      1.16
***************
*** 5,14 ****
--- 5,31 ----
  package xdoclet.modules.hibernate;
  
+ import java.io.File;
  import java.text.MessageFormat;
+ import java.util.Collection;
+ import java.util.Iterator;
+ 
+ import org.apache.commons.collections.MultiHashMap;
+ import org.apache.commons.collections.MultiMap;
+ import org.apache.commons.logging.Log;
  
  import xjavadoc.XClass;
+ import xjavadoc.XDoc;
+ import xjavadoc.XJavaDoc;
+ import xjavadoc.XPackage;
+ import xjavadoc.XTag;
  
+ import xdoclet.TemplateSubTask;
  import xdoclet.XDocletException;
+ import xdoclet.XDocletMessages;
  import xdoclet.XmlSubTask;
+ import xdoclet.tagshandler.PackageTagsHandler;
+ import xdoclet.template.TemplateEngine;
+ import xdoclet.template.TemplateException;
+ import xdoclet.util.LogUtil;
  import xdoclet.util.Translator;
  
***************
*** 67,70 ****
--- 84,89 ----
      private String  hibernateVersion = HibernateVersion.HIBERNATE_1_1;
  
+     private boolean generationPerClass = true;
+ 
      //~ Constructors 
...................................................................................................
  
***************
*** 118,122 ****
      public void execute() throws XDocletException
      {
- 
          if (hibernateVersion.equals(HibernateVersion.HIBERNATE_1_1)) {
              setPublicId(HIBERNATE_PUBLICID_11);
--- 137,140 ----
***************
*** 135,139 ****
              setDtdURL(getClass().getResource(DTD_FILE_NAME_30));
          }
!         startProcess();
      }
  
--- 153,158 ----
              setDtdURL(getClass().getResource(DTD_FILE_NAME_30));
          }
!         generationPerClass = true;
!         startUserFileMappingProcess();
      }
  
***************
*** 143,155 ****
       * Called when the engine is started
       *
!      * @exception XDocletException  Thrown in case of problem
       */
-     protected void engineStarted()
-          throws XDocletException
-     {
-         
System.out.println(Translator.getString(XDocletModulesHibernateMessages.class,
-             XDocletModulesHibernateMessages.GENERATING_HIBERNATE_FOR,
-             new String[]{getCurrentClass().getQualifiedName()}));
-     }
  
      /**
--- 162,167 ----
       * Called when the engine is started
       *
!      * @return
       */
  
      /**
***************
*** 164,167 ****
--- 176,316 ----
  
      /**
+      * Always set false for Hibernate
+      *
+      * @return   The GenerationPerClass value
+      */
+     private boolean isGenerationPerClass()
+     {
+         return generationPerClass;
+     }
+ 
+     private void startUserFileMappingProcess() throws XDocletException
+     {
+         Log log = LogUtil.getLog(HibernateSubTask.class, 
"startUserFileMappingProcess");
+ 
+         Collection classes;
+         MultiMap mapFiles = new MultiHashMap();
+         XJavaDoc xjavadoc = getXJavaDoc();
+ 
+         generationPerClass = false;
+ 
+         xjavadoc.setUseNodeParser(false);
+         if (processInnerClasses()) {
+             classes = xjavadoc.getSourceClasses();
+         }
+         else {
+             classes = xjavadoc.getSourceClasses(new 
XJavaDoc.NoInnerClassesPredicate());
+         }
+ 
+         // First, look through all source classes and find out the mapping 
file
+         // that they should be written to.
+ 
+         for (Iterator i = classes.iterator(); i.hasNext(); ) {
+             XClass clazz = (XClass) i.next();
+ 
+             if (log.isDebugEnabled()) {
+                 log.debug("Checking for mapping target, class: " + clazz);
+             }
+ 
+             XDoc doc = clazz.getDoc();
+ 
+             // First decision point: if there is a hibernate.mapping tag
+             // then a new mapping file will be generated for this source file.
+             if (doc.hasTag("hibernate.mapping")) {
+                 if (log.isDebugEnabled()) {
+                     log.debug("class " + clazz + " has mapping tag");
+                 }
+ 
+                 XTag tag = doc.getTag("hibernate.mapping");
+                 String file = null;
+ 
+                 // If the hibernate.mapping tag specifies the name of the
+                 // mapping file to generate, use that name.  Otherwise,
+                 // determine the mapping file name based on the source
+                 // class name.
+                 if (tag.getAttributeNames().contains("file")) {
+                     if (log.isDebugEnabled()) {
+                         log.debug("class has mapping file attribute");
+                     }
+ 
+                     XPackage pak = clazz.getContainingPackage();
+                     String package_structure = null;
+ 
+                     if (isPrefixWithPackageStructure() == true)
+                         package_structure = 
PackageTagsHandler.packageNameAsPathFor(pak);
+                     else
+                         package_structure = null;
+ 
+                     String packageName = 
isPackageSubstitutionInheritanceSupported() == true ? package_structure : null;
+                     String name = tag.getAttributeValue("file");
+                     String destinationFile = 
MessageFormat.format(getDestinationFile(), new Object[]{name});
+ 
+                     if (log.isDebugEnabled()) {
+                         log.debug("packageName: " + packageName);
+                         log.debug("destinationFile: " + packageName);
+                         log.debug("destDir: " + getDestDir());
+                     }
+                     file = new File(packageName, destinationFile).toString();
+                 }
+                 else {
+                     if (log.isDebugEnabled()) {
+                         log.debug("class does not have mapping file 
attribute");
+                         log.debug("generatedFileName: " + 
getGeneratedFileName(clazz));
+                         log.debug("destDir: " + getDestDir());
+                     }
+                     file = getGeneratedFileName(clazz);
+                 }
+                 // Add the class to the collection of source classes that
+                 // will be mapped to this mapping file.  It's quite possible
+                 // for many classes to be mapped to a single mapping file,
+                 // so a multimap is used to keep track of all classes that
+                 // belong to each mapping file.
+                 mapFiles.put(file, clazz);
+             }
+             else {
+                 // The class does not have a mapping tag, but we will map it
+                 // if it has a class, query, or sql-query tag.
+                 if (log.isDebugEnabled()) {
+                     log.debug("class does not have mapping tag");
+                 }
+                 //                if (matchesGenerationRules(clazz)) {
+                 if (doc.hasTag("hibernate.class") || 
doc.hasTag("hibernate.query") || doc.hasTag("hibernate.sql-query")) {
+                     if (log.isDebugEnabled()) {
+                         //                        log.debug("class matches 
generation rules");
+                         log.debug("class has class, query, or sql-query tag");
+                         log.debug("generatedFileName: " + 
getGeneratedFileName(clazz));
+                         log.debug("destDir: " + getDestDir());
+                     }
+ 
+                     String file = getGeneratedFileName(clazz);
+ 
+                     mapFiles.put(file, clazz);
+                 }
+             }
+         }
+ 
+         // Now that the mapping files have all been determined, loop through
+         // them and generate each one.
+         for (Iterator i = mapFiles.keySet().iterator(); i.hasNext(); ) {
+             String file = (String) i.next();
+ 
+             setDestinationFile(file);
+ 
+             if (log.isDebugEnabled()) {
+                 log.debug("file=" + file);
+             }
+ 
+             // Set the collection of classes that belong in this current
+             // mapping file.
+             //
+             // TODO: there's got to be a better way to do this, without
+             // a static var in HibernateTagsHandler!
+             HibernateTagsHandler.setMappedClasses((Collection) 
mapFiles.get(file));
+ 
+             startProcess();
+         }
+     }
+ 
+     /**
       * @author    <a href="mailto:[EMAIL PROTECTED]">Matt Raible</a>
       * @created   February 23, 2003
***************
*** 186,188 ****
--- 335,338 ----
          }
      }
+ 
  }

Index: HibernateTagsHandler.java
===================================================================
RCS file: 
/cvsroot/xdoclet/xdoclet/modules/hibernate/src/xdoclet/modules/hibernate/HibernateTagsHandler.java,v
retrieving revision 1.46
retrieving revision 1.47
diff -C2 -r1.46 -r1.47
*** HibernateTagsHandler.java   9 Jan 2006 01:31:31 -0000       1.46
--- HibernateTagsHandler.java   11 Apr 2006 15:01:12 -0000      1.47
***************
*** 40,43 ****
--- 40,49 ----
  {
  
+     // This is a collection of all the classes that belong in
+     // the 'current' mapping file.
+     //
+     // TODO: there's got to be a better way!
+     private static Collection mappedClasses;
+ 
      // prefix for @hibernate.component columns
      private LinkedList columnPrefixes = new LinkedList();
***************
*** 53,56 ****
--- 59,72 ----
      private Parameter currentOtherMapping;
  
+     public static Collection getMappedClasses()
+     {
+         return mappedClasses;
+     }
+ 
+     public static void setMappedClasses(Collection mappedClasses)
+     {
+         HibernateTagsHandler.mappedClasses = mappedClasses;
+     }
+ 
      /**
       * Returns full path of hibernate file for the current class.
***************
*** 469,472 ****
--- 485,515 ----
  
      /**
+      * Iterates over all classes to be mapped in current mapping file.
+      *
+      * @param template              The body of the block tag
+      * @param attributes            The attributes of the template tag
+      * @exception XDocletException  Description of Exception
+      * @doc.tag                     type="block"
+      */
+     public void forAllMappedClasses(String template, Properties attributes) 
throws XDocletException
+     {
+         Log log = LogUtil.getLog(HibernateTagsHandler.class, 
"forAllMappedClasses");
+ 
+         if (log.isDebugEnabled()) {
+             log.debug("Mapped classes are: " + mappedClasses.toString());
+         }
+ 
+         Iterator i = mappedClasses.iterator();
+ 
+         while (i.hasNext()) {
+             XClass clazz = (XClass) i.next();
+ 
+             pushCurrentClass(clazz);
+             generate(template);
+             popCurrentClass();
+         }
+     }
+ 
+     /**
       * Iterates over all classes marked as persistent.
       *



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
xdoclet-devel mailing list
xdoclet-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to