1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.wsdl.parser;
27
28import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
29import com.sun.xml.internal.ws.api.wsdl.parser.XMLEntityResolver;
30import com.sun.xml.internal.ws.streaming.TidyXMLStreamReader;
31import org.xml.sax.EntityResolver;
32import org.xml.sax.InputSource;
33import org.xml.sax.SAXException;
34
35import java.io.IOException;
36import java.io.InputStream;
37import java.net.URL;
38
39/**
40 * Wraps {@link EntityResolver} into {@link com.sun.xml.internal.ws.api.wsdl.parser.XMLEntityResolver}.
41 *
42 * @author Kohsuke Kawaguchi
43 */
44final class EntityResolverWrapper implements XMLEntityResolver {
45    private final EntityResolver core;
46    private boolean useStreamFromEntityResolver = false;
47
48    public EntityResolverWrapper(EntityResolver core) {
49        this.core = core;
50    }
51
52    public EntityResolverWrapper(EntityResolver core, boolean useStreamFromEntityResolver) {
53        this.core = core;
54        this.useStreamFromEntityResolver =  useStreamFromEntityResolver;
55    }
56
57    public Parser resolveEntity(String publicId, String systemId) throws SAXException, IOException {
58        InputSource source = core.resolveEntity(publicId,systemId);
59        if(source==null)
60            return null;    // default
61
62        // ideally entity resolvers should be giving us the system ID for the resource
63        // (or otherwise we won't be able to resolve references within this imported WSDL correctly),
64        // but if none is given, the system ID before the entity resolution is better than nothing.
65        if(source.getSystemId()!=null)
66            systemId = source.getSystemId();
67
68        URL url = new URL(systemId);
69        InputStream stream;
70        if (useStreamFromEntityResolver) {
71                stream = source.getByteStream();
72        } else {
73                stream = url.openStream();
74        }
75        return new Parser(url,
76                new TidyXMLStreamReader(XMLStreamReaderFactory.create(url.toExternalForm(), stream, true), stream));
77    }
78}
79