|
16 | 16 | package org.androidannotations.internal.generation; |
17 | 17 |
|
18 | 18 | import java.io.IOException; |
| 19 | +import java.nio.charset.Charset; |
| 20 | +import java.nio.charset.UnsupportedCharsetException; |
19 | 21 |
|
20 | 22 | import javax.annotation.processing.Filer; |
21 | 23 |
|
| 24 | +import org.androidannotations.Option; |
22 | 25 | import org.androidannotations.internal.process.ModelProcessor; |
| 26 | +import org.androidannotations.logger.Logger; |
| 27 | +import org.androidannotations.logger.LoggerFactory; |
23 | 28 |
|
24 | 29 | import com.helger.jcodemodel.writer.PrologCodeWriter; |
25 | 30 |
|
26 | 31 | public class CodeModelGenerator { |
27 | 32 |
|
| 33 | + public static final Option OPTION_ENCODING = new Option("encoding", "UTF-8"); |
| 34 | + |
| 35 | + private static final Logger LOGGER = LoggerFactory.getLogger(CodeModelGenerator.class); |
| 36 | + |
28 | 37 | private final Filer filer; |
29 | 38 | private final String header; |
| 39 | + private final String encoding; |
30 | 40 |
|
31 | | - public CodeModelGenerator(Filer filer, String aaVersion) { |
| 41 | + public CodeModelGenerator(Filer filer, String aaVersion, String encoding) { |
32 | 42 | this.filer = filer; |
33 | 43 | this.header = "DO NOT EDIT THIS FILE.\n" |
34 | 44 | + "Generated using AndroidAnnotations " + aaVersion + ".\n\n" |
35 | 45 | + "You can create a larger work that contains this file and distribute that work under terms of your choice.\n"; |
| 46 | + this.encoding = encoding; |
36 | 47 | } |
37 | 48 |
|
38 | 49 | public void generate(ModelProcessor.ProcessResult processResult) throws IOException { |
| 50 | + Charset charset = getCharset(); |
39 | 51 |
|
40 | | - SourceCodeWriter sourceCodeWriter = new SourceCodeWriter(filer, processResult.originatingElements); |
| 52 | + SourceCodeWriter sourceCodeWriter = new SourceCodeWriter(filer, processResult.originatingElements, charset); |
41 | 53 |
|
42 | 54 | PrologCodeWriter prologCodeWriter = new PrologCodeWriter(sourceCodeWriter, header); |
43 | 55 |
|
44 | | - processResult.codeModel.build(prologCodeWriter, new ResourceCodeWriter(filer)); |
| 56 | + processResult.codeModel.build(prologCodeWriter, new ResourceCodeWriter(filer, charset)); |
| 57 | + } |
| 58 | + |
| 59 | + private Charset getCharset() { |
| 60 | + try { |
| 61 | + return Charset.forName(encoding); |
| 62 | + } catch (UnsupportedCharsetException exception) { |
| 63 | + Charset defaultCharset = Charset.defaultCharset(); |
| 64 | + |
| 65 | + LOGGER.warn("The requested charset ({}) is not available, falling back to platform default ({}).", encoding, defaultCharset); |
| 66 | + |
| 67 | + return defaultCharset; |
| 68 | + } |
45 | 69 | } |
46 | 70 | } |
0 commit comments