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

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