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 ; 7 import java.io.*; 8 9 12 public class SourceCopy extends Copy 13 { 14 protected static final String PACKAGE = "package"; 15 16 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 e = fileCopyMap.keys(); 35 while (e.hasMoreElements()) 36 { 37 String fromFile = (String ) 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 packageName = getPackage( sourceFile ); 49 if ( packageName != null && packageName.length() > 0 ) 50 { 51 String 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 ioe) 62 { 63 String 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 83 protected String getPackage(File sourceFile) 84 throws IOException 85 { 86 BufferedReader in = null; 87 String packageName = null; 88 String 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 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 |