SourceZip.java revision b1b95ced57761caa9672bbf5c6c33cd461cda791
1/* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 22package com.github.javaparser.utils; 23 24import static com.github.javaparser.ParseStart.COMPILATION_UNIT; 25import static com.github.javaparser.Providers.provider; 26import static com.github.javaparser.utils.Utils.assertNotNull; 27 28import java.io.IOException; 29import java.nio.file.Path; 30import java.nio.file.Paths; 31import java.util.ArrayList; 32import java.util.Collections; 33import java.util.List; 34import java.util.zip.ZipEntry; 35import java.util.zip.ZipFile; 36 37import com.github.javaparser.JavaParser; 38import com.github.javaparser.ParseResult; 39import com.github.javaparser.ParserConfiguration; 40import com.github.javaparser.ast.CompilationUnit; 41 42/** 43 * A collection of Java source files and its sub-directories located in a ZIP or JAR file on the file system. 44 * Files can be parsed with a callback. 45 * 46 */ 47public class SourceZip { 48 49 private final Path zipPath; 50 private JavaParser javaParser; 51 52 /** 53 * Create a new ZIP parser. An instance of {@link JavaParser} with the default {@link ParserConfiguration} will be 54 * used to parse the ZIP. 55 * 56 * @param zipPath The absolute path of ZIP file to parse. 57 */ 58 public SourceZip(Path zipPath) { 59 this(zipPath, new ParserConfiguration()); 60 } 61 62 /** 63 * Create a new ZIP parser. An instance of {@link JavaParser} with the given configuration will be used to parse 64 * the ZIP. 65 * 66 * @param zipPath The absolute path of ZIP file to parse. 67 * @param configuration The configuration to initiate the default parser with. 68 */ 69 public SourceZip(Path zipPath, ParserConfiguration configuration) { 70 assertNotNull(zipPath); 71 assertNotNull(configuration); 72 this.zipPath = zipPath.normalize(); 73 this.javaParser = new JavaParser(configuration); 74 Log.info("New source zip at \"%s\"", this.zipPath); 75 } 76 77 /** 78 * Tries to parse all '.java' files in the ZIP located at this <i>SourceZip</i>'s path and returns the parse 79 * results in a list. 80 * 81 * @return A list of path-compilation unit pairs. 82 * 83 * @throws {@link IOException} If an error occurs while trying to parse the given source. 84 */ 85 public List<Pair<Path, ParseResult<CompilationUnit>>> parse() throws IOException { 86 Log.info("Parsing zip at \"%s\"", zipPath); 87 try (ZipFile zipFile = new ZipFile(zipPath.toFile())) { 88 List<Pair<Path, ParseResult<CompilationUnit>>> results = new ArrayList<>(); 89 for (ZipEntry entry : Collections.list(zipFile.entries())) { 90 if (!entry.isDirectory() && entry.getName().endsWith(".java")) { 91 Log.info("Parsing zip entry \"%s\"", entry.getName()); 92 final ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, 93 provider(zipFile.getInputStream(entry))); 94 results.add(new Pair(Paths.get(entry.getName()), result)); 95 } 96 } 97 return results; 98 } 99 } 100 101 /** 102 * Get the path of the ZIP file to be parsed. 103 * 104 * @return The absolute path of this ZIP file. 105 */ 106 public Path getZipPath() { 107 return zipPath; 108 } 109 110 /** 111 * Get the parser used for parsing. 112 * 113 * @return The currently set parser. 114 */ 115 public JavaParser getJavaParser() { 116 return javaParser; 117 } 118 119 /** 120 * Set the parser that is used for parsing. 121 * 122 * @param javaParser The parser to use. 123 */ 124 public SourceZip setJavaParser(JavaParser javaParser) { 125 assertNotNull(javaParser); 126 this.javaParser = javaParser; 127 return this; 128 } 129} 130