ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, 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.ws.wsdl.parser; ohair@286: ohair@286: import com.sun.istack.internal.SAXParseException2; ohair@286: import com.sun.tools.internal.ws.resources.WsdlMessages; 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: ohair@286: import java.io.IOException; ohair@286: import java.net.URI; ohair@286: import java.net.URISyntaxException; ohair@286: import java.net.URL; ohair@286: ohair@286: /** ohair@286: * XMLFilter that finds references to other schema files from ohair@286: * SAX events. ohair@286: * 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: * ohair@286: * @author ohair@286: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) ohair@286: * Vivek Pandey ohair@286: */ ohair@286: public abstract class AbstractReferenceFinderImpl extends XMLFilterImpl { 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: * ohair@286: * @param nsURI ohair@286: * Namespace URI of the current element ohair@286: * @param localName ohair@286: * Local name of the current element ohair@286: * @return ohair@286: * It's OK to return a relative URL. ohair@286: */ ohair@286: protected abstract String findExternalResource( String nsURI, String localName, Attributes atts); ohair@286: alanb@368: @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) return; // non found ohair@286: ohair@286: try { ohair@286: // absolutize URL. alanb@368: assert locator != null; alanb@368: String lsi = locator.getSystemId(); alanb@368: String ref; alanb@368: if (lsi.startsWith("jar:")) { alanb@368: int bangIdx = lsi.indexOf('!'); alanb@368: if (bangIdx > 0) { alanb@368: ref = new URL(new URL(lsi), relativeRef).toString(); alanb@368: } else alanb@368: ref = relativeRef; alanb@368: } else alanb@368: ref = new URI(lsi).resolve(new URI(relativeRef)).toString(); ohair@286: ohair@286: // then parse this schema as well, ohair@286: // but don't mark this document as a root. ohair@286: parent.parse(ref,false); ohair@286: } catch( URISyntaxException e ) { ohair@286: SAXParseException spe = new SAXParseException2( ohair@286: WsdlMessages.ABSTRACT_REFERENCE_FINDER_IMPL_UNABLE_TO_PARSE(relativeRef,e.getMessage()), 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: WsdlMessages.ABSTRACT_REFERENCE_FINDER_IMPL_UNABLE_TO_PARSE(relativeRef,e.getMessage()), ohair@286: locator, e ); ohair@286: ohair@286: fatalError(spe); ohair@286: throw spe; ohair@286: } ohair@286: } ohair@286: ohair@286: private Locator locator; ohair@286: alanb@368: @Override ohair@286: public void setDocumentLocator(Locator locator) { ohair@286: super.setDocumentLocator(locator); ohair@286: this.locator = locator; ohair@286: } ohair@286: }