src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/parser/SchemaConstraintChecker.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.xjc.reader.xmlschema.parser;
aoqi@0 27
aoqi@0 28 import java.io.File;
aoqi@0 29 import java.io.IOException;
aoqi@0 30
aoqi@0 31 import javax.xml.transform.Source;
aoqi@0 32 import javax.xml.transform.sax.SAXSource;
aoqi@0 33 import javax.xml.validation.SchemaFactory;
aoqi@0 34
aoqi@0 35 import com.sun.tools.internal.xjc.ConsoleErrorReporter;
aoqi@0 36 import com.sun.tools.internal.xjc.ErrorReceiver;
aoqi@0 37 import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
aoqi@0 38
aoqi@0 39 import com.sun.xml.internal.bind.v2.util.XmlFactory;
aoqi@0 40 import org.w3c.dom.ls.LSInput;
aoqi@0 41 import org.w3c.dom.ls.LSResourceResolver;
aoqi@0 42 import org.xml.sax.EntityResolver;
aoqi@0 43 import org.xml.sax.InputSource;
aoqi@0 44 import org.xml.sax.SAXException;
aoqi@0 45
aoqi@0 46 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
aoqi@0 47
aoqi@0 48 /**
aoqi@0 49 * Checks XML Schema XML representation constraints and
aoqi@0 50 * schema component constraints by using JAXP 1.3 validation framework.
aoqi@0 51 * <p/>
aoqi@0 52 *
aoqi@0 53 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
aoqi@0 54 * @author Ryan Shoemaker (ryan.shoemaker@sun.com)
aoqi@0 55 */
aoqi@0 56 public class SchemaConstraintChecker {
aoqi@0 57
aoqi@0 58 /**
aoqi@0 59 * @param schemas Schema files to be checked.
aoqi@0 60 * @param errorHandler detected errors will be reported to this handler.
aoqi@0 61 * @return true if there was no error, false if there were errors.
aoqi@0 62 */
aoqi@0 63 public static boolean check(InputSource[] schemas,
aoqi@0 64 ErrorReceiver errorHandler,
aoqi@0 65 final EntityResolver entityResolver,
aoqi@0 66 boolean disableXmlSecurity) {
aoqi@0 67
aoqi@0 68 ErrorReceiverFilter errorFilter = new ErrorReceiverFilter(errorHandler);
aoqi@0 69 boolean hadErrors = false;
aoqi@0 70
aoqi@0 71 SchemaFactory sf = XmlFactory.createSchemaFactory(W3C_XML_SCHEMA_NS_URI, disableXmlSecurity);
aoqi@0 72 XmlFactory.allowExternalAccess(sf, "all", disableXmlSecurity);
aoqi@0 73 sf.setErrorHandler(errorFilter);
aoqi@0 74 if( entityResolver != null ) {
aoqi@0 75 sf.setResourceResolver(new LSResourceResolver() {
aoqi@0 76 public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
aoqi@0 77 try {
aoqi@0 78 // XSOM passes the namespace URI to the publicID parameter.
aoqi@0 79 // we do the same here .
aoqi@0 80 InputSource is = entityResolver.resolveEntity(namespaceURI, systemId);
aoqi@0 81 if(is==null) return null;
aoqi@0 82 return new LSInputSAXWrapper(is);
aoqi@0 83 } catch (SAXException e) {
aoqi@0 84 // TODO: is this sufficient?
aoqi@0 85 return null;
aoqi@0 86 } catch (IOException e) {
aoqi@0 87 // TODO: is this sufficient?
aoqi@0 88 return null;
aoqi@0 89 }
aoqi@0 90 }
aoqi@0 91 });
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 try {
aoqi@0 95 XmlFactory.allowExternalDTDAccess(sf, "all", disableXmlSecurity);
aoqi@0 96 sf.newSchema(getSchemaSource(schemas, entityResolver));
aoqi@0 97 } catch (SAXException e) {
aoqi@0 98 // TODO: we haven't thrown exceptions from here before. should we just trap them and return false?
aoqi@0 99 hadErrors = true;
aoqi@0 100 } catch( OutOfMemoryError e) {
aoqi@0 101 errorHandler.warning(null,Messages.format(Messages.WARN_UNABLE_TO_CHECK_CORRECTNESS));
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 return !(hadErrors || errorFilter.hadError());
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 /**
aoqi@0 108 * convert an array of {@link InputSource InputSource} into an
aoqi@0 109 * array of {@link Source Source}
aoqi@0 110 *
aoqi@0 111 * @param schemas array of {@link InputSource InputSource}
aoqi@0 112 * @return array of {@link Source Source}
aoqi@0 113 */
aoqi@0 114 private static Source[] getSchemaSource(InputSource[] schemas, EntityResolver entityResolver) throws SAXException {
aoqi@0 115 SAXSource[] sources = new SAXSource[schemas.length];
aoqi@0 116 for (int i = 0; i < schemas.length; i++) {
aoqi@0 117 sources[i] = new SAXSource(schemas[i]);
aoqi@0 118 // sources[i].getXMLReader().setEntityResolver(entityResolver);
aoqi@0 119 }
aoqi@0 120 return sources;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 // quick test
aoqi@0 124 public static void main(String[] args) throws IOException {
aoqi@0 125 InputSource[] sources = new InputSource[args.length];
aoqi@0 126 for (int i = 0; i < args.length; i++)
aoqi@0 127 sources[i] = new InputSource(new File(args[i]).toURL().toExternalForm());
aoqi@0 128
aoqi@0 129 check(sources, new ConsoleErrorReporter(), null, true);
aoqi@0 130 }
aoqi@0 131 }

mercurial