Package entagged.tageditor.exceptions

Examples of entagged.tageditor.exceptions.OperationException


    }

    protected void directoryAction(java.util.List files)
        throws OperationException {
      if (files.size() <= 2)
        throw new OperationException(
            "Not enough files to create a good FreedbID, skipping");

      // Sort the files in this album
      Collections.sort(files, new Comparator() {
        public int compare(Object a, Object b) {
          File fa = (File) a;
          File fb = (File) b;
          return fa.getName().compareToIgnoreCase(fb.getName());
        }
      });

      // Creates the album to be used by freedb
      float[] f = new float[files.size()];
      Iterator it = files.iterator();
      int i = 0;
      while (it.hasNext()) {
        f[i] = ((AudioFile) it.next()).getPreciseLength();
        i++;
      }

      // Connect to freedb
      FreedbReadResult[] fdbresults = null;
      try {
        FreedbQueryResult[] fdbqr = freedb.query(f);

        System.out.println(LangageManager.getProperty(
            "freedb.searchsuccessful").replaceAll("%1",
            new Integer(fdbqr.length).toString()));
        System.out.println("\t"
            + LangageManager.getProperty("freedb.readingresults"));

        int maxResults = (fdbqr.length > MAX_RESULTS) ? MAX_RESULTS
            : fdbqr.length;
        fdbresults = new FreedbReadResult[maxResults];

        for (int h = 0; h < maxResults; h++) {
          fdbresults[h] = freedb.read(fdbqr[h]);
          System.out
              .println("\t"
                  + LangageManager.getProperty(
                      "freedb.resultof").replaceAll("%1",
                      new Integer(h + 1).toString())
                      .replaceAll(
                          "%2",
                          new Integer(fdbqr.length)
                              .toString())
                      .replaceAll(
                          "%3",
                          new Integer((h + 1) * 100
                              / fdbqr.length)
                              .toString()));
        }
      } catch (FreedbException ex) {
        System.out.println(LangageManager
            .getProperty("freedb.noresult"));
        throw new OperationException(ex.getMessage());
      }

      // Write the tag-------------
      // Select the best result, rely on the comparable interface for
      // readresults
      Arrays.sort(fdbresults);
      FreedbReadResult fdbrr = fdbresults[fdbresults.length - 1];

      it = files.iterator();
      i = 0;
      while (it.hasNext()) {
        AudioFile af = (AudioFile) it.next();

        Tag tag = af.getTag();
        tag.setTitle(fdbrr.getTrackTitle(i).trim());
        tag.setAlbum(fdbrr.getAlbum().trim());
        tag.setGenre(fdbrr.getGenre().trim());
        tag.setArtist(fdbrr.getArtist().trim());
        tag.setYear(fdbrr.getYear().trim());
        tag.setComment(fdbrr.getAlbumComment().trim());
        tag.setTrack(new DecimalFormat("00").format(i + 1));

        if (tag instanceof Id3v2Tag)
          tag.setEncoding(PreferencesManager
              .get("entagged.tag.encoding"));

        try {
          AudioFileIO.write(af);
        } catch (CannotWriteException e) {
          throw new OperationException(
              "Album skipped after this file: " + e.getMessage());
        }
        i++;
      }
    }
View Full Code Here


            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());
        }
    }
View Full Code Here

TOP

Related Classes of entagged.tageditor.exceptions.OperationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.