src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/AbstractReferenceFinderImpl.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,132 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, 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 com.sun.istack.internal.SAXParseException2;
    1.32 +import org.xml.sax.Attributes;
    1.33 +import org.xml.sax.Locator;
    1.34 +import org.xml.sax.SAXException;
    1.35 +import org.xml.sax.SAXParseException;
    1.36 +import org.xml.sax.helpers.XMLFilterImpl;
    1.37 +
    1.38 +import java.io.File;
    1.39 +import java.io.IOException;
    1.40 +import java.net.URI;
    1.41 +import java.net.URISyntaxException;
    1.42 +
    1.43 +/**
    1.44 + * XMLFilter that finds references to other schema files from
    1.45 + * SAX events.
    1.46 + * <p/>
    1.47 + * This implementation is a base implementation for typical case
    1.48 + * where we just need to look for a particular attribute which
    1.49 + * contains an URL to another schema file.
    1.50 + *
    1.51 + * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.52 + */
    1.53 +public abstract class AbstractReferenceFinderImpl extends XMLFilterImpl {
    1.54 +
    1.55 +    protected final DOMForest parent;
    1.56 +
    1.57 +    protected AbstractReferenceFinderImpl(DOMForest _parent) {
    1.58 +        this.parent = _parent;
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * IF the given element contains a reference to an external resource,
    1.63 +     * return its URL.
    1.64 +     *
    1.65 +     * @param nsURI     Namespace URI of the current element
    1.66 +     * @param localName Local name of the current element
    1.67 +     * @return It's OK to return a relative URL.
    1.68 +     */
    1.69 +    protected abstract String findExternalResource(String nsURI, String localName, Attributes atts);
    1.70 +
    1.71 +    @Override
    1.72 +    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    1.73 +            throws SAXException {
    1.74 +        super.startElement(namespaceURI, localName, qName, atts);
    1.75 +
    1.76 +        String relativeRef = findExternalResource(namespaceURI, localName, atts);
    1.77 +        if (relativeRef == null) {
    1.78 +            return; // not found
    1.79 +        }
    1.80 +        try {
    1.81 +            // absolutize URL.
    1.82 +            String lsi = locator.getSystemId();
    1.83 +            String ref;
    1.84 +            URI relRefURI = new URI(relativeRef);
    1.85 +            if (relRefURI.isAbsolute())
    1.86 +                ref = relativeRef;
    1.87 +            else {
    1.88 +                if (lsi.startsWith("jar:")) {
    1.89 +                    int bangIdx = lsi.indexOf('!');
    1.90 +                    if (bangIdx > 0) {
    1.91 +                        ref = lsi.substring(0, bangIdx + 1)
    1.92 +                                + new URI(lsi.substring(bangIdx + 1)).resolve(new URI(relativeRef)).toString();
    1.93 +                    } else {
    1.94 +                        ref = relativeRef;
    1.95 +                    }
    1.96 +                } else {
    1.97 +                    ref = new URI(lsi).resolve(new URI(relativeRef)).toString();
    1.98 +                }
    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 +            if (parent != null) { // this is there to allow easier testing
   1.104 +                parent.parse(ref, false);
   1.105 +            }
   1.106 +        } catch (URISyntaxException e) {
   1.107 +            String msg = e.getMessage();
   1.108 +            if (new File(relativeRef).exists()) {
   1.109 +                msg = Messages.format(Messages.ERR_FILENAME_IS_NOT_URI) + ' ' + msg;
   1.110 +            }
   1.111 +
   1.112 +            SAXParseException spe = new SAXParseException2(
   1.113 +                    Messages.format(Messages.ERR_UNABLE_TO_PARSE, relativeRef, msg),
   1.114 +                    locator, e);
   1.115 +
   1.116 +            fatalError(spe);
   1.117 +            throw spe;
   1.118 +        } catch (IOException e) {
   1.119 +            SAXParseException spe = new SAXParseException2(
   1.120 +                    Messages.format(Messages.ERR_UNABLE_TO_PARSE, relativeRef, e.getMessage()),
   1.121 +                    locator, e);
   1.122 +
   1.123 +            fatalError(spe);
   1.124 +            throw spe;
   1.125 +        }
   1.126 +    }
   1.127 +
   1.128 +    private Locator locator;
   1.129 +
   1.130 +    @Override
   1.131 +    public void setDocumentLocator(Locator locator) {
   1.132 +        super.setDocumentLocator(locator);
   1.133 +        this.locator = locator;
   1.134 +    }
   1.135 +}

mercurial