Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- public class SoundSenseCustomizer {
- public static void main(String[] args) {
- System.out.println("SoundSenseCustomizer is starting ...");
- String userdir = System.getProperty("user.dir");
- String filesep = System.getProperty("file.separator");
- final File folder = new File(userdir + filesep + "packs" + filesep + "seasons");
- try {
- final File seasonpath = new File(folder.getCanonicalPath() + filesep + "seasons.xml");
- checkfilepath(seasonpath);
- XMLDocument doc = new XMLDocument(seasonpath);
- NodeList snlist = doc.getElementsByTagName("sound");
- // go through the season
- for (Season season : Season.values()) {
- System.out.println("\nCurrent Season: " + season);
- File curfolder = new File(folder, filesep + season);
- String[] songlist = curfolder.list();
- // go through all "song" nodes
- for (int j = 0; j < snlist.getLength(); j++) {
- Node sn = snlist.item(j);
- Element snele = (Element) sn;
- // pick the correct season node
- if (snele.getAttribute("logPattern").toUpperCase()
- .endsWith((season + " has arrived on the calendar\\.)").toUpperCase())) {
- NodeList sFlist = sn.getChildNodes();
- // go through the songlist and compare them with the
- // current season node
- for (String currentsong : songlist) {
- String seasonslashcurrentsong = season + "/" + currentsong;
- if (!wrongfileending(currentsong) && !entryalreadyexists(seasonslashcurrentsong, sFlist)) {
- doc.appendelement(seasonslashcurrentsong, sn);
- }
- }
- }
- }
- }
- File xmlrelease = new File(folder.getCanonicalPath() + filesep + "seasonsnew.xml");
- doc.releasexml(xmlrelease);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // Check if seasonslashsong already exist in Nodelise sFlist as atribute of
- // "filename"
- private static boolean entryalreadyexists(String seasonslashsong, NodeList sFlist) {
- for (int k = 1; k < sFlist.getLength(); k = k + 2) {
- Node sF = sFlist.item(k);
- Element sFele = (Element) sF;
- if (seasonslashsong.equals(sFele.getAttribute("fileName"))) {
- return true;
- }
- }
- return false;
- }
- // Check if something exists at path
- private static void checkfilepath(File path) {
- if (!path.exists()) {
- System.err.println(path + " was not found. Are you sure you did everything correct?");
- System.exit(1);
- }
- }
- // Check if file ends with one of the Acceptedfiles
- private static boolean wrongfileending(String file) {
- boolean notaccepted = true;
- for (Acceptedfiles accfil : Acceptedfiles.values()) {
- if (file.trim().endsWith("." + accfil))
- notaccepted = false;
- }
- return notaccepted;
- }
- public enum Season {
- spring, summer, autumn, winter
- }
- public enum Acceptedfiles {
- mp3, ogg, wav, aiff, au
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement