Skip to content

Commit

Permalink
updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed May 8, 2024
1 parent 1e53523 commit e267e30
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static IDocumentAnalyser createDefaultAnalyser(final ReportConfiguration
}

/**
* A DocumentAnalyser for the license
* A DocumentAnalyser a collection of licenses
*/
private final static class DefaultAnalyser implements IDocumentAnalyser {

Expand Down
25 changes: 18 additions & 7 deletions apache-rat-core/src/main/java/org/apache/rat/api/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public enum Type {
private final MetaData metaData;
private final String name;

/**
* Creates an instance.
* @param name the name of the resource.
*/
protected Document(String name) {
this.name = name;
this.metaData = new MetaData();
Expand All @@ -64,6 +68,7 @@ public final String getName() {
return name;
}

@Override
public int compareTo(Document doc) {
return getPath().compareTo(doc.getPath());
}
Expand All @@ -81,9 +86,14 @@ public boolean equals(Object obj) {
return getPath().equals(((Document)obj).getPath());
}

/**
* Get the path that identifies the document.
* @return
*/
public Path getPath() {
return Paths.get(getName());
}

/**
* Reads the contents of this document.
*
Expand Down Expand Up @@ -111,13 +121,6 @@ public final MetaData getMetaData() {
return metaData;
}

/**
* Tests if this a composite document.
*
* @return true if composite, false otherwise
*/
public abstract boolean isComposite();

/**
* Representations suitable for logging.
* @return a <code>String</code> representation
Expand All @@ -129,8 +132,16 @@ public String toString()
return String.format("%s( name = %s metaData = %s )", this.getClass().getSimpleName(), getName(), getMetaData());
}

/**
* Determines if this Document is a directory type.
* @return {@code true} if this is a directory.
*/
public abstract boolean isDirectory();

/**
* Gets a sorted set of Documents that are children of this document.
* @return A sorted set of child Documents. May be empty
*/
public abstract SortedSet<Document> listChildren();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,33 @@
import org.apache.rat.api.MetaData;
import org.apache.rat.api.RatException;

/**
* A Document that wraps an Archive entry.
*/
public class ArchiveEntryDocument extends Document {

/** The contents of the entry */
private final byte[] contents;

/** The path for the entry */
private final Path path;

/**
* Creates an Archive entry.
* @param path The path for the entry.
* @param contents the contents of the entry.
*/
public ArchiveEntryDocument(Path path, byte[] contents) {
super(DocumentImplUtils.toName(path.toFile()));
this.path = path;
this.contents = contents;
}

@Override
public InputStream inputStream() throws IOException {
return new ByteArrayInputStream(contents);
}

public boolean isComposite() {
return DocumentImplUtils.isZipStream(new ByteArrayInputStream(contents));
}

@Override
public boolean isDirectory() {
return false;
Expand All @@ -64,6 +71,7 @@ public SortedSet<Document> listChildren() {
return Collections.emptySortedSet();
}

@Override
public Reader reader() throws IOException {
return new InputStreamReader(new ByteArrayInputStream(contents), StandardCharsets.UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,13 @@

public class DocumentImplUtils {

/**
* Normalizes a file name. Accounts for Windows to Unix conversion.
* @param file
* @return
*/
public final static String toName(File file) {
String path = file.getPath();
return path.replace('\\', '/');
}

public static final boolean isZipStream(InputStream stream) {
ZipInputStream zip = new ZipInputStream(stream);
try {
zip.getNextEntry();
return true;
} catch (IOException e) {
return false;
} finally {
IOUtils.closeQuietly(zip);
}
}

public static final boolean isZip(File file) {
try {
return isZipStream(new FileInputStream(file));
} catch (IOException e) {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@
import org.apache.rat.api.MetaData;

/**
* Document wrapping a file of undetermined composition.
*
* Document wrapping a File object
*/
public class FileDocument extends Document {

/** the wrapped file */
private final File file;


/**
* Creates a File document.
* @param file the file to wrap.
*/
public FileDocument(final File file) {
super(DocumentImplUtils.toName(file));
this.file = file;
Expand All @@ -54,10 +58,6 @@ public Path getPath() {
return file.toPath();
}

public boolean isComposite() {
return DocumentImplUtils.isZip(file);
}

@Override
public boolean isDirectory() {
return file.isDirectory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public void run(final RatReport report) throws RatException {
}
}

/**
* Creates an input stream from the Directory being walked.
* @return
* @throws IOException
*/
private InputStream createInputStream() throws IOException {
return new BufferedInputStream(getDocument().inputStream());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,9 @@
*/
public class DirectoryWalker extends Walker {

/** The directories to ignore */
private final IOFileFilter directoriesToIgnore;

// /**
// * Constructs a walker.
// *
// * @param file the directory to walk (not null).
// * @param filesToIgnore filters input files (optional),
// * or null when no filtering should be performed
// * @param directoriesToIgnore filters directories (optional), or null when no filtering should be performed.
// */
// public DirectoryWalker(final File file, final FilenameFilter filesToIgnore, final IOFileFilter directoriesToIgnore) {
// super(new FileDocument(file), filesToIgnore);
// this.directoriesToIgnore = directoriesToIgnore == null ? FalseFileFilter.FALSE : directoriesToIgnore;
// }

/**
* Constructs a directory walker.
*
Expand All @@ -69,7 +57,7 @@ public DirectoryWalker(final ReportConfiguration config, Document document) {
* Process a directory, restricted directories will be ignored.
*
* @param report The report to process the directory with
* @param file the directory to process
* @param document the document to process.
* @throws RatException on error.
*/
private void processDirectory(final RatReport report, Document document) throws RatException {
Expand All @@ -93,19 +81,19 @@ public void run(final RatReport report) throws RatException {
* Process a directory, ignoring any files/directories set to be ignored.
*
* @param report the report to use in processing
* @param file the run the report against
* @param document the document to run the report against
* @throws RatException on error
*/
protected void process(final RatReport report, Document document) throws RatException {
final SortedSet<Document> documents = document.listChildren();
if (documents != null) {

// breadth first traversal
processNonDirectories(report, documents);
processDirectories(report, documents);
}
}

/** test for directory that is not ignored */
private boolean isNotIgnoredDirectory(Path path) {
return !directoriesToIgnore.accept(path.getParent().toFile(), path.toString());
}
Expand All @@ -114,7 +102,7 @@ private boolean isNotIgnoredDirectory(Path path) {
* Process all directories in a set of file objects, ignoring any directories set to be ignored.
*
* @param report the report to use in processing
* @param files the files to process (only directories will be processed)
* @param documents the documentss to process (only directories will be processed)
* @throws RatException on error
*/
private void processDirectories(final RatReport report, final SortedSet<Document> documents) throws RatException {
Expand All @@ -129,7 +117,7 @@ private void processDirectories(final RatReport report, final SortedSet<Document
* Process all files in a set of file objects, ignoring any files set to be ignored.
*
* @param report the report to use in processing
* @param files the files to process (only files will be processed)
* @param documents the documents to process (only files will be processed)
* @throws RatException on error
*/
private void processNonDirectories(final RatReport report, final SortedSet<Document> documents) throws RatException {
Expand Down

This file was deleted.

26 changes: 10 additions & 16 deletions apache-rat-core/src/main/java/org/apache/rat/walker/Walker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@
*/
public abstract class Walker implements IReportable {

/** The file that this walker started at */


/** The file name filter that the walker is applying */
private final FilenameFilter filesToIgnore;
/** The document this walker is walking */
private final Document document;

public Walker(final Document document, final FilenameFilter filesToIgnore) {
/**
* Create the walker
* @param document The document the walker is walking.
* @param filesToIgnore the Files to ignore. If {@code null} no files are ignored.
*/
protected Walker(final Document document, final FilenameFilter filesToIgnore) {
this.document = document;
this.filesToIgnore = filesToIgnore == null ? FalseFileFilter.FALSE : filesToIgnore;
}
Expand All @@ -53,18 +56,9 @@ protected Document getDocument() {
}

/**
* Test if the specified file should be ignored.
* @param document the document to test.
* @return {@code true} if the file should be ignored.
*/
protected final boolean isNotIgnored() {
return isNotIgnored(document.getPath());
}

/**
* Test if the specified file should be ignored.
* @param document the document to test.
* @return {@code true} if the file should be ignored.
* Test if the specified path should be ignored.
* @param pth the Path to test
* @return {@code true} if the file should not be ignored.
*/
protected final boolean isNotIgnored(Path pth) {
return !filesToIgnore.accept(pth.getParent().toFile(), pth.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,6 @@ public InputStream inputStream() throws IOException {
return stream;
}


@Override
public boolean isComposite() {
return false;
}

@Override
public boolean isDirectory() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ public Reader reader() throws IOException {
return reader;
}


@Override
public boolean isComposite() {
return false;
}

@Override
public boolean isDirectory() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public String getURL() {
return url;
}

@Override
public boolean isComposite() {
return false;
}

@Override
public boolean isDirectory() {
return false;
Expand Down
Loading

0 comments on commit e267e30

Please sign in to comment.