aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc.reader.internalizer; aoqi@0: aoqi@0: import com.sun.istack.internal.SAXParseException2; aoqi@0: import org.xml.sax.Attributes; aoqi@0: import org.xml.sax.Locator; aoqi@0: import org.xml.sax.SAXException; aoqi@0: import org.xml.sax.SAXParseException; aoqi@0: import org.xml.sax.helpers.XMLFilterImpl; aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.net.URI; aoqi@0: import java.net.URISyntaxException; aoqi@0: aoqi@0: /** aoqi@0: * XMLFilter that finds references to other schema files from aoqi@0: * SAX events. aoqi@0: *

aoqi@0: * This implementation is a base implementation for typical case aoqi@0: * where we just need to look for a particular attribute which aoqi@0: * contains an URL to another schema file. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: */ aoqi@0: public abstract class AbstractReferenceFinderImpl extends XMLFilterImpl { aoqi@0: aoqi@0: protected final DOMForest parent; aoqi@0: aoqi@0: protected AbstractReferenceFinderImpl(DOMForest _parent) { aoqi@0: this.parent = _parent; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * IF the given element contains a reference to an external resource, aoqi@0: * return its URL. aoqi@0: * aoqi@0: * @param nsURI Namespace URI of the current element aoqi@0: * @param localName Local name of the current element aoqi@0: * @return It's OK to return a relative URL. aoqi@0: */ aoqi@0: protected abstract String findExternalResource(String nsURI, String localName, Attributes atts); aoqi@0: aoqi@0: @Override aoqi@0: public void startElement(String namespaceURI, String localName, String qName, Attributes atts) aoqi@0: throws SAXException { aoqi@0: super.startElement(namespaceURI, localName, qName, atts); aoqi@0: aoqi@0: String relativeRef = findExternalResource(namespaceURI, localName, atts); aoqi@0: if (relativeRef == null) { aoqi@0: return; // not found aoqi@0: } aoqi@0: try { aoqi@0: // absolutize URL. aoqi@0: String lsi = locator.getSystemId(); aoqi@0: String ref; aoqi@0: URI relRefURI = new URI(relativeRef); aoqi@0: if (relRefURI.isAbsolute()) aoqi@0: ref = relativeRef; aoqi@0: else { aoqi@0: if (lsi.startsWith("jar:")) { aoqi@0: int bangIdx = lsi.indexOf('!'); aoqi@0: if (bangIdx > 0) { aoqi@0: ref = lsi.substring(0, bangIdx + 1) aoqi@0: + new URI(lsi.substring(bangIdx + 1)).resolve(new URI(relativeRef)).toString(); aoqi@0: } else { aoqi@0: ref = relativeRef; aoqi@0: } aoqi@0: } else { aoqi@0: ref = new URI(lsi).resolve(new URI(relativeRef)).toString(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // then parse this schema as well, aoqi@0: // but don't mark this document as a root. aoqi@0: if (parent != null) { // this is there to allow easier testing aoqi@0: parent.parse(ref, false); aoqi@0: } aoqi@0: } catch (URISyntaxException e) { aoqi@0: String msg = e.getMessage(); aoqi@0: if (new File(relativeRef).exists()) { aoqi@0: msg = Messages.format(Messages.ERR_FILENAME_IS_NOT_URI) + ' ' + msg; aoqi@0: } aoqi@0: aoqi@0: SAXParseException spe = new SAXParseException2( aoqi@0: Messages.format(Messages.ERR_UNABLE_TO_PARSE, relativeRef, msg), aoqi@0: locator, e); aoqi@0: aoqi@0: fatalError(spe); aoqi@0: throw spe; aoqi@0: } catch (IOException e) { aoqi@0: SAXParseException spe = new SAXParseException2( aoqi@0: Messages.format(Messages.ERR_UNABLE_TO_PARSE, relativeRef, e.getMessage()), aoqi@0: locator, e); aoqi@0: aoqi@0: fatalError(spe); aoqi@0: throw spe; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private Locator locator; aoqi@0: aoqi@0: @Override aoqi@0: public void setDocumentLocator(Locator locator) { aoqi@0: super.setDocumentLocator(locator); aoqi@0: this.locator = locator; aoqi@0: } aoqi@0: }