@Override
 public LSInput resolveResource(
     String type, String namespaceURI, String publicId, String systemId, String baseURI) {
   // systemId should be mandatory
   if (systemId == null) {
     throw new IllegalArgumentException(
         String.format(
             "Resource: %s refers an invalid resource without SystemId."
                 + " Invalid resource has type: %s, namespaceURI: %s, publicId: %s, systemId: %s, baseURI: %s",
             resourceUri, type, namespaceURI, publicId, systemId, baseURI));
   }
   String resourceURI = null;
   // Build up the relative path for using relatedURI and baseURI
   if (baseURI == null) {
     relatedURI = FileUtil.compactPath(getUri(systemId), '/');
     resourceURI = relatedURI;
   } else {
     String relatedPath = relatedURIMap.get(baseURI);
     if (relatedPath == null) {
       relatedPath = FileUtil.onlyPath(relatedURI);
       if (relatedPath == null) {
         relatedPath = "";
       }
       relatedURI = FileUtil.compactPath(FileUtil.onlyPath(relatedURI) + "/" + systemId, '/');
       resourceURI = relatedURI;
       relatedURIMap.put(baseURI, relatedPath);
     } else {
       resourceURI = FileUtil.compactPath(relatedPath + "/" + systemId, '/');
       relatedURI = resourceURI;
     }
   }
   return new DefaultLSInput(publicId, systemId, baseURI, resourceURI);
 }
Ejemplo n.º 2
0
  @Override
  public Source resolve(String href, String base) throws TransformerException {
    // supports the empty href
    if (ObjectHelper.isEmpty(href)) {
      href = location;
    }
    if (ObjectHelper.isEmpty(href)) {
      throw new TransformerException("include href is empty");
    }

    LOG.trace("Resolving URI with href: {} and base: {}", href, base);

    String scheme = ResourceHelper.getScheme(href);
    if (scheme != null) {
      // need to compact paths for file/classpath as it can be relative paths using .. to go
      // backwards
      if ("file:".equals(scheme)) {
        // compact path use file OS separator
        href = FileUtil.compactPath(href);
      } else if ("classpath:".equals(scheme)) {
        // for classpath always use /
        href = FileUtil.compactPath(href, '/');
      }
      LOG.debug("Resolving URI from {}: {}", scheme, href);

      InputStream is;
      try {
        is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, href);
      } catch (IOException e) {
        throw new TransformerException(e);
      }
      return new StreamSource(is);
    }

    // if href and location is the same, then its the initial resolve
    if (href.equals(location)) {
      String path = baseScheme + href;
      return resolve(path, base);
    }

    // okay then its relative to the starting location from the XSLT component
    String path = FileUtil.onlyPath(location);
    if (ObjectHelper.isEmpty(path)) {
      path = baseScheme + href;
      return resolve(path, base);
    } else {
      if (ResourceHelper.hasScheme(path)) {
        path = path + "/" + href;
      } else {
        path = baseScheme + path + "/" + href;
      }
      return resolve(path, base);
    }
  }
Ejemplo n.º 3
0
  public void changeCurrentDirectory(String path) throws GenericFileOperationFailedException {
    LOG.trace("changeCurrentDirectory({})", path);
    if (ObjectHelper.isEmpty(path)) {
      return;
    }

    // must compact path so SFTP server can traverse correctly, make use of the '/'
    // separator because JSch expects this as the file separator even on Windows
    String before = path;
    char separatorChar = '/';
    path = FileUtil.compactPath(path, separatorChar);
    if (LOG.isTraceEnabled()) {
      LOG.trace(
          "Compacted path: {} -> {} using separator: {}",
          new Object[] {before, path, separatorChar});
    }

    // not stepwise should change directory in one operation
    if (!endpoint.getConfiguration().isStepwise()) {
      doChangeDirectory(path);
      return;
    }
    if (getCurrentDirectory().startsWith(path)) {
      // extract the path segment relative to the target path and make sure it keeps the preceding
      // '/' for the regex op
      String p = getCurrentDirectory().substring(path.length() - (path.endsWith("/") ? 1 : 0));
      if (p.length() == 0) {
        return;
      }
      // the first character must be '/' and hence removed
      path = UP_DIR_PATTERN.matcher(p).replaceAll("/..").substring(1);
    }

    // if it starts with the root path then a little special handling for that
    if (FileUtil.hasLeadingSeparator(path)) {
      // change to root path
      doChangeDirectory(path.substring(0, 1));
      path = path.substring(1);
    }

    // split into multiple dirs
    final String[] dirs = path.split("/|\\\\");

    if (dirs == null || dirs.length == 0) {
      // path was just a relative single path
      doChangeDirectory(path);
      return;
    }

    // there are multiple dirs so do this in chunks
    for (String dir : dirs) {
      doChangeDirectory(dir);
    }
  }
Ejemplo n.º 4
0
  public void changeToParentDirectory() throws GenericFileOperationFailedException {
    LOG.trace("changeToParentDirectory()");
    String current = getCurrentDirectory();

    String parent = FileUtil.compactPath(current + "/..");
    // must start with absolute
    if (!parent.startsWith("/")) {
      parent = "/" + parent;
    }

    changeCurrentDirectory(parent);
  }