• 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         "supplemental-temp",
24         "transforms",
25         "validity"),
26     ldmlBCP47("common/dtd/ldmlBCP47.dtd", "1.7.2", null,
27         "bcp47"),
28     keyboard("keyboards/dtd/ldmlKeyboard.dtd", "22.1", null,
29         "../keyboards"),
30     platform("keyboards/dtd/ldmlPlatform.dtd", "22.1", null,
31         "../keyboards");
32     public static final Set<DtdType> STANDARD_SET = ImmutableSet.of(
33         ldmlBCP47, supplementalData, ldml, keyboard, platform);
34 
35     static Pattern FIRST_ELEMENT = PatternCache.get("//([^/\\[]*)");
36 
37     public final String dtdPath;
38     public final DtdType rootType;
39     public final String firstVersion;
40     public final Set<String> directories;
41 
DtdType(String dtdPath)42     private DtdType(String dtdPath) {
43         this(dtdPath, null, null);
44     }
45 
DtdType(String dtdPath, DtdType realType)46     private DtdType(String dtdPath, DtdType realType) {
47         this(dtdPath, null, realType);
48     }
49 
DtdType(String dtdPath, String firstVersion, DtdType realType, String... directories)50     private DtdType(String dtdPath, String firstVersion, DtdType realType, String... directories) {
51         this.dtdPath = dtdPath;
52         this.rootType = realType == null ? this : realType;
53         this.firstVersion = firstVersion;
54         this.directories = ImmutableSet.copyOf(directories);
55     }
56 
fromPath(String elementOrPath)57     public static DtdType fromPath(String elementOrPath) {
58         Matcher m = FIRST_ELEMENT.matcher(elementOrPath);
59         m.lookingAt();
60         return DtdType.valueOf(m.group(1));
61     }
62 
63     /**
64      * Print a header for an XML file, where the generatedBy is normally MethodHandles.lookup().lookupClass().
65      * The only time it needs to be changed is if it is not being called directly from the generating tool.
66      * @param generatedBy
67      * @return
68      */
header(Class<?> generatedBy)69     public String header(Class<?> generatedBy) {
70         String gline = "";
71         if (generatedBy != null) {
72             gline = "\n\tGENERATED DATA — do not manually update!"
73                 + "\n\t\tGenerated by tool:\t" + generatedBy.getSimpleName() + "\n";
74             for (Annotation annotation : generatedBy.getAnnotations()) {
75                 if (annotation instanceof CLDRTool) {
76                     gline += "\t\tTool documented on:\t" + ((CLDRTool) annotation).url() + "\n";
77                     break;
78                 }
79             }
80         }
81 
82         return "<?xml version='1.0' encoding='UTF-8' ?>\n"
83         + "<!DOCTYPE " + this + " SYSTEM '../../" + dtdPath + "'>\n" // "common/dtd/ldmlSupplemental.dtd"
84         + "<!--\n"
85         + CldrUtility.getCopyrightString("\t")
86         + gline
87         + " -->\n"
88         + "<" + this + ">\n";
89     }
90 }