org.milyn.xml.XsdDOMValidator Maven / Gradle / Ivy
The newest version!
/*
Milyn - Copyright (C) 2006 - 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (version 2.1) as published by the Free Software
Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details:
http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.xml;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import org.milyn.util.ClassUtil;
import org.milyn.assertion.AssertArgument;
import javax.xml.XMLConstants;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.Validator;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.dom.DOMSource;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
/**
* XSD DOM Validator.
*
* Iterates through the document (DOM) gathering the namespaces. It validates
* based on the convention that the gathered namespace XSDs are provided on the
* classpath. It uses the namespace path (URI.getPath()), prepending it with "/META-INF"
* to perform a classpath resource lookup for the XSD i.e. the XSDs must be provided on
* the classpath below the "META-INF" package.
*
* @author [email protected]
*/
public class XsdDOMValidator extends XsdValidator {
private Document document;
private URI defaultNamespace;
private List namespaces = new ArrayList();
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);
// Using the namespace URI list, create the XSD Source array used to
// create the merged Schema instance...
List