• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
62     /**
63      * Print a header for an XML file, where the generatedBy is normally MethodHandles.lookup().lookupClass().
64      * The only time it needs to be changed is if it is not being called directly from the generating tool.
65      * @param generatedBy
66      * @return
67      */
header(Class<?> generatedBy)68     public String header(Class<?> generatedBy) {
69         String gline = "";
70         if (generatedBy != null) {
71             gline = "\n\tGENERATED DATA — do not manually update!"
72                 + "\n\t\tGenerated by tool:\t" + generatedBy.getSimpleName() + "\n";
73             for (Annotation annotation : generatedBy.getAnnotations()) {
74                 if (annotation instanceof CLDRTool) {
75                     gline += "\t\tTool documented on:\t" + ((CLDRTool) annotation).url() + "\n";
76                     break;
77                 }
78             }
79         }
80 
81         return "<?xml version='1.0' encoding='UTF-8' ?>\n"
82         + "<!DOCTYPE " + this + " SYSTEM '../../" + dtdPath + "'>\n" // "common/dtd/ldmlSupplemental.dtd"
83         + "<!--\n"
84         + "\t© 1991-2017 Unicode, Inc.\n"
85         + "\tUnicode and the Unicode Logo are registered trademarks of Unicode, Inc. in the U.S. and other countries.\n"
86         + "\tFor terms of use, see http://www.unicode.org/copyright.html.\n"
87         + "\tCLDR data files are interpreted according to the LDML specification (http://unicode.org/reports/tr35/).\n"
88         + gline
89         + " -->\n"
90         + "<" + this + ">\n";
91     }
92 }