2012-01-09 11 views
4

Javaソースファイルを持つソースフォルダがあります。これらのJavaファイルには異なるパッケージがあります。 javacコマンドを使用すると、パッケージ構造を生成できますが、パッケージにはソースファイルではなくクラスファイルが含まれます。Javaソースファイルからパッケージ構造を生成するAPIはありますか?

は、Javaファイルからパッケージ構造を生成し、Windowsにしていることを仮定すると、特定のパッケージ

+2

したがって、あなたの.javaファイルは正しい構造ではありませんか?それは愚かな質問かもしれませんが、なぜですか? –

+0

ソースファイルのルートフォルダと同じ出力フォルダを指定すると、javacはソースファイルの横にクラスファイルを配置します。 – Manish

+0

@ManishSharmaは、OPが求めているものではありません。 – adarshr

答えて

1

ここで私はこの問題を思いつきました。これは、javaファイルを開き、パッケージ名を読み取り、構造体を生成し、その構造体にファイルをコピーします。改善のための提案は大歓迎です。 :)

public final class FileListing { 

private Map packageMap; 


public void createPackageStructure(String sourceDir) throws FileNotFoundException 
{ 
    FileListing fileListing = new FileListing(); 
File startingDirectory= new File(sourceDir); 

    fileListing.packageMap = new HashMap(); 
    List<File> files = fileListing.getFileListing(startingDirectory, fileListing.getPackageMap()); 

    fileListing.moveFiles(fileListing.packageMap); 

} 


public List<File> getFileListing(File aStartingDir, Map packageMap) throws FileNotFoundException 
{ 
    validateDirectory(aStartingDir); 
    List<File> result = getFileListingNoSort(aStartingDir,packageMap); 
    Collections.sort(result); 
    return result; 
} 


private List<File> getFileListingNoSort(File aStartingDir, Map packageMap) throws FileNotFoundException 
{ 
    List<File> result = new ArrayList<File>(); 
    File[] filesAndDirs = aStartingDir.listFiles(); 
    List<File> filesDirs = Arrays.asList(filesAndDirs); 

    for(File file : filesDirs) 
    { 
     result.add(file); 
     if(file.isFile()) 
     { 
      packageMap.put(file, readPackageName(file.getAbsolutePath()).replace(".", "/").replace(";", "/")); 
     } 
     else 
     { 
      //must be a directory 
      //recursive call! 
      List<File> deeperList = getFileListingNoSort(file,packageMap); 
      result.addAll(deeperList); 
     } 
    } 
return result; 
} 

public String readPackageName(String filePath) 
{ 
    String packageName=null; 
    String line; 
    String temp[] = new String[2]; 
    BufferedReader br=null; 
    try{ 
     File javaFile = new File(filePath); 
     br = new BufferedReader(new FileReader(javaFile)); 
     while((line=br.readLine())!=null) 
     { 
      if(line.indexOf("package")!=-1) 
      { 
       temp = line.split(" "); 
       break; 
      } 
     } 
     br.close(); 

    }catch(FileNotFoundException fnfe) 
    { 
     fnfe.printStackTrace(); 
    }catch(IOException ioe) 
    { 
     ioe.printStackTrace(); 
    } 
    return temp[1]; 
} 

public void moveFiles(Map packageMap) 
{ 
Set keySet = packageMap.keySet(); 
Iterator it = keySet.iterator(); 
    File sourceFile, destFile, destDirs; 
InputStream in = null; 
OutputStream out = null; 
byte[] buf = new byte[1024]; 
int len; 

    try{ 
    while(it.hasNext()) 
     { 
     sourceFile = (File)it.next(); 
     destDirs = new File("src/"+(String)packageMap.get(sourceFile)); 
     destFile = new File("src/"+ (String)packageMap.get(sourceFile)+"/"+sourceFile.getName()); 
     destDirs.mkdirs(); 
     in = new FileInputStream(sourceFile); 
     out = new FileOutputStream(destFile); 

     while((len = in.read(buf)) > 0){ 
      out.write(buf, 0, len); 
     } 
     } 
    }catch(FileNotFoundException fnfe) 
    { 
     fnfe.printStackTrace(); 
    }catch(IOException ioe) 
    { 
     ioe.printStackTrace(); 
    } 
} 

static private void validateDirectory (File aDirectory) throws FileNotFoundException 
{ 
    if (aDirectory == null) { 
    throw new IllegalArgumentException("Directory should not be null."); 
    } 
    if (!aDirectory.exists()) { 
    throw new FileNotFoundException("Directory does not exist: " + aDirectory); 
    } 
    if (!aDirectory.isDirectory()) { 
    throw new IllegalArgumentException("Is not a directory: " + aDirectory); 
    } 
    if (!aDirectory.canRead()) { 
    throw new IllegalArgumentException("Directory cannot be read: " + aDirectory); 
    } 
} 

public Map getPackageMap() 
{ 
    return this.packageMap; 
} 
} 
2

にJavaファイルを置く任意のAPIがあり、私はそれを行うためにバッチスクリプトを書きました。

以下の内容をsource.batにコピーしてください。ファイルをすべて見つけた場所にsource.batを置き、実行してください。

@echo off 
@setlocal enabledelayedexpansion 

for /f "usebackq delims=" %%f in (`dir /s /b *.java`) do (
    set file=%%~nxf 

    for /f "usebackq delims=" %%p in (`findstr package %%~nxf`) do (
     set package=%%p 

     set package=!package:*.java:=! 
     set package=!package:package =! 
     set package=!package:;=! 
     set package=!package:.=\! 

     echo Expanding !package!... 

     mkdir !package! 
     xcopy /f %%~nxf !package! 
    ) 
) 

@endlocal 

ただし、Unix/Linuxの場合は、bashスクリプトです。私はこれがはるかに良いと簡潔な方法で行うことができると確信していますが、それは間違いなく動作します。

#! /bin/bash 

for file in *.java 
do 
    package=`grep -h 'package' $file` 
    package=`echo $package | sed 's/package//g'` 
    package=`echo $package | sed 's/;//g'` 
    package=`echo $package | sed 's/\./\//g'` 

    echo Expanding $package... 
    mkdir -p $package 

    cp $file $package 
done 
+0

私は 'パッケージ= $(grep -h -m 1パッケージ" $ file "| sed -e 's /.*パッケージ[[:space:]] \ + \(。* \)[[:space:]] ] *;。*/\ 1/'-e' s /\./\// g ') 'となります。これは一度にすべてを行います。 @PhilippWendlerおかげさまで、 –

+0

私はオンエラーが大好き! – adarshr

関連する問題