private void gatherNamespaces(Element element, List<URI> namespaceSources) throws SAXException { NamedNodeMap attributes = element.getAttributes(); int attributeCount = attributes.getLength(); for (int i = 0; i < attributeCount; i++) { Attr attribute = (Attr) attributes.item(i); String namespace = attribute.getNamespaceURI(); if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespace)) { try { namespaceSources.add(new URI(attribute.getValue())); } catch (URISyntaxException e) { throw new SAXException( "Cannot validate this document with this class. Namespaces must be valid URIs. Found Namespace: '" + attribute.getValue() + "'.", e); } } } NodeList childNodes = element.getChildNodes(); int childCount = childNodes.getLength(); for (int i = 0; i < childCount; i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { gatherNamespaces((Element) child, namespaceSources); } } }
public XsdDOMValidator(Document document) throws SAXException { AssertArgument.isNotNull(document, "document"); this.document = document; // Get the default namespace... String defaultNamespaceString = getDefaultNamespace(document.getDocumentElement()); if (defaultNamespaceString != null) { try { defaultNamespace = new URI(defaultNamespaceString); } catch (URISyntaxException e) { throw new SAXException( "Cannot validate this document with this class. Namespaces must be valid URIs. Default Namespace: '" + defaultNamespaceString + "'.", e); } } // Get the full namespace list... gatherNamespaces(document.getDocumentElement(), namespaces); }