Debian Codegraph @

net/sf/saxon/om/QNameParser.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2013 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.om;
9
10 import net.sf.saxon.lib.NamespaceConstant;
11 import net.sf.saxon.query.XQueryParser;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.value.Whitespace;
14
15 /**
16 * Parser to handle QNames in either lexical QName or EQName syntax, including resolving any prefix against
17 * a URIResolver. The parser can be instantiated with various options to control the returned error code,
18 * the handling of defaults, etc.
19 */
20 public class QNameParser {
21
22 private NamespaceResolver resolver;
23 private boolean acceptEQName = false;
24 private String defaultNamespace = null;
25 private String errorOnBadSyntax = "XPST0003";
26 private String errorOnUnresolvedPrefix = "XPST0081";
27 private XQueryParser.Unescaper unescaper = null;
28
29 public QNameParser(NamespaceResolver resolver) {
30 this.resolver = resolver;
31 }
32
33 public void setNamespaceResolver(NamespaceResolver resolver) {
34 this.resolver = resolver;
35 }
36
37 public void setAcceptEQName(boolean acceptEQName) {
38 this.acceptEQName = acceptEQName;
39 }
40
41 public void setUnescaper(XQueryParser.Unescaper unescaper) {
42 this.unescaper = unescaper;
43 }
44
45 public void setDefaultNamespace(String defaultNamespace) {
46 this.defaultNamespace = defaultNamespace;
47 }
48
49 public void setErrorOnBadSyntax(String code) {
50 errorOnBadSyntax = code;
51 }
52
53 public void setErrorOnUnresolvedPrefix(String code) {
54 errorOnUnresolvedPrefix = code;
55 }
56
57 /**
58 * Make a structured QName from a lexical QName, using a supplied NamespaceResolver to
59 * resolve the prefix. The local part of the QName is checked for validity; the prefix is
60 * not checked, on the grounds that an invalid prefix will fail to resolve to a URI.
61 *
62 * @param lexicalName the QName as a lexical name (prefix:local)
63 * @return the StructuredQName object corresponding to this lexical QName
64 * @throws net.sf.saxon.trans.XPathException if the namespace prefix is not in scope or if the value is lexically
65 * invalid. Error code FONS0004 is set if the namespace prefix has not been declared; error
66 * code FOCA0002 is set if the name is lexically invalid. These may need to be
67 * changed on return depending on the caller's requirements.
68 */
69
70 public StructuredQName parse(CharSequence lexicalName) throws XPathException {
71 lexicalName = Whitespace.trimWhitespace(lexicalName);
72 if (acceptEQName && lexicalName.length() >= 4 && lexicalName.charAt(0) == 'Q' && lexicalName.charAt(1) == '{') {
73 String name = lexicalName.toString();
74 int endBrace = name.indexOf('}');
75 if (endBrace < 0) {
76 throw new XPathException("Invalid EQName: closing brace not found", errorOnBadSyntax);
77 } else if (endBrace == name.length() - 1) {
78 throw new XPathException("Invalid EQName: local part is missing", errorOnBadSyntax);
79 }
80 String uri = name.substring(2, endBrace).toString();
81 //String uri = Whitespace.collapseWhitespace(name.substring(2, endBrace)).toString();
82 if (unescaper != null && uri.contains("&")) {
83 uri = unescaper.unescape(uri).toString();
84 }
85 if (uri.equals(NamespaceConstant.XMLNS)) {
86 throw new XPathException("The string '" + NamespaceConstant.XMLNS + "' cannot be used as a namespace URI", "XQST0070");
87 }
88 String local = name.substring(endBrace + 1);
89 checkLocalName(local);
90 return new StructuredQName("", uri, local);
91 }
92 try {
93 String[] parts = NameChecker.getQNameParts(lexicalName);
94 checkLocalName(parts[1]);
95 if (parts[0].isEmpty()) {
96 return new StructuredQName("", defaultNamespace, parts[1]);
97 }
98 String uri = resolver.getURIForPrefix(parts[0], false);
99 if (uri == null) {
100 throw new XPathException("Namespace prefix '" + parts[0] + "' has not been declared", errorOnUnresolvedPrefix);
101 }
102 return new StructuredQName(parts[0], uri, parts[1]);
103 } catch (QNameException e) {
104 throw new XPathException(e.getMessage(), errorOnBadSyntax);
105 }
106 }
107
108 private void checkLocalName(String local) throws XPathException {
109 if (!NameChecker.isValidNCName(local)) {
110 throw new XPathException("Invalid EQName: local part is not a valid NCName", errorOnBadSyntax);
111 }
112 }
113
114 }
115