Skip to content

Commit

Permalink
starting licenseParser. related to #56, #57, #58
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Pinto committed Jul 2, 2013
1 parent 3eb0c9a commit 31f9883
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 14 deletions.
26 changes: 26 additions & 0 deletions src/java/main/br/ufpe/cin/groundhog/License.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package br.ufpe.cin.groundhog;

/**
* Represents the license used in the project
*/
public class License {

private String name;
private String entireContent;

public License(String name) {
this.name = name;
}

public License(String name, String entireContent) {
this.entireContent = entireContent;
}

public String getName() {
return name;
}

public String getEntireContent() {
return entireContent;
}
}
4 changes: 2 additions & 2 deletions src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import br.ufpe.cin.groundhog.crawler.ForgeCrawler;
import br.ufpe.cin.groundhog.http.HttpModule;
import br.ufpe.cin.groundhog.http.Requests;
import br.ufpe.cin.groundhog.parser.JavaParser;
import br.ufpe.cin.groundhog.parser.NotAJavaProjectException;
import br.ufpe.cin.groundhog.parser.formater.Formater;
import br.ufpe.cin.groundhog.parser.java.JavaParser;
import br.ufpe.cin.groundhog.parser.java.NotAJavaProjectException;
import br.ufpe.cin.groundhog.scmclient.EmptyProjectAtDateException;
import br.ufpe.cin.groundhog.scmclient.GitClient;
import br.ufpe.cin.groundhog.scmclient.ScmModule;
Expand Down
2 changes: 1 addition & 1 deletion src/java/main/br/ufpe/cin/groundhog/main/TestMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import br.ufpe.cin.groundhog.crawler.ForgeCrawler;
import br.ufpe.cin.groundhog.http.HttpModule;
import br.ufpe.cin.groundhog.http.Requests;
import br.ufpe.cin.groundhog.parser.JavaParser;
import br.ufpe.cin.groundhog.parser.formater.FormaterFactory;
import br.ufpe.cin.groundhog.parser.java.JavaParser;
import br.ufpe.cin.groundhog.scmclient.GitClient;
import br.ufpe.cin.groundhog.scmclient.ScmModule;
import br.ufpe.cin.groundhog.search.SearchGitHub;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Map.Entry;

import au.com.bytecode.opencsv.CSVWriter;
import br.ufpe.cin.groundhog.parser.MutableInt;
import br.ufpe.cin.groundhog.parser.java.MutableInt;

public class CSVFormater extends Formater {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.HashMap;

import br.ufpe.cin.groundhog.parser.MutableInt;
import br.ufpe.cin.groundhog.parser.java.MutableInt;

public abstract class Formater {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package br.ufpe.cin.groundhog.parser.formater;

import br.ufpe.cin.groundhog.parser.UnsupportedMetricsFormatException;
import br.ufpe.cin.groundhog.parser.java.UnsupportedMetricsFormatException;

public class FormaterFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.HashMap;

import br.ufpe.cin.groundhog.parser.MutableInt;
import br.ufpe.cin.groundhog.parser.java.MutableInt;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.ufpe.cin.groundhog.parser;
package br.ufpe.cin.groundhog.parser.java;

import java.util.HashMap;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.ufpe.cin.groundhog.parser;
package br.ufpe.cin.groundhog.parser.java;

import java.util.HashMap;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.ufpe.cin.groundhog.parser;
package br.ufpe.cin.groundhog.parser.java;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -87,7 +87,7 @@ private HashMap<String, HashMap<String, MutableInt>> invokeProcessor() throws IO
* @throws IOException if something wrong happens when closing source file manager
*/
public HashMap<String, HashMap<String, MutableInt>> parse() throws IOException {
logger.info("Running parser..");
logger.info("Running java parser..");
recursiveSearch(folder);
if (!filesList.isEmpty()) {
return invokeProcessor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.ufpe.cin.groundhog.parser;
package br.ufpe.cin.groundhog.parser.java;

public class MutableInt {
int value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.ufpe.cin.groundhog.parser;
package br.ufpe.cin.groundhog.parser.java;

import br.ufpe.cin.groundhog.GroundhogException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.ufpe.cin.groundhog.parser;
package br.ufpe.cin.groundhog.parser.java;

import br.ufpe.cin.groundhog.GroundhogException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.ufpe.cin.groundhog.parser.license;

import br.ufpe.cin.groundhog.GroundhogException;

public class LicenseNotFoundException extends GroundhogException {

/**
*
*/
private static final long serialVersionUID = -6933755608744941883L;

public LicenseNotFoundException(String msg) {
super(msg);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package br.ufpe.cin.groundhog.parser.license;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.ufpe.cin.groundhog.License;
import br.ufpe.cin.groundhog.parser.java.JavaParser;

public class LicenseParser {

private static Logger logger = LoggerFactory.getLogger(JavaParser.class);

private final File[] files;

public LicenseParser(File folder) {
this.files = folder.listFiles();
}

/**
* Parses the top level folder looking for licenses files
*/
License parser() {
logger.info("Running license parser..");

try {
for (File file : files) {
if (isText(file)) {
boolean containsLicense = containLicense(file);
if(containsLicense) {
return extractLicense(file);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
throw new LicenseNotFoundException("No license found for project %s");
}

private License extractLicense(File file) {
// TODO Auto-generated method stub
return null;
}

private boolean containLicense(File file) {
// TODO Auto-generated method stub
return false;
}

private boolean isText(final File file)
throws IOException {
final int BUFFER_SIZE = 10 * 1024;
boolean isText = true;
byte[] buffer = new byte[BUFFER_SIZE];

final RandomAccessFile fis = new RandomAccessFile(file, "r");
try {
fis.seek(0);
final int read = fis.read(buffer);
int lastByteTranslated = 0;
for (int i = 0; i < read && isText; i++) {
final byte b = buffer[i];
int ub = b & (0xff);
int utf8value = lastByteTranslated + ub;
lastByteTranslated = (ub) << 8;

if (ub == 0x09
|| ub == 0x0A
|| ub == 0x0C
|| ub == 0x0D
|| (ub >= 0x20 && ub <= 0x7E)
|| (ub >= 0xA0 && ub <= 0xEE)
|| (utf8value >= 0x2E2E && utf8value <= 0xC3BF)) {

} else {
isText = false;
}
}
} finally {
try {
fis.close();
} catch (final Throwable th) {
}

}
return isText;
}
}

0 comments on commit 31f9883

Please sign in to comment.