1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/AbstractReferenceFinderImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,129 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.xjc.reader.internalizer; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.io.File; 1.33 +import java.net.URI; 1.34 +import java.net.URISyntaxException; 1.35 + 1.36 +import com.sun.istack.internal.SAXParseException2; 1.37 + 1.38 +import org.xml.sax.Attributes; 1.39 +import org.xml.sax.Locator; 1.40 +import org.xml.sax.SAXException; 1.41 +import org.xml.sax.SAXParseException; 1.42 +import org.xml.sax.helpers.XMLFilterImpl; 1.43 + 1.44 +/** 1.45 + * XMLFilter that finds references to other schema files from 1.46 + * SAX events. 1.47 + * 1.48 + * This implementation is a base implementation for typical case 1.49 + * where we just need to look for a particular attribute which 1.50 + * contains an URL to another schema file. 1.51 + * 1.52 + * @author 1.53 + * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 1.54 + */ 1.55 +public abstract class AbstractReferenceFinderImpl extends XMLFilterImpl { 1.56 + 1.57 + protected final DOMForest parent; 1.58 + 1.59 + protected AbstractReferenceFinderImpl(DOMForest _parent) { 1.60 + this.parent = _parent; 1.61 + } 1.62 + 1.63 + /** 1.64 + * IF the given element contains a reference to an external resource, 1.65 + * return its URL. 1.66 + * 1.67 + * @param nsURI 1.68 + * Namespace URI of the current element 1.69 + * @param localName 1.70 + * Local name of the current element 1.71 + * @return 1.72 + * It's OK to return a relative URL. 1.73 + */ 1.74 + protected abstract String findExternalResource(String nsURI, String localName, Attributes atts); 1.75 + 1.76 + @Override 1.77 + public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 1.78 + throws SAXException { 1.79 + super.startElement(namespaceURI, localName, qName, atts); 1.80 + 1.81 + String relativeRef = findExternalResource(namespaceURI, localName, atts); 1.82 + if (relativeRef == null) { 1.83 + return; // non found 1.84 + } 1.85 + try { 1.86 + // absolutize URL. 1.87 + String lsi = locator.getSystemId(); 1.88 + String ref; 1.89 + if (lsi.startsWith("jar:")) { 1.90 + int bangIdx = lsi.indexOf('!'); 1.91 + if (bangIdx > 0) { 1.92 + ref = lsi.substring(0, bangIdx + 1) 1.93 + + new URI(lsi.substring(bangIdx + 1)).resolve(new URI(relativeRef)).toString(); 1.94 + } else { 1.95 + ref = relativeRef; 1.96 + } 1.97 + } else { 1.98 + ref = new URI(lsi).resolve(new URI(relativeRef)).toString(); 1.99 + } 1.100 + 1.101 + // then parse this schema as well, 1.102 + // but don't mark this document as a root. 1.103 + parent.parse(ref, false); 1.104 + } catch (URISyntaxException e) { 1.105 + String msg = e.getMessage(); 1.106 + if (new File(relativeRef).exists()) { 1.107 + msg = Messages.format(Messages.ERR_FILENAME_IS_NOT_URI) + ' ' + msg; 1.108 + } 1.109 + 1.110 + SAXParseException spe = new SAXParseException2( 1.111 + Messages.format(Messages.ERR_UNABLE_TO_PARSE, relativeRef, msg), 1.112 + locator, e); 1.113 + 1.114 + fatalError(spe); 1.115 + throw spe; 1.116 + } catch (IOException e) { 1.117 + SAXParseException spe = new SAXParseException2( 1.118 + Messages.format(Messages.ERR_UNABLE_TO_PARSE, relativeRef, e.getMessage()), 1.119 + locator, e); 1.120 + 1.121 + fatalError(spe); 1.122 + throw spe; 1.123 + } 1.124 + } 1.125 + private Locator locator; 1.126 + 1.127 + @Override 1.128 + public void setDocumentLocator(Locator locator) { 1.129 + super.setDocumentLocator(locator); 1.130 + this.locator = locator; 1.131 + } 1.132 +};