001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.xjc.reader.xmlschema.parser;
037:
038: import java.io.File;
039: import java.io.IOException;
040:
041: import javax.xml.transform.Source;
042: import javax.xml.transform.sax.SAXSource;
043: import javax.xml.validation.SchemaFactory;
044:
045: import com.sun.tools.xjc.ConsoleErrorReporter;
046: import com.sun.tools.xjc.ErrorReceiver;
047: import com.sun.tools.xjc.util.ErrorReceiverFilter;
048:
049: import org.w3c.dom.ls.LSInput;
050: import org.w3c.dom.ls.LSResourceResolver;
051: import org.xml.sax.EntityResolver;
052: import org.xml.sax.InputSource;
053: import org.xml.sax.SAXException;
054:
055: import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
056:
057: /**
058: * Checks XML Schema XML representation constraints and
059: * schema component constraints by using JAXP 1.3 validation framework.
060: * <p/>
061: *
062: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
063: * @author Ryan Shoemaker (ryan.shoemaker@sun.com)
064: */
065: public class SchemaConstraintChecker {
066:
067: /**
068: * @param schemas Schema files to be checked.
069: * @param errorHandler detected errors will be reported to this handler.
070: * @return true if there was no error, false if there were errors.
071: */
072: public static boolean check(InputSource[] schemas,
073: ErrorReceiver errorHandler,
074: final EntityResolver entityResolver) {
075:
076: ErrorReceiverFilter errorFilter = new ErrorReceiverFilter(
077: errorHandler);
078: boolean hadErrors = false;
079:
080: SchemaFactory sf = SchemaFactory
081: .newInstance(W3C_XML_SCHEMA_NS_URI);
082: sf.setErrorHandler(errorFilter);
083: if (entityResolver != null) {
084: sf.setResourceResolver(new LSResourceResolver() {
085: public LSInput resolveResource(String type,
086: String namespaceURI, String publicId,
087: String systemId, String baseURI) {
088: try {
089: // XSOM passes the namespace URI to the publicID parameter.
090: // we do the same here .
091: InputSource is = entityResolver.resolveEntity(
092: namespaceURI, systemId);
093: if (is == null)
094: return null;
095: return new LSInputSAXWrapper(is);
096: } catch (SAXException e) {
097: // TODO: is this sufficient?
098: return null;
099: } catch (IOException e) {
100: // TODO: is this sufficient?
101: return null;
102: }
103: }
104: });
105: }
106:
107: try {
108: sf.newSchema(getSchemaSource(schemas));
109: } catch (SAXException e) {
110: // TODO: we haven't thrown exceptions from here before. should we just trap them and return false?
111: hadErrors = true;
112: } catch (OutOfMemoryError e) {
113: errorHandler.warning(null, Messages
114: .format(Messages.WARN_UNABLE_TO_CHECK_CORRECTNESS));
115: }
116:
117: return !(hadErrors || errorFilter.hadError());
118: }
119:
120: /**
121: * convert an array of {@link InputSource InputSource} into an
122: * array of {@link Source Source}
123: *
124: * @param schemas array of {@link InputSource InputSource}
125: * @return array of {@link Source Source}
126: */
127: private static Source[] getSchemaSource(InputSource[] schemas) {
128: SAXSource[] sources = new SAXSource[schemas.length];
129: for (int i = 0; i < schemas.length; i++)
130: sources[i] = new SAXSource(schemas[i]);
131: return sources;
132: }
133:
134: // quick test
135: public static void main(String[] args) throws IOException {
136: InputSource[] sources = new InputSource[args.length];
137: for (int i = 0; i < args.length; i++)
138: sources[i] = new InputSource(new File(args[i]).toURL()
139: .toExternalForm());
140:
141: check(sources, new ConsoleErrorReporter(), null);
142: }
143: }
|