Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.sling.api.resource;


The ResourceUtil class provides helper methods dealing with resources.
 
 public class ResourceUtil {

    
Resolves relative path segments '.' and '..' in the absolute path. Returns null if not possible (.. points above root) or if path is not absolute.
 
     public static String normalize(String path) {
 
         // don't care for empty paths
         if (path.length() == 0) {
             return path;
         }
 
         // prepare the path buffer with trailing slash (simplifies impl)
         int absOffset = (path.charAt(0) == '/') ? 0 : 1;
         char[] buf = new char[path.length() + 1 + absOffset];
         if (absOffset == 1) {
             buf[0] = '/';
         }
         path.getChars(0, path.length(), bufabsOffset);
         buf[buf.length - 1] = '/';
 
         int lastSlash = 0; // last slash in path
         int numDots = 0; // number of consecutive dots after last slash
 
         int bufPos = 0;
         for (int bufIdx = lastSlashbufIdx < buf.lengthbufIdx++) {
             char c = buf[bufIdx];
             if (c == '/') {
                 if (numDots == 2) {
                     if (bufPos == 0) {
                         return null;
                     }
 
                     do {
                         bufPos--;
                     } while (bufPos > 0 && buf[bufPos] != '/');
                 }
 
                 lastSlash = bufIdx;
                 numDots = 0;
             } else if (c == '.' && numDots < 2) {
                 numDots++;
             } else {
                 // find the next slash
                 int nextSlash = bufIdx + 1;
                 while (nextSlash < buf.length && buf[nextSlash] != '/') {
                     nextSlash++;
                 }
 
                 // append up to the next slash (or end of path)
                 if (bufPos < lastSlash) {
                     int segLen = nextSlash - bufIdx + 1;
                     System.arraycopy(buflastSlashbufbufPossegLen);
                     bufPos += segLen;
                 } else {
                     bufPos = nextSlash;
                 }
 
                 numDots = 0;
                 lastSlash = nextSlash;
                 bufIdx = nextSlash;
             }
         }
 
         String resolved;
         if (bufPos == 0 && numDots == 0) {
             resolved = (absOffset == 0) ? "/" : "";
         } else if ((bufPos - absOffset) == path.length()) {
             resolved = path;
         } else {
             resolved = new String(bufabsOffsetbufPos - absOffset);
         }
        return resolved;
    }

    
Utility method returns the parent path of the given path, which is normalized by normalize(java.lang.String) before resolving the parent.

Parameters:
path The path whose parent is to be returned.
Returns:
null if path is the root path (/) or if path is a single name containing no slash (/) characters.
Throws:
java.lang.IllegalArgumentException If the path cannot be normalized by the normalize(java.lang.String) method.
java.lang.NullPointerException If path is null.
    public static String getParent(String path) {
        if ("/".equals(path)) {
            return null;
        }
        // normalize path (remove . and ..)
        path = normalize(path);
        // if normalized to root, there is no parent
        if (path == null || "/".equals(path)) {
            return null;
        }
        // find the last slash, after which to cut off
        int lastSlash = path.lastIndexOf('/');
        if (lastSlash < 0) {
            // no slash in the path
            return null;
        } else if (lastSlash == 0) {
            // parent is root
            return "/";
        }
        return path.substring(0, lastSlash);
    }

    
Utility method returns the parent resource of the resource.

Returns:
The parent resource or null if the rsrc is the root.
Throws:
java.lang.NullPointerException If rsrc is null.
    public static Resource getParent(Resource rsrc) {
        final String parentPath = getParent(rsrc.getPath());
        if ( parentPath == null ) {
            return null;
        }
        return rsrc.getResourceResolver().resolve(parentPath);
    }

    
Utility method returns the parent resource of the resource.

Throws:
java.lang.NullPointerException If rsrc is null.
    public static String getName(Resource rsrc) {
        final String name = getName(rsrc.getPath());
        return name;
    }

    
Utility method returns the name of the given path, which is normalized by normalize(java.lang.String) before resolving the name.

Parameters:
path The path whose name (the last path element) is to be returned.
Returns:
The empty string if path is the root path (/) or if path is a single name containing no slash (/) characters.
Throws:
java.lang.IllegalArgumentException If the path cannot be normalized by the normalize(java.lang.String) method.
java.lang.NullPointerException If path is null.
    public static String getName(String path) {
        if ("/".equals(path)) {
            return "";
        }
        // normalize path (remove . and ..)
        path = normalize(path);
        if ("/".equals(path)) {
            return "";
        }
        // find the last slash
        return path.substring(path.lastIndexOf('/') + 1);
    }

    
Returns true if the resource res is a synthetic resource.

This method checks whether the resource is an instance of the org.apache.sling.resource.SyntheticResource class.

Parameters:
res The Resource to check whether it is a synthetic resource.
Returns:
true if res is a synthetic resource. false is returned if res is null or not an instance of the org.apache.sling.resource.SyntheticResource class.
    public static boolean isSyntheticResource(Resource res) {
        return res instanceof SyntheticResource;
    }

    
Returns true if the resource res is a "star resource". A star resource is a resource returned from the ResourceResolver.resolve(HttpServletRequest) whose path terminates in a /*. Generally such resource result from requests to something like /some/path/* or /some/path/*.html which may be used web applications to uniformly handle resources to be created.

This method checks whether the resource path ends with a /* indicating such a star resource.

Parameters:
res The Resource to check whether it is a star resource.
Returns:
true if res is to be considered a star resource.
Throws:
java.lang.NullPointerException if res is null.
    public static boolean isStarResource(Resource res) {
        return res.getPath().endsWith("/*");
    }

    
Returns true if the resource res is a non-existing resource.

This method checks the resource type of the resource to match the well-known resource type sling:nonexisting of the NonExistingResource class defined in the Sling API.

Parameters:
res The Resource to check whether it is a non-existing resource.
Returns:
true if res is to be considered a non-existing resource.
Throws:
java.lang.NullPointerException if res is null.
    public static boolean isNonExistingResource(Resource res) {
    }
New to GrepCode? Check out our FAQ X