├── .gitignore ├── ASTExtractor.properties ├── srcpy ├── astextractor_test.py └── astextractor.py ├── src ├── astextractor │ ├── ASTExtractorTest.java │ ├── ASTExtractorProperties.java │ ├── MainApp.java │ ├── PythonBinder.java │ └── ASTExtractor.java ├── helpers │ ├── XMLHelpers.java │ ├── FileSystemHelpers.java │ └── ParseHelpers.java └── astparser │ └── JavaASTParser.java ├── pom.xml ├── README.md └── LICENSE.md /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | dist/ 3 | target/ 4 | .project 5 | .classpath 6 | .settings 7 | *.class 8 | .pydevproject 9 | __pycache__ 10 | *.pyc 11 | -------------------------------------------------------------------------------- /ASTExtractor.properties: -------------------------------------------------------------------------------- 1 | # Fill this file with the nodes that should be left as is and the ones that should be omitted 2 | LEAF = PackageDeclaration, ImportDeclaration, ParameterizedType, ArrayType, VariableDeclarationFragment 3 | OMIT = Javadoc, Block 4 | -------------------------------------------------------------------------------- /srcpy/astextractor_test.py: -------------------------------------------------------------------------------- 1 | from astextractor import ASTExtractor 2 | 3 | if __name__ == '__main__': 4 | '''Used as a test for the python bindings''' 5 | ast_extractor = ASTExtractor("../target/ASTExtractor-0.5.jar", "../ASTExtractor.properties") 6 | ast = ast_extractor.parse_string( 7 | "import org.myclassimports;\n" + 8 | "\n" + 9 | "public class MyClass {\n" + 10 | " private int myvar;\n" + 11 | "\n" + 12 | " public MyClass(int myvar) {\n" + 13 | " this.myvar = myvar;\n" + 14 | " }\n" + 15 | "\n" + 16 | " public void getMyvar() {\n" + 17 | " return myvar;\n" + 18 | " }\n" + 19 | "}\n" 20 | ) 21 | print(ast) 22 | ast_extractor.close() 23 | -------------------------------------------------------------------------------- /src/astextractor/ASTExtractorTest.java: -------------------------------------------------------------------------------- 1 | package astextractor; 2 | 3 | import astextractor.ASTExtractorProperties; 4 | import astextractor.ASTExtractor; 5 | 6 | /** 7 | * Test for the {@link ASTExtractor} class. 8 | * 9 | * @author themis 10 | */ 11 | public class ASTExtractorTest { 12 | 13 | /** 14 | * Function used to test the AST extractor. 15 | * 16 | * @param args unused parameter. 17 | */ 18 | public static void main(String[] args) { 19 | ASTExtractorProperties.setProperties("ASTExtractor.properties"); 20 | 21 | // @formatter:off 22 | String ast = ASTExtractor.parseString("" 23 | + "import org.myclassimports;\n" 24 | + "" 25 | + "public class MyClass {\n" 26 | + " private int myvar;\n" 27 | + "\n" 28 | + " public MyClass(int myvar) {\n" 29 | + " this.myvar = myvar;\n" 30 | + " }\n" 31 | + "" 32 | + " public void getMyvar() {\n" 33 | + " return myvar;\n" 34 | + " }\n" 35 | + "}\n" 36 | ); 37 | // @formatter:on 38 | System.out.println(ast); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/astextractor/ASTExtractorProperties.java: -------------------------------------------------------------------------------- 1 | package astextractor; 2 | 3 | import java.util.HashSet; 4 | 5 | import helpers.ParseHelpers; 6 | 7 | /** 8 | * Handles setting the properties for omitting nodes or keeping them as leafs. 9 | * 10 | * @author themis 11 | */ 12 | public class ASTExtractorProperties { 13 | 14 | /** 15 | * The nodes of the AST that should be printed as they are. 16 | */ 17 | public static HashSet LEAF = new HashSet(); 18 | 19 | /** 20 | * The nodes of the AST that should be omitted. 21 | */ 22 | public static HashSet OMIT = new HashSet(); 23 | 24 | /** 25 | * Sets the properties given a properties file. 26 | * 27 | * @param propertiesFile the file that contains the properties. 28 | */ 29 | public static void setProperties(String propertiesFile) { 30 | LEAF.clear(); 31 | OMIT.clear(); 32 | for (String rule : ParseHelpers.parseProperties(propertiesFile)) { 33 | String[] srule = rule.split("="); 34 | if (srule[1].equals("LEAF")) 35 | LEAF.add(srule[0]); 36 | else if (srule[1].equals("OMIT")) 37 | OMIT.add(srule[0]); 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/helpers/XMLHelpers.java: -------------------------------------------------------------------------------- 1 | package helpers; 2 | 3 | import java.io.StringReader; 4 | import java.io.StringWriter; 5 | 6 | import javax.xml.transform.OutputKeys; 7 | import javax.xml.transform.Source; 8 | import javax.xml.transform.Transformer; 9 | import javax.xml.transform.TransformerFactory; 10 | import javax.xml.transform.stream.StreamResult; 11 | import javax.xml.transform.stream.StreamSource; 12 | 13 | /** 14 | * Helper functions for XML strings. 15 | * 16 | * @author themis 17 | */ 18 | public class XMLHelpers { 19 | 20 | /** 21 | * Formats an XML string to pretty print it. 22 | * 23 | * @param input the input XML string. 24 | * @param indent the indent characters for the output XML string. 25 | * @return an XML string indented. 26 | */ 27 | public static String formatXML(String input, int indent) { 28 | try { 29 | Source xmlInput = new StreamSource(new StringReader(input)); 30 | StringWriter stringWriter = new StringWriter(); 31 | StreamResult xmlOutput = new StreamResult(stringWriter); 32 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); 33 | transformerFactory.setAttribute("indent-number", indent); 34 | Transformer transformer = transformerFactory.newTransformer(); 35 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 36 | transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 37 | transformer.transform(xmlInput, xmlOutput); 38 | return xmlOutput.getWriter().toString(); 39 | } catch (Exception e) { 40 | throw new RuntimeException(e); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/astextractor/MainApp.java: -------------------------------------------------------------------------------- 1 | package astextractor; 2 | 3 | import helpers.ParseHelpers; 4 | 5 | /** 6 | * Contains the main class of the application. 7 | * 8 | * @author themis 9 | */ 10 | public class MainApp { 11 | 12 | /** 13 | * Prints the help message of the command line interface. 14 | */ 15 | private static void printHelpMessage() { 16 | System.out.println("ASTExtractor: Abstract Syntax Tree Extractor for Java Source Code\n"); 17 | System.out.println("Run as:\n java -jar ASTExtractor.jar -project=\"path/to/project\"" 18 | + " -properties=\"path/to/propertiesfile\" -repr=XML|JSON"); 19 | System.out.println("Or as:\n java -jar ASTExtractor.jar -file=\"path/to/file\"" 20 | + " -properties=\"path/to/propertiesfile\" -repr=XML|JSON"); 21 | System.out.println("where -properties allows setting the location of the properties file" 22 | + " (default is no properties so all syntax tree nodes are returned)"); 23 | System.out.println("and -repr allows selecting the representation of the tree (default is XML)"); 24 | } 25 | 26 | /** 27 | * Executes the application. 28 | * 29 | * @param args arguments for executing in command line mode. 30 | */ 31 | public static void main(String args[]) { 32 | if (args.length > 0) { 33 | String[] arguments = ParseHelpers.parseArgs(args); 34 | String project = arguments[0]; 35 | String file = arguments[1]; 36 | String properties = arguments[2]; 37 | String repr = "XML"; 38 | if (project.length() > 0 ^ file.length() > 0) { 39 | if (arguments[3].length() > 0 && !(arguments[3].equals("JSON") || arguments[3].equals("XML"))) 40 | printHelpMessage(); 41 | else { 42 | ASTExtractorProperties.setProperties(properties); 43 | if (arguments[3].equals("JSON") || arguments[3].equals("XML")) 44 | repr = arguments[3]; 45 | String result = ""; 46 | if (project.length() > 0) 47 | result = ASTExtractor.parseFolder(project, repr); 48 | else if (file.length() > 0) 49 | result = ASTExtractor.parseFile(file, repr); 50 | System.out.println(result); 51 | } 52 | } else { 53 | printHelpMessage(); 54 | } 55 | } else { 56 | printHelpMessage(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/helpers/FileSystemHelpers.java: -------------------------------------------------------------------------------- 1 | package helpers; 2 | 3 | import java.io.File; 4 | import java.io.FileNotFoundException; 5 | import java.util.ArrayList; 6 | import java.util.Scanner; 7 | 8 | /** 9 | * Helper functions for handling file system operations. 10 | * 11 | * @author themis 12 | */ 13 | public class FileSystemHelpers { 14 | 15 | /** 16 | * Reads a file into a string. 17 | * 18 | * @param filename the filename of the file to be read. 19 | * @return the contents of the file, or null if the file does not exist. 20 | */ 21 | public static String readFileToString(String filename) { 22 | try { 23 | Scanner scanner = new Scanner(new File(filename)); 24 | scanner.useDelimiter("\\A"); 25 | String text = scanner.hasNext() ? scanner.next() : ""; 26 | scanner.close(); 27 | return text; 28 | } catch (FileNotFoundException e) { 29 | e.printStackTrace(); 30 | } 31 | return null; 32 | } 33 | 34 | /** 35 | * Finds all the java files of a folder and all its subfolders recursively. 36 | * 37 | * @param folderName the path to the folder. 38 | * @param files a list that is filled with the java files of the folder and its subfolders. 39 | */ 40 | private static void findJavaFilesOfFolderRecursively(String folderName, ArrayList files) { 41 | File root = new File(folderName); 42 | File[] list = root.listFiles(); 43 | if (list == null) 44 | return; 45 | for (File file : list) { 46 | if (file.isDirectory()) 47 | findJavaFilesOfFolderRecursively(file.getAbsolutePath(), files); 48 | else if (file.getName().endsWith(".java")) 49 | files.add(file.getAbsoluteFile()); 50 | } 51 | } 52 | 53 | /** 54 | * Finds all the java files of a folder and all its subfolders recursively. 55 | * 56 | * @param folderName the path to the folder. 57 | * @return a list containing the java files of the folder and its subfolders. 58 | */ 59 | public static ArrayList getJavaFilesOfFolderRecursively(String folderName) { 60 | ArrayList files = new ArrayList(); 61 | findJavaFilesOfFolderRecursively(folderName, files); 62 | return files; 63 | } 64 | 65 | /** 66 | * Returns a relative path given two paths, a base on and the one to be relativized. 67 | * 68 | * @param base the base path. 69 | * @param path the path to be relativized. 70 | * @return the relative path. 71 | */ 72 | public static String getRelativePath(String base, String path) { 73 | return new File(base).toURI().relativize(new File(path).toURI()).getPath(); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/astextractor/PythonBinder.java: -------------------------------------------------------------------------------- 1 | package astextractor; 2 | 3 | import java.nio.charset.Charset; 4 | import java.util.Scanner; 5 | 6 | import javax.xml.bind.DatatypeConverter; 7 | 8 | /** 9 | * Class used to bind this library to a python file. 10 | * 11 | * @author themis 12 | */ 13 | public class PythonBinder { 14 | 15 | /** 16 | * Function used to bind this library to a python file. It works by opening a {@link Scanner} on the standard input, 17 | * reading the required task in the form of a message and writing the result as a message in the standard output. 18 | * The base 64 format is used to send the messages. 19 | * 20 | * @param args the one and only parameter must be the path to the properties file. 21 | */ 22 | public static void main(String[] args) { 23 | // Set the properties of the extractor 24 | String properties = args[0]; 25 | ASTExtractorProperties.setProperties(properties); 26 | 27 | Scanner scanner = new Scanner(System.in); 28 | while (scanner.hasNextLine()) { 29 | // Receive message and decode it 30 | String b64message = scanner.nextLine(); 31 | String message = new String(DatatypeConverter.parseBase64Binary(b64message)); 32 | 33 | // Operate on message and return response 34 | String messageresult = ""; 35 | if (message.equals("START_OF_TRANSMISSION")) { 36 | messageresult = message; 37 | String b64messageresult = DatatypeConverter 38 | .printBase64Binary(messageresult.getBytes(Charset.forName("US-ASCII"))); 39 | System.out.println(b64messageresult); 40 | System.out.flush(); 41 | continue; 42 | } else if (message.equals("END_OF_TRANSMISSION")) { 43 | messageresult = message; 44 | String b64messageresult = DatatypeConverter 45 | .printBase64Binary(messageresult.getBytes(Charset.forName("US-ASCII"))); 46 | System.out.println(b64messageresult); 47 | System.out.flush(); 48 | break; 49 | } else { 50 | String messageHeader = message.split("_-_")[0]; 51 | message = message.substring(messageHeader.length() + 3); 52 | String parseType = messageHeader.split("_")[1]; 53 | String parseRepr = messageHeader.split("_")[2]; 54 | if (parseType.equals("STRING")) 55 | messageresult = ASTExtractor.parseString(message, parseRepr); 56 | else if (parseType.equals("FILE")) 57 | messageresult = ASTExtractor.parseFile(message, parseRepr); 58 | else if (parseType.equals("FOLDER")) 59 | messageresult = ASTExtractor.parseFolder(message, parseRepr); 60 | String b64messageresult = DatatypeConverter 61 | .printBase64Binary(messageresult.getBytes(Charset.forName("US-ASCII"))); 62 | System.out.println(b64messageresult); 63 | System.out.flush(); 64 | } 65 | } 66 | scanner.close(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | ASTExtractor 6 | ASTExtractor 7 | 0.5 8 | 9 | 10 | UTF-8 11 | UTF-8 12 | 13 | 14 | 15 | 16 | org.eclipse.jdt 17 | org.eclipse.jdt.core 18 | 3.23.0 19 | 20 | 21 | org.eclipse.core 22 | org.eclipse.core.runtime 23 | 3.7.0 24 | 25 | 26 | org.eclipse.core 27 | org.eclipse.core.resources 28 | 3.7.100 29 | 30 | 31 | org.json 32 | json 33 | 20200518 34 | 35 | 36 | javax.xml.bind 37 | jaxb-api 38 | 2.3.1 39 | 40 | 41 | 42 | 43 | src 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-compiler-plugin 48 | 3.8.1 49 | 50 | 1.8 51 | 1.8 52 | 53 | 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-javadoc-plugin 58 | 3.2.0 59 | 60 | 61 | attach-javadocs 62 | prepare-package 63 | 64 | javadoc 65 | 66 | 67 | 68 | 69 | 70 | 71 | org.apache.maven.plugins 72 | maven-assembly-plugin 73 | 3.3.0 74 | 75 | 76 | jar-with-dependencies 77 | 78 | 79 | 80 | astextractor.MainApp 81 | 82 | 83 | 84 | 85 | 86 | make-assembly 87 | package 88 | 89 | single 90 | 91 | 92 | false 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /src/helpers/ParseHelpers.java: -------------------------------------------------------------------------------- 1 | package helpers; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * Helper functions for parsing arguments or properties files. 9 | * 10 | * @author themis 11 | */ 12 | public class ParseHelpers { 13 | 14 | /** 15 | * Parses the command line arguments. 16 | * 17 | * @param args the arguments to be parsed. 18 | * @return a string with the values of the arguments. 19 | */ 20 | public static String[] parseArgs(String[] args) { 21 | List col = new ArrayList(); 22 | for (String arg : args) { 23 | String narg = arg.trim(); 24 | if (narg.contains("=")) { 25 | for (String n : narg.split("=")) { 26 | col.add(n); 27 | } 28 | } else 29 | col.add(arg.trim()); 30 | } 31 | boolean sproject = false; 32 | boolean sfile = false; 33 | boolean sproperties = false; 34 | boolean srepr = false; 35 | String project = ""; 36 | String file = ""; 37 | String properties = ""; 38 | String repr = ""; 39 | for (String c : col) { 40 | if (c.startsWith("-project")) { 41 | sproject = true; 42 | sfile = false; 43 | sproperties = false; 44 | srepr = false; 45 | } else if (c.startsWith("-file")) { 46 | sproject = false; 47 | sfile = true; 48 | sproperties = false; 49 | srepr = false; 50 | } else if (c.startsWith("-properties")) { 51 | sproject = false; 52 | sfile = false; 53 | sproperties = true; 54 | srepr = false; 55 | } else if (c.startsWith("-repr")) { 56 | sproject = false; 57 | sfile = false; 58 | sproperties = false; 59 | srepr = true; 60 | } else { 61 | if (sproject) 62 | project += c + " "; 63 | else if (sfile) 64 | file += c + " "; 65 | else if (sproperties) 66 | properties += c + " "; 67 | else if (srepr) 68 | repr += c + " "; 69 | } 70 | } 71 | return new String[] { project.trim(), file.trim(), properties.trim(), repr.trim().toUpperCase() }; 72 | } 73 | 74 | /** 75 | * Parses a properties file with the OMIT and ASIS types of nodes. 76 | * 77 | * @param filename the filename of the properties file 78 | * @return an ArrayList containing node types and category (ASIS or OMIT). 79 | */ 80 | public static ArrayList parseProperties(String filename) { 81 | ArrayList rules = new ArrayList(); 82 | if (new File(filename).isFile()) { 83 | String propertiesString = FileSystemHelpers.readFileToString(filename); 84 | String lines[] = propertiesString.split("\\r?\\n"); 85 | for (String line : lines) { 86 | line = line.trim(); 87 | if (line.startsWith("LEAF")) { 88 | String[] sline = line.split("=")[1].trim().split(","); 89 | for (String string : sline) 90 | rules.add(string.trim() + "=LEAF"); 91 | } else if (line.startsWith("OMIT")) { 92 | String[] sline = line.split("=")[1].trim().split(","); 93 | for (String string : sline) 94 | rules.add(string.trim() + "=OMIT"); 95 | } 96 | } 97 | } 98 | return rules; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/astparser/JavaASTParser.java: -------------------------------------------------------------------------------- 1 | package astparser; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | 7 | import org.eclipse.jdt.core.dom.AST; 8 | import org.eclipse.jdt.core.dom.ASTNode; 9 | import org.eclipse.jdt.core.dom.ASTParser; 10 | import org.eclipse.jdt.core.dom.CompilationUnit; 11 | import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 12 | 13 | import astextractor.ASTExtractorProperties; 14 | 15 | /** 16 | * Handles the parsing of java files and the extraction of their Abstract Syntax Trees (ASTs). 17 | * 18 | * @author themis 19 | */ 20 | public class JavaASTParser { 21 | 22 | /** 23 | * Retrieves the children of an ASTNode. 24 | * 25 | * @param node the ASTNode of which the children are retrieved. 26 | * @return the children of the given ASTNode. 27 | */ 28 | @SuppressWarnings("unchecked") 29 | private static ArrayList getChildren(ASTNode node) { 30 | ArrayList flist = new ArrayList(); 31 | List list = node.structuralPropertiesForType(); 32 | for (int i = 0; i < list.size(); i++) { 33 | StructuralPropertyDescriptor curr = (StructuralPropertyDescriptor) list.get(i); 34 | Object child = node.getStructuralProperty(curr); 35 | if (child instanceof List) { 36 | flist.addAll((Collection) child); 37 | } else if (child instanceof ASTNode) { 38 | flist.add((ASTNode) child); 39 | } else { 40 | } 41 | } 42 | return flist; 43 | } 44 | 45 | /** 46 | * Recursively visits all nodes of the AST and exports it as an XML StringBuffer. 47 | * 48 | * @param result the result as a StringBuffer. 49 | * @param indent the indent at the current level. 50 | * @param node the current ASTNode that is examined. 51 | */ 52 | private static void visitNode(StringBuffer result, String indent, ASTNode node) { 53 | ArrayList children = getChildren(node); 54 | String nodeType = ASTNode.nodeClassForType(node.getNodeType()).getSimpleName(); 55 | if (ASTExtractorProperties.OMIT.contains(nodeType)) { 56 | // Do nothing 57 | } else if (ASTExtractorProperties.LEAF.contains(nodeType)) { 58 | result.append(indent + "<" + nodeType + ">"); 59 | result.append(node.toString().trim().replace("&", "&").replace("<", "<").replace(">", ">")); 60 | result.append("\n"); 61 | } else if (children.size() > 0) { 62 | result.append(indent + "<" + nodeType + ">\n"); 63 | for (ASTNode child : children) { 64 | visitNode(result, indent + " ", child); 65 | } 66 | result.append(indent + "\n"); 67 | } else { 68 | result.append(indent + "<" + nodeType + ">"); 69 | result.append(node.toString().replace("&", "&").replace("<", "<").replace(">", ">")); 70 | result.append("\n"); 71 | } 72 | } 73 | 74 | /** 75 | * Visits an AST and exports it as an XML string. 76 | * 77 | * @param root the root ASTNode of the tree. 78 | * @return an XML string representation of the tree. 79 | */ 80 | protected static String visitTree(ASTNode root) { 81 | StringBuffer result = new StringBuffer(""); 82 | visitNode(result, "", root); 83 | return result.toString(); 84 | } 85 | 86 | /** 87 | * Parses the contents of a java file and returns its AST as an XML string. 88 | * 89 | * @param str the contents of a java file given as a string. 90 | * @return an XML string representation of the AST of the java file contents. 91 | */ 92 | public static String parse(String str) { 93 | ASTParser parser = ASTParser.newParser(AST.JLS14); 94 | parser.setSource(str.toCharArray()); 95 | parser.setKind(ASTParser.K_COMPILATION_UNIT); 96 | final CompilationUnit cu = (CompilationUnit) parser.createAST(null); 97 | return visitTree(cu); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/astextractor/ASTExtractor.java: -------------------------------------------------------------------------------- 1 | package astextractor; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | 6 | import org.json.XML; 7 | 8 | import astparser.JavaASTParser; 9 | import helpers.FileSystemHelpers; 10 | import helpers.XMLHelpers; 11 | 12 | /** 13 | * Contains all functions for extracting Abstract Syntax Trees (ASTs) from java files. 14 | * 15 | * @author themis 16 | */ 17 | public class ASTExtractor { 18 | 19 | /** 20 | * Parses the contents of a java file and returns its AST. 21 | * 22 | * @param fileContents the contents of a java file, given as a String. 23 | * @return a String containing the AST of the java file in XML format. 24 | */ 25 | public static String parseString(String fileContents) { 26 | return parseString(fileContents, "XML"); 27 | } 28 | 29 | /** 30 | * Parses the contents of a java file and returns its AST. 31 | * 32 | * @param fileContents the contents of a java file, given as a String. 33 | * @param astFormat the format of the returned AST, either "XML" or "JSON". 34 | * @return a String containing the AST of the java file in XML or JSON format. 35 | */ 36 | public static String parseString(String fileContents, String astFormat) { 37 | String result = JavaASTParser.parse(fileContents); 38 | if (astFormat.equals("JSON")) 39 | return XML.toJSONObject(result).toString(3); 40 | else 41 | return XMLHelpers.formatXML(result, 3); 42 | } 43 | 44 | /** 45 | * Parses a java file and returns its AST. 46 | * 47 | * @param filename the filename of the java file to be parsed. 48 | * @return a String containing the AST of the java file in XML format. 49 | */ 50 | public static String parseFile(String filename) { 51 | return parseFile(filename, "XML"); 52 | } 53 | 54 | /** 55 | * Parses a java file and returns its AST. 56 | * 57 | * @param filename the filename of the java file to be parsed. 58 | * @param astFormat the format of the returned AST, either "XML" or "JSON". 59 | * @return a String containing the AST of the java file in XML or JSON format. 60 | */ 61 | public static String parseFile(String filename, String astFormat) { 62 | String result = parseString(FileSystemHelpers.readFileToString(filename)); 63 | if (astFormat.equals("JSON")) 64 | return XML.toJSONObject(result).toString(3); 65 | else 66 | return XMLHelpers.formatXML(result, 3); 67 | } 68 | 69 | /** 70 | * Parses all the files of a folder and returns a unified AST. 71 | * 72 | * @param folderName the path of the folder of which the files are parsed. 73 | * @return an AST containing all the files of a folder in XML format. 74 | */ 75 | public static String parseFolder(String folderName) { 76 | return parseFolder(folderName, "XML"); 77 | } 78 | 79 | /** 80 | * Parses all the files of a folder and returns a unified AST. 81 | * 82 | * @param folderName the path of the folder of which the files are parsed. 83 | * @param astFormat the format of the returned AST, either "XML" or "JSON". 84 | * @return an AST containing all the files of a folder in XML or JSON format. 85 | */ 86 | public static String parseFolder(String folderName, String astFormat) { 87 | String folderAbsolutePath = new File(folderName).getAbsolutePath(); 88 | ArrayList files = FileSystemHelpers.getJavaFilesOfFolderRecursively(folderName); 89 | StringBuilder results = new StringBuilder("\n"); 90 | for (File file : files) { 91 | String fileAbsolutePath = file.getAbsolutePath(); 92 | String filePath = FileSystemHelpers.getRelativePath(folderAbsolutePath, fileAbsolutePath); 93 | String result = parseFile(fileAbsolutePath); 94 | results.append("\n" + filePath + "\n\n" + result + "\n\n"); 95 | } 96 | results.append("\n"); 97 | if (astFormat.equals("JSON")) 98 | return XML.toJSONObject(results.toString()).toString(3); 99 | else 100 | return XMLHelpers.formatXML(results.toString(), 3); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ASTExtractor: Abstract Syntax Tree Extractor for Java Source Code 2 | ================================================================= 3 | ASTExtractor is an Abstract Syntax Tree (AST) extractor for Java source code, based 4 | on the Eclipse compiler. The tool functions as a wrapper of the Eclipse compiler and 5 | allows exporting the AST of source code files or projects in XML and JSON formats. 6 | The tool has a command line interface and can also be used as a library. 7 | The documentation is available at [http://thdiaman.github.io/ASTExtractor/](http://thdiaman.github.io/ASTExtractor/) 8 | 9 | Executing in Command Line mode 10 | ------------------------------ 11 | Execute as:
java -jar ASTExtractor.jar -project="path/to/project" -properties="path/to/propertiesfile" -repr=XML|JSON
12 | for projects, or as:
java -jar ASTExtractor.jar -file="path/to/file" -properties="path/to/propertiesfile" -repr=XML|JSON
13 | for java files, where -properties allows setting the location of the properties file (default is no properties so all syntax tree 14 | nodes are returned) and -repr allows selecting the representation of the tree (default is XML). 15 | 16 | Using as a library 17 | ------------------ 18 | Import the library in your code. Set a location for the properties file using
ASTExtractorProperties.setProperties("ASTExtractor.properties");
. 19 | Then, you can use it as follows: 20 | - For folders containing java files:
String ast = ASTExtractor.parseFolder("path/to/folder/");
21 | - For java files:
String ast = ASTExtractor.parseFile("path/to/file.java");
22 | - For contents of java files (i.e. strings): 23 |
String ast = ASTExtractor.parseString(""
24 | 			 + "import org.myclassimports;\n"
25 | 			 + "\n"
26 | 			 + "public class MyClass {\n"
27 | 			 + "	private int myvar;\n"
28 | 			 + "\n"
29 | 			 + "	public MyClass(int myvar) {\n"
30 | 			 + "		this.myvar = myvar;\n"
31 | 			 + "	}\n"
32 | 			 + "\n"
33 | 			 + "	public void getMyvar() {\n"
34 | 			 + "		return myvar;\n"
35 | 			 + "	}\n"
36 | 			 + "}\n"
37 | 			 );
38 | If JSON is required as the output representation then use these functions with a second string 39 | argument that can be either "XML" or "JSON". 40 | 41 | Using in Python 42 | --------------- 43 | ASTExtractor also has python bindings. Using the python wrapper is simple. At first, the library 44 | has to be imported and the ASTExtractor object has to be initialized given the path to the jar 45 | of the library and the path to the properties file of the library: 46 |
ast_extractor = ASTExtractor("path/to/ASTExtractor.jar", "path/to/ASTExtractor.properties")
47 | After that, you can use it as follows: 48 | - For folders containing java files:
ast = ast_extractor.parse_folder("path/to/folder/");
49 | - For java files:
ast = ast_extractor.parse_file("path/to/file.java");
50 | - For contents of java files (i.e. strings): 51 |
ast = ast_extractor.parse_string(
52 | 			"import org.myclassimports;\n" + 
53 | 			"\n" + 
54 | 			"public class MyClass {\n" + 
55 | 			"   private int myvar;\n" + 
56 | 			"\n" + 
57 | 			"   public MyClass(int myvar) {\n" + 
58 | 			"      this.myvar = myvar;\n" + 
59 | 			"   }\n" + 
60 | 			"\n" + 
61 | 			"   public void getMyvar() {\n" + 
62 | 			"      return myvar;\n" + 
63 | 			"   }\n" + 
64 | 			"}\n"
65 | 	)
66 | If JSON is required as the output representation then use these functions with a second string 67 | argument that can be either "XML" or "JSON". 68 | 69 | Note that after using the library, you have to close the ASTExtractor object using function close, i.e.:
ast_extractor.close()
70 | 71 | Controlling the output 72 | ---------------------- 73 | An Abstract Syntax Tree can be very complex, including details for every identifier of the code. 74 | In ASTExtractor, the complexity of the tree can be controlled using the ASTExtractor.properties 75 | file. In this file, the user can select the nodes that should not appear in the final tree (OMIT) 76 | and the nodes that should not be analyzed further, i.e. that should be forced to be leaf nodes (LEAF) 77 | The default options are shown in the following example ASTExtractor.properties file: 78 |
LEAF = PackageDeclaration, ImportDeclaration, ParameterizedType, ArrayType, VariableDeclarationFragment
79 | OMIT = Javadoc, Block
80 | 81 | -------------------------------------------------------------------------------- /srcpy/astextractor.py: -------------------------------------------------------------------------------- 1 | import os 2 | import base64 3 | import subprocess 4 | from subprocess import STDOUT, PIPE 5 | 6 | class _ASTExtractor(object): 7 | """ 8 | Inner python binding to the ASTExtractor library. It works by executing the jar file as a subprocess 9 | and opening pipes to the standard input and standard output so that messages can be sent and received. 10 | Instead of using this class, it is highly recommended to use the abstracted ASTExtractor class. 11 | """ 12 | def __init__(self, path_to_ASTExtractor_jar, path_to_ASTExtractor_properties): 13 | """ 14 | Initializes this inner extractor. 15 | 16 | :param path_to_ASTExtractor_jar: the path to the ASTExtractor jar. 17 | :param path_to_ASTExtractor_properties: the path to the ASTExtractor properties file. 18 | """ 19 | self.cmd = ['java', '-cp', path_to_ASTExtractor_jar, 'astextractor.PythonBinder', path_to_ASTExtractor_properties] 20 | self.proc = subprocess.Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT) 21 | self.nummessages = 0 22 | line = self.send_message("START_OF_TRANSMISSION") 23 | if line != "START_OF_TRANSMISSION": 24 | print("Error in Java compiler!!") 25 | exit() 26 | 27 | def close_extractor(self): 28 | """ 29 | Closes the extractor. 30 | """ 31 | return self.send_message("END_OF_TRANSMISSION") == "END_OF_TRANSMISSION" 32 | 33 | def restart_extractor(self, force=False): 34 | """ 35 | Restarts the extractor. 36 | """ 37 | if force or self.send_message("END_OF_TRANSMISSION") == "END_OF_TRANSMISSION": 38 | self.proc = subprocess.Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT) 39 | self.nummessages = 0 40 | else: 41 | print("Error in Java compiler!!") 42 | exit() 43 | 44 | def get_ast(self, code_entity, code_entity_properties): 45 | """ 46 | Returns the AST of a code_entity. 47 | 48 | :param code_entity: the path or the contents of the code entity. 49 | :param code_entity_properties: the properties of the code entity including its type and expected representation. 50 | """ 51 | self.nummessages += 1 52 | if self.nummessages == 10000: 53 | self.restart_extractor() 54 | return self.send_message(code_entity_properties + code_entity) 55 | 56 | def send_message(self, message): 57 | """ 58 | Sends a new message to the ASTExtractor jar. 59 | 60 | :param message: the message to be sent. 61 | """ 62 | decodedbytes = message.encode(encoding='ascii') 63 | b64encodedbytes = base64.b64encode(decodedbytes) 64 | self.proc.stdin.write(b64encodedbytes + b"\r\n") 65 | self.proc.stdin.flush() 66 | line = self.proc.stdout.readline() 67 | try: 68 | b64decodedbytes = base64.b64decode(line) 69 | decodedline = b64decodedbytes.decode() 70 | except: 71 | self.restart_extractor(True) 72 | decodedline = "" 73 | return decodedline 74 | 75 | class ASTExtractor(_ASTExtractor): 76 | """ 77 | Class used as a python binding to the ASTExtractor library. It contains functions for parsing java code to AST. 78 | """ 79 | def __init__(self, path_to_ASTExtractor_jar, path_to_ASTExtractor_properties): 80 | """ 81 | Initializes this AST Extractor. 82 | 83 | :param path_to_ASTExtractor_jar: the path to the ASTExtractor jar 84 | :param path_to_ASTExtractor_properties: the path to the ASTExtractor properties file. 85 | """ 86 | super(ASTExtractor, self).__init__(path_to_ASTExtractor_jar, path_to_ASTExtractor_properties) 87 | 88 | def parse_string(self, file_contents, representation="XML"): 89 | """ 90 | Parses the contents of a java file and returns its AST. 91 | 92 | :param file_contents: the contents of a java file, given as a string. 93 | :param representation: the format of the returned AST, either "XML" or "JSON", default is "XML".. 94 | :returns: a string containing the AST of the java file in XML or JSON format. 95 | """ 96 | return super(ASTExtractor, self).get_ast(file_contents, "PARSE_STRING_" + representation + "_-_") 97 | 98 | def parse_file(self, filename, representation="XML"): 99 | """ 100 | Parses a java file and returns its AST. 101 | 102 | :param filename: the filename of the java file to be parsed. 103 | :param representation: the format of the returned AST, either "XML" or "JSON", default is "XML".. 104 | :returns: a string containing the AST of the java file in XML or JSON format. 105 | """ 106 | filename = os.path.abspath(filename) 107 | return super(ASTExtractor, self).get_ast(filename, "PARSE_FILE_" + representation + "_-_") 108 | 109 | def parse_folder(self, folder_name, representation="XML"): 110 | """ 111 | Parses all the files of a folder and returns a unified AST. 112 | 113 | :param folder_name: the path of the folder of which the files are parsed. 114 | :param representation: the format of the returned AST, either "XML" or "JSON", default is "XML".. 115 | :returns: an AST containing all the files of a folder in XML format. 116 | """ 117 | folder_name = os.path.abspath(folder_name) 118 | return super(ASTExtractor, self).get_ast(folder_name, "PARSE_FOLDER_" + representation + "_-_") 119 | 120 | def close(self): 121 | """ 122 | Closes the AST Extractor. Note that this function must be called after using the class. 123 | Otherwise, this may result to a memory leak. 124 | """ 125 | super(ASTExtractor, self).close_extractor() 126 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Licences used by ASTExtractor 2 | ============================= 3 | ASTExtractor is licensed under the Apache License, Version 2.0. 4 | Third-party libraries used include: 5 | - Eclipse Java Development Tools Core, licensed under the Eclipse Public License, Version 1.0 6 | - JSON In Java, licensed under the The JSON License 7 | These licences are provided in this file. 8 | 9 | Licence for ASTExtractor 10 | ------------------------ 11 | Licensed under the Apache License, Version 2.0 (the "License"); 12 | you may not use this file except in compliance with the License. 13 | You may obtain a copy of the License at 14 | 15 | 16 | 17 | Unless required by applicable law or agreed to in writing, software 18 | distributed under the License is distributed on an "AS IS" BASIS, 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | See the License for the specific language governing permissions and 21 | limitations under the License. 22 | 23 | ``` 24 | ------------------------------------------------------------------------- 25 | Apache License 26 | Version 2.0, January 2004 27 | http://www.apache.org/licenses/ 28 | 29 | 30 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 31 | 32 | 1. Definitions. 33 | 34 | "License" shall mean the terms and conditions for use, reproduction, 35 | and distribution as defined by Sections 1 through 9 of this document. 36 | 37 | "Licensor" shall mean the copyright owner or entity authorized by 38 | the copyright owner that is granting the License. 39 | 40 | "Legal Entity" shall mean the union of the acting entity and all 41 | other entities that control, are controlled by, or are under common 42 | control with that entity. For the purposes of this definition, 43 | "control" means (i) the power, direct or indirect, to cause the 44 | direction or management of such entity, whether by contract or 45 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 46 | outstanding shares, or (iii) beneficial ownership of such entity. 47 | 48 | "You" (or "Your") shall mean an individual or Legal Entity 49 | exercising permissions granted by this License. 50 | 51 | "Source" form shall mean the preferred form for making modifications, 52 | including but not limited to software source code, documentation 53 | source, and configuration files. 54 | 55 | "Object" form shall mean any form resulting from mechanical 56 | transformation or translation of a Source form, including but 57 | not limited to compiled object code, generated documentation, 58 | and conversions to other media types. 59 | 60 | "Work" shall mean the work of authorship, whether in Source or 61 | Object form, made available under the License, as indicated by a 62 | copyright notice that is included in or attached to the work 63 | (an example is provided in the Appendix below). 64 | 65 | "Derivative Works" shall mean any work, whether in Source or Object 66 | form, that is based on (or derived from) the Work and for which the 67 | editorial revisions, annotations, elaborations, or other modifications 68 | represent, as a whole, an original work of authorship. For the purposes 69 | of this License, Derivative Works shall not include works that remain 70 | separable from, or merely link (or bind by name) to the interfaces of, 71 | the Work and Derivative Works thereof. 72 | 73 | "Contribution" shall mean any work of authorship, including 74 | the original version of the Work and any modifications or additions 75 | to that Work or Derivative Works thereof, that is intentionally 76 | submitted to Licensor for inclusion in the Work by the copyright owner 77 | or by an individual or Legal Entity authorized to submit on behalf of 78 | the copyright owner. For the purposes of this definition, "submitted" 79 | means any form of electronic, verbal, or written communication sent 80 | to the Licensor or its representatives, including but not limited to 81 | communication on electronic mailing lists, source code control systems, 82 | and issue tracking systems that are managed by, or on behalf of, the 83 | Licensor for the purpose of discussing and improving the Work, but 84 | excluding communication that is conspicuously marked or otherwise 85 | designated in writing by the copyright owner as "Not a Contribution." 86 | 87 | "Contributor" shall mean Licensor and any individual or Legal Entity 88 | on behalf of whom a Contribution has been received by Licensor and 89 | subsequently incorporated within the Work. 90 | 91 | 2. Grant of Copyright License. Subject to the terms and conditions of 92 | this License, each Contributor hereby grants to You a perpetual, 93 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 94 | copyright license to reproduce, prepare Derivative Works of, 95 | publicly display, publicly perform, sublicense, and distribute the 96 | Work and such Derivative Works in Source or Object form. 97 | 98 | 3. Grant of Patent License. Subject to the terms and conditions of 99 | this License, each Contributor hereby grants to You a perpetual, 100 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 101 | (except as stated in this section) patent license to make, have made, 102 | use, offer to sell, sell, import, and otherwise transfer the Work, 103 | where such license applies only to those patent claims licensable 104 | by such Contributor that are necessarily infringed by their 105 | Contribution(s) alone or by combination of their Contribution(s) 106 | with the Work to which such Contribution(s) was submitted. If You 107 | institute patent litigation against any entity (including a 108 | cross-claim or counterclaim in a lawsuit) alleging that the Work 109 | or a Contribution incorporated within the Work constitutes direct 110 | or contributory patent infringement, then any patent licenses 111 | granted to You under this License for that Work shall terminate 112 | as of the date such litigation is filed. 113 | 114 | 4. Redistribution. You may reproduce and distribute copies of the 115 | Work or Derivative Works thereof in any medium, with or without 116 | modifications, and in Source or Object form, provided that You 117 | meet the following conditions: 118 | 119 | (a) You must give any other recipients of the Work or 120 | Derivative Works a copy of this License; and 121 | 122 | (b) You must cause any modified files to carry prominent notices 123 | stating that You changed the files; and 124 | 125 | (c) You must retain, in the Source form of any Derivative Works 126 | that You distribute, all copyright, patent, trademark, and 127 | attribution notices from the Source form of the Work, 128 | excluding those notices that do not pertain to any part of 129 | the Derivative Works; and 130 | 131 | (d) If the Work includes a "NOTICE" text file as part of its 132 | distribution, then any Derivative Works that You distribute must 133 | include a readable copy of the attribution notices contained 134 | within such NOTICE file, excluding those notices that do not 135 | pertain to any part of the Derivative Works, in at least one 136 | of the following places: within a NOTICE text file distributed 137 | as part of the Derivative Works; within the Source form or 138 | documentation, if provided along with the Derivative Works; or, 139 | within a display generated by the Derivative Works, if and 140 | wherever such third-party notices normally appear. The contents 141 | of the NOTICE file are for informational purposes only and 142 | do not modify the License. You may add Your own attribution 143 | notices within Derivative Works that You distribute, alongside 144 | or as an addendum to the NOTICE text from the Work, provided 145 | that such additional attribution notices cannot be construed 146 | as modifying the License. 147 | 148 | You may add Your own copyright statement to Your modifications and 149 | may provide additional or different license terms and conditions 150 | for use, reproduction, or distribution of Your modifications, or 151 | for any such Derivative Works as a whole, provided Your use, 152 | reproduction, and distribution of the Work otherwise complies with 153 | the conditions stated in this License. 154 | 155 | 5. Submission of Contributions. Unless You explicitly state otherwise, 156 | any Contribution intentionally submitted for inclusion in the Work 157 | by You to the Licensor shall be under the terms and conditions of 158 | this License, without any additional terms or conditions. 159 | Notwithstanding the above, nothing herein shall supersede or modify 160 | the terms of any separate license agreement you may have executed 161 | with Licensor regarding such Contributions. 162 | 163 | 6. Trademarks. This License does not grant permission to use the trade 164 | names, trademarks, service marks, or product names of the Licensor, 165 | except as required for reasonable and customary use in describing the 166 | origin of the Work and reproducing the content of the NOTICE file. 167 | 168 | 7. Disclaimer of Warranty. Unless required by applicable law or 169 | agreed to in writing, Licensor provides the Work (and each 170 | Contributor provides its Contributions) on an "AS IS" BASIS, 171 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 172 | implied, including, without limitation, any warranties or conditions 173 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 174 | PARTICULAR PURPOSE. You are solely responsible for determining the 175 | appropriateness of using or redistributing the Work and assume any 176 | risks associated with Your exercise of permissions under this License. 177 | 178 | 8. Limitation of Liability. In no event and under no legal theory, 179 | whether in tort (including negligence), contract, or otherwise, 180 | unless required by applicable law (such as deliberate and grossly 181 | negligent acts) or agreed to in writing, shall any Contributor be 182 | liable to You for damages, including any direct, indirect, special, 183 | incidental, or consequential damages of any character arising as a 184 | result of this License or out of the use or inability to use the 185 | Work (including but not limited to damages for loss of goodwill, 186 | work stoppage, computer failure or malfunction, or any and all 187 | other commercial damages or losses), even if such Contributor 188 | has been advised of the possibility of such damages. 189 | 190 | 9. Accepting Warranty or Additional Liability. While redistributing 191 | the Work or Derivative Works thereof, You may choose to offer, 192 | and charge a fee for, acceptance of support, warranty, indemnity, 193 | or other liability obligations and/or rights consistent with this 194 | License. However, in accepting such obligations, You may act only 195 | on Your own behalf and on Your sole responsibility, not on behalf 196 | of any other Contributor, and only if You agree to indemnify, 197 | defend, and hold each Contributor harmless for any liability 198 | incurred by, or claims asserted against, such Contributor by reason 199 | of your accepting any such warranty or additional liability. 200 | 201 | END OF TERMS AND CONDITIONS 202 | ``` 203 | 204 | Licence for Eclipse Java Development Tools Core 205 | ----------------------------------------------- 206 | The Eclipse Java Development Tools Core and all included documentation is made available by Eclipse Software Foundation. Except indicated below, the Content is provided to you under the terms and conditions of the Eclipse Public License Version 1.0 ("EPL"). A copy of the EPL is available at http://www.eclipse.org/legal/epl-v10.html. 207 | 208 | ``` 209 | ------------------------------------------------------------------------- 210 | Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS 211 | OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION 212 | OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 213 | 1.DEFINITIONS 214 | 215 | "Contribution means: 216 | 217 | a) in the case of the initial Contributor, the initial code and documentation 218 | distributed under this Agreement, and 219 | 220 | b) in the case of each subsequent Contributor: 221 | 222 | i) changes to the Program, and 223 | 224 | ii) additions to the Program; 225 | 226 | where such changes and/or additions to the Program originate from and are distributed 227 | by that particular Contributor. A Contribution 'originates' from a Contributor if it 228 | was added to the Program by such Contributor itself or anyone acting on such 229 | Contributor's behalf. Contributions do not include additions to the Program which: 230 | (i) are separate modules of software distributed in conjunction with the Program under 231 | their own license agreement, and (ii) are not derivative works of the Program. 232 | 233 | "Contributor means any person or entity that distributes the Program. 234 | 235 | "Licensed Patents mean patent claims licensable by a Contributor which "are necessarily 236 | infringed by the use or sale of its Contribution "alone or when combined with the Program. 237 | 238 | "Program means the Contributions distributed in accordance with this "Agreement. 239 | 240 | "Recipient means anyone who receives the Program under this Agreement, "including all Contributors. 241 | 1.GRANT OF RIGHTS 242 | 243 | a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a 244 | non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare 245 | derivative works of, publicly display, publicly perform, distribute and sublicense 246 | the Contribution of such Contributor, if any, and such derivative works, in source code 247 | and object code form. 248 | 249 | b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a 250 | non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, 251 | use, sell, offer to sell, import and otherwise transfer the Contribution of such 252 | Contributor, if any, in source code and object code form. This patent license shall 253 | apply to the combination of the Contribution and the Program if, at the time the 254 | Contribution is added by the Contributor, such addition of the Contribution causes 255 | such combination to be covered by the Licensed Patents. The patent license shall not 256 | apply to any other combinations which include the Contribution. No hardware per se is 257 | licensed hereunder. 258 | 259 | c) Recipient understands that although each Contributor grants the licenses to its 260 | Contributions set forth herein, no assurances are provided by any Contributor that the 261 | Program does not infringe the patent or other intellectual property rights of any other 262 | entity. Each Contributor disclaims any liability to Recipient for claims brought by any 263 | other entity based on infringement of intellectual property rights or otherwise. As a 264 | condition to exercising the rights and licenses granted hereunder, each Recipient hereby 265 | assumes sole responsibility to secure any other intellectual property rights needed, if 266 | any. For example, if a third party patent license is required to allow Recipient to 267 | distribute the Program, it is Recipient's responsibility to acquire that license before 268 | distributing the Program. 269 | 270 | d) Each Contributor represents that to its knowledge it has sufficient copyright rights in 271 | its Contribution, if any, to grant the copyright license set forth in this Agreement. 272 | 273 | 1.REQUIREMENTS 274 | 275 | A Contributor may choose to distribute the Program in object code form under its own license 276 | agreement, provided that: 277 | 278 | a) it complies with the terms and conditions of this Agreement; and 279 | 280 | b) its license agreement: 281 | 282 | i) effectively disclaims on behalf of all Contributors all warranties and conditions, express 283 | and implied, including warranties or conditions of title and non-infringement, and implied 284 | warranties or conditions of merchantability and fitness for a particular purpose; 285 | 286 | ii) effectively excludes on behalf of all Contributors all liability for damages, including 287 | direct, indirect, special, incidental and consequential damages, such as lost profits; 288 | 289 | iii) states that any provisions which differ from this Agreement are offered by that Contributor 290 | alone and not by any other party; and 291 | 292 | iv) states that source code for the Program is available from such Contributor, and informs 293 | licensees how to obtain it in a reasonable manner on or through a medium customarily used 294 | for software exchange. 295 | 296 | When the Program is made available in source code form: 297 | 298 | a) it must be made available under this Agreement; and 299 | 300 | b) a copy of this Agreement must be included with each copy of the Program. 301 | 302 | Contributors may not remove or alter any copyright notices contained within the Program. 303 | 304 | Each Contributor must identify itself as the originator of its Contribution, if any, in a 305 | manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. 306 | 307 | 1.COMMERCIAL DISTRIBUTION 308 | 309 | Commercial distributors of software may accept certain responsibilities with respect to end 310 | users, business partners and the like. While this license is intended to facilitate the commercial 311 | use of the Program, the Contributor who includes the Program in a commercial product offering 312 | should do so in a manner which does not create potential liability for other Contributors. 313 | Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor 314 | ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor 315 | ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising 316 | from claims, lawsuits and other legal actions brought by a third party against the Indemnified 317 | Contributor to the extent caused by the acts or omissions of such Commercial Contributor in 318 | connection with its distribution of the Program in a commercial product offering. The obligations 319 | in this section do not apply to any claims or Losses relating to any actual or alleged intellectual 320 | property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the 321 | Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, 322 | and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. 323 | The Indemnified Contributor may participate in any such claim at its own expense. 324 | 325 | For example, a Contributor might include the Program in a commercial product offering, Product X. 326 | That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes 327 | performance claims, or offers warranties related to Product X, those performance claims and warranties 328 | are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor 329 | would have to defend claims against the other Contributors related to those performance claims and 330 | warranties, and if a court requires any other Contributor to pay any damages as a result, the 331 | Commercial Contributor must pay those damages. 332 | 333 | 1.NO WARRANTY 334 | 335 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT 336 | WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY 337 | WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 338 | Each Recipient is solely responsible for determining the appropriateness of using and distributing the 339 | Program and assumes all risks associated with its exercise of rights under this Agreement, including but 340 | not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss 341 | of data, programs or equipment, and unavailability or interruption of operations. 342 | 343 | 1.DISCLAIMER OF LIABILITY 344 | 345 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY 346 | LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 347 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 348 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 349 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE 350 | POSSIBILITY OF SUCH DAMAGES. 351 | 352 | 1.GENERAL 353 | 354 | If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect 355 | the validity or enforceability of the remainder of the terms of this Agreement, and without further 356 | action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make 357 | such provision valid and enforceable. 358 | 359 | If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim 360 | in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software 361 | or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 362 | 2(b) shall terminate as of the date such litigation is filed. 363 | 364 | All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material 365 | terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after 366 | becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient 367 | agrees to cease use and distribution of the Program as soon as reasonably practicable. However, 368 | Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program 369 | shall continue and survive. 370 | 371 | Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency 372 | the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward 373 | reserves the right to publish new versions (including revisions) of this Agreement from time to time. 374 | No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is 375 | the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the 376 | Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a 377 | distinguishing version number. The Program (including Contributions) may always be distributed subject 378 | to the version of the Agreement under which it was received. In addition, after a new version of the 379 | Agreement is published, Contributor may elect to distribute the Program (including its Contributions) 380 | under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no 381 | rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, 382 | by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement 383 | are reserved. 384 | 385 | This Agreement is governed by the laws of the State of New York and the intellectual property laws of the 386 | United States of America. No party to this Agreement will bring a legal action under this Agreement more than 387 | one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting 388 | litigation. 389 | ``` 390 | 391 | Licence for JSON In Java 392 | ------------------------ 393 | The JSON In Java and all included documentation is provided to you under the terms and conditions of the JSON License. A copy of the JSON License is available at http://json.org/license.html. 394 | 395 | ``` 396 | ------------------------------------------------------------------------- 397 | Copyright (c) 2002 JSON.org 398 | 399 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 400 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation 401 | the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 402 | and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 403 | 404 | The above copyright notice and this permission notice shall be included in all copies or substantial portions 405 | of the Software. 406 | 407 | The Software shall be used for Good, not Evil. 408 | 409 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 410 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 411 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 412 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 413 | DEALINGS IN THE SOFTWARE. 414 | ``` 415 | --------------------------------------------------------------------------------