CVS: xdoclet/modules/jdo/src/xdoclet/modules/jdo JdoObjectIdGeneratorTagsHandler.java,1.4,1.5

Marco Schulze <[email protected]>
Newsgroups gmane.comp.java.xdoclet.devel
Message-ID <[email protected]>
Update of /cvsroot/xdoclet/xdoclet/modules/jdo/src/xdoclet/modules/jdo
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1239/modules/jdo/src/xdoclet/modules/jdo

Modified Files:
	JdoObjectIdGeneratorTagsHandler.java 
Log Message:
added support of include-files (imports and body) in objectid-creation

Index: JdoObjectIdGeneratorTagsHandler.java
===================================================================
RCS file: /cvsroot/xdoclet/xdoclet/modules/jdo/src/xdoclet/modules/jdo/JdoObjectIdGeneratorTagsHandler.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** JdoObjectIdGeneratorTagsHandler.java	29 Aug 2005 22:55:05 -0000	1.4
--- JdoObjectIdGeneratorTagsHandler.java	31 Aug 2005 19:47:00 -0000	1.5
***************
*** 5,18 ****
--- 5,26 ----
  package xdoclet.modules.jdo;
  
+ import java.io.File;
+ import java.io.FileReader;
+ import java.io.Reader;
+ import java.nio.CharBuffer;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
+ import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.List;
+ import java.util.Map;
  import java.util.Properties;
  import java.util.Set;
  
  import org.apache.commons.logging.Log;
+ 
+ import xjavadoc.SourceClass;
  import xjavadoc.XClass;
  import xjavadoc.XDoc;
***************
*** 54,57 ****
--- 62,71 ----
      private static Set primitiveClassNames = null;
  
+     /**
+      * key: String qualifiedClassName<br/>
+      * value: {@link SourceClass} sourceClass
+      */
+     private Map     sourceFileMap = null;
+ 
      public static XField getCurrentPrimaryKeyField()
      {
***************
*** 294,301 ****
       * @return
       * @exception XDocletException
       */
      public String code(Properties attributes) throws XDocletException
      {
!         return getCurrentClass().getDoc().getTagAttributeValue("jdo.create-objectid-class", "code");
      }
  
--- 308,366 ----
       * @return
       * @exception XDocletException
+      * @deprecated                  Use {@link #includeBody(Properties)} instead!
       */
      public String code(Properties attributes) throws XDocletException
      {
!         String res = getCurrentClass().getDoc().getTagAttributeValue("jdo.create-objectid-class", "code");
! 
!         if (res != null) {
!             LogUtil.getLog(JdoObjectIdGeneratorTagsHandler.class, "code").warn("The attribute 'code' for the tag '@jdo.create-objectid-class' is deprecated! Use 'include-body' (in combination with 'include-imports') instead! Class: " + getCurrentClass().getQualifiedName());
!             System.err.println("The attribute 'code' for the tag '@jdo.create-objectid-class' is deprecated! Use 'include-body' (in combination with 'include-imports') instead! Class: " + getCurrentClass().getQualifiedName());
!         }
!         return res;
!     }
! 
!     public String includeImports(Properties attributes) throws XDocletException
!     {
!         String fileName = getCurrentClass().getDoc().getTagAttributeValue("jdo.create-objectid-class", "include-imports");
! 
!         return includeFile(getCurrentClass(), fileName);
!     }
! 
!     public String includeBody(Properties attributes) throws XDocletException
!     {
!         String fileName = getCurrentClass().getDoc().getTagAttributeValue("jdo.create-objectid-class", "include-body");
! 
!         return includeFile(getCurrentClass(), fileName);
!     }
! 
!     /**
!      * @param className                 The fully qualified name of the class.
!      * @param throwExceptionIfNotFound
!      * @return                          Returns either <tt>null</tt> (if <tt>throwExceptionIfNotFound == false</tt> and
!      *      the class is not known) or the searched instance of SourceClass.
!      * @throws XDocletException         If <tt>throwExceptionIfNotFound == true</tt> and the specified <tt>className
!      *      </tt> is unknown.
!      */
!     protected SourceClass getSourceClass(String className, boolean throwExceptionIfNotFound)
!          throws XDocletException
!     {
!         if (sourceFileMap == null) {
!             Map m = new HashMap();
! 
!             for (Iterator it = getXJavaDoc().getSourceClasses().iterator(); it.hasNext(); ) {
!                 SourceClass sc = (SourceClass) it.next();
! 
!                 m.put(sc.getQualifiedName(), sc);
!             }
!             sourceFileMap = m;
!         }
! 
!         SourceClass sc = (SourceClass) sourceFileMap.get(className);
! 
!         if (throwExceptionIfNotFound && sc == null)
!             throw new XDocletException("The class \"" + className + "\" is not known!");
! 
!         return sc;
      }
  
***************
*** 331,333 ****
--- 396,442 ----
          return objectIDClass;
      }
+ 
+     /**
+      * @param clazz              The class which is used as anchor if the <tt>fileName</tt> is specified relatively.
+      * @param fileName           An absolute or more likely relative (to clazz) fileName of the file to be included.
+      * @return                   Returns the content of the specified file or <tt>null</tt> if <tt>fileName</tt> is <tt>
+      *      null</tt> or an empty string.
+      * @throws XDocletException  In case, the specified file does not exist or cannot be read.
+      */
+     protected String includeFile(XClass clazz, String fileName)
+          throws XDocletException
+     {
+         if (fileName == null || "".equals(fileName))
+             return null;
+ 
+         SourceClass sourceClass = getSourceClass(clazz.getQualifiedName(), true);
+         String classFileName = sourceClass.getFile().getPath();
+         File classFile = new File(classFileName);
+         File classDir = classFile.getParentFile();
+         File includeFile = new File(classDir, fileName);
+ 
+         if (includeFile.length() > Integer.MAX_VALUE)
+             throw new XDocletException("File \"" + includeFile.getAbsolutePath() + "\" is too big!");
+ 
+         CharBuffer buf = CharBuffer.allocate((int) includeFile.length());
+ 
+         try {
+ 
+             Reader r = new FileReader(includeFile);
+ 
+             try {
+                 r.read(buf);
+             }
+             finally {
+                 r.close();
+             }
+ 
+         }
+         catch (Exception x) {
+             throw new XDocletException(x, "Reading include file failed!");
+         }
+ 
+         buf.flip();
+         return buf.toString();
+     }
  }



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.