Code example for URIResolver

Methods: resolve

0
    throws TransformerException 
  { 
    Source source = null;
    if (userResolver != null)
      { 
        source = userResolver.resolve(base, href);
      } 
    return resolveDOM(source, href, base);
  } 
 
  DOMSource resolveDOM(Source source, String base, String href)
    throws TransformerException 
  { 
    if (source != null && source instanceof DOMSource)
      { 
        return (DOMSource) source;
      } 
    String systemId = (source == null) ? null : source.getSystemId();
    long lastModified = 0L, lastLastModified = 0L;
 
    try 
      { 
        Node node = null;
        InputStream in = null;
        if (source != null && source instanceof StreamSource)
          { 
            StreamSource ss = (StreamSource) source;
            in = ss.getInputStream();
            if (in == null)
              { 
                Reader reader = ss.getReader();
                if (reader != null)
                  { 
                    in = new ReaderInputStream(reader);
                  } 
              } 
          } 
        else if (source != null && source instanceof SAXSource)
          { 
            SAXSource ss = (SAXSource) source;
            InputSource input = ss.getInputSource();
            if (input != null)
              { 
                if (systemId == null)
                  systemId = input.getSystemId();
                XMLReader reader = ss.getXMLReader();
                if (reader != null)
                  return parse(input, reader);
              } 
          } 
        if (in == null)
          { 
            URL url = resolveURL(systemId, base, href);
            if (url != null)
              { 
                systemId = url.toString();
                node = nodeCache.get(systemId);
                // Is the resource up to date? 
                URLConnection conn = url.openConnection();
                Long llm = lastModifiedCache.get(systemId);
                if (llm != null)
                  { 
                    lastLastModified = llm.longValue();
                    conn.setIfModifiedSince(lastLastModified);
                  } 
                conn.connect();
                lastModified = conn.getLastModified();
                if (node != null &&
                    lastModified > 0L &&
                    lastModified <= lastLastModified)
                  { 
                    // Resource unchanged 
                    return new DOMSource(node, systemId);
                  } 
                else 
                  { 
                    // Resource new or modified 
                    in = conn.getInputStream();
                    nodeCache.put(systemId, node);
                    lastModifiedCache.put(systemId, new Long(lastModified));
                  } 
              } 
            else 
              { 
                throw new TransformerException("can't resolve URL: " +
                                               systemId);
              } 
          } 
        InputSource input = new InputSource(in);
        input.setSystemId(systemId);
        DocumentBuilder builder = getDocumentBuilder();
        node = builder.parse(input);
        return new DOMSource(node, systemId);
      } 
    catch (IOException e)
      { 
        throw new TransformerException(e);
      } 
    catch (SAXException e)
      { 
        throw new TransformerException(e);
      } 
  } 
 
  URL resolveURL(String systemId, String base, String href)
    throws IOException 
  { 
    URL url = null;
    try 
      { 
        if (systemId != null)
          { 
            try 
              { 
                url = new URL(systemId);
              } 
            catch (MalformedURLException e)
              { 
                // Try building from base + href 
              } 
          } 
        if (url == null)
          { 
            if (base != null)
              { 
                URL baseURL = new URL(base);
                url = new URL(baseURL, href);
              } 
            else if (href != null)
              { 
                url = new URL(href);
              } 
            else 
              { 
                // See below 
                throw new MalformedURLException(systemId);
              } 
          } 
        return url;
      } 
    catch (MalformedURLException e)
      { 
        // Fall back to local filesystem 
        File file = null;
        if (href == null)
          { 
            href = systemId;
          } 
        if (base != null)
          { 
            int lsi = base.lastIndexOf(File.separatorChar);
            if (lsi != -1 && lsi < base.length() - 1)
              { 
                base = base.substring(0, lsi);
              } 
            File baseFile = new File(base);
            file = new File(baseFile, href);
          } 
        else if (href != null)
          { 
            file = new File(href);
          } 
        return (file == null) ? null : file.toURL();
      } 
  } 
 
  DocumentBuilder getDocumentBuilder()
    throws TransformerException 
  { 
    try 
      { 
        if (builder == null)
          { 
            DocumentBuilderFactory factory =
              DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setExpandEntityReferences(true);
            builder = factory.newDocumentBuilder();
          } 
        if (userResolver != null)
          { 
            builder.setEntityResolver(new URIResolverEntityResolver(userResolver));
          } 
        if (userListener != null)
          { 
            builder.setErrorHandler(new ErrorListenerErrorHandler(userListener));
          }