YANGTOOLS-706: split out rfc8040-model-api
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / Utils.java
index a6414a29c125b4bd1619dd824d29a7e109e25129..693bfd5c498ae9c850c838340f23cd99a02935d2 100644 (file)
@@ -57,8 +57,12 @@ public final class Utils {
     private static final Pattern PATH_ABS = Pattern.compile("/[^/].*");
     @RegEx
     private static final String YANG_XPATH_FUNCTIONS_STRING =
-            "(re-match|deref|derived-from(-or-self)?|enum-value|bit-is-set)(\\()";
+            "(re-match|deref|derived-from(-or-self)?|enum-value|bit-is-set)([ \t\r\n]*)(\\()";
     private static final Pattern YANG_XPATH_FUNCTIONS_PATTERN = Pattern.compile(YANG_XPATH_FUNCTIONS_STRING);
+    private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
+    private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
+    private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
+    private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
 
     private static final ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
         @Override
@@ -105,7 +109,7 @@ public final class Utils {
             final String argumentValue) {
         final Set<SchemaNodeIdentifier.Relative> uniqueConstraintNodes = new HashSet<>();
         for (final String uniqueArgToken : SPACE_SPLITTER.split(argumentValue)) {
-            final SchemaNodeIdentifier nodeIdentifier = Utils.nodeIdentifierFromPath(ctx, uniqueArgToken);
+            final SchemaNodeIdentifier nodeIdentifier = nodeIdentifierFromPath(ctx, uniqueArgToken);
             SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
                     "Unique statement argument '%s' contains schema node identifier '%s' "
                             + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
@@ -177,7 +181,8 @@ public final class Utils {
         return identifier;
     }
 
-    static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
+    @SuppressWarnings("checkstyle:illegalCatch")
+    public static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
         // FIXME: is the path trimming really necessary??
         final List<QName> qNames = new ArrayList<>();
         for (final String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
@@ -216,8 +221,13 @@ public final class Utils {
                  * in the inner string and trim the result.
                  */
                 checkDoubleQuotedString(innerStr, yangVersion, ref);
-                sb.append(innerStr.replace("\\\"", "\"").replace("\\\\", "\\").replace("\\n", "\n")
-                        .replace("\\t", "\t"));
+                sb.append(ESCAPED_TAB.matcher(
+                    ESCAPED_LF.matcher(
+                        ESCAPED_BACKSLASH.matcher(
+                            ESCAPED_DQUOT.matcher(innerStr).replaceAll("\\\""))
+                        .replaceAll("\\\\"))
+                    .replaceAll("\\\n"))
+                    .replaceAll("\\\t"));
             } else if (firstChar == '\'' && lastChar == '\'') {
                 /*
                  * According to RFC6020 a single quote character cannot occur in