a9417ca4d6d176750f5f85dd9e4d013b4b92e24c
[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 com.google.common.base.CharMatcher;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.regex.Pattern;
14 import javax.annotation.Nullable;
15 import org.antlr.v4.runtime.tree.TerminalNode;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
17 import org.opendaylight.yangtools.yang.common.YangVersion;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.SchemaNodeIdentifierBuildNamespace;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
24
25 public final class Utils {
26     private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
27     private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
28     private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
29     private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
30     private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
31
32     private Utils() {
33         throw new UnsupportedOperationException();
34     }
35
36     public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
37             final StatementSourceReference ref) {
38         return stringFromStringContext(context, YangVersion.VERSION_1, ref);
39     }
40
41     public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
42             final YangVersion yangVersion, final StatementSourceReference ref) {
43         final StringBuilder sb = new StringBuilder();
44         List<TerminalNode> strings = context.STRING();
45         if (strings.isEmpty()) {
46             strings = Collections.singletonList(context.IDENTIFIER());
47         }
48         for (final TerminalNode stringNode : strings) {
49             final String str = stringNode.getText();
50             final char firstChar = str.charAt(0);
51             final char lastChar = str.charAt(str.length() - 1);
52             if (firstChar == '"' && lastChar == '"') {
53                 final String innerStr = str.substring(1, str.length() - 1);
54                 /*
55                  * Unescape escaped double quotes, tabs, new line and backslash
56                  * in the inner string and trim the result.
57                  */
58                 checkDoubleQuotedString(innerStr, yangVersion, ref);
59                 sb.append(ESCAPED_TAB.matcher(
60                     ESCAPED_LF.matcher(
61                         ESCAPED_BACKSLASH.matcher(
62                             ESCAPED_DQUOT.matcher(innerStr).replaceAll("\\\""))
63                         .replaceAll("\\\\"))
64                     .replaceAll("\\\n"))
65                     .replaceAll("\\\t"));
66             } else if (firstChar == '\'' && lastChar == '\'') {
67                 /*
68                  * According to RFC6020 a single quote character cannot occur in
69                  * a single-quoted string, even when preceded by a backslash.
70                  */
71                 sb.append(str.substring(1, str.length() - 1));
72             } else {
73                 checkUnquotedString(str, yangVersion, ref);
74                 sb.append(str);
75             }
76         }
77         return sb.toString();
78     }
79
80     private static void checkUnquotedString(final String str, final YangVersion yangVersion,
81             final StatementSourceReference ref) {
82         if (yangVersion == YangVersion.VERSION_1_1) {
83             SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
84                 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
85         }
86     }
87
88     private static void checkDoubleQuotedString(final String str, final YangVersion yangVersion,
89             final StatementSourceReference ref) {
90         if (yangVersion == YangVersion.VERSION_1_1) {
91             for (int i = 0; i < str.length() - 1; i++) {
92                 if (str.charAt(i) == '\\') {
93                     switch (str.charAt(i + 1)) {
94                         case 'n':
95                         case 't':
96                         case '\\':
97                         case '\"':
98                             i++;
99                             break;
100                         default:
101                             throw new SourceException(ref, "YANG 1.1: illegal double quoted string (%s). In double "
102                                     + "quoted string the backslash must be followed by one of the following character "
103                                     + "[n,t,\",\\], but was '%s'.", str, str.charAt(i + 1));
104                     }
105                 }
106             }
107         }
108     }
109
110     @Nullable
111     public static StatementContextBase<?, ?, ?> findNode(final StmtContext<?, ?, ?> rootStmtCtx,
112             final SchemaNodeIdentifier node) {
113         return (StatementContextBase<?, ?, ?>) rootStmtCtx.getFromNamespace(SchemaNodeIdentifierBuildNamespace.class,
114             node);
115     }
116 }