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.bindinfo;
037:
038: import javax.xml.bind.JAXBException;
039: import javax.xml.bind.Unmarshaller;
040: import javax.xml.bind.UnmarshallerHandler;
041: import javax.xml.bind.helpers.DefaultValidationEventHandler;
042: import javax.xml.validation.ValidatorHandler;
043:
044: import com.sun.tools.xjc.Options;
045: import com.sun.tools.xjc.SchemaCache;
046: import com.sun.tools.xjc.reader.Const;
047: import com.sun.xml.xsom.parser.AnnotationContext;
048: import com.sun.xml.xsom.parser.AnnotationParser;
049: import com.sun.xml.xsom.parser.AnnotationParserFactory;
050:
051: import org.xml.sax.Attributes;
052: import org.xml.sax.ContentHandler;
053: import org.xml.sax.EntityResolver;
054: import org.xml.sax.ErrorHandler;
055: import org.xml.sax.SAXException;
056: import org.xml.sax.helpers.XMLFilterImpl;
057:
058: /**
059: * Implementation of XSOM {@link AnnotationParserFactory} that
060: * parses JAXB customization declarations.
061: *
062: * @author
063: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
064: */
065: public class AnnotationParserFactoryImpl implements
066: AnnotationParserFactory {
067: public AnnotationParserFactoryImpl(Options opts) {
068: this .options = opts;
069: }
070:
071: private final Options options;
072: /**
073: * Lazily created validator, so that the schema for binding won't be
074: * prepared unless absolutely necessary.
075: */
076: private ValidatorHandler validator;
077:
078: public AnnotationParser create() {
079: return new AnnotationParser() {
080: private Unmarshaller u = BindInfo.getJAXBContext()
081: .createUnmarshaller();
082:
083: private UnmarshallerHandler handler;
084:
085: public ContentHandler getContentHandler(
086: AnnotationContext context,
087: String parentElementName,
088: final ErrorHandler errorHandler,
089: EntityResolver entityResolver) {
090:
091: // return a ContentHandler that validates the customization and also
092: // parses them into the internal structure.
093: if (handler != null)
094: // interface contract violation.
095: // this method will be called only once.
096: throw new AssertionError();
097:
098: if (options.debugMode)
099: try {
100: u
101: .setEventHandler(new DefaultValidationEventHandler());
102: } catch (JAXBException e) {
103: throw new AssertionError(e); // ridiculous!
104: }
105:
106: handler = u.getUnmarshallerHandler();
107:
108: // configure so that the validator will receive events for JAXB islands
109: return new ForkingFilter(handler) {
110: @Override
111: public void startElement(String uri,
112: String localName, String qName,
113: Attributes atts) throws SAXException {
114: super .startElement(uri, localName, qName, atts);
115: if ((uri.equals(Const.JAXB_NSURI) || uri
116: .equals(Const.XJC_EXTENSION_URI))
117: && getSideHandler() == null) {
118: // set up validator
119: if (validator == null)
120: validator = BindInfo.bindingFileSchema
121: .newValidator();
122: validator.setErrorHandler(errorHandler);
123: startForking(uri, localName, qName, atts,
124: new ValidatorProtecter(validator));
125: }
126: }
127: };
128: }
129:
130: public Object getResult(Object existing) {
131: if (handler == null)
132: // interface contract violation.
133: // the getContentHandler method must have been called.
134: throw new AssertionError();
135:
136: try {
137: BindInfo result = (BindInfo) handler.getResult();
138:
139: if (existing != null) {
140: BindInfo bie = (BindInfo) existing;
141: bie.absorb(result);
142: return bie;
143: } else {
144: if (!result.isPointless())
145: return result; // just annotation. no meaningful customization
146: else
147: return null;
148: }
149: } catch (JAXBException e) {
150: throw new AssertionError(e);
151: }
152: }
153: };
154: }
155:
156: private static final class ValidatorProtecter extends XMLFilterImpl {
157: public ValidatorProtecter(ContentHandler h) {
158: setContentHandler(h);
159: }
160:
161: public void startPrefixMapping(String prefix, String uri)
162: throws SAXException {
163: // work around a bug in the validator implementation in Tiger
164: super.startPrefixMapping(prefix.intern(), uri);
165: }
166: }
167: }
|