| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.regex; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.lib.ParseOptions; |
| 12 |
import net.sf.saxon.lib.Validation; |
| 13 |
import net.sf.saxon.om.AxisInfo; |
| 14 |
import net.sf.saxon.om.NodeInfo; |
| 15 |
import net.sf.saxon.pattern.NameTest; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 18 |
import net.sf.saxon.type.Type; |
| 19 |
import net.sf.saxon.z.IntArraySet; |
| 20 |
import net.sf.saxon.z.IntHashMap; |
| 21 |
import net.sf.saxon.z.IntToIntHashMap; |
| 22 |
import net.sf.saxon.z.IntToIntMap; |
| 23 |
|
| 24 |
import javax.xml.transform.stream.StreamSource; |
| 25 |
import java.io.InputStream; |
| 26 |
import java.util.ArrayList; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class CaseVariants { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private static IntToIntMap monoVariants = null; |
| 38 |
private static IntHashMap<int[]> polyVariants = null; |
| 39 |
|
| 40 |
|
| 41 |
static void build() { |
| 42 |
|
| 43 |
monoVariants = new IntToIntHashMap(2500); |
| 44 |
polyVariants = new IntHashMap<>(100); |
| 45 |
|
| 46 |
InputStream in = Configuration.locateResource("casevariants.xml", new ArrayList<>(), new ArrayList<>()); |
| 47 |
if (in == null) { |
| 48 |
throw new RuntimeException("Unable to read casevariants.xml file"); |
| 49 |
} |
| 50 |
|
| 51 |
Configuration config = new Configuration(); |
| 52 |
ParseOptions options = new ParseOptions(); |
| 53 |
options.setSchemaValidationMode(Validation.SKIP); |
| 54 |
NodeInfo doc; |
| 55 |
try { |
| 56 |
doc = config.buildDocumentTree(new StreamSource(in, "casevariants.xml"), options).getRootNode(); |
| 57 |
} catch (XPathException e) { |
| 58 |
throw new RuntimeException("Failed to build casevariants.xml", e); |
| 59 |
} |
| 60 |
|
| 61 |
AxisIterator iter = doc.iterateAxis(AxisInfo.DESCENDANT, new NameTest(Type.ELEMENT, "", "c", config.getNamePool())); |
| 62 |
while (true) { |
| 63 |
NodeInfo item = iter.next(); |
| 64 |
if (item == null) { |
| 65 |
break; |
| 66 |
} |
| 67 |
String code = item.getAttributeValue("", "n"); |
| 68 |
int icode = Integer.parseInt(code, 16); |
| 69 |
String variants = item.getAttributeValue("", "v"); |
| 70 |
String[] vhex = variants.split(","); |
| 71 |
int[] vint = new int[vhex.length]; |
| 72 |
for (int i=0; i<vhex.length; i++) { |
| 73 |
vint[i] = Integer.parseInt(vhex[i], 16); |
| 74 |
} |
| 75 |
if (vhex.length == 1) { |
| 76 |
monoVariants.put(icode, vint[0]); |
| 77 |
} else { |
| 78 |
polyVariants.put(icode, vint); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static int[] getCaseVariants(int code) { |
| 91 |
if (monoVariants == null) { |
| 92 |
build(); |
| 93 |
} |
| 94 |
int mono = monoVariants.get(code); |
| 95 |
if (mono != monoVariants.getDefaultValue()) { |
| 96 |
return new int[]{mono}; |
| 97 |
} else { |
| 98 |
int[] result = polyVariants.get(code); |
| 99 |
if (result == null) { |
| 100 |
return IntArraySet.EMPTY_INT_ARRAY; |
| 101 |
} else { |
| 102 |
return result; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static int[] ROMAN_VARIANTS = {0x0130, 0x0131, 0x212A, 0x017F}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
} |