Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding file encoding option (defualt = UTF-8) #79

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<classpathentry kind="lib" path="lib/org.eclipse.core.runtime_3.13.0.v20170207-1030.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.common_3.9.0.v20170207-1454.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.preferences_3.7.0.v20170126-2132.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.jdt.core_3.13.50.v20171007-0855.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.jdt.core_3.13.50.v20171007-0855.jar" sourcepath="/org.eclipse.jdt.core"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi_3.12.50.v20170928-1321.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.7.jar"/>
<classpathentry kind="lib" path="lib/fame.jar"/>
Expand Down
Binary file modified lib/verveine.extractor.java.jar
Binary file not shown.
41 changes: 24 additions & 17 deletions src/fr/inria/verveine/extractor/java/EntityDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import org.moosetechnology.model.famix.famixtraits.TWithTypes;

import ch.akuhn.fame.Repository;
import fr.inria.verveine.extractor.java.utils.FileContentExtractor;
import fr.inria.verveine.extractor.java.utils.ImplicitVarBinding;
import fr.inria.verveine.extractor.java.utils.Util;

Expand Down Expand Up @@ -2666,30 +2665,38 @@ public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TM
}

/**
* Creates and returns a FAMIX Comment and associates it with an Entity (ex: for Javadocs)
* Creates and returns a Famix Comment and associates it with an Entity (ex: for Javadocs)
* @param jCmt -- the content (String) of the comment
* @param owner -- the entity that is commented
* @param commentText -- whether to export the source anchor of the comment (position in file) or its content (string)
* @return the FAMIX Comment
* @return the Famix Comment
*/
public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, boolean commentText) {
public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner) {
Comment cmt = null;

if ( (jCmt != null) && (owner != null) ) {

cmt = new Comment();
if (commentText) {
IndexedFileAnchor position = createIndexedFileAnchor(jCmt);
if (position != null) {
cmt.setContent(
FileContentExtractor.getFileContent(position.getFileName(),
(int)position.getStartPos(),
(int)position.getEndPos()) );
}
}
else {
addSourceAnchor(cmt, jCmt);
}
addSourceAnchor(cmt, jCmt);
famixRepoAdd(cmt);
cmt.setCommentedEntity(owner);
}

return cmt;
}

/**
* Creates and returns a Famix Comment and associates it with an Entity
* @param jCmt -- the content (String) of the comment
* @param owner -- the entity that is commented
* @param content -- the text of the comment
* @return the Famix Comment
*/
public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, String content) {
Comment cmt = null;

if ( (jCmt != null) && (owner != null) ) {
cmt = new Comment();
cmt.setContent(content );
famixRepoAdd(cmt);
cmt.setCommentedEntity(owner);
}
Expand Down
87 changes: 59 additions & 28 deletions src/fr/inria/verveine/extractor/java/VerveineJOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.eclipse.jdt.core.dom.ASTParser;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -38,6 +39,11 @@ public static AnchorOptions getValue(String option) {
*/
public final static String OUTPUT_FILE = "output";

/**
* Default encodings of the java files to read
*/
private static final String DEFAULT_FILE_ENCODING = "UTF-8";

/**
* Option for MSE output format
*/
Expand Down Expand Up @@ -85,6 +91,11 @@ public static AnchorOptions getValue(String option) {
*/
protected Collection<Pattern> excludeMatchers;

/**
* File encoding to use to read java files
*/
protected String fileEncoding = DEFAULT_FILE_ENCODING;

/**
* Name of the file where to put the MSE model.
* Defaults to {@link VerveineParser#OUTPUT_FILE}
Expand Down Expand Up @@ -142,6 +153,7 @@ public void setOptions( String[] args) {
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
usage();
throw e;
}
}

Expand Down Expand Up @@ -178,6 +190,7 @@ protected int setOption( String[] args, int i) throws IllegalArgumentException {

if (arg.equals("-h")) {
usage();
System.exit(0);
}
else if (arg.matches("-1\\.[1-7]") || arg.matches("-[1-7]")) {
setCodeVersion(arg);
Expand All @@ -188,6 +201,9 @@ else if (arg.matches("-1\\.[1-7]") || arg.matches("-[1-7]")) {
} else if ((arg.charAt(0) == '-') && (arg.endsWith("cp"))) {
classPathOptions = setOptionClassPath(classPathOptions, args, i);
argumentsTreated++;
} else if (arg.equals("-encoding")) {
setOptionEncoding(args, i);
argumentsTreated++;
} else if (arg.equals("-anchor")) {
setOptionAnchor(args, i);
argumentsTreated++;
Expand Down Expand Up @@ -227,21 +243,21 @@ else if (arg.equals("-debugging")) {
*/
protected String[] setOptionClassPath( String[] classPath, String[] args, int i) throws IllegalArgumentException {
if (args[i].equals("-autocp")) {
if (i < args.length) {
if (i+1 < args.length) {
return addToClassPath(classPath, collectAllJars(args[i+1]) );
} else {
throw new IllegalArgumentException("-autocp requires a root folder");
}
}
else if (args[i].equals("-filecp")) {
if (i < args.length) {
if (i+1 < args.length) {
return addToClassPath(classPath, readAllJars(args[i+1]));
} else {
throw new IllegalArgumentException("-filecp requires a filename");
}
}
else if (args[i].equals("-cp")) {
if (i < args.length) {
if (i+1 < args.length) {
return addToClassPath(classPath, Arrays.asList(args[i+1].split(System.getProperty("path.separator"))));
}
else {
Expand All @@ -251,6 +267,40 @@ else if (args[i].equals("-cp")) {
return classPath;
}

protected void setOptionEncoding(String[] args, int i) {
if (i+1 < args.length) {
this.fileEncoding = args[i + 1].trim();
if (Charset.availableCharsets().get(this.fileEncoding) == null) {
throw new IllegalArgumentException("Unknown file encoding: -encoding " + this.fileEncoding);
}
} else {
throw new IllegalArgumentException("-encoding requires an encoding name (eg. " + DEFAULT_FILE_ENCODING + ")");
}
}

protected void setOptionAnchor(String[] args, int i) {
if (i+1 < args.length) {
String anchor = args[i + 1].trim();
anchors = VerveineJOptions.AnchorOptions.getValue(anchor);
if (anchors == null) {
throw new IllegalArgumentException("unknown option to -anchor: " + anchor);
}
} else {
throw new IllegalArgumentException("-anchor requires an option (none|default|assoc)");
}
}

protected void setOptionFormat(String[] args, int i) {
if (i+1 < args.length) {
this.outputFormat = args[i + 1].trim();
if ((! this.outputFormat.equalsIgnoreCase(MSE_OUTPUT_FORMAT)) && (! this.outputFormat.equalsIgnoreCase(JSON_OUTPUT_FORMAT))) {
throw new IllegalArgumentException("unknown option to -format: " + outputFormat);
}
} else {
throw new IllegalArgumentException("-format requires an option (mse|json)");
}
}

protected List<String> collectAllJars(String sDir) {
File[] faFiles = new File(sDir).listFiles();
List<String> tmpPath = new ArrayList<String>();
Expand Down Expand Up @@ -299,29 +349,6 @@ protected List<String> readAllJars(String filename) {
return tmpPath;
}

protected void setOptionAnchor(String[] args, int i) {
if (i < args.length) {
String anchor = args[i + 1].trim();
anchors = VerveineJOptions.AnchorOptions.getValue(anchor);
if (anchors == null) {
throw new IllegalArgumentException("unknown option to -anchor: " + anchor);
}
} else {
throw new IllegalArgumentException("-anchor requires an option (none|default|assoc)");
}
}

protected void setOptionFormat(String[] args, int i) {
if (i < args.length) {
outputFormat = args[i + 1].trim();
if ((!outputFormat.equalsIgnoreCase(MSE_OUTPUT_FORMAT)) && (!outputFormat.equalsIgnoreCase(JSON_OUTPUT_FORMAT))) {
throw new IllegalArgumentException("unknown option to -format: " + outputFormat);
}
} else {
throw new IllegalArgumentException("-format requires an option (mse|json)");
}
}

protected void usage() {
System.err.println("Usage: VerveineJ [-h] [-i] [-o <output-file-name>] [-prettyPrint] [-summary] [-alllocals] [-anchor (none|default|assoc)] [-cp CLASSPATH | -autocp DIR] [-1.1 | -1 | -1.2 | -2 | ... | -1.7 | -7] <files-to-parse> | <dirs-to-parse>");
System.err.println(" [-h] prints this message");
Expand All @@ -333,6 +360,7 @@ protected void usage() {
System.err.println(" Summarizing at the level of classes does not produce Methods, Attributes, Accesses, and Invocations");
System.err.println(" Everything is represented as references between classes: e.g. \"A.m1() invokes B.m2()\" is uplifted to \"A references B\"");
System.err.println(" [-alllocals] Forces outputing all local variables, even those with primitive type (incompatible with \"-summary\")");
System.err.println(" [-encoding <file-encoding-name>] File encoding to use for reading the source code default: " + DEFAULT_FILE_ENCODING);
System.err.println(" [-anchor (none|entity|default|assoc)] options for source anchor information:\n" +
" - no entity\n" +
" - only named entities [default]\n" +
Expand All @@ -344,14 +372,13 @@ protected void usage() {
System.err.println(" [-excludepath GLOBBINGEXPR] A globbing expression of file path to exclude from parsing");
System.err.println(" [-1.1 | -1 | -1.2 | -2 | ... | -1.7 | -7] specifies version of Java");
System.err.println(" <files-to-parse>|<dirs-to-parse> list of source files to parse or directories to search for source files");
System.exit(0);

}

protected void setCodeVersion(String arg) {
if (codeVers != null) {
System.err.println("Trying to set twice code versions: " + codeVers + " and " + arg);
usage();
throw new IllegalArgumentException();
} else if (arg.equals("-1.1") || arg.equals("-1")) {
codeVers = JavaCore.VERSION_1_1;
} else if (arg.equals("-1.2") || arg.equals("-2")) {
Expand Down Expand Up @@ -551,4 +578,8 @@ public boolean commentsAsText() {
return commentText;
}

public String getFileEncoding() {
return fileEncoding;
}

}

This file was deleted.

Loading