1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/parser/SchemaConstraintChecker.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.xjc.reader.xmlschema.parser; 1.30 + 1.31 +import java.io.File; 1.32 +import java.io.IOException; 1.33 + 1.34 +import javax.xml.transform.Source; 1.35 +import javax.xml.transform.sax.SAXSource; 1.36 +import javax.xml.validation.SchemaFactory; 1.37 + 1.38 +import com.sun.tools.internal.xjc.ConsoleErrorReporter; 1.39 +import com.sun.tools.internal.xjc.ErrorReceiver; 1.40 +import com.sun.tools.internal.xjc.util.ErrorReceiverFilter; 1.41 + 1.42 +import org.w3c.dom.ls.LSInput; 1.43 +import org.w3c.dom.ls.LSResourceResolver; 1.44 +import org.xml.sax.EntityResolver; 1.45 +import org.xml.sax.InputSource; 1.46 +import org.xml.sax.SAXException; 1.47 + 1.48 +import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 1.49 + 1.50 +/** 1.51 + * Checks XML Schema XML representation constraints and 1.52 + * schema component constraints by using JAXP 1.3 validation framework. 1.53 + * <p/> 1.54 + * 1.55 + * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 1.56 + * @author Ryan Shoemaker (ryan.shoemaker@sun.com) 1.57 + */ 1.58 +public class SchemaConstraintChecker { 1.59 + 1.60 + /** 1.61 + * @param schemas Schema files to be checked. 1.62 + * @param errorHandler detected errors will be reported to this handler. 1.63 + * @return true if there was no error, false if there were errors. 1.64 + */ 1.65 + public static boolean check(InputSource[] schemas, 1.66 + ErrorReceiver errorHandler, final EntityResolver entityResolver) { 1.67 + 1.68 + ErrorReceiverFilter errorFilter = new ErrorReceiverFilter(errorHandler); 1.69 + boolean hadErrors = false; 1.70 + 1.71 + SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); 1.72 + sf.setErrorHandler(errorFilter); 1.73 + if( entityResolver != null ) { 1.74 + sf.setResourceResolver(new LSResourceResolver() { 1.75 + public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { 1.76 + try { 1.77 + // XSOM passes the namespace URI to the publicID parameter. 1.78 + // we do the same here . 1.79 + InputSource is = entityResolver.resolveEntity(namespaceURI, systemId); 1.80 + if(is==null) return null; 1.81 + return new LSInputSAXWrapper(is); 1.82 + } catch (SAXException e) { 1.83 + // TODO: is this sufficient? 1.84 + return null; 1.85 + } catch (IOException e) { 1.86 + // TODO: is this sufficient? 1.87 + return null; 1.88 + } 1.89 + } 1.90 + }); 1.91 + } 1.92 + 1.93 + try { 1.94 + sf.newSchema(getSchemaSource(schemas, entityResolver)); 1.95 + } catch (SAXException e) { 1.96 + // TODO: we haven't thrown exceptions from here before. should we just trap them and return false? 1.97 + hadErrors = true; 1.98 + } catch( OutOfMemoryError e) { 1.99 + errorHandler.warning(null,Messages.format(Messages.WARN_UNABLE_TO_CHECK_CORRECTNESS)); 1.100 + } 1.101 + 1.102 + return !(hadErrors || errorFilter.hadError()); 1.103 + } 1.104 + 1.105 + /** 1.106 + * convert an array of {@link InputSource InputSource} into an 1.107 + * array of {@link Source Source} 1.108 + * 1.109 + * @param schemas array of {@link InputSource InputSource} 1.110 + * @return array of {@link Source Source} 1.111 + */ 1.112 + private static Source[] getSchemaSource(InputSource[] schemas, EntityResolver entityResolver) throws SAXException { 1.113 + SAXSource[] sources = new SAXSource[schemas.length]; 1.114 + for (int i = 0; i < schemas.length; i++) { 1.115 + sources[i] = new SAXSource(schemas[i]); 1.116 +// sources[i].getXMLReader().setEntityResolver(entityResolver); 1.117 + } 1.118 + return sources; 1.119 + } 1.120 + 1.121 + // quick test 1.122 + public static void main(String[] args) throws IOException { 1.123 + InputSource[] sources = new InputSource[args.length]; 1.124 + for (int i = 0; i < args.length; i++) 1.125 + sources[i] = new InputSource(new File(args[i]).toURL().toExternalForm()); 1.126 + 1.127 + check(sources, new ConsoleErrorReporter(), null); 1.128 + } 1.129 +}