001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.reader.xmlschema.parser;
027:
028: import java.io.File;
029: import java.io.IOException;
030:
031: import javax.xml.transform.Source;
032: import javax.xml.transform.sax.SAXSource;
033: import javax.xml.validation.SchemaFactory;
034:
035: import com.sun.tools.internal.xjc.ConsoleErrorReporter;
036: import com.sun.tools.internal.xjc.ErrorReceiver;
037: import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
038:
039: import org.w3c.dom.ls.LSInput;
040: import org.w3c.dom.ls.LSResourceResolver;
041: import org.xml.sax.EntityResolver;
042: import org.xml.sax.InputSource;
043: import org.xml.sax.SAXException;
044:
045: import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
046:
047: /**
048: * Checks XML Schema XML representation constraints and
049: * schema component constraints by using JAXP 1.3 validation framework.
050: * <p/>
051: *
052: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
053: * @author Ryan Shoemaker (ryan.shoemaker@sun.com)
054: */
055: public class SchemaConstraintChecker {
056:
057: /**
058: * @param schemas Schema files to be checked.
059: * @param errorHandler detected errors will be reported to this handler.
060: * @return true if there was no error, false if there were errors.
061: */
062: public static boolean check(InputSource[] schemas,
063: ErrorReceiver errorHandler,
064: final EntityResolver entityResolver) {
065:
066: ErrorReceiverFilter errorFilter = new ErrorReceiverFilter(
067: errorHandler);
068: boolean hadErrors = false;
069:
070: SchemaFactory sf = SchemaFactory
071: .newInstance(W3C_XML_SCHEMA_NS_URI);
072: sf.setErrorHandler(errorFilter);
073: if (entityResolver != null) {
074: sf.setResourceResolver(new LSResourceResolver() {
075: public LSInput resolveResource(String type,
076: String namespaceURI, String publicId,
077: String systemId, String baseURI) {
078: try {
079: // XSOM passes the namespace URI to the publicID parameter.
080: // we do the same here .
081: InputSource is = entityResolver.resolveEntity(
082: namespaceURI, systemId);
083: if (is == null)
084: return null;
085: return new LSInputSAXWrapper(is);
086: } catch (SAXException e) {
087: // TODO: is this sufficient?
088: return null;
089: } catch (IOException e) {
090: // TODO: is this sufficient?
091: return null;
092: }
093: }
094: });
095: }
096:
097: try {
098: sf.newSchema(getSchemaSource(schemas));
099: } catch (SAXException e) {
100: // TODO: we haven't thrown exceptions from here before. should we just trap them and return false?
101: hadErrors = true;
102: } catch (OutOfMemoryError e) {
103: errorHandler.warning(null, Messages
104: .format(Messages.WARN_UNABLE_TO_CHECK_CORRECTNESS));
105: }
106:
107: return !(hadErrors || errorFilter.hadError());
108: }
109:
110: /**
111: * convert an array of {@link InputSource InputSource} into an
112: * array of {@link Source Source}
113: *
114: * @param schemas array of {@link InputSource InputSource}
115: * @return array of {@link Source Source}
116: */
117: private static Source[] getSchemaSource(InputSource[] schemas) {
118: SAXSource[] sources = new SAXSource[schemas.length];
119: for (int i = 0; i < schemas.length; i++)
120: sources[i] = new SAXSource(schemas[i]);
121: return sources;
122: }
123:
124: // quick test
125: public static void main(String[] args) throws IOException {
126: InputSource[] sources = new InputSource[args.length];
127: for (int i = 0; i < args.length; i++)
128: sources[i] = new InputSource(new File(args[i]).toURL()
129: .toExternalForm());
130:
131: check(sources, new ConsoleErrorReporter(), null);
132: }
133: }
|