Android Open Source - PlayMusicExporter Music Path Builder






From Project

Back to project page PlayMusicExporter.

License

The source code is released under:

Copyright (c) 2015 David Schulte Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Soft...

If you think the Android project PlayMusicExporter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2015 David Schulte//from w  w  w  .  ja  va 2s .  com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.arcus.playmusicexporter2.utils;

import android.text.TextUtils;

import de.arcus.framework.logger.Logger;
import de.arcus.playmusiclib.items.MusicTrack;

/**
 * Helper class te create a path by a user defined structure
 */
public class MusicPathBuilder {
    /**
     * Hides the constructor
     */
    private MusicPathBuilder() {}

    /**
     * Generates a path from a user defined patter
     * @param musicTrack The music track data
     * @param patter The patter
     * @return Returns the file path
     */
    public static String Build(MusicTrack musicTrack, String patter)
    {
        String path = "";
        int pos = 0;

        // While there is an open tag
        while (patter.indexOf('{', pos) >= 0)
        {
            // Gets the start and the end of the tag
            int posStart = patter.indexOf('{', pos);
            int posEnd = patter.indexOf('}', posStart);

            // Gets the equal sign
            int posEqual = patter.indexOf('=', posStart);

            // Adds the part between this tag and the last one to the path
            path += patter.substring(pos, posStart);

            if (posEnd >= 0) {
                // Name of the tag
                String name;
                String value = "";

                // There is an equal sign
                if (posEqual >= 0 && posEqual < posEnd) {
                    name = patter.substring(posStart + 1, posEqual);
                } else {
                    name = patter.substring(posStart + 1, posEnd);
                }

                // Trim and lower
                name = name.trim().toLowerCase();

                // Gets the values
                switch (name) {
                    case "album-artist":
                        if (!TextUtils.isEmpty(musicTrack.getAlbumArtist()))
                            value = musicTrack.getAlbumArtist();
                        break;
                    case "album":
                        if (!TextUtils.isEmpty(musicTrack.getAlbum()))
                            value = musicTrack.getAlbum();
                        break;
                    case "group":
                        if (!TextUtils.isEmpty(musicTrack.getContainerName()))
                            value = musicTrack.getContainerName();
                        break;
                    case "artist":
                        if (!TextUtils.isEmpty(musicTrack.getArtist()))
                            value = musicTrack.getArtist();
                        break;
                    case "title":
                        if (!TextUtils.isEmpty(musicTrack.getTitle()))
                            value = musicTrack.getTitle();
                        break;
                    case "disc":
                        if (musicTrack.getDiscNumber() > 0)
                            value = String.valueOf(musicTrack.getDiscNumber());
                        break;
                    case "no":
                        if (musicTrack.getTrackNumber() > 0)
                            value = String.valueOf(musicTrack.getTrackNumber());
                        break;
                    case "year":
                        if (!TextUtils.isEmpty(musicTrack.getYear()))
                            value = musicTrack.getYear();
                        break;
                    case "genre":
                        if (!TextUtils.isEmpty(musicTrack.getGenre()))
                            value = musicTrack.getGenre();
                        break;
                    default:
                        // Unknown tag
                        Logger.getInstance().logWarning("MusicPathBuilder", "Unknown tag '" + name + "'");
                        break;
                }

                // Equal sign exists
                if (posEqual >= 0 && posEqual < posEnd && !TextUtils.isEmpty(value)) {
                    String format = patter.substring(posEqual + 1, posEnd);

                    // Gets the insert sign
                    int posInsertStart = format.indexOf('$');
                    if (posInsertStart >= 0) {

                        int posInsertEnd = posInsertStart + 1;
                        // Search the end
                        while(posInsertEnd < format.length() && format.charAt(posInsertEnd) == '$') {
                            posInsertEnd ++;
                        }

                        // Fill Zeros
                        while(value.length() < posInsertEnd - posInsertStart) {
                            value = "0" + value;
                        }

                        // Adds the rest of the format to the value
                        value = format.substring(0, posInsertStart) + value + format.substring(posInsertEnd);

                    } else {
                        // Missing insert sign
                        Logger.getInstance().logWarning("MusicPathBuilder", "Cloud not find replace symbol ('$') of format attribute in tag '" + name + "'");
                    }
                }

                // Adds the value
                path += cleanFilename(value);

                pos = posEnd + 1;
            } else {
                path += "{";
                pos = posStart + 1;
                Logger.getInstance().logWarning("MusicPathBuilder", "Cloud not find end symbol ('}') of the tag in patter '" + patter + "'");
            }
        }

        // Insert end
        path += patter.substring(pos, patter.length());

        // Remove double slash
        while(path.contains("//"))
            path = path.replace("//", "/");

        // Return path
        return path;
    }

    /**
     * Removes forbidden chars in the filename
     * @param filename The filename
     * @return Returns the new clean filename
     */
    public static String cleanFilename(String filename)
    {
        // Forbidden chars
        filename = filename.replace('\\', '-');
        filename = filename.replace(':', '-');
        filename = filename.replace('*', '-');
        filename = filename.replace('?', '-');
        filename = filename.replace('"', '-');
        filename = filename.replace('<', '-');
        filename = filename.replace('>', '-');
        filename = filename.replace('|', '-');

        return filename;
    }
}




Java Source Code List

de.arcus.framework.ApplicationTest.java
de.arcus.framework.crashhandler.CrashActivity.java
de.arcus.framework.crashhandler.CrashHandler.java
de.arcus.framework.logger.Logger.java
de.arcus.framework.settings.AppSettings.java
de.arcus.framework.superuser.SuperUserCommandCallback.java
de.arcus.framework.superuser.SuperUserCommand.java
de.arcus.framework.superuser.SuperUserTools.java
de.arcus.framework.superuser.SuperUser.java
de.arcus.framework.utils.FileTools.java
de.arcus.framework.utils.MediaScanner.java
de.arcus.playmusicexporter2.ApplicationTest.java
de.arcus.playmusicexporter2.activitys.MusicTrackDetailActivity.java
de.arcus.playmusicexporter2.activitys.MusicTrackListActivity.java
de.arcus.playmusicexporter2.adapter.MusicTrackAdapter.java
de.arcus.playmusicexporter2.adapter.MusicTrackListAdapter.java
de.arcus.playmusicexporter2.fragments.MusicTrackDetailFragment.java
de.arcus.playmusicexporter2.fragments.MusicTrackListFragment.java
de.arcus.playmusicexporter2.fragments.NavigationDrawerFragment.java
de.arcus.playmusicexporter2.utils.ImageViewLoader.java
de.arcus.playmusicexporter2.utils.MusicPathBuilder.java
de.arcus.playmusiclib.AllAccessExporter.java
de.arcus.playmusiclib.ApplicationTest.java
de.arcus.playmusiclib.PlayMusicManager.java
de.arcus.playmusiclib.datasources.AlbumDataSource.java
de.arcus.playmusiclib.datasources.ArtistDataSource.java
de.arcus.playmusiclib.datasources.DataSource.java
de.arcus.playmusiclib.datasources.MusicTrackDataSource.java
de.arcus.playmusiclib.datasources.PlaylistDataSource.java
de.arcus.playmusiclib.enums.ID3v2Version.java
de.arcus.playmusiclib.exceptions.CouldNotOpenDatabaseException.java
de.arcus.playmusiclib.exceptions.NoSuperUserException.java
de.arcus.playmusiclib.exceptions.PlayMusicNotFoundException.java
de.arcus.playmusiclib.items.Album.java
de.arcus.playmusiclib.items.Artist.java
de.arcus.playmusiclib.items.MusicTrackList.java
de.arcus.playmusiclib.items.MusicTrack.java
de.arcus.playmusiclib.items.Playlist.java