Debian Codegraph @

net/sf/saxon/pattern/UnionQNameTest.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.pattern;
9
10 import net.sf.saxon.om.StructuredQName;
11 import net.sf.saxon.tree.util.FastStringBuffer;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 /**
17 * A QNameTest that is the union of a number of supplied QNameTests
18 */
19 public class UnionQNameTest implements QNameTest {
20
21 List<QNameTest> tests;
22
23 public UnionQNameTest(List<QNameTest> tests) {
24 this.tests = new ArrayList<>(tests);
25 }
26
27 /**
28 * Test whether the QNameTest matches a given QName
29 *
30 * @param qname the QName to be matched
31 * @return true if the name matches, false if not
32 */
33
34 public boolean matches(StructuredQName qname) {
35 for (QNameTest test : tests) {
36 if (test.matches(qname)) {
37 return true;
38 }
39 }
40 return false;
41 }
42
43 /**
44 * The toString() method defines the format used in a package export, so it must be re-parseable
45 * @return a string representation: the individual qname tests, separated by vertical bar
46 */
47
48 public String toString() {
49 boolean started = false;
50 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
51 for (QNameTest qt : tests) {
52 if (started) {
53 fsb.append("|");
54 } else {
55 started = true;
56 }
57 fsb.append(qt.toString());
58 }
59 return fsb.toString();
60 }
61
62 /**
63 * Generate Javascript code to test if a name matches the test.
64 *
65 * @return JS code as a string. The generated code will be used
66 * as the body of a JS function in which the argument name "q" is an
67 * XdmQName object holding the name. The XdmQName object has properties
68 * uri and local.
69 * @param targetVersion the version of Saxon-JS being targeted
70 */
71 @Override
72 public String generateJavaScriptNameTest(int targetVersion) {
73 FastStringBuffer fsb = new FastStringBuffer(256);
74 boolean started = false;
75 for (QNameTest qt : tests) {
76 if (started) {
77 fsb.append("||");
78 } else {
79 started = true;
80 }
81 String test = qt.generateJavaScriptNameTest(targetVersion);
82 fsb.append("(" + test + ")");
83 }
84 return fsb.toString();
85 }
86 }
87