442 |
Set foundPackages = new LinkedHashSet(); |
Set foundPackages = new LinkedHashSet(); |
443 |
for (Iterator pit = option_sourcepath.iterator(); pit.hasNext(); ) { |
for (Iterator pit = option_sourcepath.iterator(); pit.hasNext(); ) { |
444 |
File sourceDir = (File)pit.next(); |
File sourceDir = (File)pit.next(); |
445 |
findPackages(null, sourceDir, foundPackages); |
findPackages("", sourceDir, foundPackages); |
446 |
} |
} |
447 |
addFoundPackages(null, foundPackages); |
addFoundPackages(null, foundPackages); |
448 |
|
for (Iterator packageIt = foundPackages.iterator(); packageIt.hasNext(); ) { |
449 |
|
String packageName = (String)packageIt.next(); |
450 |
|
if (null == packageName) { |
451 |
|
packageName = ""; |
452 |
|
} |
453 |
|
rootDoc.addSpecifiedPackageName(packageName); |
454 |
|
} |
455 |
} |
} |
456 |
|
|
457 |
for (Iterator it = packageAndClasses.iterator(); it.hasNext();) |
for (Iterator it = packageAndClasses.iterator(); it.hasNext();) |
670 |
* Verify that the given file is a valid Java source file and that |
* Verify that the given file is a valid Java source file and that |
671 |
* it specifies the given package. |
* it specifies the given package. |
672 |
*/ |
*/ |
673 |
private static boolean isValidJavaFile(File file, |
private boolean isValidJavaFile(File file, |
674 |
String expectedPackage) |
String expectedPackage) |
675 |
{ |
{ |
676 |
// FIXME: Scan file looking for package declaration and check that |
try { |
677 |
// it's matching; perhaps check that name of public class and |
InputStream in = new BufferedInputStream(new FileInputStream(file)); |
678 |
// filename match. |
|
679 |
|
int ch, prevChar = 0; |
680 |
|
|
681 |
|
final int STATE_DEFAULT = 0; |
682 |
|
final int STATE_COMMENT = 1; |
683 |
|
final int STATE_LINE_COMMENT = 2; |
684 |
|
|
685 |
|
int state = STATE_DEFAULT; |
686 |
|
|
687 |
return true; |
StringBuffer word = new StringBuffer(); |
688 |
|
int wordIndex = 0; |
689 |
|
|
690 |
|
while ((ch = in.read()) >= 0) { |
691 |
|
String completeWord = null; |
692 |
|
|
693 |
|
switch (state) { |
694 |
|
case STATE_COMMENT: |
695 |
|
if (prevChar == '*' && ch == '/') { |
696 |
|
state = STATE_DEFAULT; |
697 |
|
} |
698 |
|
break; |
699 |
|
|
700 |
|
case STATE_LINE_COMMENT: |
701 |
|
if (ch == '\n') { |
702 |
|
state = STATE_DEFAULT; |
703 |
|
} |
704 |
|
break; |
705 |
|
|
706 |
|
case STATE_DEFAULT: |
707 |
|
if (prevChar == '/' && ch == '*') { |
708 |
|
word.deleteCharAt(word.length() - 1); |
709 |
|
if (word.length() > 0) { |
710 |
|
completeWord = word.toString(); |
711 |
|
word.setLength(0); |
712 |
|
} |
713 |
|
state = STATE_COMMENT; |
714 |
|
} |
715 |
|
else if (prevChar == '/' && ch == '/') { |
716 |
|
word.deleteCharAt(word.length() - 1); |
717 |
|
if (word.length() > 0) { |
718 |
|
completeWord = word.toString(); |
719 |
|
word.setLength(0); |
720 |
|
} |
721 |
|
state = STATE_LINE_COMMENT; |
722 |
|
} |
723 |
|
else if (" \t\r\n".indexOf(ch) >= 0) { |
724 |
|
if (word.length() > 0) { |
725 |
|
completeWord = word.toString(); |
726 |
|
word.setLength(0); |
727 |
|
} |
728 |
|
} |
729 |
|
else if (1 == wordIndex && ';' == ch) { |
730 |
|
if (word.length() > 0) { |
731 |
|
completeWord = word.toString(); |
732 |
|
word.setLength(0); |
733 |
|
} |
734 |
|
else { |
735 |
|
// empty package name in source file: "package ;" -> invalid source file |
736 |
|
in.close(); |
737 |
|
return false; |
738 |
|
} |
739 |
|
} |
740 |
|
else { |
741 |
|
word.append((char)ch); |
742 |
|
} |
743 |
|
break; |
744 |
|
} |
745 |
|
|
746 |
|
if (null != completeWord) { |
747 |
|
if (0 == wordIndex && !"package".equals(completeWord)) { |
748 |
|
in.close(); |
749 |
|
return "".equals(expectedPackage); |
750 |
|
} |
751 |
|
else if (1 == wordIndex) { |
752 |
|
in.close(); |
753 |
|
return expectedPackage.equals(completeWord); |
754 |
|
} |
755 |
|
++ wordIndex; |
756 |
|
} |
757 |
|
|
758 |
|
prevChar = ch; |
759 |
|
} |
760 |
|
|
761 |
|
// no package or class found before end-of-file -> invalid source file |
762 |
|
|
763 |
|
in.close(); |
764 |
|
return false; |
765 |
|
} |
766 |
|
catch (IOException e) { |
767 |
|
reporter.printWarning("Could not examine file " + file + ": " + e); |
768 |
|
return false; |
769 |
|
} |
770 |
} |
} |
771 |
|
|
772 |
/** |
/** |
774 |
* package specified by its name and its directory. Add the names |
* package specified by its name and its directory. Add the names |
775 |
* of all valid packages to the result list. |
* of all valid packages to the result list. |
776 |
*/ |
*/ |
777 |
private static void findPackages(String subpackage, |
private void findPackages(String subpackage, |
778 |
File packageDir, |
File packageDir, |
779 |
Set result) |
Set result) |
780 |
{ |
{ |
781 |
File[] files = packageDir.listFiles(); |
File[] files = packageDir.listFiles(); |
782 |
if (null != files) { |
if (null != files) { |
784 |
File file = files[i]; |
File file = files[i]; |
785 |
if (!file.isDirectory() && file.getName().endsWith(".java")) { |
if (!file.isDirectory() && file.getName().endsWith(".java")) { |
786 |
if (isValidJavaFile(file, subpackage)) { |
if (isValidJavaFile(file, subpackage)) { |
787 |
result.add(subpackage); |
if ("".equals(subpackage)) { |
788 |
|
result.add(null); |
789 |
|
} |
790 |
|
else { |
791 |
|
result.add(subpackage); |
792 |
|
} |
793 |
break; |
break; |
794 |
} |
} |
795 |
} |
} |
798 |
File file = files[i]; |
File file = files[i]; |
799 |
if (file.isDirectory()) { |
if (file.isDirectory()) { |
800 |
String newSubpackage; |
String newSubpackage; |
801 |
if (null != subpackage) { |
if (null != subpackage && subpackage.length() > 0) { |
802 |
newSubpackage = subpackage + "." + file.getName(); |
newSubpackage = subpackage + "." + file.getName(); |
803 |
} |
} |
804 |
else { |
else { |