Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]
    1   //
    2   //  Neuros Database Manipulator
    3   //  Copyright (C) 2003  Neuros Database Manipulator
    4   //
    5   //  This program is free software; you can redistribute it and/or modify
    6   //  it under the terms of the GNU General Public License as published by
    7   //  the Free Software Foundation; either version 2 of the License, or
    8   //  (at your option) any later version.
    9   //
   10   //  This program is distributed in the hope that it will be useful,
   11   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13   //  GNU General Public License for more details.
   14   //
   15   //  You should have received a copy of the GNU General Public License
   16   //  along with this program; if not, write to the Free Software
   17   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18   //
   19   //  For information about Neuros Database Manipulator and its authors,
   20   //  please contact the Neuros Database Manipulator Web Site at
   21   //  http://neurosdbm.sourceforge.net
   22   //
   23   //
   24   
   25   package net.sourceforge.neurosdbm.db;
   26   
   27   
   28   import java.io.File;
   29   import java.io.FileInputStream;
   30   import java.io.FileOutputStream;
   31   import java.io.FileNotFoundException;
   32   import java.io.IOException;
   33   import java.util.regex.Pattern;
   34   import java.util.regex.Matcher;
   35   import java.nio.channels.FileChannel;
   36   import helliker.id3.NullsoftID3GenreTable;
   37   import net.sourceforge.neurosdbm.NeurosProperties;
   38   import net.sourceforge.neurosdbm.Logging;
   39   
   40   public class Util {
   41   
   42     public static String intAsHex(int value) {
   43       if (value < 16) {
   44         return "0x000" + Integer.toHexString(value);
   45       }
   46   
   47       if (value < 16 * 16) {
   48         return "0x00" + Integer.toHexString(value);
   49       }
   50   
   51       if (value < 16 * 16 * 16) {
   52         return "0x0" + Integer.toHexString(value);
   53       }
   54       
   55       return "0x" + Integer.toHexString(value);
   56     }
   57   
   58   
   59     public static String wordOffsetDebug(int wordOffset) {
   60       return "[" + wordOffset + "@" + Util.intAsHex(wordOffset * 2) + "]";
   61     }
   62   
   63   
   64     public static void fileCopy(String sourcePath, String destinationPath,
   65                                 String filename)
   66       throws FileNotFoundException, IOException {
   67   
   68       fileCopy(sourcePath, destinationPath, filename, filename);
   69     }
   70     
   71     // Stolen and modified from Sun's tutorial.
   72     public static void fileCopy(String sourcePath, String destinationPath,
   73                                 String sourceFilename,
   74                                 String destinationFilename)
   75       throws IOException, FileNotFoundException {
   76   
   77       String destination = null;
   78       try {
   79         File destinationPathFile = new File(destinationPath);
   80         destinationPathFile.mkdirs();
   81         String source = sourcePath + File.separator + sourceFilename;
   82         destination = destinationPath + File.separator + destinationFilename;
   83         FileChannel sourceChannel = 
   84           new FileInputStream(source).getChannel();
   85         FileChannel destinationChannel = 
   86           new FileOutputStream(destination).getChannel();
   87         // Copy source file to destination file
   88         destinationChannel.transferFrom 
   89           (sourceChannel, 0, sourceChannel.size());
   90         sourceChannel.close();
   91         destinationChannel.close();
   92       } catch (IOException exp) {
   93         if (destination != null) {
   94           // If there was an error, delete the existing destination -
   95           // it's prboably screwed up or incomplete
   96           File destinationFile = new File(destination);
   97           if (destinationFile.exists()) {
   98             destinationFile.delete();
   99           }
  100         }
  101         throw exp;
  102       }
  103     }
  104   
  105   
  106     static int woidOffset(byte[] bytes, int offset) {
  107       offset = 2*offset;
  108       while (true) {
  109         if (offset == bytes.length) {
  110           return -1;
  111         }
  112         if ((bytes[offset] == 'W')
  113             && (bytes[offset+1] == 'O')
  114             && (bytes[offset+2] == 'I')
  115             && (bytes[offset+3] == 'D')) {
  116           return offset/2;
  117         }
  118         offset += 2;
  119       }
  120     }
  121   
  122   
  123     public static String getExtension(File f) {
  124       String extension = "";
  125       String s = f.getName();
  126       int i = s.lastIndexOf('.');
  127       
  128       if (i > 0 &&  i < s.length() - 1) {
  129         extension = s.substring(i+1).toLowerCase();
  130       }
  131       return extension;
  132     }
  133   
  134     
  135     public static String timeDisplay(long time) {
  136       int hour = (int)(time / 3600);
  137       int min = (int)((time - hour*3600) / 60);
  138       int sec = (int)(time % 60);
  139       if (hour == 0) {
  140         if (sec < 10)
  141           return min + ":0" + sec;
  142         return min + ":" + sec;
  143       } else {
  144         String returnString = new String();
  145         returnString += hour + ":";
  146         if (min < 10)
  147           returnString += "0";
  148         returnString += min + ":";
  149         if (sec < 10)
  150           returnString += "0";
  151         returnString += sec;
  152         return returnString;
  153       }
  154     }
  155   
  156   
  157     public static boolean isExtendedASCII(char c) {
  158       if ((c & 0xFF80) != 0) {
  159         return true;
  160       }
  161       if (c == '\n') {
  162         return true;
  163       }
  164       if (c == '\r') {
  165         return true;
  166       }
  167       if (c == 0) {
  168         return true;
  169       }
  170       return false;
  171     }
  172   
  173   
  174     // replace extended ascii with '_'
  175     //TODO: refactor method call - put it in NewTrack class  
  176     public static String replaceExtendedASCII(String oldString) {
  177         if (NeurosProperties.isCustomFieldsProcessing()){
  178             return oldString; //don't change anything, will be processed further
  179         }
  180       StringBuffer returnBuffer = new StringBuffer();
  181       for (int cnt=0; cnt<oldString.length(); cnt++) {
  182         char c = oldString.charAt(cnt);
  183         if ((int)c != 0) {  // remove null chars
  184           if (!convertExtendedASCII(c, returnBuffer)) {
  185             if (isExtendedASCII(c)) {
  186               returnBuffer.append('_');
  187             } else {
  188               returnBuffer.append(c);
  189             }
  190           }
  191         }
  192       }
  193       return returnBuffer.toString();
  194     }
  195   
  196     
  197     // remove extended ascii
  198     //TODO: refactor method call - put it in NewTrack class  
  199     public static String removeExtendedASCII(String oldString) {
  200         if (NeurosProperties.isCustomFieldsProcessing()){
  201             return oldString; //don't change anything, will be processed further
  202         }
  203       StringBuffer returnBuffer = new StringBuffer();
  204       for (int cnt=0; cnt<oldString.length(); cnt++) {
  205         char c = oldString.charAt(cnt);
  206         if (!convertExtendedASCII(c, returnBuffer)) {
  207           if (!isExtendedASCII(c)) {
  208             returnBuffer.append(c);
  209           }
  210         }
  211       }
  212       return returnBuffer.toString();
  213     }
  214   
  215   
  216     private static boolean convertExtendedASCII(char c, StringBuffer buf) {
  217       if (!NeurosProperties.isConvertExtendedASCII()) {
  218         return false;
  219       }
  220       if (c == 0xfffd) {
  221         Logging.warning("Unicode replacement character found: [" + c + "]");
  222         return false;
  223       }
  224       switch (c) {
  225       case 192:
  226       case 193:
  227       case 194:
  228       case 195:
  229       case 196:
  230       case 197:
  231         buf.append('A');
  232         return true;
  233       case 198:
  234         buf.append('A');
  235         buf.append('E');
  236         return true;
  237       case 199:
  238         buf.append('C');
  239         return true;
  240       case 200:
  241       case 201:
  242       case 202:
  243       case 203:
  244         buf.append('E');
  245         return true;
  246       case 204:
  247       case 205:
  248       case 206:
  249       case 207:
  250         buf.append('I');
  251         return true;
  252       case 208:
  253         buf.append('D');
  254         return true;
  255       case 209:
  256         buf.append('N');
  257         return true;
  258       case 210:
  259       case 211:
  260       case 212:
  261       case 213:
  262       case 214:
  263       case 216:
  264         buf.append('O');
  265         return true;
  266       case 215:
  267         buf.append('x');
  268         return true;
  269       case 217:
  270       case 218:
  271       case 219:
  272       case 220:
  273         buf.append('U');
  274         return true;
  275       case 221:
  276         buf.append('Y');
  277         return true;
  278       case 222:
  279         buf.append('p');
  280         return true;
  281       case 223:
  282         buf.append('B');
  283         return true;
  284       case 224:
  285       case 225:
  286       case 226:
  287       case 227:
  288       case 228:
  289       case 229:
  290         buf.append('a');
  291         return true;
  292       case 230:
  293         buf.append('a');
  294         buf.append('e');
  295         return true;
  296       case 231:
  297         buf.append('c');
  298         return true;
  299       case 232:
  300       case 233:
  301       case 234:
  302       case 235:
  303         buf.append('e');
  304         return true;
  305       case 236:
  306       case 237:
  307       case 238:
  308       case 239:
  309         buf.append('i');
  310         return true;
  311       case 240:
  312       case 242:
  313       case 243:
  314       case 244:
  315       case 245:
  316       case 246:
  317       case 248:
  318         buf.append('o');
  319         return true;
  320       case 241:
  321         buf.append('n');
  322         return true;
  323       case 249:
  324       case 250:
  325       case 251:
  326       case 252:
  327         buf.append('u');
  328         return true;
  329       case 253:
  330       case (char)255:
  331         buf.append('y');
  332         return true;
  333       case 254:
  334         buf.append('p');
  335         return true;
  336       }
  337       return false;
  338     }
  339   
  340     
  341     public static String removeIllegalCharsFromFilename(String oldString) {
  342       oldString = oldString.replaceAll("[//\\?*:\'\"><|\n\r]", "");
  343       oldString = oldString.replaceAll("^[.]*", "");
  344       oldString = oldString.replaceAll("[.]*$", "");
  345       return oldString;
  346     }
  347   
  348   
  349     // replace extended ascii with '_'
  350     public static String removeNulls(String oldString) {
  351       if (NeurosProperties.isCustomFieldsProcessing()){
  352           return oldString; //don't change anything, will be processed further
  353       }      
  354       if (oldString == null) {
  355         return null;
  356       }
  357       StringBuffer returnBuffer = new StringBuffer();
  358       for (int cnt=0; cnt<oldString.length(); cnt++) {
  359         char c = oldString.charAt(cnt);
  360         if ((int)c != 0) {  // remove null chars
  361           returnBuffer.append(c);
  362         }
  363       }
  364       return returnBuffer.toString();
  365     }
  366   
  367     
  368     public static String fixGenre(String oldGenre) {
  369       Pattern p = Pattern.compile("\\(([0-9]*)\\)(.*)");
  370       Matcher m = p.matcher(oldGenre);
  371       if (m.matches()) {
  372         int genreNumber = Integer.parseInt(m.group(1));
  373         String genreString = NullsoftID3GenreTable.getGenre(genreNumber);
  374         if (genreString == null) {
  375           return oldGenre;
  376         }
  377         return genreString;
  378       }
  379       return oldGenre;
  380     }
  381   
  382   
  383     public static String randomString(int length) {
  384   		StringBuffer sb = new StringBuffer();
  385   		for (int i = 0; i < length;i++) {
  386   			sb.append( (char) (65 + (int) ( Math.random() * (90-65))));
  387       }
  388   		return sb.toString();
  389     }
  390   }

Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]