Debian Codegraph @

net/sf/saxon/regex/CaseVariants.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class holds data about the case-variants of Unicode characters. The data is automatically
30 * generated from the Unicode database.
31 */
32 public class CaseVariants {
33
34 // Use one hashmap for characters with a single case variant, another for characters with multiple
35 // case variants, to reduce the number of objects that need to be allocated
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 * Get the case variants of a character
85 *
86 * @param code the character whose case variants are required
87 * @return the case variants of the character, excluding the character itself
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 * Get the case variants of roman letters (A-Z, a-z), other than the letters A-Z and a-z themselves
109 */
110
111 /*@NotNull*/ public static int[] ROMAN_VARIANTS = {0x0130, 0x0131, 0x212A, 0x017F};
112
113 // The data file casevariants.xml was formed by applying the following query to the XML
114 // version of the Unicode database (for Saxon 9.6, the Unicode 6.2.0 version was used)
115
116 // declare namespace u = "http://www.unicode.org/ns/2003/ucd/1.0";
117 // <variants>{
118 // let $chars := doc('ucd.all.flat.xml')/ * / * /u:char[@suc!='#' or @slc!='#']
119 // for $c in $chars
120 // let $variants := ($chars[(@cp, @suc[.!='#']) = $c/(@cp, @suc[.!='#'])] |
121 // $chars[(@cp, @slc[.!='#']) = $c/(@cp, @slc[.!='#'])]) except $c
122 // return
123 // if (count($variants) gt 0) then
124 // <c n="{$c/@cp}" v="{string-join($variants/@cp, ",")}"/>
125 // else ()
126 //
127 // }</variants>
128
129 }