Index: lutinutil/src/java/org/codelutin/util/ArrayUtil.java diff -u lutinutil/src/java/org/codelutin/util/ArrayUtil.java:1.1 lutinutil/src/java/org/codelutin/util/ArrayUtil.java:1.2 --- lutinutil/src/java/org/codelutin/util/ArrayUtil.java:1.1 Tue Nov 2 16:51:28 2004 +++ lutinutil/src/java/org/codelutin/util/ArrayUtil.java Thu Nov 4 13:51:54 2004 @@ -23,9 +23,9 @@ * Created: 31 oct. 2004 * * @author Benjamin Poussin - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Mise a jour: $Date: 2004/11/02 16:51:28 $ + * Mise a jour: $Date: 2004/11/04 13:51:54 $ * par : $Author: bpoussin $ */ @@ -44,6 +44,22 @@ ArrayList result = new ArrayList(a.length); for(int i=0; i * Copyright Code Lutin -* @version $Revision: 1.16 $ +* @version $Revision: 1.17 $ * -* Mise a jour: $Date: 2004/10/14 19:39:03 $ -* par : $Author: pineau $ +* Mise a jour: $Date: 2004/11/04 13:51:54 $ +* par : $Author: bpoussin $ */ package org.codelutin.util; import java.io.File; +import java.io.FileInputStream; import java.io.InputStream; +import java.lang.ClassLoader; +import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; -import java.util.StringTokenizer; +import java.util.logging.Level; import java.util.logging.Logger; +import java.util.StringTokenizer; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import java.util.ArrayList; -import java.lang.reflect.Method; -import java.lang.ClassLoader; import javax.swing.ImageIcon; -import java.util.logging.Level; /** @@ -83,22 +84,31 @@ * @return l'url de la ressource * @throws ResourceNotFoundException si la resource n'a pas ete trouvee */ - static public URL getURL(String nom) throws ResourceNotFoundException{ + static public URL getURL(String name) throws ResourceNotFoundException{ // on recherche d'abord sur le filesystem - File file = new File(nom); - if (file.exists()) - try{ - return file.toURI().toURL(); - }catch(MalformedURLException eee){ - Logger.getLogger("org.codelutin.util.Resource.getURL").warning("Le fichier '"+file+"' a été trouvé, mais il n'a pas pu etre converti en URL"); + File file = new File(name); + if (file.exists()){ + try{ + return file.toURI().toURL(); + }catch(MalformedURLException eee){ + Logger.getLogger("org.codelutin.util.Resource.getURL").warning("Le fichier '" + file + "' a été trouvé, mais il n'a pas pu etre converti en URL"); + } } //on ne l'a pas trouve on recherche dans le classpath - URL url = findResource(nom); - if (url != null) + + // on supprime le / devant le nom de la ressource, sinon elle + // n'est pas trouve (pas de recherche dans les differents + // element du classpath. + if(name.length() > 1 && name.startsWith("/")){ + name = name.substring(1); + } + URL url = ClassLoader.getSystemClassLoader().getResource(name); + if (url != null){ return url; + } - throw new ResourceNotFoundException("La resource "+nom+ + throw new ResourceNotFoundException("La resource " + name + " n'a pas été trouvée"); } @@ -115,41 +125,59 @@ } } +// /** +// * Method findResource +// * Recherche une ressource en structant la variable java.class.path +// * du systeme, elle correspond au CLASSPATH +// * @param name le nom de la ressource rechercher +// * @return URL retourne null si une erreur c produite ou que la ressource +// * n'a pas ete trouvee. +// */ +// static protected URL findResource(String name){ +// URL result = null; +// try{ +// on supprime le / devant le nom de la ressource, sinon elle +// n'est pas trouve (pas de recherche dans les differents +// element du classpath. +// if(name.length() > 1 && name.startsWith("/")){ +// name = name.substring(1); +// } +// String cp = System.getProperty("java.class.path", "."); +// StringTokenizer stok = new StringTokenizer(cp, File.pathSeparator); +// URL url[] = new URL[stok.countTokens()]; +// int i=0; +// while(stok.hasMoreTokens()){ +// url[i++] = new File(stok.nextToken()).toURI().toURL(); +// } +// URLClassLoader ucl = URLClassLoader.newInstance(url); +// result = ucl.findResource(name); +// }catch(MalformedURLException e){ +// Logger.getLogger("org.codelutin.util.Resource.findResource").fine("Erreur lors de la recherche de la ressource:"+name); +// } +// if (result==null) { +// ClassLoader cl = Resource.class.getClassLoader(); + ClassLoader cl = Thread.currentThread().getContextClassLoader(); +// result = cl.getResource(name); +// } +// +// return result; +// } + + /** - * Method findResource - * Recherche une ressource en structant la variable java.class.path - * du systeme, elle correspond au CLASSPATH - * @param name le nom de la ressource rechercher - * @return URL retourne null si une erreur c produite ou que la ressource - * n'a pas ete trouvee. + * Retourner la liste des fichiers du classLoader. Ces fichiers doivent + * correspondent au pattern donne + * + *@param classLoader le classLoader + *@param pattern le nom du fichier a extraire du + * fichier compressé ou durepertoire doit correspondre + * au pattern (repertoire + nom compris). + *@return la liste des urls correspondant au pattern + *@exception Exception s'il y a un problème une exception est levée */ - static protected URL findResource(String name){ - URL result = null; - try{ - //on supprime le / devant le nom de la ressource, sinon elle - //n'est pas trouve (pas de recherche dans les differents - //element du classpath. - if(name.length() > 1 && name.startsWith("/")) - name = name.substring(1); - String cp = System.getProperty("java.class.path", "."); - StringTokenizer stok = new StringTokenizer(cp, File.pathSeparator); - URL url[] = new URL[stok.countTokens()]; - int i=0; - while(stok.hasMoreTokens()){ - url[i++] = new File(stok.nextToken()).toURI().toURL(); - } - URLClassLoader ucl = URLClassLoader.newInstance(url); - result = ucl.findResource(name); - }catch(MalformedURLException e){ - Logger.getLogger("org.codelutin.util.Resource.findResource").fine("Erreur lors de la recherche de la ressource:"+name); - } - if (result==null) { - ClassLoader cl = Resource.class.getClassLoader(); - //ClassLoader cl = Thread.currentThread().getContextClassLoader(); - result = cl.getResource(name); - } - - return result; + static public List getURLs(String pattern) + throws Exception { + return getURLs(pattern, null); } /** @@ -163,14 +191,13 @@ *@return la liste des urls correspondant au pattern *@exception Exception s'il y a un problème une exception est levée */ - - static public List getURLs(URLClassLoader classLoader, String pattern) + static public List getURLs(String pattern, URLClassLoader classLoader) throws Exception { - - if (classLoader==null){ - classLoader=(URLClassLoader)Thread.currentThread().getContextClassLoader(); + if(classLoader == null){ + classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader(); } - LinkedList urlList = new LinkedList(); + + HashList urlList = new HashList(); URL[] arrayURL = classLoader.getURLs(); for(int i=0;i0?args[0]:"/org/codelutin/lutinbuilder/LutinBuilder.class"; - System.out.println(getURL(r)); - } +// /** +// * Test +// */ +// static public void main(String [] args) throws Exception { +// String r = args.length>0?args[0]:"/org/codelutin/lutinbuilder/LutinBuilder.class"; +// System.out.println(getURL(r)); +// } } // Resource Index: lutinutil/src/java/org/codelutin/util/GZUtil.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/GZUtil.java:1.1 --- /dev/null Thu Nov 4 13:51:59 2004 +++ lutinutil/src/java/org/codelutin/util/GZUtil.java Thu Nov 4 13:51:54 2004 @@ -0,0 +1,106 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004 Code Lutin, Cédric Pineau, + * Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * GZUtil.java + * + * Created: 3 nov. 2004 + * + * @author Benjamin Poussin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2004/11/04 13:51:54 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.util; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +public class GZUtil { // GZUtil + + /** + * Retourne la string decompressee + */ + public static StringBuffer bytesToStringBuffer(byte[] in){ + try{ + if (in == null || in.length == 0){ + return new StringBuffer(""); + } + GZIPInputStream gz = new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(in))); + StringBuffer sb = new StringBuffer(); + int c; + + while((c=gz.read()) != -1){ + sb.append((char)c); + } + gz.close(); + return sb; + } + catch(IOException eee){ + throw new GZUtilException("Probleme dans la decompression", eee); + } + } + + /** + * Retourne la string decompressee + */ + public static String bytesToString(byte[] in){ + return bytesToStringBuffer(in).toString(); + } + + /** + * Retourne la string compressee + */ + public static byte[] stringBufferToBytes(StringBuffer elem){ + return stringToBytes(elem.toString()); + } + + /** + * Retourne la string compressee + */ + public static byte[] stringToBytes(String elem){ + try{ + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream gz = new GZIPOutputStream(new BufferedOutputStream(baos)); + Reader sr = new BufferedReader(new StringReader(elem)); + int c; + while((c=sr.read()) != -1){ + gz.write((char)c); + } + gz.close(); + + return baos.toByteArray(); + } + catch(IOException eee){ + throw new GZUtilException("Probleme dans la compression", eee); + } + } + +} // GZUtil + Index: lutinutil/src/java/org/codelutin/util/GZUtilException.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/GZUtilException.java:1.1 --- /dev/null Thu Nov 4 13:51:59 2004 +++ lutinutil/src/java/org/codelutin/util/GZUtilException.java Thu Nov 4 13:51:54 2004 @@ -0,0 +1,43 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004 Code Lutin, Cédric Pineau, + * Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * GZUtilException.java + * + * Created: 3 nov. 2004 + * + * @author Benjamin Poussin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2004/11/04 13:51:54 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.util; + +public class GZUtilException extends RuntimeException { // GZUtilException + public GZUtilException (String msg){ + super(msg); + } + + public GZUtilException (String msg, Throwable eee){ + super(msg, eee); + } +} // GZUtilException + Index: lutinutil/src/java/org/codelutin/util/HashList.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/HashList.java:1.1 --- /dev/null Thu Nov 4 13:51:59 2004 +++ lutinutil/src/java/org/codelutin/util/HashList.java Thu Nov 4 13:51:54 2004 @@ -0,0 +1,132 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004 Code Lutin, Cédric Pineau, + * Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * HashList.java + * + * Created: 2 nov. 2004 + * + * @author Benjamin Poussin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2004/11/04 13:51:54 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.util; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; + +/** +* Cette objet permet de gerer l'unicité des objets ajouté. +* Lorsque l'on appelle la methode add sur cette objet, il verifie +* en premier que l'objet n'est pas deja dans la liste. S'il y est +* alors il n'est pas ajouter, sinon il est ajouter. +* L'utilisation de la methode set n'est pas permise +* +* FIXME: la serialisation n'est pas modifier, c-a-d que seul le tableau est +* conserve et pas la hashSet, donc apres recuperation, l'uticite n'est plus +* garantie. Il faut donc surcharger readObject et writeObject pour conserver +* le HashSet +*/ +public class HashList extends ArrayList { // HashList + + protected HashSet set = new HashSet(); + + public HashList(){ + super(); + } + public HashList(Collection c){ + addAll(c); + } + public HashList(int initialCapacity){ + super(initialCapacity); + } + + public Object set(int index, Object element) { + throw new UnsupportedOperationException("You can't use set method in HashList"); + } + + public boolean add(Object o) { + boolean result = !contains(o); + add(size(), o); + return result; + } + + public void add(int index, Object element) { + if(set.add(element)){ + super.add(index, element); + } + } + + /** + * supprime l'element demandé. Si l'elment n'existe pas alors, null + * est retrouné. + */ + public Object remove(int index) { + if(set.remove(get(index))){ + return super.remove(index); + } + return null; + } + public void clear() { + set.clear(); + super.clear(); + } + public boolean addAll(Collection c) { + boolean modified = false; + Iterator e = c.iterator(); + while (e.hasNext()) { + if(add(e.next())) + modified = true; + } + return modified; + + } + public boolean addAll(int index, Collection c) { + boolean modified = false; + Iterator e = c.iterator(); + while (e.hasNext()) { + add(index++, e.next()); + modified = true; + } + return modified; + + } + + protected void removeRange(int fromIndex, int toIndex) { + for(int i=toIndex-1; i>=fromIndex; i--){ + remove(i); + } + } + + public boolean contains(Object elem) { + return set.contains(elem); + } + + public Object clone() { + HashList result = new HashList(this); + return result; + } + +} // HashList + Index: lutinutil/src/java/org/codelutin/util/HashMapMultiKey.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/HashMapMultiKey.java:1.1 --- /dev/null Thu Nov 4 13:51:59 2004 +++ lutinutil/src/java/org/codelutin/util/HashMapMultiKey.java Thu Nov 4 13:51:54 2004 @@ -0,0 +1,179 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004 Code Lutin, Cédric Pineau, + * Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * HashMapMultiKey.java + * + * Created: 2 nov. 2004 + * + * @author Benjamin Poussin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2004/11/04 13:51:54 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.util; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.ArrayList; + +/** + * Permet de stocker un element suivant un ensemble de cle. + */ +public class HashMapMultiKey extends HashMap { // HashMapMultiKey + + /** key: un objet, value une list de key pour la vrai hashmap */ + protected HashMap keys = new HashMap(); + + public HashMapMultiKey() { + super(); + } + + /** + * Retourne toutes les cles qui contiennent au moins une fois l'élément + * e. + * @param e l'element que doivent contenir les cles + * @return la liste des cles. Si aucune cle ne contient l'element e, + * alors la liste retourné est vide + */ + public LinkedList getKeys(Object e){ + LinkedList list = (LinkedList)keys.get(e); + if( list == null){ + list = new LinkedList(); + keys.put(e, list); + } + return list; + } + + /** + * Method put + * + * @param key une collection d'objet + * @param value la valeur a associer a la cle. + * @return l'ancienne valeur pour cette cle, ou null, s'il n'y + * avait pas d'ancienne valeur. + * @throws IllegalArgumentException si le parametre key n'est pas + * une collection d'objet. + */ + public Object put(Object key, Object value){ + if(key instanceof Key){ + // ajout dans la hash des cles + Key keyObject = (Key)key; + for(int i=0; i + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2004/11/04 13:51:54 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.util; + +public class ObjectUtil { // ObjectUtil + + /** + * ObjectUtil constructor + * private because of this class is a static class : nobody + * can make an instance of this class + */ + private ObjectUtil() {} + + /** + * Method toObject + * + * @param o Object to transform + * @return the same object + */ + public static Object toObject(Object o){ + return o; + } + + /** + * Method toObject + * + * transform a char to a Character Object + * @param c the char to transform + * @return the Charactere object corresponding + */ + public static Object toObject(char c){ + return new Character(c); + } + + /** + * Method toObject + * + * transform a byte to a Byte Object + * @param b the byte to transform + * @return the byte object corresponding + */ + public static Object toObject(byte b){ + return new Byte(b); + } + + /** + * Method toObject + * + * transform a short to a Short object + * @param s the short to transform + * @return the Short object corresponding + */ + public static Object toObject(short s){ + return new Short(s); + } + + /** + * Method toObject + * + * transform an int to an Integer object + * @param i the int to transform + * @return the Integer Object corresponding + */ + public static Object toObject(int i){ + return new Integer(i); + } + + /** + * Method toObject + * + * transform a long to a Long object + * @param l the long to transform + * @return the Long Object corresponding + */ + public static Object toObject(long l){ + return new Long(l); + } + + /** + * Method toObject + * + * transform a float to a Float Object + * @param f the float to transform + * @return the Float Object corresponding + */ + public static Object toObject(float f){ + return new Float(f); + } + + /** + * Method toObject + * + * transform a double to a Double object + * @param d the double to transform + * @return the Double object corresponding + */ + public static Object toObject(double d){ + return new Double(d); + } + + /** + * Method toObject + * + * transform a boolean to a Boolean object + * @param b the boolean to transform + * @return the Boolean object corresponding + */ + public static Object toObject(boolean b){ + return b?Boolean.TRUE:Boolean.FALSE; + } + +} // ObjectUtil +