+ return internedQName(ctx,
+ InferenceException.throwIfNull(qnameModule, ctx.getStatementSourceReference(),
+ "Cannot resolve QNameModule for '%s'", value), localName);
+ }
+
+ /**
+ * Parse a YANG identifier string in context of a statement.
+ *
+ * @param ctx Statement context
+ * @param str String to be parsed
+ * @return An interned QName
+ * @throws NullPointerException if any of the arguments are null
+ * @throws SourceException if the string is not a valid YANG identifier
+ */
+ public static QName parseIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
+ SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
+ "Identifier may not be an empty string");
+ return internedQName(ctx, str);
+ }
+
+ public static QName parseNodeIdentifier(StmtContext<?, ?, ?> ctx, final String prefix,
+ final String localName) {
+ final QNameModule module = getModuleQNameByPrefix(ctx, prefix);
+ if (module != null) {
+ return internedQName(ctx, module, localName);
+ }
+
+ if (ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
+ final Optional<? extends StmtContext<?, ?, ?>> optOrigCtx = ctx.getOriginalCtx();
+ if (optOrigCtx.isPresent()) {
+ ctx = optOrigCtx.get();
+ final QNameModule origModule = getModuleQNameByPrefix(ctx, prefix);
+ if (origModule != null) {
+ return internedQName(ctx, origModule, localName);
+ }
+ }
+ }
+
+ throw new InferenceException(ctx.getStatementSourceReference(), "Cannot resolve QNameModule for '%s'", prefix);
+ }
+
+ /**
+ * Parse a YANG node identifier string in context of a statement.
+ *
+ * @param ctx Statement context
+ * @param str String to be parsed
+ * @return An interned QName
+ * @throws NullPointerException if any of the arguments are null
+ * @throws SourceException if the string is not a valid YANG node identifier
+ */
+ public static QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
+ SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
+ "Node identifier may not be an empty string");
+
+ final int colon = str.indexOf(':');
+ if (colon == -1) {
+ return internedQName(ctx, str);
+ }
+
+ final String prefix = str.substring(0, colon);
+ SourceException.throwIf(prefix.isEmpty(), ctx.getStatementSourceReference(),
+ "String '%s' has an empty prefix", str);
+ final String localName = str.substring(colon + 1);
+ SourceException.throwIf(localName.isEmpty(), ctx.getStatementSourceReference(),
+ "String '%s' has an empty identifier", str);
+
+ return parseNodeIdentifier(ctx, prefix, localName);
+ }
+
+ private static QName internedQName(final StmtContext<?, ?, ?> ctx, final String localName) {
+ return internedQName(ctx, getRootModuleQName(ctx), localName);
+ }
+
+ private static QName internedQName(final StmtContext<?, ?, ?> ctx, final QNameModule module,
+ final String localName) {
+ final QName template;
+ try {
+ template = QName.create(module, localName);
+ } catch (IllegalArgumentException e) {
+ throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid identifier '%s'", localName);
+ }
+ return template.intern();