- private Module resolveModuleForPrefix(final String prefix,
- final Module parent) {
- if ((prefix != null) && (parent != null)) {
- final Set<ModuleImport> imports = parent.getImports();
-
- if (imports != null) {
- for (final ModuleImport impModule : imports) {
- final String impModPrefix = impModule.getPrefix();
- if ((impModPrefix != null) && prefix.equals(impModPrefix)) {
- return resolveModuleFromContext(prefix,
- impModule.getModuleName());
+ public static Module findParentModule(final SchemaContext context, final SchemaNode schemaNode) {
+ if (context == null) {
+ throw new IllegalArgumentException("Schema Context reference cannot be NULL!");
+ }
+ if (schemaNode == null) {
+ throw new IllegalArgumentException("Schema Node cannot be NULL!");
+ }
+
+ final SchemaPath schemaPath = schemaNode.getPath();
+ if (schemaPath == null) {
+ throw new IllegalStateException("Schema Path for Schema Node is not " +
+ "set properly (Schema Path is NULL)");
+ }
+ final List<QName> qnamedPath = schemaPath.getPath();
+ if (qnamedPath == null || qnamedPath.isEmpty()) {
+ throw new IllegalStateException("Schema Path contains invalid state of path parts." +
+ "The Schema Path MUST contain at least ONE QName which defines namespace and Local name" +
+ "of path.");
+ }
+ final QName qname = qnamedPath.get(0);
+ return context.findModuleByNamespace(qname.getNamespace());
+ }
+
+ private static DataSchemaNode findSchemaNodeForGivenPath(final SchemaContext context, final Module module,
+ final Queue<QName> qnamedPath) {
+ if ((module != null) && (module.getNamespace() != null)
+ && (qnamedPath != null)) {
+ DataNodeContainer nextNode = module;
+ final URI moduleNamespace = module.getNamespace();
+
+ QName childNodeQName = null;
+ DataSchemaNode schemaNode = null;
+ while ((nextNode != null) && !qnamedPath.isEmpty()) {
+ childNodeQName = qnamedPath.peek();
+ if (childNodeQName != null) {
+ final URI childNodeNamespace = childNodeQName.getNamespace();
+
+ schemaNode = nextNode.getDataChildByName(childNodeQName);
+ if (schemaNode != null) {
+ if (schemaNode instanceof ContainerSchemaNode) {
+ nextNode = (ContainerSchemaNode) schemaNode;
+ } else if (schemaNode instanceof ListSchemaNode) {
+ nextNode = (ListSchemaNode) schemaNode;
+ } else {
+ nextNode = null;
+ }
+ } else if (!childNodeNamespace.equals(moduleNamespace)) {
+ final Module nextModule = context
+ .findModuleByNamespace(childNodeNamespace);
+ schemaNode = findSchemaNodeForGivenPath(context, nextModule,
+ qnamedPath);
+ return schemaNode;