Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: s IT Solutions AT Spardat GmbH - initial API and implementation /
 
 
 /*
  * Created on : 04.2003
  * Created by : s3595
  */
 package at.spardat.xma.boot.util;
 
 import java.io.File;
 import java.net.URL;
 
class: CDBUtil

Author(s):
s3595 Chr. Schaefer (CGS)
Version:
$Id: Util.java 2084 2007-11-27 14:53:31Z s3460 $
 
 public class Util {
 
     private static Util instance_;

    
private constructor
 
     private Util() {}

    
Get the single instance of Util. If no instance exists it is created.

Returns:
the singleton Util
 
     public static Util getInstance() {
        if == null) {
          synchronized(Util.class) {
           if == null )
              = new Util();
          }
        }
       return ;
     }

    
Converts a URL into a local, relative path representation.

Parameters:
location the url
Returns:
the file_
 
     public StringBuffer urlToPath(URL location) {
         StringBuffer path = new StringBuffer();
 
         path.append(location.getProtocol());
         path.append(.);
         path.append(location.getHost());
 
         if(location.getPort()!= -1 ) {
             path.append(.);
             path.append( Integer.toStringlocation.getPort(), 10));
         }
         path.append(location.getPath().replace('/'.));
 
         if(location.getQuery() != null ) {
           path.append. );
           path.append.);
 
           String checksum = HashChecksum.calcCheckSum(location.getQuery().getBytes());
           path.append(checksum);
 //        path.append( Integer.toString( location.getQuery().hashCode() ) );
         }
 
       return path;
     }

    
Parse a Locale from String

Parameters:
localeStr the locale string to be parsed.
It is save with "null" as input
 
     public Locale getLocale(String localeStr) {
         if ( localeStr == null || localeStr.length() < 2)
             return null;
 
         String language = localeStr.substring(0, 2);
         String country = (localeStr.length()<5) ? "" : localeStr.substring(3, 5);
        String variant = (localeStr.length()<7) ? "" : localeStr.substring(6, 8);
        // null is not allowed n locale but "" is
        return new Locale(languagecountryvariant);
    }


    
Compare strings that can be null

Parameters:
s1 string-input
s2 string-input
ignore true will ignore case
Returns:
true if equal otherwise false
    public boolean compare(String s1String s2boolean ignore) {
        if (s1==s2)
            return true;
        if (s1==null || s2==null)
            return false;
        if (ignore)
          return s1.equalsIgnoreCase(s2);
        else
          return s1.equals(s2);
    }

    
Compares a URL using string compare of its protocol, host, port, path, query, and anchor. This method avoids the host name lookup that URL.equals does for http: protocol URLs. It may not return the same value as the URL.equals method ( different hostnames that resolve to the same IP address )

Parameters:
u1 compare input 1
u2 compare input 2
Returns:
true if equal
    public boolean urlEquals(URL u1URL u2) {
        if (u1==u2)
          return true;
        if (u1==null || u2==null)
          return false;
        if (!compare(u1.getProtocol(), u2.getProtocol(), true) ||
            !compare(u1.getHost(), u2.getHost(), true) ||
            (u1.getPort() != u2.getPort()) ||
            !compare(u1.getPath(), u2.getPath(), false) ||
            !compare(u1.getQuery(), u2.getQuery(), false) ||
            !compare(u1.getRef(), u2.getRef(), false))
          return false;
        else
          return true;
    }// end urlEquals


    
Clean up a string by removing characters that can't/should not appear in a local file_ name.

Parameters:
in the file path to fix
Returns:
the fixed file path
    public String fixPath(String in) {
       StringBuffer buf = new StringBufferin );
       this.fixPath(buf);
      return buf.toString();
    }


    
Clean up a string by removing characters that can't/should not appear in a local file_ name.

Parameters:
path the path to fix. Bad characters are replaced in this StringBuffer.
    public void fixPath(StringBuffer path) {
     // bad characters for the filesystem are:
     char badChars[] = {'"','#''%''*''/'':''<''>''?''\\''|' };
     int lLength = path.length();
     // prepare sorted. instead of sorting here
     Arrays.sort(badChars);
     // do not escape the current file seperator
     int iPosFS = Arrays.binarySearch(badChars.);
     StringBuffer temp = new StringBufferpath.capacity() + lLength/4 );
     for (int i=0; i < path.length(); i++) {
        char now = path.charAt(i);
         int iPos = Arrays.binarySearch(badCharsnow);
         ifiPos < 0 || iPos == iPosFS) {
           temp.append(now);
         } else{
           temp.append"%" + Integer.toHexString(now) );
         }
     }//for
      path.setLength(0);
      path.appendtemp.toString());
    }

    
remove a directory and all it�s content

Parameters:
filedir the directory to remove
log_ where to report any errors
    public void removeApplicationRecursive(File filedirLogger log_) {
        File[] files = filedir.listFiles();
        if(files==nullreturn;
        for (int i = 0; i < files.lengthi++) {
            File next = files[i];
            ifnext.isFile() ){
                log_.log(."removing: {0}"next.toString());
                next.delete();
            }
            ifnext.isDirectory() ) {
                removeApplicationRecursivenextlog_ );
            }
            ifnext.isDirectory() && next.listFiles().length == 0) {
                log_.log(."removing: {0}"next.toString());
                next.delete();
            }
        }
    }

    
Closes as stream and logs an eventual Exception.

Parameters:
os the output stream to close
id identifieing the stream e.g.: the filename
Author(s):
s2877
Since:
1.3.1
    static public void close (OutputStream osString id) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception x) {
                Logger.getLogger("bootrt.bootRuntime").log(.,"error closing "+id,x);
            }
         }
     }

    
Closes as stream and logs an eventual Exception.

Parameters:
is the input stream to close
id identifieing the stream e.g.: the filename
Author(s):
s2877
Since:
1.3.1
    static public void close (InputStream isString id) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception x) {
                Logger.getLogger("bootrt.bootRuntime").log(.,"error closing "+id,x);
            }
         }
     }
New to GrepCode? Check out our FAQ X