public static ResourcePath parse(String pathString) {
    String rootElement;
    int lastIndex;
    if (pathString.startsWith(WINDOWS_NETWORK_ROOT)) {
      rootElement = WINDOWS_NETWORK_ROOT;
      lastIndex = 2;
    } else if (pathString.startsWith("/")) {
      rootElement = "";
      lastIndex = 1;
    } else {
      char first = pathString.charAt(0);
      if (pathString.length() > 1 && pathString.charAt(1) == ':' && Character.isLetter(first)) {
        rootElement = Character.toUpperCase(first) + ":";
        lastIndex = 2;
        if (pathString.length() > 2
            && (pathString.charAt(2) == '/' || pathString.charAt(2) == '\\')) {
          lastIndex = 3;
        }
      } else {
        throw new IllegalArgumentException(
            "The path string ["
                + pathString
                + "] is not an absolute path starting at a drive root");
      }
    }

    DynamicArray<String> results = tokenizePathFragment(pathString, lastIndex);

    results = normalizePath(results);

    return construct(rootElement, results, results.size() - 1);
  }
  private static DynamicArray<String> tokenizePathFragment(String pathString, int lastIndex) {
    DynamicArray<String> results = new DynamicArray<String>();
    for (int i = lastIndex; i < pathString.length(); i++) {
      char c = pathString.charAt(i);
      if (c == '/' || c == '\\') {
        results.add(pathString.substring(lastIndex, i));
        lastIndex = i + 1;
      }
    }

    pathString = pathString.substring(lastIndex);
    results.add(pathString);
    return results;
  }
  private static DynamicArray<String> normalizePath(DynamicArray<String> pathElements) {
    DynamicArray<String> results = new DynamicArray<String>(pathElements.size());
    for (int i = 0; i < pathElements.size; i++) {
      String s = (String) pathElements.data[i];
      if (s.equals(".") || s.equals("/") || s.equals("\\") || s.equals("")) {
        // no-op
      } else if (s.equals("..")) {
        // TODO - Throw if no more elements
        results.remove(results.size() - 1);
      } else {
        results.add(s);
      }
    }

    return results;
  }
Exemple #4
0
  private static String[] split(String fqn) {
    String theRest = fqn;
    DynamicArray<String> parts = new DynamicArray<String>();
    while (theRest != null) {
      int iParam = theRest.indexOf('<');
      int iDot = theRest.indexOf('.');
      int iArray = theRest.indexOf('[');
      String part;
      if (iParam == 0) {
        if (iArray > 0) {
          part = theRest.substring(0, iArray);
          theRest = iArray < theRest.length() ? theRest.substring(iArray) : null;
        } else {
          if (theRest.charAt(theRest.length() - 1) != '>') {
            throw new RuntimeException("\"" + theRest + "\" does not end with '>'");
          }
          part = theRest;
          theRest = null;
        }
      } else if (iArray == 0) {
        part = theRest.substring(0, 2);
        theRest = part.length() == theRest.length() ? null : theRest.substring(2);
      } else if (iParam > 0) {
        if (iDot > 0 && iDot < iParam) {
          part = theRest.substring(0, iDot);
          theRest = iDot + 1 < theRest.length() ? theRest.substring(iDot + 1) : null;
        } else {
          part = theRest.substring(0, iParam);
          theRest = iParam < theRest.length() ? theRest.substring(iParam) : null;
        }
      } else if (iDot > 0) {
        part = theRest.substring(0, iDot);
        theRest = iDot + 1 < theRest.length() ? theRest.substring(iDot + 1) : null;
      } else {
        part = theRest;
        theRest = null;
      }
      parts.add(StringCache.get(part));
    }

    return parts.toArray(new String[parts.size()]);
  }