Skip to content
Draft
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 @@ -8,6 +8,8 @@
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.unicode.cldr.icu.LDMLConstants;
import org.xml.sax.InputSource;

Expand All @@ -17,6 +19,7 @@ public class DoctypeXmlStreamWrapper {
private static final byte DOCTYPE_BYTES[] = DOCTYPE.getBytes(StandardCharsets.UTF_8);
// the string to look for: xmlns="
private static final String XMLNS_EQUALS = LDMLConstants.XMLNS + "=\"";
private static final String XMLNS_SCHEMA_BASE = XMLNS_EQUALS + CLDRURLS.CLDR_SCHEMA_BASE + "/";

/**
* Size of the input buffer, needs to be able to handle any expansion when the header is updated
Expand Down Expand Up @@ -84,12 +87,35 @@ private static String fixup(byte[] inbuf, String encoding) {
}
}

private static final Pattern numberAndType = PatternCache.get("^([0-9][0-9])\\/([a-zA-Z0-9]+)");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to support any schema url including v45, etc. Ideally we would check conformsTo here, but at this level we don't typically have access to cldr-archive.


/** Fix an input String, including DOCTYPE */
private static String fixup(final String s) {
// exit if nothing matches
for (final DtdType d : DtdType.values()) {
if (s.contains(XMLNS_EQUALS + d.getNsUrl())) {
return fixup(s, d);
// Does it contain any CLDR-looking schemas?
final int xmlnsIndex = s.indexOf(XMLNS_SCHEMA_BASE);
if (xmlnsIndex != -1) {
final String remainder = s.substring(xmlnsIndex + XMLNS_SCHEMA_BASE.length());
final Matcher m = numberAndType.matcher(remainder);
if (m.lookingAt()) {
// final String ver = m.group(1); // TODO: CldrVersion.from(ver);
final String type = m.group(2);
// is it a valid DtdType?
final DtdType d = DtdType.valueOf(type);
if (d != null) {
// fix it up unconditionally.
// TODO: Check version # here.
return fixup(s, d);
} else {
System.err.println("Bad DTDtype: " + type + " in : " + s.substring(0, 100));
}
} else {
System.err.println(
"Nomatch in "
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • remove debug

+ remainder.substring(0, 100)
+ " against "
+ numberAndType.pattern()
+ " though "
+ m.toString());
}
}
// couldn't fix it, just pass through
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ public String getXsdPath() {
return dtdPath.replaceAll("\\.dtd$", ".xsd");
}

/** The xmlns name for this dtd type */
public String getNsUrl() {
return CLDRURLS.CLDR_CURVER_BASE + "/" + name();
}

/** The current version DTD as a URI */
String getDtdUri() {
return new File(CLDRPaths.BASE_DIRECTORY, dtdPath).toURI().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.unicode.cldr.util.CLDRConfig;
import org.unicode.cldr.util.DoctypeXmlStreamWrapper;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

Expand All @@ -29,7 +30,7 @@ public class TestKeyboardFlatten {
})
void TestBrokenImports(final String path) throws IOException {
try (final InputStream input = TestKeyboardFlatten.class.getResourceAsStream(path); ) {
final InputSource source = new InputSource(input);
final InputSource source = DoctypeXmlStreamWrapper.wrap(new InputSource(input));
// Expect failure.
assertThrows(
IllegalArgumentException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ public TimingInfo check(File systemID) {
xmlReader.setErrorHandler(new MyErrorHandler());
InputSource is = new InputSource(fis);
is.setSystemId(systemID.toString());
DoctypeXmlStreamWrapper.wrap(is);
xmlReader.parse(is);
Comment on lines -388 to -389
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error, the wrap() function had no side effect (it returns a wrapped object)

xmlReader.parse(DoctypeXmlStreamWrapper.wrap(is));
// fis.close();
} catch (SAXException | IOException e) {
errln("\t" + "Can't read " + systemID + "\t" + e.getClass() + "\t" + e.getMessage());
Expand Down
Loading