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:
037: package com.sun.tools.xjc;
038:
039: import java.lang.reflect.Field;
040: import java.lang.reflect.Method;
041: import java.net.URL;
042:
043: import javax.xml.validation.Schema;
044: import javax.xml.validation.SchemaFactory;
045: import javax.xml.validation.ValidatorHandler;
046:
047: import com.sun.xml.bind.v2.WellKnownNamespace;
048:
049: import org.xml.sax.SAXException;
050:
051: /**
052: * Wraps a JAXP {@link Schema} object and lazily instanciate it.
053: *
054: * Also fix bug 6246922.
055: *
056: * This object is thread-safe. There should be only one instance of
057: * this for the whole VM.
058: *
059: * @author Kohsuke Kawaguchi
060: */
061: public final class SchemaCache {
062:
063: private Schema schema;
064:
065: private final URL source;
066:
067: public SchemaCache(URL source) {
068: this .source = source;
069: }
070:
071: public ValidatorHandler newValidator() {
072: synchronized (this ) {
073: if (schema == null) {
074: try {
075: schema = SchemaFactory.newInstance(
076: WellKnownNamespace.XML_SCHEMA).newSchema(
077: source);
078: } catch (SAXException e) {
079: // we make sure that the schema is correct before we ship.
080: throw new AssertionError(e);
081: }
082: }
083: }
084:
085: ValidatorHandler handler = schema.newValidatorHandler();
086: fixValidatorBug6246922(handler);
087:
088: return handler;
089: }
090:
091: /**
092: * Fix the bug 6246922 if we are running inside Tiger.
093: */
094: private void fixValidatorBug6246922(ValidatorHandler handler) {
095: try {
096: Field f = handler.getClass().getDeclaredField(
097: "errorReporter");
098: f.setAccessible(true);
099: Object errorReporter = f.get(handler);
100:
101: Method get = errorReporter.getClass().getDeclaredMethod(
102: "getMessageFormatter", String.class);
103: Object currentFormatter = get.invoke(errorReporter,
104: "http://www.w3.org/TR/xml-schema-1");
105: if (currentFormatter != null)
106: return;
107:
108: // otherwise attempt to set
109: Method put = null;
110: for (Method m : errorReporter.getClass()
111: .getDeclaredMethods()) {
112: if (m.getName().equals("putMessageFormatter")) {
113: put = m;
114: break;
115: }
116: }
117: if (put == null)
118: return; // unable to find the putMessageFormatter
119:
120: ClassLoader cl = errorReporter.getClass().getClassLoader();
121: String className = "com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter";
122: Class xsformatter;
123: if (cl == null) {
124: xsformatter = Class.forName(className);
125: } else {
126: xsformatter = cl.loadClass(className);
127: }
128:
129: put.invoke(errorReporter,
130: "http://www.w3.org/TR/xml-schema-1", xsformatter
131: .newInstance());
132: } catch (Throwable t) {
133: // this code is heavily relying on an implementation detail of JAXP RI,
134: // so any error is likely because of the incompatible change in it.
135: // don't die if that happens. Just continue. The worst case is a illegible
136: // error messages, which are much better than not compiling schemas at all.
137: }
138: }
139: }
|