xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/icu/XPPUtil.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 // Copyright 2009 Google Inc. All Rights Reserved.
2 
3 package org.unicode.cldr.icu;
4 
5 import org.unicode.cldr.util.CLDRFile;
6 import org.unicode.cldr.util.XPathParts;
7 
8 // Notes:
9 // - xpp.set (currently) resets the entire state of the xpp, so there are no side effects
10 // of these calls.
11 // - This is not used in a multi-threaded environment, so a static instance is ok.  If
12 // this turns out not to be true, make thread-local.
13 
14 public class XPPUtil {
getXpathName(String xpath)15     public static String getXpathName(String xpath) {
16         XPathParts xpp = XPathParts.getFrozenInstance(xpath);
17         return xpp.getElement(-1);
18     }
19 
getXpathName(String xpath, int pos)20     public static String getXpathName(String xpath, int pos) {
21         XPathParts xpp = XPathParts.getFrozenInstance(xpath);
22         return xpp.getElement(pos);
23     }
24 
getAttributeValue(String xpath, String element, String attribute)25     public static String getAttributeValue(String xpath, String element, String attribute) {
26         XPathParts xpp = XPathParts.getFrozenInstance(xpath);
27         int el = xpp.findElement(element);
28         if (el == -1) {
29             return null;
30         }
31         return xpp.getAttributeValue(el, attribute);
32     }
33 
getAttributeValue(String xpath, String attribute)34     public static String getAttributeValue(String xpath, String attribute) {
35         XPathParts xpp = XPathParts.getFrozenInstance(xpath);
36         return xpp.getAttributeValue(-1, attribute);
37     }
38 
getBasicAttributeValue( CLDRFile whichFile, String xpath, String attribute)39     public static String getBasicAttributeValue(
40             CLDRFile whichFile, String xpath, String attribute) {
41         String fullPath = whichFile.getFullXPath(xpath);
42         if (fullPath == null) {
43             return null;
44         }
45         return getAttributeValue(fullPath, attribute);
46     }
47 
findAttributeValue(CLDRFile file, String xpath, String attribute)48     public static String findAttributeValue(CLDRFile file, String xpath, String attribute) {
49         String fullPath = file.getFullXPath(xpath);
50         XPathParts xpp = XPathParts.getFrozenInstance(fullPath);
51         for (int j = 1; j <= xpp.size(); j++) {
52             String v = xpp.getAttributeValue(0 - j, attribute);
53             if (v != null) return v;
54         }
55         return null;
56     }
57 }
58