BUG-7057: introduce UntrustedXML class
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / ParserListenerUtils.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.yangtools.yang.parser.impl;
10
11 import com.google.common.base.Optional;
12 import java.util.List;
13 import org.antlr.v4.runtime.ParserRuleContext;
14 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
15
16 public final class ParserListenerUtils {
17
18     private ParserListenerUtils() {
19     }
20
21
22     /**
23      * Check this base type.
24      *
25      * @param typeName
26      *            base YANG type name
27      * @param moduleName
28      *            name of current module
29      * @param line
30      *            line in module
31      * @throws YangParseException
32      *             if this is one of YANG type which MUST contain additional
33      *             informations in its body
34      */
35     public static void checkMissingBody(final String typeName, final String moduleName, final int line) {
36         switch (typeName) {
37         case "decimal64":
38             throw new YangParseException(moduleName, line,
39                     "The 'fraction-digits' statement MUST be present if the type is 'decimal64'.");
40         case "identityref":
41             throw new YangParseException(moduleName, line,
42                     "The 'base' statement MUST be present if the type is 'identityref'.");
43         case "leafref":
44             throw new YangParseException(moduleName, line,
45                     "The 'path' statement MUST be present if the type is 'leafref'.");
46         case "bits":
47             throw new YangParseException(moduleName, line, "The 'bit' statement MUST be present if the type is 'bits'.");
48         case "enumeration":
49             throw new YangParseException(moduleName, line,
50                     "The 'enum' statement MUST be present if the type is 'enumeration'.");
51         }
52     }
53
54     public static <T extends ParserRuleContext> Optional<T> getFirstContext(final ParserRuleContext context,final Class<T> contextType) {
55         List<T> potential = context.getRuleContexts(contextType);
56         if (potential.isEmpty()) {
57             return Optional.absent();
58         }
59         return Optional.of(potential.get(0));
60     }
61
62 }