- if (namespace == null) {
- Set<URI> potentialUris = Collections.emptySet();
- potentialUris = resolveAllPotentialNamespaces(nodeNamePart, dataSchemaNode);
- if (potentialUris.contains(getCurrentNamespace())) {
- namespace = getCurrentNamespace();
- } else if (potentialUris.size() == 1) {
- namespace = potentialUris.iterator().next();
- } else if (potentialUris.size() > 1) {
- throw new IllegalStateException("Choose suitable module name for element "+nodeNamePart+":"+toModuleNames(potentialUris));
- } else if (potentialUris.isEmpty()) {
- throw new IllegalStateException("Schema node with name "+nodeNamePart+" wasn't found.");
- }
- }
-
- return new NamespaceAndName(nodeNamePart, namespace);
- }
-
- private String toModuleNames(Set<URI> potentialUris) {
- final StringBuilder builder = new StringBuilder();
- for (URI potentialUri : potentialUris) {
- builder.append("\n");
- //FIXME how to get information about revision from JSON input? currently first available is used.
- builder.append(schema.findModuleByNamespace(potentialUri).iterator().next().getName());
- }
- return builder.toString();
- }
-
- private Set<URI> resolveAllPotentialNamespaces(final String elementName, final DataSchemaNode dataSchemaNode) {
- final Set<URI> potentialUris = new HashSet<>();
- final Set<ChoiceNode> choices = new HashSet<>();
- if (dataSchemaNode instanceof DataNodeContainer) {
- for (DataSchemaNode childSchemaNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
- if (childSchemaNode instanceof ChoiceNode) {
- choices.add((ChoiceNode)childSchemaNode);
- } else if (childSchemaNode.getQName().getLocalName().equals(elementName)) {
- potentialUris.add(childSchemaNode.getQName().getNamespace());
- }
- }
-
- for (ChoiceNode choiceNode : choices) {
- for (ChoiceCaseNode concreteCase : choiceNode.getCases()) {
- potentialUris.addAll(resolveAllPotentialNamespaces(elementName, concreteCase));
- }
- }
- }
- return potentialUris;