Class PathParser
- java.lang.Object
-
- org.simpleframework.common.parse.Parser
-
- org.simpleframework.http.parse.PathParser
-
- All Implemented Interfaces:
Path
public class PathParser extends Parser implements Path
This is used to parse a path given as part of a URI. This will read the path, normalize it, and break it up into its components. The normalization of the path is the conversion of the path given into it's actual path by removing the references to the parent directories and to the current dir.If the path that this represents is
/usr/bin/../etc/./README
then the actual path, normalized, is/usr/etc/README
. Once the path has been normalized it is possible to acquire the segments as an array of strings, which allows simple manipulation of the path.Although RFC 2396 defines the path within a URI to have parameters this does not extract those parameters this will simply normalize the path and include the path parameters in the path. If the path is to be converted into a OS specific file system path that has the parameters extracted then the
AddressParser
should be used.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
PathParser.Token
This is used so that thePathParser
can speed up the parsing of the data.private class
PathParser.TokenList
TheTokenList
class is used to store a list of tokens.
-
Field Summary
Fields Modifier and Type Field Description private PathParser.Token
dir
Used to store the highest directory path.private PathParser.Token
ext
Used to store consumed file extension.private PathParser.TokenList
list
Used to store the individual path segments.private PathParser.Token
name
Used to store consumed name characters.private PathParser.Token
path
Used to store consumed normalized path name.
-
Constructor Summary
Constructors Constructor Description PathParser()
The default constructor will create aPathParser
that contains no specifics.PathParser(java.lang.String path)
This is primarily a convineance constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
extension()
This will simply read the characters from the end of the buffer until it encounters the first peroid character.java.lang.String
getDirectory()
This will return the highest directory that exists within the path.java.lang.String
getExtension()
This will return the extension that the file name contains.java.lang.String
getName()
This will return the full name of the file without the path.java.lang.String
getPath()
This will return the normalized path.java.lang.String
getPath(int from)
This will return the normalized path from the specified path segment.java.lang.String
getPath(int from, int count)
This will return the normalized path from the specified path segment.private java.lang.String
getRelative(char[] text, int off, int len)
This will return the path as it is relative to the issued path.java.lang.String
getRelative(java.lang.String path)
This will return the path as it is relative to the issued path.private java.lang.String
getRelative(PathParser path)
This is used by thegetRelative(String)
to normalize the path string and determine if it contains a highest directory which is shared with the path that is represented by this object.java.lang.String[]
getSegments()
This method is used to break the path into individual parts called segments, see RFC 2396.protected void
init()
This will initialize the parser so that it is in a ready state.private void
name()
This will extract the full name of the file without the path.private void
normalize()
The normalization of the path is the conversion of the path given into it's actual path by removing the references to the parent directorys and to the current dir.protected void
parse()
This will parse the path in such a way that it ensures that at no stage there are trailing back references, using path normalization.private void
path()
This will extract the path of the givenString
after it has been normalized.private void
segments()
This wil extract each individual segment from the path and also extract the highest directory.java.lang.String
toString()
This will return the normalized path.
-
-
-
Field Detail
-
list
private PathParser.TokenList list
Used to store the individual path segments.
-
name
private PathParser.Token name
Used to store consumed name characters.
-
ext
private PathParser.Token ext
Used to store consumed file extension.
-
dir
private PathParser.Token dir
Used to store the highest directory path.
-
path
private PathParser.Token path
Used to store consumed normalized path name.
-
-
Constructor Detail
-
PathParser
public PathParser()
The default constructor will create aPathParser
that contains no specifics. The instance will returnnull
for all the get methods. ThePathParser
's get methods may be populated by using the parse method.
-
PathParser
public PathParser(java.lang.String path)
This is primarily a convineance constructor. This will parse theString
given to extract the specifics. This could be achived by calling the default no-arg constructor and then using the instance to invoke theparse
method on thatString
to extract the parts.- Parameters:
path
- aString
containing a path value
-
-
Method Detail
-
parse
protected void parse()
This will parse the path in such a way that it ensures that at no stage there are trailing back references, using path normalization. The need to remove the back references is so that thisPathParser
will create the sameString
path given a set of paths that have different back references. For example the paths/path/../path
and/path
are the same path but differentString
's.This will NOT parse an immediate back reference as this signifies a path that cannot exist. So a path such as
/../
will result in a null for all methods. Paths such as../bin
will not be allowed.
-
init
protected void init()
This will initialize the parser so that it is in a ready state. This allows the parser to be used to parse many paths. This will clear the parse buffer objects and reset the offset to point to the start of the char buffer. The count variable is reset by theParser.parse
method.
-
getExtension
public java.lang.String getExtension()
This will return the extension that the file name contains. For example a file namefile.en_US.extension
will produce an extension ofextension
. This will return null if the path contains no file extension.- Specified by:
getExtension
in interfacePath
- Returns:
- this will return the extension this path contains
-
getName
public java.lang.String getName()
This will return the full name of the file without the path. As regargs the definition of the path in RFC 2396 the name would be considered the last path segment. So if the path was/usr/README
the name isREADME
. Also for directorys the name of the directory in the last path segment is returned. This returns the name without any of the path parameters. As RFC 2396 defines the path to have path parameters after the path segments.
-
getPath
public java.lang.String getPath()
This will return the normalized path. The normalized path is the path without any references to its parent or itself. So if the path to be parsed is/usr/../etc/./
the path is/etc/
. If the path that this represents is a path with an immediate back reference then this will return null. This is the path with all its information even the parameter information if it was defined in the path.
-
getPath
public java.lang.String getPath(int from)
This will return the normalized path from the specified path segment. This allows various path parts to be acquired in an efficient means what does not require copy operations of the use ofsubstring
invocations. Of particular interest is the extraction of context based paths. This is the path with all its information even the parameter information if it was defined in the path.
-
getPath
public java.lang.String getPath(int from, int count)
This will return the normalized path from the specified path segment. This allows various path parts to be acquired in an efficient means what does not require copy operations of the use ofsubstring
invocations. Of particular interest is the extraction of context based paths. This is the path with all its information even the parameter information if it was defined in the path.
-
getDirectory
public java.lang.String getDirectory()
This will return the highest directory that exists within the path. This is used to that files within the same path can be acquired. An example of that this would do given the path/pub/./bin/README
would be to return the highest directory path/pub/bin/
. The "/" character will allways be the last character in the path.- Specified by:
getDirectory
in interfacePath
- Returns:
- this method will return the highest directory
-
getSegments
public java.lang.String[] getSegments()
This method is used to break the path into individual parts called segments, see RFC 2396. This can be used as an easy way to compare paths and to examine the directory tree that the path points to. For example, if an path was broken from the string/usr/bin/../etc
then the segments returned would beusr
andetc
as the path is normalized before the segments are extracted.- Specified by:
getSegments
in interfacePath
- Returns:
- return all the path segments within the directory
-
getRelative
public java.lang.String getRelative(java.lang.String path)
This will return the path as it is relative to the issued path. This in effect will chop the start of this path if it's start matches the highest directory of the given path as ofgetDirectory
. This is useful if paths that are relative to a specific location are required. To illustrate what this method will do the following example is provided. If this object represented the path string/usr/share/rfc/rfc2396.txt
and the issued path was/usr/share/text.txt
then this will return the path string/rfc/rfc2396.txt
.- Specified by:
getRelative
in interfacePath
- Parameters:
path
- the path prefix to acquire a relative path- Returns:
- returns a path relative to the one it is given otherwize this method will return null
-
getRelative
private java.lang.String getRelative(PathParser path)
This is used by thegetRelative(String)
to normalize the path string and determine if it contains a highest directory which is shared with the path that is represented by this object. If the path has leading back references, such as../
, then the result of this is null. The returned path begins with a '/'.- Parameters:
path
- the path prefix to acquire a relative path- Returns:
- returns a path relative to the one it is given otherwize this method will return null
-
getRelative
private java.lang.String getRelative(char[] text, int off, int len)
This will return the path as it is relative to the issued path. This in effect will chop the start of this path if it's start matches the highest directory of the given path as ofgetDirectory
. This is useful if paths that are relative to a specific location are required. To illustrate what this method will do the following example is provided. If this object represented the path string/usr/share/rfc/rfc2396.txt
and the issued path was/usr/share/text.txt
then this will return the path string/rfc/rfc2396.txt
.- Parameters:
text
- the path prefix to acquire a relative pathoff
- this is the offset within the text to readlen
- this is the number of characters in the path- Returns:
- returns a path relative to the one it is given otherwize this method will return null
-
path
private void path()
This will extract the path of the givenString
after it has been normalized. If the path can not be normalized then the count is set to -1 and the path cannot be extracted. When this happens then the path parameter isnull
.
-
extension
private void extension()
This will simply read the characters from the end of the buffer until it encounters the first peroid character. When this is read it will store the file extension and remove the characters from the buffer.
-
segments
private void segments()
This wil extract each individual segment from the path and also extract the highest directory. The path segments are basically the strings delimited by the '/' character of a normalized path. As well as extracting the path segments this will also extract the directory of path, that is, the the path up to the last occurance of the '/' character.
-
normalize
private void normalize()
The normalization of the path is the conversion of the path given into it's actual path by removing the references to the parent directorys and to the current dir. So if the path given was/usr/bin/../etc/./README
then the actual path, the normalized path, is/usr/etc/README
.This method ensures the if there are an illegal number of back references that the path will be evaluated as empty. This can evaluate any path configuration, this includes any references like
../
or/..
within the path.
-
name
private void name()
This will extract the full name of the file without the path. As regards the definition of the path in RFC 2396 the name would be considered the last path segment. So if the path was/usr/README
the name isREADME
. Also for directorys the name of the directory in the last path segment is returned. This returns the name without any of the path parameters. As RFC 2396 defines the path to have path parameters after the path segments. So the path for the directory "/usr/bin;param=value/;param=value" would result in the name "bin". If the path given was "/" then there will be nothing in the buffer becauseextract
will have removed it.
-
toString
public java.lang.String toString()
This will return the normalized path. The normalized path is the path without any references to its parent or itself. So if the path to be parsed is/usr/../etc/./
the path is/etc/
. If the path that this represents is a path with an immediate back reference then this will return null. This is the path with all its information even the parameter information if it was defined in the path.
-
-