Debian Codegraph @

net/sf/saxon/tree/util/ProcInstParser.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.tree.util;
9
10 /**
11 * ProcInstParser is used to parse pseudo-attributes within Processing Instructions. This is a utility
12 * class that is never instantiated.
13 */
14
15
16 public class ProcInstParser {
17
18 /**
19 * Class is never instantiated
20 */
21
22 private ProcInstParser() {
23 }
24
25 /**
26 * Get a pseudo-attribute. This is useful only if the processing instruction data part
27 * uses pseudo-attribute syntax, which it does not have to. This syntax is as described
28 * in the W3C Recommendation "Associating Style Sheets with XML Documents".
29 *
30 * @return the value of the pseudo-attribute if present, or null if not
31 */
32
33 /*@Nullable*/
34 public static String getPseudoAttribute(/*@NotNull*/ String content, /*@NotNull*/ String name) {
35
36 int pos = 0;
37 while (pos <= content.length() - 4) { // minimum length of x=""
38 int nextQuote = -1;
39 for (int q = pos; q < content.length(); q++) {
40 if (content.charAt(q) == '"' || content.charAt(q) == '\'') {
41 nextQuote = q;
42 break;
43 }
44 }
45 if (nextQuote < 0) {
46 return null;
47 }
48
49 int closingQuote = content.indexOf(content.charAt(nextQuote), nextQuote + 1);
50 if (closingQuote < 0) {
51 return null;
52 }
53 int nextName = content.indexOf(name, pos);
54 if (nextName < 0) {
55 return null;
56 }
57 if (nextName < nextQuote) {
58 // check only spaces and equal signs between the name and the quote
59 boolean found = true;
60 for (int s = nextName + name.length(); s < nextQuote; s++) {
61 char c = content.charAt(s);
62 if (!Character.isWhitespace(c) && c != '=') {
63 found = false;
64 break;
65 }
66 }
67 if (found) {
68 String val = content.substring(nextQuote + 1, closingQuote);
69 String u = unescape(val);
70 if (u == null) {
71 // unescaping failed
72 return val;
73 } else {
74 return u;
75 }
76 }
77 }
78 pos = closingQuote + 1;
79 }
80 return null;
81 }
82
83 /**
84 * Interpret character references and built-in entity references.
85 *
86 * @return value with character references and built-in entity references expanded.
87 * If badly-formed or unrecognized references are found, return null
88 */
89
90 /*@Nullable*/
91 private static String unescape(/*@NotNull*/ String value) {
92 if (value.indexOf('&') < 0) {
93 return value;
94 }
95 FastStringBuffer sb = new FastStringBuffer(value.length());
96 for (int i = 0; i < value.length(); i++) {
97 char c = value.charAt(i);
98 if (c == '&') {
99 if (i + 2 < value.length() && value.charAt(i + 1) == '#') {
100 if (value.charAt(i + 2) == 'x') {
101 int x = i + 3;
102 int charval = 0;
103 while (x < value.length() && value.charAt(x) != ';') {
104 int digit = "0123456789abcdef".indexOf(value.charAt(x));
105 if (digit < 0) {
106 digit = "0123456789ABCDEF".indexOf(value.charAt(x));
107 }
108 if (digit < 0) {
109 return null;
110 }
111 charval = charval * 16 + digit;
112 x++;
113 }
114 char hexchar = (char) charval;
115 sb.append(hexchar);
116 i = x;
117 } else {
118 int x = i + 2;
119 int charval = 0;
120 while (x < value.length() && value.charAt(x) != ';') {
121 int digit = "0123456789".indexOf(value.charAt(x));
122 if (digit < 0) {
123 return null;
124 }
125 charval = charval * 10 + digit;
126 x++;
127 }
128 char decchar = (char) charval;
129 sb.append(decchar);
130 i = x;
131 }
132 } else if (value.substring(i + 1).startsWith("lt;")) {
133 sb.append('<');
134 i += 3;
135 } else if (value.substring(i + 1).startsWith("gt;")) {
136 sb.append('>');
137 i += 3;
138 } else if (value.substring(i + 1).startsWith("amp;")) {
139 sb.append('&');
140 i += 4;
141 } else if (value.substring(i + 1).startsWith("quot;")) {
142 sb.append('"');
143 i += 5;
144 } else if (value.substring(i + 1).startsWith("apos;")) {
145 sb.append('\'');
146 i += 5;
147 } else {
148 return null;
149 }
150
151 } else {
152 sb.append(c);
153 }
154 }
155 return sb.toString();
156 }
157
158 }