/*
-* Common
-*
-* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
-*
-* Contact:
-* BonYong Lee <bonyong.lee@samsung.com>
-*
-* Licensed 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.
-*
-* Contributors:
-* - S-Core Co., Ltd
-*
-*/
+ * Common
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * BonYong Lee <bonyong.lee@samsung.com>
+ *
+ * Licensed 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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
package org.tizen.common.util;
import static org.tizen.common.util.ArrayUtil.size;
*/
public class FilenameUtil
{
- /**
- * character using separator
- */
- public static final char SEPARATOR_DIRECTORY = '/';
- /**
- * result of {@link #isVaildName(String)} and {@link #isVaildName(String, int)
- */
- public static final int IS_VALID_NAME = 0;
- public static final int HAS_INVALID_CHARACTER = 1;
- public static final int HAS_NO_NAME = 2;
- public static final int IS_NULL = 3;
-
- protected static final Logger logger = LoggerFactory.getLogger( FilenameUtil.class );
-
- /**
- * Separate path to directory name fragements
- *
- * If you want to use '\' in file name for windows, use quote mark( ' ).
- *
- * @param path file path
- *
- * @return canonical path's fragements
- */
- public static
- String[]
- getCanonicalFragments(
- final String path
- )
- {
- final char CH_ESCAPE = '\\';
- final char CH_QUOTE = '\'';
-
- final int ST_NORMAL = 0;
- final int ST_READY_TO_ESCAPE =2;
- final int ST_QUOTE = 3;
-
- final ArrayList<String> ret = new ArrayList<String>();
- char[] characters = path.toCharArray();
-
- StringBuilder buffer = new StringBuilder();
- int status = ST_NORMAL;
- for ( int i = 0, n = characters.length ; i < n ; ++i )
- {
- final char ch = characters[i];
- switch ( status )
+ /**
+ * character using separator
+ */
+ public static final char SEPARATOR_DIRECTORY = '/';
+ /**
+ * result of {@link #isVaildName(String)} and {@link #isVaildName(String, int)
+ */
+ public static final int IS_VALID_NAME = 0;
+ public static final int HAS_INVALID_CHARACTER = 1;
+ public static final int HAS_NO_NAME = 2;
+ public static final int IS_NULL = 3;
+
+ protected static final Logger logger = LoggerFactory.getLogger( FilenameUtil.class );
+
+ /**
+ * Separate path to directory name fragements
+ *
+ * If you want to use '\' in file name for windows, use quote mark( ' ).
+ *
+ * @param path file path
+ *
+ * @return canonical path's fragements
+ */
+ public static
+ String[]
+ getCanonicalFragments(
+ final String path
+ )
+ {
+ final char CH_ESCAPE = '\\';
+ final char CH_QUOTE = '\'';
+
+ final int ST_NORMAL = 0;
+ final int ST_READY_TO_ESCAPE =2;
+ final int ST_QUOTE = 3;
+
+ final ArrayList<String> ret = new ArrayList<String>();
+ char[] characters = path.toCharArray();
+
+ StringBuilder buffer = new StringBuilder();
+ int status = ST_NORMAL;
+ for ( int i = 0, n = characters.length ; i < n ; ++i )
+ {
+ final char ch = characters[i];
+ switch ( status )
{
- case ST_NORMAL:
- if ( SEPARATOR_DIRECTORY == ch || File.separatorChar == ch )
- {
- final String fragment = buffer.toString();
- if ( !isEmpty( fragment ) )
+ case ST_NORMAL:
+ if ( SEPARATOR_DIRECTORY == ch || File.separatorChar == ch )
{
- ret.add( fragment );
+ final String fragment = buffer.toString();
+ if ( !isEmpty( fragment ) )
+ {
+ ret.add( fragment );
+ }
+ buffer.delete( 0, buffer.length() );
+ }
+ else if ( CH_QUOTE == ch )
+ {
+ status = ST_QUOTE;
+ }
+ else
+ {
+ buffer.append( ch );
+ }
+ break;
+ case ST_READY_TO_ESCAPE:
+ if ( CH_QUOTE != ch )
+ {
+ buffer.append( CH_ESCAPE );
+ buffer.append( ch );
}
- buffer.delete( 0, buffer.length() );
- }
- else if ( CH_QUOTE == ch )
- {
status = ST_QUOTE;
- }
- else
- {
- buffer.append( ch );
- }
- break;
- case ST_READY_TO_ESCAPE:
- if ( CH_QUOTE != ch )
- {
- buffer.append( CH_ESCAPE );
- buffer.append( ch );
- }
- status = ST_QUOTE;
- break;
- case ST_QUOTE:
- if ( CH_QUOTE == ch )
- {
- status = ST_NORMAL;
- }
- else if ( CH_ESCAPE == ch )
- {
- status = ST_READY_TO_ESCAPE;
- }
- else
- {
- buffer.append( ch );
- }
- break;
+ break;
+ case ST_QUOTE:
+ if ( CH_QUOTE == ch )
+ {
+ status = ST_NORMAL;
+ }
+ else if ( CH_ESCAPE == ch )
+ {
+ status = ST_READY_TO_ESCAPE;
+ }
+ else
+ {
+ buffer.append( ch );
+ }
+ break;
- default:
- break;
+ default:
+ break;
}
- }
-
- if ( 0 < buffer.length() )
- {
+ }
+
+ if ( 0 < buffer.length() )
+ {
final String fragment = buffer.toString();
if ( !isEmpty( fragment ) )
{
ret.add( fragment );
}
buffer.delete( 0, buffer.length() );
- }
-
- return ret.toArray( new String[0] );
- }
-
- /**
- * return tailing path in <code>path</code> from <code>start</code>
- *
- * @param path file path
- * @param start the number of path fragment
- *
- * @return result file path fragments
- */
- public static
- String
- getTailingPath(
- final String path,
- final int start
- )
- {
- final String[] fragments = getCanonicalFragments( path );
-
- final StringBuilder buffer = new StringBuilder();
-
- for ( int i = Math.max( 0, size( fragments ) - start ), n = size( fragments ) ; i < n ; ++i )
- {
- buffer.append( fragments[i] );
- if ( i < size( fragments ) - 1 )
- {
- buffer.append( SEPARATOR_DIRECTORY );
- }
- }
-
- return buffer.toString();
- }
-
- /**
- * remove <code>numberOfPath</code> path fragments from <code>path</code>
- * @param path file path
- * @param numberOfPath the number of path fragment to remove
- *
- * @return result file path
- */
- public static
- String
- removeTailingPath(
- final String path,
- final int numberOfPath
- )
- {
- logger.trace( "Path :{}, Index :{}", path, numberOfPath );
- final String[] fragments = getCanonicalFragments( path );
-
- final StringBuilder buffer = new StringBuilder();
-
- for ( int i = 0, n = Math.max( 0, size( fragments ) - numberOfPath ) ; i < n ; ++i )
- {
- buffer.append( SEPARATOR_DIRECTORY );
- buffer.append( fragments[i] );
- }
-
- final String ret = buffer.toString();
- if ( 0 < ret.length() )
- {
- return buffer.toString().substring( path.startsWith( "/" )||path.startsWith( File.separator )?0:1 );
- }
-
- return "";
- }
-
- /**
- * concatenate <code>path</code> and <code>pathToAdd</code>
- *
- * @param path parent path
- * @param pathToAdd path fragment to add
- *
- * @return result file path
- */
- public static
- String
- addTailingPath(
- final String path,
- final String pathToAdd
- )
- {
- // TODO Verify canonical form
- return trimTrailingCharacter( path, SEPARATOR_DIRECTORY ) + SEPARATOR_DIRECTORY + trimLeadingCharacter( pathToAdd, SEPARATOR_DIRECTORY );
- }
-
- /**
- * return value if <code>root</code> contains <code>filePath</code> as descendant
- *
- * @param root file path
- * @param filePath file path
- *
- * @return boolean value to check
- */
- public static
- boolean
- isAncestor( final String root, final String filePath )
- {
- final String[] rootFragments = FilenameUtil.getCanonicalFragments( root );
- final String[] fileFragments = FilenameUtil.getCanonicalFragments( filePath );
-
- if ( fileFragments.length < rootFragments.length )
- {
- return false;
- }
-
- for ( int i = 0, n = rootFragments.length ; i < n ; ++i )
- {
- if ( !ObjectUtil.equals( rootFragments[i], fileFragments[i] ) )
- {
- return false;
- }
- }
-
-
- return true;
- }
-
+ }
+
+ return ret.toArray( new String[0] );
+ }
+
+ /**
+ * return tailing path in <code>path</code> from <code>start</code>
+ *
+ * @param path file path
+ * @param start the number of path fragment
+ *
+ * @return result file path fragments
+ */
+ public static
+ String
+ getTailingPath(
+ final String path,
+ final int start
+ )
+ {
+ final String[] fragments = getCanonicalFragments( path );
+
+ final StringBuilder buffer = new StringBuilder();
+
+ for ( int i = Math.max( 0, size( fragments ) - start ), n = size( fragments ) ; i < n ; ++i )
+ {
+ buffer.append( fragments[i] );
+ if ( i < size( fragments ) - 1 )
+ {
+ buffer.append( SEPARATOR_DIRECTORY );
+ }
+ }
+
+ return buffer.toString();
+ }
+
+ /**
+ * remove <code>numberOfPath</code> path fragments from <code>path</code>
+ * @param path file path
+ * @param numberOfPath the number of path fragment to remove
+ *
+ * @return result file path
+ */
+ public static
+ String
+ removeTailingPath(
+ final String path,
+ final int numberOfPath
+ )
+ {
+ logger.trace( "Path :{}, Index :{}", path, numberOfPath );
+ final String[] fragments = getCanonicalFragments( path );
+
+ final StringBuilder buffer = new StringBuilder();
+
+ for ( int i = 0, n = Math.max( 0, size( fragments ) - numberOfPath ) ; i < n ; ++i )
+ {
+ buffer.append( SEPARATOR_DIRECTORY );
+ buffer.append( fragments[i] );
+ }
+
+ final String ret = buffer.toString();
+ if ( 0 < ret.length() )
+ {
+ return buffer.toString().substring( path.startsWith( "/" )||path.startsWith( File.separator )?0:1 );
+ }
+
+ return "";
+ }
+
+ /**
+ * concatenate <code>path</code> and <code>pathToAdd</code>
+ *
+ * @param path parent path
+ * @param pathToAdd path fragment to add
+ *
+ * @return result file path
+ */
+ public static
+ String
+ addTailingPath(
+ final String path,
+ final String pathToAdd
+ )
+ {
+ // TODO Verify canonical form
+ return trimTrailingCharacter( path, SEPARATOR_DIRECTORY ) + SEPARATOR_DIRECTORY + trimLeadingCharacter( pathToAdd, SEPARATOR_DIRECTORY );
+ }
+
+ /**
+ * return value if <code>root</code> contains <code>filePath</code> as descendant
+ *
+ * @param root file path
+ * @param filePath file path
+ *
+ * @return boolean value to check
+ */
+ public static
+ boolean
+ isAncestor( final String root, final String filePath )
+ {
+ final String[] rootFragments = FilenameUtil.getCanonicalFragments( root );
+ final String[] fileFragments = FilenameUtil.getCanonicalFragments( filePath );
+
+ if ( fileFragments.length < rootFragments.length )
+ {
+ return false;
+ }
+
+ for ( int i = 0, n = rootFragments.length ; i < n ; ++i )
+ {
+ if ( !ObjectUtil.equals( rootFragments[i], fileFragments[i] ) )
+ {
+ return false;
+ }
+ }
+
+
+ return true;
+ }
+
/**
* return relative path of <code>filePath</code> based <code>root</code>
*
public static
String
getRelativePath(
- final String root,
- final String filePath
- )
+ final String root,
+ final String filePath
+ )
{
final String[] rootFragments = FilenameUtil.getCanonicalFragments( root );
final String[] fileFragments = FilenameUtil.getCanonicalFragments( filePath );
final StringBuilder buffer = new StringBuilder();
int nLoop = (nRoot < nFile)
- ? nRoot : nFile;
+ ? nRoot : nFile;
int nStartDiffer = nLoop;
return buffer.toString();
}
- /**
- * Create canonical form for file path
- *
- * @param file {@link File} with file path
- *
- * @return canonical path
- *
- * @throws IOException If file's path is illegal
- */
- public static
- String
- getCanonicalPath(
- final File file
- )
- throws IOException
- {
- final String osCanonicalPath = file.getCanonicalPath();
-
- return getCanonicalForm( osCanonicalPath );
-
- }
-
- /**
- * Create canonical form for file path
- *
- * @param file path
- *
- * @return canonical path, <code>null</code> if wrong path
- *
- * @throws IOException If file's path is illegal
- *
- * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
- */
- public static
- String
- getCanonicalPath(
- final String path
- )
- throws IOException
- {
- if ( !FileUtil.isExist( path ) ) {
- return null;
- }
- File file = new File( path );
-
- return getCanonicalPath( file );
-
- }
-
- /**
- * Convert path to canonical form
- *
- * @param path file path
- *
- * @return canonical path
- */
- public static
- String
- getCanonicalForm(
- final String path
- )
- {
- logger.trace( "Path :{}", path );
- final String[] fragments = getCanonicalFragments( path.replace( File.separatorChar, SEPARATOR_DIRECTORY ) );
-
- final Stack<String> stack = new Stack<String>();
-
- for ( int i = 0, n = size( fragments ) ; i<n ; ++i )
- {
- final String fragment = fragments[i];
- logger.trace( "Path :{}", fragment );
- if ( ".".equals( fragment ) )
- {
- continue;
- }
- else if ( "..".equals( fragment ) )
- {
- if (stack.isEmpty()) {
- continue;
- }
- stack.pop();
- }
- else
- {
- stack.push( fragment );
- }
- }
-
- if ( stack.isEmpty() )
- {
- return "" + SEPARATOR_DIRECTORY;
- }
- final StringBuilder buffer = new StringBuilder();
-
- for ( final String f : stack )
- {
- buffer.append( "/" );
- buffer.append( f );
- }
-
- return buffer.toString();
- }
-
- public static String getFilename( final String path )
- {
- return getTailingPath( path, 1 );
- }
-
+ /**
+ * Create canonical form for file path
+ *
+ * @param file {@link File} with file path
+ *
+ * @return canonical path
+ *
+ * @throws IOException If file's path is illegal
+ */
+ public static
+ String
+ getCanonicalPath(
+ final File file
+ )
+ throws IOException
+ {
+ final String osCanonicalPath = file.getCanonicalPath();
+
+ return getCanonicalForm( osCanonicalPath );
+
+ }
+
+ /**
+ * Create canonical form for file path
+ *
+ * @param file path
+ *
+ * @return canonical path, <code>null</code> if wrong path
+ *
+ * @throws IOException If file's path is illegal
+ *
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ */
+ public static
+ String
+ getCanonicalPath(
+ final String path
+ )
+ throws IOException
+ {
+ if ( !FileUtil.isExist( path ) ) {
+ return null;
+ }
+ File file = new File( path );
+
+ return getCanonicalPath( file );
+
+ }
+
+ /**
+ * Convert path to canonical form
+ *
+ * @param path file path
+ *
+ * @return canonical path
+ */
+ public static
+ String
+ getCanonicalForm(
+ final String path
+ )
+ {
+ logger.trace( "Path :{}", path );
+ final String[] fragments = getCanonicalFragments( path.replace( File.separatorChar, SEPARATOR_DIRECTORY ) );
+
+ final Stack<String> stack = new Stack<String>();
+
+ for ( int i = 0, n = size( fragments ) ; i<n ; ++i )
+ {
+ final String fragment = fragments[i];
+ logger.trace( "Path :{}", fragment );
+ if ( ".".equals( fragment ) )
+ {
+ continue;
+ }
+ else if ( "..".equals( fragment ) )
+ {
+ if (stack.isEmpty()) {
+ continue;
+ }
+ stack.pop();
+ }
+ else
+ {
+ stack.push( fragment );
+ }
+ }
+
+ if ( stack.isEmpty() )
+ {
+ return "" + SEPARATOR_DIRECTORY;
+ }
+ final StringBuilder buffer = new StringBuilder();
+
+ for ( final String f : stack )
+ {
+ buffer.append( "/" );
+ buffer.append( f );
+ }
+
+ return buffer.toString();
+ }
+
+ public static String getFilename( final String path )
+ {
+ return getTailingPath( path, 1 );
+ }
+
/**
* Returns an escaped name of the file name for linux.
*
return "\""+ str + "\"";
}
- /**
- * Extract file name without extension from full name
- *
- * @param nameWithExt file name with extension
- *
- * @return file name
- */
- public static String getName( final String nameWithExt )
- {
- Assert.notNull( nameWithExt );
-
- final int index = nameWithExt.lastIndexOf( '.' );
-
- if ( index < 0 )
- {
- return nameWithExt;
- }
-
- return nameWithExt.substring( 0, index );
- }
-
- /**
- * Extract file extension from full name
- *
- * @param nameWithExt file name with extension
- *
- * @return file extension
- */
- public static String getExtension( final String nameWithExt )
- {
- Assert.notNull( nameWithExt );
-
- final int index = nameWithExt.lastIndexOf( '.' );
-
- if ( index < 0 )
- {
- return "";
- }
-
- return nameWithExt.substring( index + 1 );
- }
-
- /**
- * Compare two file path with canonical
- *
- * @param file1 file1 to compare
- * @param file2 file2 to compare
- *
- * @return <code>true</code> if <code>file1</code> and <code>file2</code> have equal path
- */
- public static boolean equals( final File file1, final File file2 )
- {
- try
- {
- return ObjectUtil.equals(
- getCanonicalPath( file1 ),
- getCanonicalPath( file2 )
- );
- } catch ( final IOException e )
- {
- throw new IllegalArgumentException( e );
- }
- }
-
-
- /**
- * Compare <code>path1</code> and <code>path2</code> and return equality
- *
- * @param path1 file path1
- * @param path2 file path2
- *
- * @return equality
- */
- public static boolean equals( final String path1, final String path2 )
- {
- return ObjectUtil.equals( path1, path2 );
- }
-
- /**
- * Compares {@code name}'s extension and {@code ext}.
- * @param name file name
- * @param ext comparing extension
- * @return
- */
- public static boolean hasExtension(String name, String ext) {
- if (isEmpty(name)) {
- return false;
- }
- return getExtension(name).equalsIgnoreCase(ext);
- }
+ /**
+ * Extract file name without extension from full name
+ *
+ * @param nameWithExt file name with extension
+ *
+ * @return file name
+ */
+ public static String getName( final String nameWithExt )
+ {
+ Assert.notNull( nameWithExt );
+
+ final int index = nameWithExt.lastIndexOf( '.' );
+
+ if ( index < 0 )
+ {
+ return nameWithExt;
+ }
+
+ return nameWithExt.substring( 0, index );
+ }
+
+ /**
+ * Extract file extension from full name
+ *
+ * @param nameWithExt file name with extension
+ *
+ * @return file extension
+ */
+ public static String getExtension( final String nameWithExt )
+ {
+ Assert.notNull( nameWithExt );
+
+ final int index = nameWithExt.lastIndexOf( '.' );
+
+ if ( index < 0 )
+ {
+ return "";
+ }
+
+ return nameWithExt.substring( index + 1 );
+ }
+
+ /**
+ * Compare two file path with canonical
+ *
+ * @param file1 file1 to compare
+ * @param file2 file2 to compare
+ *
+ * @return <code>true</code> if <code>file1</code> and <code>file2</code> have equal path
+ */
+ public static boolean equals( final File file1, final File file2 )
+ {
+ try
+ {
+ return ObjectUtil.equals(
+ getCanonicalPath( file1 ),
+ getCanonicalPath( file2 )
+ );
+ } catch ( final IOException e )
+ {
+ throw new IllegalArgumentException( e );
+ }
+ }
+
+
+ /**
+ * Compare <code>path1</code> and <code>path2</code> and return equality
+ *
+ * @param path1 file path1
+ * @param path2 file path2
+ *
+ * @return equality
+ */
+ public static boolean equals( final String path1, final String path2 )
+ {
+ return ObjectUtil.equals( path1, path2 );
+ }
+
+ /**
+ * Compares {@code name}'s extension and {@code ext}.
+ * @param name file name
+ * @param ext comparing extension
+ * @return
+ */
+ public static boolean hasExtension(String name, String ext) {
+ if (isEmpty(name)) {
+ return false;
+ }
+ return getExtension(name).equalsIgnoreCase(ext);
+ }
}