FileDocCategorySizeDatePackage
RenamingFileFilter.javaAPI DocMyID3 for Android5274Tue Oct 07 11:15:24 BST 2008org.cmc.music.clean

RenamingFileFilter

public abstract class RenamingFileFilter extends Object implements org.cmc.music.util.FileFilter
author
charles To change the template for this generated type comment go to Window>Preferences>Java>Code Generation>Code and Comments

Fields Summary
Constructors Summary
Methods Summary
private voiddoRenaming(java.io.File file, java.lang.String new_name)


		System.out.println("renaming: '" + file.getName() + "' to '" + new_name
				+ "' at: " + file.getParent());

		File file2 = (new File(file.getParentFile(), new_name));

		if (file.getName().toLowerCase().equals(file2.getName().toLowerCase()))
		{
			try
			{
				File temp = new File(file.getParentFile(), file.getName()
						+ ".tmp");

				/*
				 * File.createTempFile( file2.getName(), ".tmp",
				 * file.getParentFile());
				 */

				//				System.out.println("temp: " + temp.getAbsolutePath());
				if (!file.renameTo(temp))
					System.out.println("rename failed file->temp: "
							+ temp.getAbsolutePath());
				if (!temp.renameTo(file2))
					System.out.println("rename failed temp->file2: "
							+ temp.getAbsolutePath());
			}
			catch (Exception e)
			{
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		else
		{
			if (file2.exists())
			{
				System.out.println("could not rename: already exists");
				FileComparator fc = new FileComparator();
				boolean exact = fc.compare(file, file2);
				if (exact)
				{
					String options[] = {
							"Delete A", "Delete B", "Ignore",
					};
					String msg = "\n" + "Identical files found in same folder:"
							+ "\n" + "" + "\n" + "A: '"
							+ file.getAbsolutePath() + "'" + "\n" + "B: '"
							+ file2.getAbsolutePath() + "'" + "\n" + "" + "\n"
							+ "";
					int choice = 0;
					{
						if (choice == 0)
						{
							System.out.println("delete left");
							file.delete();
						}
						else if (choice == 1)
						{
							System.out.println("delete right");
							file2.delete();
						}
					}
				}
			}
			else
				file.renameTo(file2);
		}

	
protected java.lang.StringgetExtension(java.lang.String s)

		int index = s.lastIndexOf('.");
		if (index < 0)
			return null;

		return s.substring(index);
	
protected java.lang.StringgetExtensionNonNull(java.lang.String s)

		String result = getExtension(s);
		if (result == null)
			return "";
		return result;
	
protected abstract java.lang.StringgetNewName2(java.io.File file, java.lang.String s)

protected java.lang.StringinsertSpacesBeforeCaps(java.lang.String s)

		StringBuffer result = new StringBuffer();

		char prev = 0;
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);

			if (Character.isLetter(c) && (i > 0) && Character.isLetter(prev)
					&& Character.isLowerCase(prev) && Character.isUpperCase(c))
				result.append(' ");

			result.append(c);

			prev = c;
		}

		return result.toString();
	
protected booleanisMusicFile(java.io.File file)

		if (file.isDirectory())
			return false;

		String s = file.getName().toLowerCase();
		return (s.endsWith(".mp3") | s.endsWith(".ogg"));
	
public booleanprocess(java.io.File file)

		if (!file.isDirectory() && !isMusicFile(file))
			return false;

		final String name = file.getName();
		String name2 = null;
		if (file.isDirectory())
		{
			name2 = getNewName2(file, name);
		}
		else
		{
			String ext = getExtension(name);
			String stripped = stripExtension(name);
			name2 = getNewName2(file, stripped);
			if (name2 != null)
				name2 = name2 + ext.toLowerCase();
		}





		if ((name2 != null) && (name2.length() > 0) && (!name.equals(name2)))
		{
			doRenaming(file, name2);
			return true;
		}
		return false;
	
protected java.lang.StringstripExtension(java.lang.String s)

		int index = s.lastIndexOf('.");
		if (index < 0)
			return s;

		return s.substring(0, index);
	
protected java.lang.StringtoTitleCase(java.lang.String s)

		StringBuffer result = new StringBuffer();
		char prev = 0;
		//		if (s.startsWith("The Jackson 5 - I'Ll Be There"))
		//			System.out.println(s);

		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			//			char next = 0;
			//			if ((i + 1) < s.length())
			//				next = s.charAt(i + 1);

			if (Character.isLetter(c))
			{
				if (i == 0)
					result.append(Character.toUpperCase(c));
				else if ((prev == '\'"))
				{
					if (i > 1 && Character.isLetter(s.charAt(i - 2)))
						result.append(Character.toLowerCase(c));
					else
						result.append(Character.toUpperCase(c));
				}
				else if (!Character.isLetter(prev))
					result.append(Character.toUpperCase(c));
				else
					result.append(Character.toLowerCase(c));
			}
			else
				result.append(c);
			prev = c;
		}



		return result.toString();