Пример #1
0
 /*
  * Utilise le CatalogResolver d'Apache pour r?soudre des URI en URL
  */
 protected static URL resoudreURI(final URL schemadir, final String uri)
     throws MalformedURLException, TransformerException {
   if (uri == null) return (null);
   if (uri.startsWith("http://")) return (new URL(uri));
   else if (schemadir != null && !uri.startsWith("urn:"))
     return (new URL(schemadir.toExternalForm() + "/" + uri));
   else {
     final URIResolver resolver = Jaxe.getURIResolver();
     if (resolver != null) {
       Source src;
       if (schemadir != null) src = resolver.resolve(uri, schemadir.toString());
       else src = resolver.resolve(uri, null);
       final URL surl = new URL(src.getSystemId());
       try {
         // pour ?viter un bug de CatalogResolver qui n'encode pas l'URI correctement...
         final URI suri =
             new URI(surl.getProtocol(), surl.getHost(), surl.getPath(), surl.getQuery(), null);
         return (suri.toURL());
       } catch (URISyntaxException ex) {
         LOG.error("resolveURI", ex);
         return (surl);
       } catch (MalformedURLException ex) {
         LOG.error("resolveURI", ex);
         return (surl);
       }
     } else return (new URL(uri));
   }
 }
  /** Opens a relative path. */
  private Source getSource(String href, String base) throws Exception {
    Path subpath;

    if (href == null) href = "";
    if (base == null) base = "/";

    if (_uriResolver != null) {
      if (href.startsWith("/") || base.equals("/")) subpath = getSearchPath().lookup(href);
      else {
        subpath = getSearchPath().lookup(base).getParent().lookup(href);
      }

      Source source = _uriResolver.resolve(href, base);

      if (source != null) {
        if (source.getSystemId() == null) source.setSystemId(subpath.getURL());

        return source;
      }
    }

    if (href.startsWith("/") || base.equals("/")) subpath = getSearchPath().lookup(href);
    else {
      if (base.startsWith("file:")) base = base.substring(5);

      subpath = getSearchPath().lookup(base).getParent().lookup(href);
    }

    return new StreamSource(subpath.getURL());
  }
Пример #3
0
 /**
  * Converts a String parameter to a JAXP Source object.
  *
  * @param param a String parameter
  * @return Source the generated Source object
  */
 protected Source convertString2Source(String param) {
   Source src;
   try {
     src = uriResolver.resolve(param, null);
   } catch (TransformerException e) {
     src = null;
   }
   if (src == null) {
     src = new StreamSource(new File(param));
   }
   return src;
 }
  Path lookupPath(String base, String href) throws TransformerException {
    if (_uriResolver != null) {
      Source source = _uriResolver.resolve(href, base);

      if (source != null) {
        String systemId = source.getSystemId();

        if (systemId != null) return getSearchPath().lookup(systemId);
      }
    }

    return getSearchPath().lookup(base).lookup(href);
  }
  /** Opens a relative path. */
  ReadStream openPath(String href, String base) throws TransformerException, IOException {
    if (_uriResolver != null) {
      Source source = _uriResolver.resolve(href, base);

      if (source != null) return openPath(source);
    }

    if (href.startsWith("/") || base.equals("/")) return getSearchPath().lookup(href).openRead();
    else {
      Path path = getSearchPath().lookup(base).getParent().lookup(href);

      if (path.exists()) return path.openRead();
      else return getSearchPath().lookup(href).openRead();
    }
  }
  /**
   * This will be called by the processor when it encounters an xsl:include, xsl:import, or
   * document() function.
   *
   * @param base The base URI that should be used.
   * @param urlString Value from an xsl:import or xsl:include's href attribute, or a URI specified
   *     in the document() function.
   * @return a Source that can be used to process the resource.
   * @throws IOException
   * @throws TransformerException
   */
  public Source resolveURI(String base, String urlString, SourceLocator locator)
      throws TransformerException, IOException {

    Source source = null;

    if (null != m_uriResolver) {
      source = m_uriResolver.resolve(urlString, base);
    }

    if (null == source) {
      String uri = SystemIDResolver.getAbsoluteURI(urlString, base);

      source = new StreamSource(uri);
    }

    return source;
  }
Пример #7
0
  /**
   * Insert the text of the file into the result tree
   *
   * <p>Processing this element inserts the contents of the URL named by the href attribute into the
   * result tree as plain text.
   *
   * <p>Optional encoding attribute can specify encoding of resource. If not specified default
   * system encoding is used.
   */
  public void process(Context context) throws TransformerException {
    Outputter out = context.getOutputter();

    String hrefAtt = getAttribute("href");
    Expression hrefExpr = makeAttributeValueTemplate(hrefAtt);
    String href = hrefExpr.evaluateAsString(context);

    String encodingAtt = getAttribute("encoding");
    Expression encodingExpr = makeAttributeValueTemplate(encodingAtt);
    String encoding = encodingExpr.evaluateAsString(context);

    String baseURI = context.getContextNodeInfo().getBaseURI();

    URIResolver resolver = context.getController().getURIResolver();

    if (resolver != null) {
      Source source = resolver.resolve(href, baseURI);
      href = source.getSystemId();
    }

    URL baseURL = null;
    URL fileURL = null;

    try {
      baseURL = new URL(baseURI);
    } catch (MalformedURLException e0) {
      // what the!?
      baseURL = null;
    }

    try {
      try {
        fileURL = new URL(baseURL, href);
      } catch (MalformedURLException e1) {
        try {
          fileURL = new URL(baseURL, "file:" + href);
        } catch (MalformedURLException e2) {
          System.out.println("Cannot open " + href);
          return;
        }
      }

      InputStreamReader isr = null;
      if (encoding.equals("") == true) isr = new InputStreamReader(fileURL.openStream());
      else isr = new InputStreamReader(fileURL.openStream(), encoding);

      BufferedReader is = new BufferedReader(isr);

      final int BUFFER_SIZE = 4096;
      char chars[] = new char[BUFFER_SIZE];
      char nchars[] = new char[BUFFER_SIZE];
      int len = 0;
      int i = 0;
      int carry = -1;

      while ((len = is.read(chars)) > 0) {
        // various new lines are normalized to LF to prevent blank lines
        // between lines

        int nlen = 0;
        for (i = 0; i < len; i++) {
          // is current char CR?
          if (chars[i] == '\r') {
            if (i < (len - 1)) {
              // skip it if next char is LF
              if (chars[i + 1] == '\n') continue;
              // single CR -> LF to normalize MAC line endings
              nchars[nlen] = '\n';
              nlen++;
              continue;
            } else {
              // if CR is last char of buffer we must look ahead
              carry = is.read();
              nchars[nlen] = '\n';
              nlen++;
              if (carry == '\n') {
                carry = -1;
              }
              break;
            }
          }
          nchars[nlen] = chars[i];
          nlen++;
        }
        out.writeContent(nchars, 0, nlen);
        // handle look aheaded character
        if (carry != -1) out.writeContent(String.valueOf((char) carry));
        carry = -1;
      }
      is.close();
    } catch (Exception e) {
      System.out.println("Cannot read " + href);
    }
  }