Debian Codegraph @

net/sf/saxon/regex/UnicodeBlocks.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.AllElementsSpaceStrippingRule;
14 import net.sf.saxon.om.AxisInfo;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.om.TreeInfo;
17 import net.sf.saxon.pattern.NameTest;
18 import net.sf.saxon.pattern.NodeKindTest;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.tree.iter.AxisIterator;
21 import net.sf.saxon.tree.util.FastStringBuffer;
22 import net.sf.saxon.type.Type;
23 import net.sf.saxon.z.IntBlockSet;
24 import net.sf.saxon.z.IntSet;
25
26 import javax.xml.transform.stream.StreamSource;
27 import java.io.InputStream;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 /**
33 * This class provides knowledge of the names and contents of Unicode character blocks,
34 * as referenced using the \p{IsXXXXX} construct in a regular expression. The underlying
35 * data is in an XML resource file UnicodeBlocks.xml
36 */
37 public class UnicodeBlocks {
38
39 private static Map<String, IntSet> blocks = null;
40
41 public static IntSet getBlock(String name) throws RESyntaxException {
42 if (blocks == null) {
43 readBlocks(new Configuration());
44 }
45 IntSet cc = blocks.get(name);
46 if (cc != null) {
47 return cc;
48 }
49 cc = blocks.get(normalizeBlockName(name));
50 return cc;
51 }
52
53 private static String normalizeBlockName(String name) {
54 FastStringBuffer fsb = new FastStringBuffer(name.length());
55 for (int i = 0; i < name.length(); i++) {
56 final char c = name.charAt(i);
57 switch (c) {
58 case ' ':
59 case '\t':
60 case '\r':
61 case '\n':
62 case '_':
63 // no action
64 break;
65 default:
66 fsb.append(c);
67 }
68 }
69 return fsb.toString();
70 }
71
72 private synchronized static void readBlocks(Configuration config) throws RESyntaxException {
73 blocks = new HashMap<String, IntSet>(250);
74 InputStream in = Configuration.locateResource("unicodeBlocks.xml", new ArrayList<String>(), new ArrayList<ClassLoader>());
75 if (in == null) {
76 throw new RESyntaxException("Unable to read unicodeBlocks.xml file");
77 }
78
79 ParseOptions options = new ParseOptions();
80 options.setSchemaValidationMode(Validation.SKIP);
81 options.setSpaceStrippingRule(AllElementsSpaceStrippingRule.getInstance());
82 TreeInfo doc;
83 try {
84 doc = config.buildDocumentTree(new StreamSource(in, "unicodeBlocks.xml"), options);
85 } catch (XPathException e) {
86 throw new RESyntaxException("Failed to process unicodeBlocks.xml: " + e.getMessage());
87 }
88
89 AxisIterator iter = doc.getRootNode().iterateAxis(AxisInfo.DESCENDANT, new NameTest(Type.ELEMENT, "", "block", config.getNamePool()));
90 while (true) {
91 NodeInfo item = iter.next();
92 if (item == null) {
93 break;
94 }
95 String blockName = normalizeBlockName(item.getAttributeValue("", "name"));
96 IntSet range = null;
97 AxisIterator ranges = item.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT);
98 while (true) {
99 NodeInfo rangeElement = ranges.next();
100 if (rangeElement == null) {
101 break;
102 }
103 int from = Integer.parseInt(rangeElement.getAttributeValue("", "from").substring(2), 16);
104 int to = Integer.parseInt(rangeElement.getAttributeValue("", "to").substring(2), 16);
105 IntSet cr = new IntBlockSet(from, to);
106 if (range == null) {
107 range = cr;
108 } else if (range instanceof IntBlockSet) {
109 range = range.mutableCopy().union(cr);
110 } else {
111 range = range.union(cr);
112 }
113 }
114 blocks.put(blockName, range);
115 }
116
117 }
118 }
119