KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > xdoclet > tools > SourceCopy


1 package org.apache.beehive.netui.compiler.xdoclet.tools;
2
3 import org.apache.tools.ant.BuildException;
4 import org.apache.tools.ant.taskdefs.Copy;
5
6 import java.util.Enumeration JavaDoc;
7 import java.io.*;
8
9 /**
10  * Extend the default ant copy task to do some verification on java and pageflow source files.
11  */

12 public class SourceCopy extends Copy
13 {
14     protected static final String JavaDoc PACKAGE = "package";
15
16     /**
17      * Override doFileOperations to verify things that a source file's
18      * package name is the same as directory name.
19      *
20      * Note: we are not checking that anything named .jpf extends PageFlowController because
21      * that would require more advanced source parsing; the netui doclet task handles this by
22      * making sure that if we find a class that extends PageFlowController, there is a corresponding
23      * jpf file.
24      */

25     protected void doFileOperations()
26     {
27         if (fileCopyMap.size() > 0)
28         {
29             log("Verifying " + fileCopyMap.size()
30                     + " source file" + (fileCopyMap.size() == 1 ? "" : "s")
31                     + " before copy");
32
33            
34             Enumeration JavaDoc e = fileCopyMap.keys();
35             while (e.hasMoreElements())
36             {
37                 String JavaDoc fromFile = (String JavaDoc) e.nextElement();
38                 
39                 if ( !fromFile.endsWith( ".java" ) && !fromFile.endsWith( ".jpf" ) &&
40                         !fromFile.endsWith( ".app" ) )
41                     continue;
42
43                 try
44                 {
45                     File sourceFile = new File( fromFile );
46                     if ( sourceFile.exists() && sourceFile.isFile() )
47                     {
48                         String JavaDoc packageName = getPackage( sourceFile );
49                         if ( packageName != null && packageName.length() > 0 )
50                         {
51                             String JavaDoc path = sourceFile.getParentFile().getPath();
52                             path = path.replace( File.separatorChar, '.' );
53                             if ( !path.endsWith( packageName ) )
54                             {
55                                 throw new BuildException( "File " + fromFile + " failed verification because its package (" +
56                                         packageName + ") differs from its directory location. This will cause errors with the pageflow compiler." );
57                             }
58                         }
59                     }
60                 }
61                 catch (Exception JavaDoc ioe)
62                 {
63                     String JavaDoc msg = "Failed to verify " + fromFile
64                             + " due to " + ioe.getMessage();
65                     throw new BuildException(msg, ioe, getLocation());
66                 }
67             }
68         }
69
70         super.doFileOperations();
71     }
72
73     /**
74      * Get the package name of a java source file. This just does some really basic parsing to find
75      * the first line that starts with "package", and then returns the rest of that line before the
76      * first semicolon. It won't catch any possible way that a package could be specified (after a comment,
77      * with line breaks, etc) but it's a good-faith effort to determine the package name. If no package
78      * name is found, the file will be skipped during the verification process.
79      * @param sourceFile
80      * @return
81      * @throws IOException
82      */

83     protected String JavaDoc getPackage(File sourceFile)
84             throws IOException
85     {
86         BufferedReader in = null;
87         String JavaDoc packageName = null;
88         String JavaDoc encoding = getEncoding();
89
90         try
91         {
92             if (encoding == null)
93             {
94                 in = new BufferedReader(new FileReader(sourceFile));
95             }
96             else
97             {
98                 in = new BufferedReader(new InputStreamReader(
99                                 new FileInputStream(sourceFile),
100                                 encoding));
101             }
102
103             String JavaDoc line = in.readLine();
104             while (line != null)
105             {
106                 if (line.length() != 0)
107                 {
108                     line = line.trim();
109                     if (line.startsWith(PACKAGE))
110                     {
111                         int semi = line.indexOf(";");
112                         if ( semi != -1 )
113                         {
114                             packageName = line.substring(PACKAGE.length() + 1, semi);
115                             packageName = packageName.trim();
116                         }
117                         break;
118                     }
119                 }
120                 line = in.readLine();
121             }
122         }
123         finally
124         {
125             if (in != null)
126             {
127                 in.close();
128             }
129         }
130
131         return packageName;
132     }
133 }
134
Popular Tags