Advertisement
Risiko94

SoundSenseCustomizer

Jun 9th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3.  
  4. import org.w3c.dom.Element;
  5. import org.w3c.dom.Node;
  6. import org.w3c.dom.NodeList;
  7.  
  8. public class SoundSenseCustomizer {
  9.     public static void main(String[] args) {
  10.         System.out.println("SoundSenseCustomizer is starting ...");
  11.         String userdir = System.getProperty("user.dir");
  12.         String filesep = System.getProperty("file.separator");
  13.         final File folder = new File(userdir + filesep + "packs" + filesep + "seasons");
  14.  
  15.         try {
  16.             final File seasonpath = new File(folder.getCanonicalPath() + filesep + "seasons.xml");
  17.             checkfilepath(seasonpath);
  18.             XMLDocument doc = new XMLDocument(seasonpath);
  19.             NodeList snlist = doc.getElementsByTagName("sound");
  20.             // go through the season
  21.             for (Season season : Season.values()) {
  22.                 System.out.println("\nCurrent Season: " + season);
  23.                 File curfolder = new File(folder, filesep + season);
  24.                 String[] songlist = curfolder.list();
  25.                 // go through all "song" nodes
  26.                 for (int j = 0; j < snlist.getLength(); j++) {
  27.                     Node sn = snlist.item(j);
  28.                     Element snele = (Element) sn;
  29.                     // pick the correct season node
  30.                     if (snele.getAttribute("logPattern").toUpperCase()
  31.                             .endsWith((season + " has arrived on the calendar\\.)").toUpperCase())) {
  32.                         NodeList sFlist = sn.getChildNodes();
  33.                         // go through the songlist and compare them with the
  34.                         // current season node
  35.                         for (String currentsong : songlist) {
  36.                             String seasonslashcurrentsong = season + "/" + currentsong;
  37.                             if (!wrongfileending(currentsong) && !entryalreadyexists(seasonslashcurrentsong, sFlist)) {
  38.                                 doc.appendelement(seasonslashcurrentsong, sn);
  39.                             }
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.             File xmlrelease = new File(folder.getCanonicalPath() + filesep + "seasonsnew.xml");
  45.             doc.releasexml(xmlrelease);
  46.  
  47.         } catch (IOException e) {
  48.             e.printStackTrace();
  49.         }
  50.     }
  51.  
  52.     // Check if seasonslashsong already exist in Nodelise sFlist as atribute of
  53.     // "filename"
  54.     private static boolean entryalreadyexists(String seasonslashsong, NodeList sFlist) {
  55.         for (int k = 1; k < sFlist.getLength(); k = k + 2) {
  56.             Node sF = sFlist.item(k);
  57.             Element sFele = (Element) sF;
  58.             if (seasonslashsong.equals(sFele.getAttribute("fileName"))) {
  59.                 return true;
  60.             }
  61.         }
  62.         return false;
  63.     }
  64.  
  65.     // Check if something exists at path
  66.     private static void checkfilepath(File path) {
  67.         if (!path.exists()) {
  68.             System.err.println(path + " was not found. Are you sure you did everything correct?");
  69.             System.exit(1);
  70.         }
  71.     }
  72.  
  73.     // Check if file ends with one of the Acceptedfiles
  74.     private static boolean wrongfileending(String file) {
  75.         boolean notaccepted = true;
  76.         for (Acceptedfiles accfil : Acceptedfiles.values()) {
  77.             if (file.trim().endsWith("." + accfil))
  78.                 notaccepted = false;
  79.         }
  80.         return notaccepted;
  81.     }
  82.  
  83.     public enum Season {
  84.         spring, summer, autumn, winter
  85.     }
  86.  
  87.     public enum Acceptedfiles {
  88.         mp3, ogg, wav, aiff, au
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement