|
|
|
@ -26,8 +26,7 @@ import java.io.FileWriter; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.ObjectOutputStream; |
|
|
|
import java.io.Writer; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.Formatter; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
@ -103,6 +102,9 @@ public class BuildMetadataProtoFromXml extends Command { |
|
|
|
" * Please don't modify it directly.\n" + |
|
|
|
" */\n\n"; |
|
|
|
|
|
|
|
private static final String REGION_CODE_CONSTS_COMMENT = |
|
|
|
" // Class containing string constants of region codes for easier testing.\n"; |
|
|
|
|
|
|
|
@Override |
|
|
|
public String getCommandName() { |
|
|
|
return CLASS_NAME; |
|
|
|
@ -208,12 +210,12 @@ public class BuildMetadataProtoFromXml extends Command { |
|
|
|
|
|
|
|
if (buildRegioncode) { |
|
|
|
SortedSet<String> regionCodeSet = new TreeSet<String>(); |
|
|
|
regionCodeSet.addAll(getDefaultRegionCodeList()); |
|
|
|
// Official code for the unknown region. |
|
|
|
regionCodeSet.add("ZZ"); |
|
|
|
regionCodeSet.addAll(BuildMetadataFromXml.buildRegionCodeList(metadataCollection)); |
|
|
|
List<String> regionCodeList = new ArrayList<String>(regionCodeSet); |
|
|
|
System.out.println("Found " + regionCodeList.size() + " region codes"); |
|
|
|
System.out.println("Found " + regionCodeSet.size() + " region codes"); |
|
|
|
|
|
|
|
writeRegionCodeConstantsToJavaFile(regionCodeList, outputDir, copyright); |
|
|
|
writeRegionCodeConstantsToJavaFile(regionCodeSet, outputDir, copyright); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
@ -359,11 +361,8 @@ public class BuildMetadataProtoFromXml extends Command { |
|
|
|
writer.addToBody(" }\n"); |
|
|
|
} |
|
|
|
|
|
|
|
private static final String REGION_CODE_CONSTS_COMMENT = |
|
|
|
" // Class containing string constants of region codes for easier testing.\n"; |
|
|
|
|
|
|
|
private static void writeRegionCodeConstantsToJavaFile(List<String> regionCodeList, String outputDir, String copyright) throws IOException { |
|
|
|
ClassWriter writer = new ClassWriter(outputDir, "RegionCode", copyright); |
|
|
|
private static void writeRegionCodeConstantsToJavaFile(Collection<String> regionCodeList, String outputDir, String copyright) throws IOException { |
|
|
|
ClassWriter writer = new ClassWriter(outputDir, "RegionCode", copyright, "final"); |
|
|
|
writer.addToBody(REGION_CODE_CONSTS_COMMENT); |
|
|
|
|
|
|
|
for (String regionCode : regionCodeList) { |
|
|
|
@ -380,23 +379,20 @@ public class BuildMetadataProtoFromXml extends Command { |
|
|
|
writer.writeToFile(); |
|
|
|
} |
|
|
|
|
|
|
|
private static List<String> getDefaultRegionCodeList() { |
|
|
|
// hard-coded list of region codes to include in the RegionCode class |
|
|
|
return Arrays.asList("ZZ"); |
|
|
|
} |
|
|
|
|
|
|
|
private static final class ClassWriter { |
|
|
|
private final String name; |
|
|
|
private final String copyright; |
|
|
|
private final String modifiers; |
|
|
|
|
|
|
|
private final SortedSet<String> imports; |
|
|
|
private final StringBuffer body; |
|
|
|
private final Formatter formatter; |
|
|
|
private final Writer writer; |
|
|
|
|
|
|
|
ClassWriter(String outputDir, String name, String copyright) throws IOException { |
|
|
|
ClassWriter(String outputDir, String name, String copyright, String modifiers) throws IOException { |
|
|
|
this.name = name; |
|
|
|
this.copyright = copyright; |
|
|
|
this.modifiers = modifiers; |
|
|
|
|
|
|
|
imports = new TreeSet<String>(); |
|
|
|
body = new StringBuffer(); |
|
|
|
@ -404,6 +400,10 @@ public class BuildMetadataProtoFromXml extends Command { |
|
|
|
writer = new BufferedWriter(new FileWriter(new File(outputDir, name + ".java"))); |
|
|
|
} |
|
|
|
|
|
|
|
ClassWriter(String outputDir, String name, String copyright) throws IOException { |
|
|
|
this(outputDir, name, copyright, "public"); |
|
|
|
} |
|
|
|
|
|
|
|
void addToImports(String name) { |
|
|
|
imports.add(name); |
|
|
|
} |
|
|
|
@ -428,7 +428,7 @@ public class BuildMetadataProtoFromXml extends Command { |
|
|
|
writer.write("\n"); |
|
|
|
} |
|
|
|
|
|
|
|
writer.write("public class " + name + " {\n"); |
|
|
|
writer.write((modifiers.isEmpty() ? "" : modifiers + " ") + "class " + name + " {\n"); |
|
|
|
writer.write(body.toString()); |
|
|
|
writer.write("}\n"); |
|
|
|
|
|
|
|
|