Do not use String.replace()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / Utils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_NAMESPACE;
11 import static org.opendaylight.yangtools.yang.common.YangConstants.YANG_XPATH_FUNCTIONS_PREFIX;
12
13 import com.google.common.base.CharMatcher;
14 import com.google.common.base.Splitter;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import javax.annotation.RegEx;
28 import javax.xml.xpath.XPath;
29 import javax.xml.xpath.XPathExpressionException;
30 import javax.xml.xpath.XPathFactory;
31 import org.antlr.v4.runtime.tree.TerminalNode;
32 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.YangVersion;
35 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
38 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
41 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public final class Utils {
48     private static final Logger LOG = LoggerFactory.getLogger(Utils.class);
49     private static final CharMatcher LEFT_PARENTHESIS_MATCHER = CharMatcher.is('(');
50     private static final CharMatcher RIGHT_PARENTHESIS_MATCHER = CharMatcher.is(')');
51     private static final CharMatcher AMPERSAND_MATCHER = CharMatcher.is('&');
52     private static final CharMatcher QUESTION_MARK_MATCHER = CharMatcher.is('?');
53     private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
54     private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings().trimResults();
55     private static final Splitter SPACE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
56     private static final Splitter COLON_SPLITTER = Splitter.on(":").omitEmptyStrings().trimResults();
57     private static final Pattern PATH_ABS = Pattern.compile("/[^/].*");
58     @RegEx
59     private static final String YANG_XPATH_FUNCTIONS_STRING =
60             "(re-match|deref|derived-from(-or-self)?|enum-value|bit-is-set)(\\()";
61     private static final Pattern YANG_XPATH_FUNCTIONS_PATTERN = Pattern.compile(YANG_XPATH_FUNCTIONS_STRING);
62     private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
63     private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
64     private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
65     private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
66
67     private static final ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
68         @Override
69         protected XPathFactory initialValue() {
70             return XPathFactory.newInstance();
71         }
72     };
73
74     private Utils() {
75         throw new UnsupportedOperationException();
76     }
77
78     /**
79      * Cleanup any resources attached to the current thread. Threads interacting with this class can cause thread-local
80      * caches to them. Invoke this method if you want to detach those resources.
81      */
82     public static void detachFromCurrentThread() {
83         XPATH_FACTORY.remove();
84     }
85
86     public static Collection<SchemaNodeIdentifier.Relative> transformKeysStringToKeyNodes(
87             final StmtContext<?, ?, ?> ctx, final String value) {
88         final List<String> keyTokens = SPACE_SPLITTER.splitToList(value);
89
90         // to detect if key contains duplicates
91         if (new HashSet<>(keyTokens).size() < keyTokens.size()) {
92             // FIXME: report all duplicate keys
93             throw new SourceException(ctx.getStatementSourceReference(), "Duplicate value in list key: %s", value);
94         }
95
96         final Set<SchemaNodeIdentifier.Relative> keyNodes = new HashSet<>();
97
98         for (final String keyToken : keyTokens) {
99
100             final SchemaNodeIdentifier.Relative keyNode = (Relative) SchemaNodeIdentifier.Relative.create(false,
101                     StmtContextUtils.qnameFromArgument(ctx, keyToken));
102             keyNodes.add(keyNode);
103         }
104
105         return keyNodes;
106     }
107
108     static Collection<SchemaNodeIdentifier.Relative> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
109             final String argumentValue) {
110         final Set<SchemaNodeIdentifier.Relative> uniqueConstraintNodes = new HashSet<>();
111         for (final String uniqueArgToken : SPACE_SPLITTER.split(argumentValue)) {
112             final SchemaNodeIdentifier nodeIdentifier = Utils.nodeIdentifierFromPath(ctx, uniqueArgToken);
113             SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
114                     "Unique statement argument '%s' contains schema node identifier '%s' "
115                             + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
116             uniqueConstraintNodes.add((SchemaNodeIdentifier.Relative) nodeIdentifier);
117         }
118         return ImmutableSet.copyOf(uniqueConstraintNodes);
119     }
120
121     private static String trimSingleLastSlashFromXPath(final String path) {
122         return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
123     }
124
125     static RevisionAwareXPath parseXPath(final StmtContext<?, ?, ?> ctx, final String path) {
126         final XPath xPath = XPATH_FACTORY.get().newXPath();
127         xPath.setNamespaceContext(StmtNamespaceContext.create(ctx,
128                 ImmutableBiMap.of(RFC6020_YANG_NAMESPACE.toString(), YANG_XPATH_FUNCTIONS_PREFIX)));
129
130         final String trimmed = trimSingleLastSlashFromXPath(path);
131         try {
132             // XPath extension functions have to be prefixed
133             // yang-specific XPath functions are in fact extended functions, therefore we have to add
134             // "yang" prefix to them so that they can be properly validated with the XPath.compile() method
135             // the "yang" prefix is bound to RFC6020 YANG namespace
136             final String prefixedXPath = addPrefixToYangXPathFunctions(trimmed, ctx);
137             // TODO: we could capture the result and expose its 'evaluate' method
138             xPath.compile(prefixedXPath);
139         } catch (final XPathExpressionException e) {
140             LOG.warn("Argument \"{}\" is not valid XPath string at \"{}\"", path, ctx.getStatementSourceReference(), e);
141         }
142
143         return new RevisionAwareXPathImpl(path, PATH_ABS.matcher(path).matches());
144     }
145
146     private static String addPrefixToYangXPathFunctions(final String path, final StmtContext<?, ?, ?> ctx) {
147         if (ctx.getRootVersion() == YangVersion.VERSION_1_1) {
148             // FIXME once Java 9 is available, change this to StringBuilder as Matcher.appendReplacement() and
149             // Matcher.appendTail() will accept StringBuilder parameter in Java 9
150             final StringBuffer result = new StringBuffer();
151             final String prefix = YANG_XPATH_FUNCTIONS_PREFIX + ":";
152             final Matcher matcher = YANG_XPATH_FUNCTIONS_PATTERN.matcher(path);
153             while (matcher.find()) {
154                 matcher.appendReplacement(result, prefix + matcher.group());
155             }
156
157             matcher.appendTail(result);
158             return result.toString();
159         }
160
161         return path;
162     }
163
164     public static QName trimPrefix(final QName identifier) {
165         final String prefixedLocalName = identifier.getLocalName();
166         final String[] namesParts = prefixedLocalName.split(":");
167
168         if (namesParts.length == 2) {
169             final String localName = namesParts[1];
170             return QName.create(identifier.getModule(), localName);
171         }
172
173         return identifier;
174     }
175
176     public static String trimPrefix(final String identifier) {
177         final List<String> namesParts = COLON_SPLITTER.splitToList(identifier);
178         if (namesParts.size() == 2) {
179             return namesParts.get(1);
180         }
181         return identifier;
182     }
183
184     @SuppressWarnings("checkstyle:illegalCatch")
185     static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
186         // FIXME: is the path trimming really necessary??
187         final List<QName> qNames = new ArrayList<>();
188         for (final String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
189             try {
190                 final QName qName = StmtContextUtils.qnameFromArgument(ctx, nodeName);
191                 qNames.add(qName);
192             } catch (final RuntimeException e) {
193                 throw new SourceException(ctx.getStatementSourceReference(), e,
194                         "Failed to parse node '%s' in path '%s'", nodeName, path);
195             }
196         }
197
198         return SchemaNodeIdentifier.create(qNames, PATH_ABS.matcher(path).matches());
199     }
200
201     public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
202             final StatementSourceReference ref) {
203         return stringFromStringContext(context, YangVersion.VERSION_1, ref);
204     }
205
206     public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
207             final YangVersion yangVersion, final StatementSourceReference ref) {
208         final StringBuilder sb = new StringBuilder();
209         List<TerminalNode> strings = context.STRING();
210         if (strings.isEmpty()) {
211             strings = Collections.singletonList(context.IDENTIFIER());
212         }
213         for (final TerminalNode stringNode : strings) {
214             final String str = stringNode.getText();
215             final char firstChar = str.charAt(0);
216             final char lastChar = str.charAt(str.length() - 1);
217             if (firstChar == '"' && lastChar == '"') {
218                 final String innerStr = str.substring(1, str.length() - 1);
219                 /*
220                  * Unescape escaped double quotes, tabs, new line and backslash
221                  * in the inner string and trim the result.
222                  */
223                 checkDoubleQuotedString(innerStr, yangVersion, ref);
224                 sb.append(ESCAPED_TAB.matcher(
225                     ESCAPED_LF.matcher(
226                         ESCAPED_BACKSLASH.matcher(
227                             ESCAPED_DQUOT.matcher(innerStr).replaceAll("\\\""))
228                         .replaceAll("\\\\"))
229                     .replaceAll("\\\n"))
230                     .replaceAll("\\\t"));
231             } else if (firstChar == '\'' && lastChar == '\'') {
232                 /*
233                  * According to RFC6020 a single quote character cannot occur in
234                  * a single-quoted string, even when preceded by a backslash.
235                  */
236                 sb.append(str.substring(1, str.length() - 1));
237             } else {
238                 checkUnquotedString(str, yangVersion, ref);
239                 sb.append(str);
240             }
241         }
242         return sb.toString();
243     }
244
245     private static void checkUnquotedString(final String str, final YangVersion yangVersion,
246             final StatementSourceReference ref) {
247         if (yangVersion == YangVersion.VERSION_1_1) {
248             SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
249                 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
250         }
251     }
252
253     private static void checkDoubleQuotedString(final String str, final YangVersion yangVersion,
254             final StatementSourceReference ref) {
255         if (yangVersion == YangVersion.VERSION_1_1) {
256             for (int i = 0; i < str.length() - 1; i++) {
257                 if (str.charAt(i) == '\\') {
258                     switch (str.charAt(i + 1)) {
259                         case 'n':
260                         case 't':
261                         case '\\':
262                         case '\"':
263                             i++;
264                             break;
265                         default:
266                             throw new SourceException(ref, "YANG 1.1: illegal double quoted string (%s). In double "
267                                     + "quoted string the backslash must be followed by one of the following character "
268                                     + "[n,t,\",\\], but was '%s'.", str, str.charAt(i + 1));
269                     }
270                 }
271             }
272         }
273     }
274
275     @Nullable
276     public static StatementContextBase<?, ?, ?> findNode(final StmtContext<?, ?, ?> rootStmtCtx,
277             final SchemaNodeIdentifier node) {
278         return (StatementContextBase<?, ?, ?>) rootStmtCtx.getFromNamespace(SchemaNodeIdentifierBuildNamespace.class,
279             node);
280     }
281
282     static @Nonnull Boolean parseBoolean(final StmtContext<?, ?, ?> ctx, final String input) {
283         if ("true".equals(input)) {
284             return Boolean.TRUE;
285         } else if ("false".equals(input)) {
286             return Boolean.FALSE;
287         } else {
288             throw new SourceException(ctx.getStatementSourceReference(),
289                 "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
290                 ctx.getPublicDefinition().getStatementName(), ctx.getPublicDefinition().getArgumentName(), input);
291         }
292     }
293
294     static String internBoolean(final String input) {
295         if ("true".equals(input)) {
296             return "true";
297         } else if ("false".equals(input)) {
298             return "false";
299         } else {
300             return input;
301         }
302     }
303
304     /**
305      * Replaces illegal characters of QName by the name of the character (e.g. '?' is replaced by "QuestionMark" etc.).
306      *
307      * @param string
308      *            input String
309      * @return result String
310      */
311     public static String replaceIllegalCharsForQName(String string) {
312         string = LEFT_PARENTHESIS_MATCHER.replaceFrom(string, "LeftParenthesis");
313         string = RIGHT_PARENTHESIS_MATCHER.replaceFrom(string, "RightParenthesis");
314         string = AMPERSAND_MATCHER.replaceFrom(string, "Ampersand");
315         string = QUESTION_MARK_MATCHER.replaceFrom(string, "QuestionMark");
316
317         return string;
318     }
319
320     public static boolean belongsToTheSameModule(final QName targetStmtQName, final QName sourceStmtQName) {
321         return targetStmtQName.getModule().equals(sourceStmtQName.getModule());
322     }
323 }