Home » geronimo-2.2-source-release » org.apache.geronimo.jaxws.wsdl » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements. See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership. The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License. You may obtain a copy of the License at
    9    *
   10    * http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied. See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   package org.apache.geronimo.jaxws.wsdl;
   20   
   21   import java.io.File;
   22   import java.io.IOException;
   23   import java.io.InputStream;
   24   import java.net.URI;
   25   import java.net.URISyntaxException;
   26   import java.net.URL;
   27   
   28   /**
   29    */
   30   public class SimpleURIResolver {
   31       
   32       private URI uri;
   33       private URL url;
   34       private InputStream is;
   35   
   36       public SimpleURIResolver() {
   37       }
   38   
   39       public SimpleURIResolver(String path) throws IOException {
   40           this("", path);
   41       }
   42   
   43       public SimpleURIResolver(String baseUriStr, String uriStr) throws IOException {
   44           if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
   45               tryJar(baseUriStr, uriStr);
   46           } else if (uriStr.startsWith("jar:")) {
   47               tryJar(uriStr);
   48           } else {
   49               tryFileSystem(baseUriStr, uriStr);
   50           }
   51       }
   52       
   53       public void resolve(String baseUriStr, String uriStr) throws IOException {        
   54           this.uri = null;
   55           this.url = null;
   56           this.is = null;
   57           
   58           if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
   59               tryJar(baseUriStr, uriStr);
   60           } else if (uriStr.startsWith("jar:")) {
   61               tryJar(uriStr);
   62           } else {
   63               tryFileSystem(baseUriStr, uriStr);
   64           }
   65       }
   66        
   67       private void tryFileSystem(String baseUriStr, String uriStr) throws IOException {
   68           try {
   69               URI relative;
   70               File uriFile = new File(uriStr);
   71               uriFile = new File(uriFile.getAbsolutePath());
   72   
   73               if (uriFile.exists()) {
   74                   relative = uriFile.toURI();
   75               } else {
   76                   relative = new URI(uriStr.replaceAll(" ", "%20"));
   77               }
   78               
   79               if (relative.isAbsolute()) {
   80                   uri = relative;
   81                   url = relative.toURL();
   82                   is = url.openStream();               
   83               } else if (baseUriStr != null) {
   84                   URI base;
   85                   File baseFile = new File(baseUriStr);
   86   
   87                   if (!baseFile.exists() && baseUriStr.startsWith("file:/")) {
   88                       baseFile = new File(baseUriStr.substring(6));
   89                   }
   90   
   91                   if (baseFile.exists()) {
   92                       base = baseFile.toURI();
   93                   } else {
   94                       base = new URI(baseUriStr);
   95                   }
   96                   
   97                   base = base.resolve(relative);
   98                   if (base.isAbsolute()) {
   99                       uri = base;
  100                       url = base.toURL();
  101                       is = url.openStream();
  102                   }
  103               }
  104           } catch (URISyntaxException e) {
  105               // do nothing
  106           }
  107       }
  108       
  109       private void tryJar(String baseStr, String uriStr) throws IOException {
  110           int i = baseStr.indexOf('!');
  111           if (i == -1) {
  112               tryFileSystem(baseStr, uriStr);
  113           }
  114   
  115           String jarBase = baseStr.substring(0, i + 1);
  116           String jarEntry = baseStr.substring(i + 1);
  117           try {
  118               URI u = new URI(jarEntry).resolve(uriStr);
  119   
  120               tryJar(jarBase + u.toString());
  121   
  122               if (is != null) {
  123                   if (u.isAbsolute()) {
  124                       url = u.toURL();
  125                   }
  126                   return;
  127               }
  128           } catch (URISyntaxException e) {
  129               // do nothing
  130           }
  131           
  132           tryFileSystem("", uriStr);
  133       }
  134       
  135       private void tryJar(String uriStr) throws IOException {
  136           int i = uriStr.indexOf('!');
  137           if (i == -1) {
  138               return;
  139           }
  140   
  141           url = new URL(uriStr);
  142           try {
  143               is = url.openStream();
  144               try {
  145                   uri = url.toURI();
  146               } catch (URISyntaxException ex) {
  147                   // ignore
  148               }
  149           } catch (IOException e) {
  150               // do nothing
  151           }
  152       }
  153       
  154       public URI getURI() {
  155           return uri;
  156       }
  157   
  158       public URL getURL() {
  159           return url;
  160       }
  161   
  162       public InputStream getInputStream() {
  163           return is;
  164       }
  165       
  166       public boolean isResolved() {
  167           return is != null;
  168       }
  169   }

Home » geronimo-2.2-source-release » org.apache.geronimo.jaxws.wsdl » [javadoc | source]