Skip to content

Commit

Permalink
Added encoding parameters to all save functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkschy committed May 21, 2019
1 parent f6ed1f0 commit fbca296
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
Expand Up @@ -173,7 +173,7 @@ public ParseResult<CompilationUnit> parse(final InputStream in) {
@Deprecated
public ParseResult<CompilationUnit> parse(final File file, final Charset encoding) throws FileNotFoundException {
ParseResult<CompilationUnit> result = parse(COMPILATION_UNIT, provider(file, encoding));
result.getResult().ifPresent(cu -> cu.setStorage(file.toPath()));
result.getResult().ifPresent(cu -> cu.setStorage(file.toPath(), encoding));
return result;
}

Expand All @@ -188,7 +188,7 @@ public ParseResult<CompilationUnit> parse(final File file, final Charset encodin
*/
public ParseResult<CompilationUnit> parse(final File file) throws FileNotFoundException {
ParseResult<CompilationUnit> result = parse(COMPILATION_UNIT, provider(file, configuration.getCharacterEncoding()));
result.getResult().ifPresent(cu -> cu.setStorage(file.toPath()));
result.getResult().ifPresent(cu -> cu.setStorage(file.toPath(), configuration.getCharacterEncoding()));
return result;
}

Expand All @@ -206,7 +206,7 @@ public ParseResult<CompilationUnit> parse(final File file) throws FileNotFoundEx
@Deprecated
public ParseResult<CompilationUnit> parse(final Path path, final Charset encoding) throws IOException {
ParseResult<CompilationUnit> result = parse(COMPILATION_UNIT, provider(path, encoding));
result.getResult().ifPresent(cu -> cu.setStorage(path));
result.getResult().ifPresent(cu -> cu.setStorage(path, encoding));
return result;
}

Expand All @@ -221,7 +221,7 @@ public ParseResult<CompilationUnit> parse(final Path path, final Charset encodin
*/
public ParseResult<CompilationUnit> parse(final Path path) throws IOException {
ParseResult<CompilationUnit> result = parse(COMPILATION_UNIT, provider(path, configuration.getCharacterEncoding()));
result.getResult().ifPresent(cu -> cu.setStorage(path));
result.getResult().ifPresent(cu -> cu.setStorage(path, configuration.getCharacterEncoding()));
return result;
}

Expand Down
Expand Up @@ -42,25 +42,25 @@
import com.github.javaparser.utils.ClassUtils;
import com.github.javaparser.utils.CodeGenerationUtils;
import com.github.javaparser.utils.Utils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import static com.github.javaparser.JavaToken.Kind.*;

import static com.github.javaparser.JavaToken.Kind.EOF;
import static com.github.javaparser.Providers.UTF8;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.Range.*;
import static com.github.javaparser.Range.range;
import static com.github.javaparser.StaticJavaParser.parseImport;
import static com.github.javaparser.StaticJavaParser.parseName;
import static com.github.javaparser.ast.Modifier.createModifierList;
import static com.github.javaparser.utils.CodeGenerationUtils.subtractPaths;
import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.Generated;
import com.github.javaparser.TokenRange;

/**
* <p>
Expand Down Expand Up @@ -564,6 +564,11 @@ public CompilationUnit setStorage(Path path) {
return this;
}

public CompilationUnit setStorage(Path path, Charset charset) {
this.storage = new Storage(this, path, charset);
return this;
}

/**
* Create (or overwrite) a module declaration in this compilation unit with name "name".
*
Expand Down Expand Up @@ -606,9 +611,18 @@ public static class Storage {

private final Path path;

private final Charset encoding;

private Storage(CompilationUnit compilationUnit, Path path) {
this.compilationUnit = compilationUnit;
this.path = path.toAbsolutePath();
this.encoding = UTF8;
}

private Storage(CompilationUnit compilationUnit, Path path, Charset encoding) {
this.compilationUnit = compilationUnit;
this.path = path.toAbsolutePath();
this.encoding = encoding;
}

/**
Expand Down Expand Up @@ -651,16 +665,27 @@ public void save() {
}

/**
* Saves a compilation unit to its original location with formatting according to the function
* passed as a parameter.
* Saves a compilation unit to its original location with formatting according to the function passed as a
* parameter.
*
* @param makeOutput a function that formats the compilation unit
*/
public void save(Function<CompilationUnit, String> makeOutput) {
save(makeOutput, encoding);
}

/**
* Saves a compilation unit to its original location with formatting and encoding according to the function and
* encoding passed as a parameter.
*
* @param makeOutput a function that formats the compilation unit
* @param encoding the encoding to use for the saved file
*/
public void save(Function<CompilationUnit, String> makeOutput, Charset encoding) {
try {
Files.createDirectories(path.getParent());
final String code = makeOutput.apply(getCompilationUnit());
Files.write(path, code.getBytes(UTF8));
Files.write(path, code.getBytes(encoding));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import com.github.javaparser.printer.PrettyPrinter;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -102,7 +103,7 @@ public ParseResult<CompilationUnit> tryToParse(String startPackage, String filen
Log.trace("Parsing %s", () -> path);
final ParseResult<CompilationUnit> result = new JavaParser(configuration)
.parse(COMPILATION_UNIT, provider(path));
result.getResult().ifPresent(cu -> cu.setStorage(path));
result.getResult().ifPresent(cu -> cu.setStorage(path, configuration.getCharacterEncoding()));
cache.put(relativePath, result);
return result;
}
Expand Down Expand Up @@ -246,7 +247,7 @@ private FileVisitResult callback(Path absolutePath, ParserConfiguration configur
Path localPath = root.relativize(absolutePath);
Log.trace("Parsing %s", () -> localPath);
ParseResult<CompilationUnit> result = new JavaParser(configuration).parse(COMPILATION_UNIT, provider(absolutePath));
result.getResult().ifPresent(cu -> cu.setStorage(absolutePath));
result.getResult().ifPresent(cu -> cu.setStorage(absolutePath, configuration.getCharacterEncoding()));
switch (callback.process(localPath, absolutePath, result)) {
case SAVE:
result.getResult().ifPresent(cu -> save(cu, absolutePath));
Expand Down Expand Up @@ -426,38 +427,68 @@ public SourceRoot add(CompilationUnit compilationUnit) {

/**
* Save the given compilation unit to the given path.
* @param cu the compilation unit
* @param path the path of the java file
*/
private SourceRoot save(CompilationUnit cu, Path path) {
return save(cu, path, parserConfiguration.getCharacterEncoding());
}

/**
* Save the given compilation unit to the given path.
* @param cu the compilation unit
* @param path the path of the java file
* @param encoding the encoding to use while saving the file
*/
private SourceRoot save(CompilationUnit cu, Path path, Charset encoding) {
assertNotNull(cu);
assertNotNull(path);
cu.setStorage(path);
cu.setStorage(path, encoding);
cu.getStorage().get().save(printer);
return this;
}

/**
* Save all previously parsed files back to a new path.
* @param root the root of the java packages
* @param encoding the encoding to use while saving the file
*/
public SourceRoot saveAll(Path root) {
public SourceRoot saveAll(Path root, Charset encoding) {
assertNotNull(root);
Log.info("Saving all files (%s) to %s", cache::size, () -> root);
for (Map.Entry<Path, ParseResult<CompilationUnit>> cu : cache.entrySet()) {
final Path path = root.resolve(cu.getKey());
if (cu.getValue().getResult().isPresent()) {
Log.trace("Saving %s", () -> path);
save(cu.getValue().getResult().get(), path);
save(cu.getValue().getResult().get(), path, encoding);
}
}
return this;
}

/**
* Save all previously parsed files back to a new path.
* @param root the root of the java packages
*/
public SourceRoot saveAll(Path root) {
return saveAll(root, parserConfiguration.getCharacterEncoding());
}

/**
* Save all previously parsed files back to where they were found.
*/
public SourceRoot saveAll() {
return saveAll(root);
}

/**
* Save all previously parsed files back to where they were found, with the given encoding.
* @param encoding the encoding to use.
*/
public SourceRoot saveAll(Charset encoding) {
return saveAll(root, encoding);
}

/**
* The Java files that have been parsed by this source root object, or have been added manually.
*/
Expand Down

0 comments on commit fbca296

Please sign in to comment.