Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDR-17540 update SupplementalDataInfo reader to not use reentrant XPathParts #3632

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import java.util.Map;
import java.util.TreeMap;

/** Like {@link XPathParts} but does not depend on DTDs, etc. Safe for use at startup. */
/**
* Similar to {@link XPathParts} but does not depend on DTDs, etc. Doesn't cache, so most
* appropriate for one-time data loading where paths will be unique. Safe for use during static
* init. API conforms to {@link XPathValue}
*/
public class SimpleXPathParts extends XPathParser {

private final class Element {
Expand All @@ -15,9 +19,18 @@ private final class Element {
public Element(String name) {
this.name = name;
}

public Map<String, String> getAttributes() {
return attributes;
}

public boolean containsAttribute(String attribute) {
return attributes.containsKey(attribute);
}
}

final List<Element> elements = new LinkedList<>();
private final String xpath;

@Override
public String getElement(int i) {
Expand All @@ -27,6 +40,28 @@ public String getElement(int i) {
return elements.get(i).name;
}

/**
* Search for an element within the path.
*
* @param elementName the element to look for
* @return element number if found, else -1 if not found
*/
public int findElement(String elementName) {
for (int i = 0; i < elements.size(); ++i) {
Element e = elements.get(i);
if (!e.name.equals(elementName)) {
continue;
}
return i;
}
return -1;
}

@Override
public boolean containsElement(String elementName) {
return findElement(elementName) >= 0;
}

@Override
public String getAttributeValue(int i, String attribute) {
if (i < 0) {
Expand All @@ -35,6 +70,22 @@ public String getAttributeValue(int i, String attribute) {
return elements.get(i).attributes.get(attribute);
}

@Override
public Map<String, String> getAttributes(int elementIndex) {
return elements.get(elementIndex >= 0 ? elementIndex : elementIndex + size())
.getAttributes();
}

@Override
public boolean containsAttribute(String attribute) {
for (final Element e : elements) {
if (e.containsAttribute(attribute)) {
return true;
}
}
return false;
}

@Override
protected void handleClearElements() {
elements.clear();
Expand All @@ -51,16 +102,28 @@ protected void handleAddAttribute(String attribute, String value) {
}

SimpleXPathParts(String xpath) {
this.xpath = xpath;
handleParse(xpath, true);
}

@Override
public String toString() {
return xpath;
}

/**
* for API compatibility
* Parse an XPath string into a read-only structure.
*
* @param xpath
* @return
* @return the parsed XPath
*/
static SimpleXPathParts getFrozenInstance(String xpath) {
static XPathValue getFrozenInstance(String xpath) {
// return the abstract class
return new SimpleXPathParts(xpath);
}

@Override
public int size() {
return elements.size();
}
}
Loading
Loading