Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
WildCAT: A Generic Framework for Context-Aware Applications. Copyright (C) 2008-2009 Bull S.A.S. Copyright (C) 2008 EMN Contact: wildcat@ow2.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Authors: - Pierre-Charles David (initial) - Nicolas Loriant -------------------------------------------------------------------------- $Id: Path.java 404 2009-01-09 09:49:34Z loris $ --------------------------------------------------------------------------
 
 package org.ow2.wildcat.hierarchy;
 
 import java.util.List;
 
A path in the wildcat hierarchy. Path have the form of url.
 
 public class Path implements Serializable {

    
Id for serializable class.
 
     private static final long serialVersionUID = 4106680673994638328L;

    
Logger.
 
     private static transient Log logger = LogFactory.getLog(Path.class);
 
     private boolean absolute;
     private String hostname;
     private final List<Stringresources;
     private String attribute;

    
Constant Path for /.
 
     public static final Path ROOT = new Path(truenullnew ArrayList<String>(),
             null);

    
Constant Path for null://
 
     public static final Path NULL = new Path(true"null"new ArrayList<String>(),
             null);

    

Parameters:
absolute
hostname
resources
attribute
 
     private Path(final boolean absolutefinal String hostnamefinal List<Stringresources,
             final String attribute) {
         super();
         this. = absolute;
         this. = hostname;
         this. = resources;
         this. = attribute;
     }

    
Convenient constructors. Constructs a path from its string representation

Parameters:
path
Throws:
MalformedPathException if the path is malformed
 
    public Path(final String paththrows MalformedPathException {
        .debug(Messages.message(Path.class.getName() + ".createString",
                path));
        PathLexer lexer = new PathLexer(new ANTLRStringStream(path));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PathParser parser = new PathParser(tokens);
        PathParser.path_return pr = null;
        try {
            pr = parser.path();
        } catch (RecognitionException e) {
            .warn(Messages.message(
                    Path.class.getName() + ".createString"path), e);
            throw new MalformedPathException("parsing failled");
        }
        this. = pr.abs;
        this. = pr.h;
        this. = pr.r;
        this. = pr.a;
    }

    
Deep copy constructor

Parameters:
path
    public Path(final Path path) {
        this. = path.hostname;
        this. = path.absolute;
        this. = new ArrayList<String>();
        for (int i = 0; i < path.resources.size(); i++) {
            this..add(path.resources.get(i));
        }
        this. = path.attribute;
    }

    
Creates a new path by appending 'name' as resource to current path

Parameters:
name
Returns:
a new path with 'name' as resource added at the end of current path
    public Path appendResource(final String name) {
        try {
            this.expectResource();
            Path n = new Path(this);
            n.resources.add(name);
            return n;
        } catch (MalformedPathException e) {
            return null;
        }
    }

    
Creates a new path by appending 'name' as attribute name to current path assumes 'name' to be a valid attribute name

Parameters:
name
Returns:
a copy of this path with 'name' as attribute name.
    public Path appendAttribute(final String name) {
        try {
            this.expectResource();
            Path n = new Path(this);
            n.attribute = name;
            return n;
        } catch (MalformedPathException e) {
            return null;
        }
    }

    
Creates a new Path by appending suffix to 'this'.

Parameters:
suffix suffix to append
Returns:
a new Path
    public Path append(final Path suffix) {
        try {
            Path p = new Path(this);
            p.expectResource();
            if (suffix == null) {
                return p;
            } else {
                suffix.expectRelative();
                p.resources.addAll(suffix.resources);
                p.attribute = suffix.attribute;
                return p;
            }
        } catch (MalformedPathException e) {
            return null;
        }
    }

    
Partially rewrite current path, changing oprefix in 'this' to nprefix.

Parameters:
oprefix the prefix of current path to delete
nprefix the new prefix
Returns:
rewritten path
    public Path replacePrefix(final Path oprefixfinal Path nprefix) {
        if (!this.isPrefix(oprefix)) {
            return null;
        }
        Path p = new Path(this);
        p.hostname = null;
        p.absolute = false;
        for (int i = 0; i < oprefix.resources.size(); i++) {
            p.resources.remove(0);
        }
        return nprefix.append(p);
    }

    

Returns:
the parent resource
    public Path getParentPath() {
        if (isOnlyAttribute() || isOnlyResource()) {
            throw new IllegalArgumentException(
                    "Unable to retrieve a parent path with only a resource name or an attribute name!");
        }
        Path superPath = new Path(this);
        if ( == null) {
            superPath.resources.remove(superPath.resources.size() - 1);
        } else {
            superPath.attribute = null;
        }
        return superPath;
    }

    
Canonicalize current path

Returns:
a new Path canonicalized
    public Path canonicalize() {
        boolean abs = this.isAbsolute();
        String h = this.;
        List<Stringr = new ArrayList<String>();
        String a = this.;
        for (String s : this.) {
            if (".".equals(s)) {
            } else if ("..".equals(s)) {
                if (r.size() > 0) {
                    if ("..".equals(r.get(r.size() - 1))) {
                        r.add("..");
                    } else {
                        r.remove(r.size() - 1);
                    }
                } else {
                    if (!abs) {
                        r.add("..");
                    }
                }
            } else {
                r.add(s);
            }
        }
        return new Path(abshra);
    }

    
Creates a new path by stripping the first resource as attribute name to current path. Only set absolute to false when the path is absolute.

Returns:
same as getRelativePart if path is absolute, else a copy of current path without the first resource
    public Path stripTop() {
        try {
            Path p = new Path(this);
            if (p.isAbsolute()) {
                p.absolute = false;
                p.hostname = null;
            } else {
                p.resources.remove(0);
            }
            return p;
        } catch (Exception e) {
            return null;
        }
    }

    
Creates a new path by stripping host and starting '/'.

Returns:
a copy of current path without host and starting '/'
    public Path getRelativePart() {
        if (!this.hasAttribute() && !this.hasResource()) {
            return null;
        }
        Path p = new Path(this);
        p.hostname = null;
        p.absolute = false;
        return p;
    }

    

Returns:
the hostname, null if none is explicitly given
    public String getHostname() {
        return this.;
    }
    public void setHostName(final String hostname) {
        this. = hostname;
    }

    

Returns:
the attribute name, null if none
    public String getAttributeName() {
        return this.;
    }

    

Returns:
the first resource name of the path
    public String getFirstResource() {
        return this..get(0);
    }

    

Returns:
true if path is absolute (it start with '/' or if hasExplicitHost. is true), false otherwise.
    public boolean isAbsolute() {
        return this. || this.hasExplicitHost();
    }

    

Returns:
true if path has an explicitly reference an host ('self' or any other hostname) false otherwise.
    public boolean hasExplicitHost() {
        return  != null;
    }

    

Returns:
true if path points an attribute, else false
    public boolean hasAttribute() {
        return this. != null;
    }

    

Returns:
true if path contains at least one resource
    public boolean hasResource() {
        return this. != null && this..size() != 0;
    }

    

Returns:
true if path only contains an attribute name, else false
    public boolean isOnlyAttribute() {
        return !this.isAbsolute()
        && (this. == null || this..size() == 0)
        && this.hasAttribute();
    }

    

Returns:
true if path is only a resource name, else false
    public boolean isOnlyResource() {
        return !this.isAbsolute() && !this.hasAttribute()
        && this..size() == 1;
    }

    
Tests if path is canonical.

Returns:
true if no resource is a relative reference ("." or "..")
    public boolean isCanonical() {
        if (this. != null) {
            for (String s : this.) {
                if (".".equals(s) || "..".equals(s)) {
                    return false;
                }
            }
        }
        return true;
    }

    
Tests if path contain pattern elements.

Returns:
true if a resource or the attribute equal "*", else false
    public boolean isPattern() {
        if (this. != null) {
            for (String s : this.) {
                if ("*".equals(s)) {
                    return true;
                }
            }
        }
        if ("*".equals(this.)) {
            return true;
        }
        return false;
    }

    
Test whether argument is a prefix of current path.

Parameters:
prefix
Returns:
trueif prefix is a prefix of current path, else false
    public boolean isPrefix(final Path prefix) {
        try {
            prefix.expectAbsolute();
            prefix.expectResource();
            prefix.expectCanonical();
            prefix.expectFinite();
            if (this..size() < prefix.resources.size()) {
                return false;
            }
            for (int i = 0; i < prefix.resources.size(); i++) {
                if (!prefix.resources.get(i).equals(this..get(i))) {
                    return false;
                }
            }
            return this..equals(prefix.hostname);
        } catch (Exception e) {
            return false;
        }
    }

    

Parameters:
hostname
Returns:
true if is valid host name, else false.
    public static boolean isValidHostname(final String hostname) {
        return isValidID(hostname);
    }

    

Parameters:
resourceName
Returns:
true if is valid resource name, else false.
    public static boolean isValidResourceName(final String resourceName) {
        return isValidID(resourceName);
    }

    

Parameters:
attributeName
Returns:
true if is valid attribute name, else false.
    public static boolean isValidAttributeName(final String attributeName) {
        return isValidID(attributeName);
    }
    private static boolean isValidID(final String atom) {
        return Pattern.matches("[_a-zA-Z][a-zA-Z_0-9]*"atom);
    }

    
Does nothing.

Throws:
MalformedPathException if path is not canonical
    public void expectCanonical() throws MalformedPathException {
        if (!this.isCanonical()) {
            throw new MalformedPathException("Canonical path expected got \'"
                    + this.toString() + "\'");
        }
    }

    
Does nothing.

Throws:
MalformedPathException if path is a pattern
    public void expectFinite() throws MalformedPathException {
        if (this.isPattern()) {
            throw new MalformedPathException("Finite path expected got \'"
                    + this.toString() + "\'");
        }
    }

    
Does nothing.

Throws:
MalformedPathException if path is not absolute
    public void expectAbsolute() throws MalformedPathException {
        if (!this.isAbsolute()) {
            throw new MalformedPathException("Absolute path expected got \'"
                    + this.toString() + "\'");
        }
    }

    
Does nothing.

Throws:
MalformedPathException if path is not relative
    public void expectRelative() throws MalformedPathException {
        if (this.isAbsolute()) {
            throw new MalformedPathException("Relative path expected got \'"
                    + this.toString() + "\'");
        }
    }

    
Does nothing.

Throws:
MalformedPathException if path denotes an attribute
    public void expectResource() throws MalformedPathException {
        if (this.hasAttribute()) {
            throw new MalformedPathException("Resource path expected got \'"
                    + this.toString() + "\'");
        }
    }

    
Does nothing

Throws:
MalformedPathException if path not denotes an attribute
    public void expectAttribute() throws MalformedPathException {
        if (!this.hasAttribute()) {
            throw new MalformedPathException("Attribute path expected got \'"
                    + this.toString() + "\'");
        }
    }
    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        String c = "";
        boolean first = true;
        if (this. != null) {
            c += this. + "://";
        } else if (this.) {
            c += "self://";
        }
        for (String r : this.) {
            if (first) {
                c += r;
                first = false;
            } else {
                c += "/" + r;
            }
        }
        if (this.hasAttribute()) {
            c += "#" + this.;
        }
        return c;
    }
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object == null || !(object instanceof Path)) {
            return false;
        }
        Path path = (Pathobject;
        // Field 'hostname' is only relevant when the path is absolute
        if ( != path.absolute
                ||  && !.equals(path.hostname)) {
            return false;
        }
        return ( == null && path.resources == null
                ||  != null && .equals(path.resources))
                    && ( == null && path.attribute == null
                            ||  != null && .equals(path.attribute));
    }
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 31 * hash + Boolean.valueOf().hashCode();
        hash = 31 * hash + ( && !=null ? .hashCode() : 0);
        hash = 31 * hash + ( != null ? .hashCode() : 0);
        hash = 31 * hash + ( != null ? .hashCode() : 0);
        return hash;
    }
New to GrepCode? Check out our FAQ X