int filei = filename.lastIndexOf(fields[i][0]);
int diri = directory.lastIndexOf(fields[i][0]);
if ((filename.indexOf(fields[i][0]) != filei)
|| (directory.indexOf(fields[i][0]) != diri)) {
throw new OperationException("Cannot have two \""
+ fields[i][0] + "\" masks in the same pattern");
}
if (filei != -1 && diri != -1) {
throw new OperationException("Cannot have two \""
+ fields[i][0]
+ "\" masks in both filename and directory pattern");
}
filenameHash.put(new Integer(filei), new Integer(i));
directoryHash.put(new Integer(diri), new Integer(i));
}
directory = replaceReservedRegexCharacters(directory);
filename = replaceReservedRegexCharacters(filename);
for (int i = 0; i < fields.length; i++) {
if (fields[i][0].equals(IGNORE_MASK)) {
filename = filename.replaceAll(fields[i][0], "(.+)");
directory = directory.replaceAll(fields[i][0], "(.+)");
} else {
filename = filename.replaceAll(fields[i][0], "(.+?)");
directory = directory.replaceAll(fields[i][0], "(.+?)");
}
}
filename = filename + "." + extension;
if (directory.endsWith(File.separator))
directory = directory.substring(0, directory.length()
- File.separator.length());
// Filename pattern matching ------------------------------------------
System.out.println("Matching:\n" + f.getName() + "\nagainst:\n"
+ filename);
Pattern pa = Pattern.compile(filename);
Matcher ma = pa.matcher(f.getName().toLowerCase());
int length = 0;
if (ma.matches()) {
for (int i = 1; i <= ma.groupCount(); i++) {
// we can find a group that is not in the hashtable, if the user
// entered %1-%1, for ex.
// We work only with multiple ignore masks, not other..
Integer pos = (Integer) filenameHash.get(new Integer(ma
.start(i)
+ length));
if (pos != null) {
fields[pos.intValue()][1] = ma.group(i);
System.out.println("Match " + i + ": " + ma.group(i));
length = length - ma.group(i).length()
+ fields[pos.intValue()][0].length();
} else {
// It was an ignore mask ok, or a double mask then we will
// fail
length = length - ma.group(i).length()
+ fields[7][0].length();
}
}
} else
throw new OperationException(LangageManager
.getProperty("opex.fpattern"));
// Filename matching finished
// ---------------------------------------------------------------------
// Directory name pattern
// matching--------------------------------------------------------------
System.out.println("Matching:\n" + f.getParentFile().getPath()
+ "\nagainst:\n" + directory);
pa = Pattern.compile(directory);
ma = pa.matcher(f.getParent().toLowerCase());
length = 0;
if (ma.matches()) {
for (int i = 1; i <= ma.groupCount(); i++) {
// we can find a group that is not in the hashtable, if the user
// entered %1-%1, for ex.
// We work only with multiple ignore masks, not other..
Integer pos = (Integer) directoryHash.get(new Integer(ma
.start(i)
+ length));
if (pos != null) {
fields[pos.intValue()][1] = ma.group(i);
System.out.println("Match " + i + ": " + ma.group(i));
length = length - ma.group(i).length()
+ fields[pos.intValue()][0].length();
} else {
// It was an ignore mask ok, or a double mask then we will
// fail
length = length - ma.group(i).length()
+ fields[7][0].length();
}
}
} else
throw new OperationException(LangageManager
.getProperty("opex.dpattern"));
// Directory matching finished
// ---------------------------------------------------------------------
// !!! Here we have to respect to order we defined in our field to know
// Wich fields correspond to wich index i
/*
* We defined it like that String[][] fields = { { ARTIST_MASK , ""}, {
* ALBUM_MASK , ""}, { TITLE_MASK , ""}, { TRACK_MASK , ""}, {
* GENRE_MASK , ""}, { YEAR_MASK , ""}, { COMMENT_MASK , ""} };
*/
Tag tag = f.getTag();
for (int i = 0; i < fields.length; i++) {
String s = fields[i][1].trim();
if (!s.equals("")) {
s = transformSet.transform(s);
switch (i) {
case 0:
tag.setArtist(s);
break;
case 1:
tag.setAlbum(s);
break;
case 2:
tag.setTitle(s);
break;
case 3:
tag.setTrack(s);
break;
case 4:
tag.setGenre(s);
break;
case 5:
tag.setYear(s);
break;
case 6:
tag.setComment(s);
break;
case 7:
break; // Ignore
}
}
}
transformSet.transformFirstCommons(tag);
if(tag instanceof Id3v2Tag)
tag.setEncoding(PreferencesManager.get("entagged.tag.encoding"));
try {
AudioFileIO.write(f);
} catch (CannotWriteException e) {
throw new OperationException(e.getMessage());
}
}