Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* 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.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Locale;
import at.spardat.xma.boot.Statics;
import at.spardat.xma.boot.logger.LogLevel;
import at.spardat.xma.boot.logger.Logger;
import at.spardat.xma.boot.transform.HashChecksum;
/**
* class: CDBUtil
*
* @author 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.
* @return the singleton Util
*/
public static Util getInstance() {
if( instance_ == null) {
synchronized(Util.class) {
if( instance_ == null )
instance_ = new Util();
}
}
return instance_;
}
/**
* Converts a URL into a local, relative path representation.
*
* @param location the url
* @return the file_
*/
public StringBuffer urlToPath(URL location) {
StringBuffer path = new StringBuffer();
path.append(location.getProtocol());
path.append(File.separatorChar);
path.append(location.getHost());
if(location.getPort()!= -1 ) {
path.append(File.separatorChar);
path.append( Integer.toString( location.getPort(), 10));
}
path.append(location.getPath().replace('/', File.separatorChar));
if(location.getQuery() != null ) {
path.append( Statics.URL_QUERY );
path.append( File.separatorChar);
String checksum = HashChecksum.calcCheckSum(location.getQuery().getBytes());
path.append(checksum);
// path.append( Integer.toString( location.getQuery().hashCode() ) );
}
return path;
}
/**
* Parse a Locale from String
* @param 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(language, country, variant);
}
/**
* Compare strings that can be null
*
* @param s1 string-input
* @param s2 string-input
* @param ignore true will ignore case
*
* @return true if equal otherwise false
*/
public boolean compare(String s1, String s2, boolean 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 )
*
* @param u1 compare input 1
* @param u2 compare input 2
* @return true if equal
*/
public boolean urlEquals(URL u1, URL 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.
* @param in the file path to fix
* @return the fixed file path
*/
public String fixPath(String in) {
StringBuffer buf = new StringBuffer( in );
this.fixPath(buf);
return buf.toString();
}
/**
* Clean up a string by removing characters that can't/should not appear in
* a local file_ name.
* @param 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, File.separatorChar);
StringBuffer temp = new StringBuffer( path.capacity() + lLength/4 );
for (int i=0; i < path.length(); i++) {
char now = path.charAt(i);
int iPos = Arrays.binarySearch(badChars, now);
if( iPos < 0 || iPos == iPosFS) {
temp.append(now);
} else{
temp.append( "%" + Integer.toHexString(now) );
}
}//for
path.setLength(0);
path.append( temp.toString());
}
/**
* remove a directory and all it?s content
*
* @param filedir the directory to remove
* @param log_ where to report any errors
*/
public void removeApplicationRecursive(File filedir, Logger log_) {
File[] files = filedir.listFiles();
if(files==null) return;
for (int i = 0; i < files.length; i++) {
File next = files[i];
if( next.isFile() ){
log_.log(LogLevel.ALL, "removing: {0}", next.toString());
next.delete();
}
if( next.isDirectory() ) {
removeApplicationRecursive( next, log_ );
}
if( next.isDirectory() && next.listFiles().length == 0) {
log_.log(LogLevel.ALL, "removing: {0}", next.toString());
next.delete();
}
}
}
/**
* Closes as stream and logs an eventual Exception.
* @param os the output stream to close
* @param id identifieing the stream e.g.: the filename
* @since 1.3.1
* @author s2877
*/
static public void close (OutputStream os, String id) {
if (os != null) {
try {
os.close();
} catch (Exception x) {
Logger.getLogger("bootrt.bootRuntime").log(LogLevel.WARNING,"error closing "+id,x);
}
}
}
/**
* Closes as stream and logs an eventual Exception.
* @param is the input stream to close
* @param id identifieing the stream e.g.: the filename
* @since 1.3.1
* @author s2877
*/
static public void close (InputStream is, String id) {
if (is != null) {
try {
is.close();
} catch (Exception x) {
Logger.getLogger("bootrt.bootRuntime").log(LogLevel.WARNING,"error closing "+id,x);
}
}
}
}