1 package org.unicode.cldr.util; 2 3 import java.lang.annotation.Annotation; 4 import java.util.Set; 5 import java.util.regex.Matcher; 6 import java.util.regex.Pattern; 7 8 import com.google.common.collect.ImmutableSet; 9 10 public enum DtdType { 11 ldml("common/dtd/ldml.dtd", null, null, 12 "main", 13 "annotations", 14 "annotationsDerived", 15 "casing", 16 "collation", 17 "rbnf", 18 "segments", 19 "subdivisions"), 20 ldmlICU("common/dtd/ldmlICU.dtd", ldml), 21 supplementalData("common/dtd/ldmlSupplemental.dtd", null, null, 22 "supplemental", 23 "transforms", 24 "validity"), 25 ldmlBCP47("common/dtd/ldmlBCP47.dtd", "1.7.2", null, 26 "bcp47"), 27 keyboard("keyboards/dtd/ldmlKeyboard.dtd", "22.1", null, 28 "../keyboards"), 29 platform("keyboards/dtd/ldmlPlatform.dtd", "22.1", null, 30 "../keyboards"); 31 public static final Set<DtdType> STANDARD_SET = ImmutableSet.of( 32 ldmlBCP47, supplementalData, ldml, keyboard, platform); 33 34 static Pattern FIRST_ELEMENT = PatternCache.get("//([^/\\[]*)"); 35 36 public final String dtdPath; 37 public final DtdType rootType; 38 public final String firstVersion; 39 public final Set<String> directories; 40 DtdType(String dtdPath)41 private DtdType(String dtdPath) { 42 this(dtdPath, null, null); 43 } 44 DtdType(String dtdPath, DtdType realType)45 private DtdType(String dtdPath, DtdType realType) { 46 this(dtdPath, null, realType); 47 } 48 DtdType(String dtdPath, String firstVersion, DtdType realType, String... directories)49 private DtdType(String dtdPath, String firstVersion, DtdType realType, String... directories) { 50 this.dtdPath = dtdPath; 51 this.rootType = realType == null ? this : realType; 52 this.firstVersion = firstVersion; 53 this.directories = ImmutableSet.copyOf(directories); 54 } 55 fromPath(String elementOrPath)56 public static DtdType fromPath(String elementOrPath) { 57 Matcher m = FIRST_ELEMENT.matcher(elementOrPath); 58 m.lookingAt(); 59 return DtdType.valueOf(m.group(1)); 60 } 61 header(Class<?> generatedBy)62 public String header(Class<?> generatedBy) { 63 String gline = ""; 64 if (generatedBy != null) { 65 gline = "\n\tGENERATED DATA — do not manually update!" 66 + "\n\t\tGenerated by tool:\t" + generatedBy.getSimpleName() + "\n"; 67 for (Annotation annotation : generatedBy.getAnnotations()) { 68 if (annotation instanceof CLDRTool) { 69 gline += "\t\tTool documented on:\t" + ((CLDRTool) annotation).url() + "\n"; 70 break; 71 } 72 } 73 } 74 75 return "<?xml version='1.0' encoding='UTF-8' ?>\n" 76 + "<!DOCTYPE " + this + " SYSTEM '../../" + dtdPath + "'>\n" // "common/dtd/ldmlSupplemental.dtd" 77 + "<!--\n" 78 + "\t© 1991-2017 Unicode, Inc.\n" 79 + "\tUnicode and the Unicode Logo are registered trademarks of Unicode, Inc. in the U.S. and other countries.\n" 80 + "\tFor terms of use, see http://www.unicode.org/copyright.html.\n" 81 + "\tCLDR data files are interpreted according to the LDML specification (http://unicode.org/reports/tr35/).\n" 82 + gline 83 + " -->\n" 84 + "<" + this + ">\n"; 85 } 86 }