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.internalizer;
027:
028: import java.io.IOException;
029: import java.net.URI;
030: import java.net.URISyntaxException;
031:
032: import com.sun.istack.internal.SAXParseException2;
033:
034: import org.xml.sax.Attributes;
035: import org.xml.sax.Locator;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.SAXParseException;
038: import org.xml.sax.helpers.XMLFilterImpl;
039:
040: /**
041: * XMLFilter that finds references to other schema files from
042: * SAX events.
043: *
044: * This implementation is a base implementation for typical case
045: * where we just need to look for a particular attribute which
046: * contains an URL to another schema file.
047: *
048: * @author
049: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
050: */
051: public abstract class AbstractReferenceFinderImpl extends XMLFilterImpl {
052: protected final DOMForest parent;
053:
054: protected AbstractReferenceFinderImpl(DOMForest _parent) {
055: this .parent = _parent;
056: }
057:
058: /**
059: * IF the given element contains a reference to an external resource,
060: * return its URL.
061: *
062: * @param nsURI
063: * Namespace URI of the current element
064: * @param localName
065: * Local name of the current element
066: * @return
067: * It's OK to return a relative URL.
068: */
069: protected abstract String findExternalResource(String nsURI,
070: String localName, Attributes atts);
071:
072: public void startElement(String namespaceURI, String localName,
073: String qName, Attributes atts) throws SAXException {
074: super .startElement(namespaceURI, localName, qName, atts);
075:
076: String relativeRef = findExternalResource(namespaceURI,
077: localName, atts);
078: if (relativeRef == null)
079: return; // non found
080:
081: try {
082: // absolutize URL.
083: String ref = new URI(locator.getSystemId()).resolve(
084: new URI(relativeRef)).toString();
085:
086: // then parse this schema as well,
087: // but don't mark this document as a root.
088: parent.parse(ref, false);
089: } catch (URISyntaxException e) {
090: SAXParseException spe = new SAXParseException2(Messages
091: .format(Messages.ERR_UNABLE_TO_PARSE, relativeRef,
092: e.getMessage()), locator, e);
093:
094: fatalError(spe);
095: throw spe;
096: } catch (IOException e) {
097: SAXParseException spe = new SAXParseException2(Messages
098: .format(Messages.ERR_UNABLE_TO_PARSE, relativeRef,
099: e.getMessage()), locator, e);
100:
101: fatalError(spe);
102: throw spe;
103: }
104: }
105:
106: private Locator locator;
107:
108: public void setDocumentLocator(Locator locator) {
109: super.setDocumentLocator(locator);
110: this.locator = locator;
111: }
112: };
|