aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc.reader.xmlschema.parser; aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: aoqi@0: import javax.xml.transform.Source; aoqi@0: import javax.xml.transform.sax.SAXSource; aoqi@0: import javax.xml.validation.SchemaFactory; aoqi@0: aoqi@0: import com.sun.tools.internal.xjc.ConsoleErrorReporter; aoqi@0: import com.sun.tools.internal.xjc.ErrorReceiver; aoqi@0: import com.sun.tools.internal.xjc.util.ErrorReceiverFilter; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.util.XmlFactory; aoqi@0: import org.w3c.dom.ls.LSInput; aoqi@0: import org.w3c.dom.ls.LSResourceResolver; aoqi@0: import org.xml.sax.EntityResolver; aoqi@0: import org.xml.sax.InputSource; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; aoqi@0: aoqi@0: /** aoqi@0: * Checks XML Schema XML representation constraints and aoqi@0: * schema component constraints by using JAXP 1.3 validation framework. aoqi@0: *

aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: * @author Ryan Shoemaker (ryan.shoemaker@sun.com) aoqi@0: */ aoqi@0: public class SchemaConstraintChecker { aoqi@0: aoqi@0: /** aoqi@0: * @param schemas Schema files to be checked. aoqi@0: * @param errorHandler detected errors will be reported to this handler. aoqi@0: * @return true if there was no error, false if there were errors. aoqi@0: */ aoqi@0: public static boolean check(InputSource[] schemas, aoqi@0: ErrorReceiver errorHandler, aoqi@0: final EntityResolver entityResolver, aoqi@0: boolean disableXmlSecurity) { aoqi@0: aoqi@0: ErrorReceiverFilter errorFilter = new ErrorReceiverFilter(errorHandler); aoqi@0: boolean hadErrors = false; aoqi@0: aoqi@0: SchemaFactory sf = XmlFactory.createSchemaFactory(W3C_XML_SCHEMA_NS_URI, disableXmlSecurity); aoqi@0: XmlFactory.allowExternalAccess(sf, "all", disableXmlSecurity); aoqi@0: sf.setErrorHandler(errorFilter); aoqi@0: if( entityResolver != null ) { aoqi@0: sf.setResourceResolver(new LSResourceResolver() { aoqi@0: public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { aoqi@0: try { aoqi@0: // XSOM passes the namespace URI to the publicID parameter. aoqi@0: // we do the same here . aoqi@0: InputSource is = entityResolver.resolveEntity(namespaceURI, systemId); aoqi@0: if(is==null) return null; aoqi@0: return new LSInputSAXWrapper(is); aoqi@0: } catch (SAXException e) { aoqi@0: // TODO: is this sufficient? aoqi@0: return null; aoqi@0: } catch (IOException e) { aoqi@0: // TODO: is this sufficient? aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: }); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: XmlFactory.allowExternalDTDAccess(sf, "all", disableXmlSecurity); aoqi@0: sf.newSchema(getSchemaSource(schemas, entityResolver)); aoqi@0: } catch (SAXException e) { aoqi@0: // TODO: we haven't thrown exceptions from here before. should we just trap them and return false? aoqi@0: hadErrors = true; aoqi@0: } catch( OutOfMemoryError e) { aoqi@0: errorHandler.warning(null,Messages.format(Messages.WARN_UNABLE_TO_CHECK_CORRECTNESS)); aoqi@0: } aoqi@0: aoqi@0: return !(hadErrors || errorFilter.hadError()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * convert an array of {@link InputSource InputSource} into an aoqi@0: * array of {@link Source Source} aoqi@0: * aoqi@0: * @param schemas array of {@link InputSource InputSource} aoqi@0: * @return array of {@link Source Source} aoqi@0: */ aoqi@0: private static Source[] getSchemaSource(InputSource[] schemas, EntityResolver entityResolver) throws SAXException { aoqi@0: SAXSource[] sources = new SAXSource[schemas.length]; aoqi@0: for (int i = 0; i < schemas.length; i++) { aoqi@0: sources[i] = new SAXSource(schemas[i]); aoqi@0: // sources[i].getXMLReader().setEntityResolver(entityResolver); aoqi@0: } aoqi@0: return sources; aoqi@0: } aoqi@0: aoqi@0: // quick test aoqi@0: public static void main(String[] args) throws IOException { aoqi@0: InputSource[] sources = new InputSource[args.length]; aoqi@0: for (int i = 0; i < args.length; i++) aoqi@0: sources[i] = new InputSource(new File(args[i]).toURL().toExternalForm()); aoqi@0: aoqi@0: check(sources, new ConsoleErrorReporter(), null, true); aoqi@0: } aoqi@0: }