001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    /*
023     * JBoss, Home of Professional Open Source
024     *
025     * Distributable under LGPL license.
026     * See terms of license at gnu.org.
027     */
028    
029    package org.granite.util;
030    
031    import java.io.IOException;
032    import java.io.InputStream;
033    import java.util.StringTokenizer;
034    
035    /**
036     * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
037     */
038    public class Strings
039    {
040       
041       public static String unqualify(String name)
042       {
043          return unqualify(name, '.');
044       }
045       
046       public static String unqualify(String name, char sep)
047       {
048          return name.substring( name.lastIndexOf(sep)+1, name.length() );
049       }
050       
051       public static boolean isEmpty(String string)
052       {
053          return string == null || string.trim().length() == 0; 
054       }
055       
056       public static String nullIfEmpty(String string)
057       {
058          return isEmpty(string) ? null : string;
059       }
060    
061       public static String toString(Object component)
062       {
063          try {
064             PropertyDescriptor[] props = Introspector.getPropertyDescriptors(component.getClass());
065             StringBuilder builder = new StringBuilder();
066             for (PropertyDescriptor descriptor : props)
067             {
068                builder.append( descriptor.getName() )
069                   .append("=")
070                   .append( descriptor.getReadMethod().invoke(component) )
071                   .append("; ");
072             }
073             return builder.toString();
074          }
075          catch (Exception e) {
076             return "";
077          }
078       }
079    
080       public static String[] split(String strings, String delims)
081       {
082          if (strings==null)
083          {
084             return new String[0];
085          }
086          
087          StringTokenizer tokens = new StringTokenizer(strings, delims);
088          String[] result = new String[ tokens.countTokens() ];
089          int i=0;
090          while ( tokens.hasMoreTokens() )
091          {
092             result[i++] = tokens.nextToken();
093          }
094          return result;
095       }
096       
097       public static String toString(Object... objects)
098       {
099          return toString(" ", objects);
100       }
101       
102       public static String toString(String sep, Object... objects)
103       {
104          if (objects.length==0) return "";
105          StringBuilder builder = new StringBuilder();
106          for (Object object: objects)
107          {
108             builder.append(sep).append(object);
109          }
110          return builder.substring(2);
111       }
112       
113       public static String toClassNameString(String sep, Object... objects)
114       {
115          if (objects.length==0) return "";
116          StringBuilder builder = new StringBuilder();
117          for (Object object: objects)
118          {
119             builder.append(sep);
120             if (object==null)
121             {
122                builder.append("null");
123             }
124             else
125             {
126                builder.append( object.getClass().getName() );
127             }
128          }
129          return builder.substring(2);
130       }
131       
132       public static String toString(String sep, Class<?>... classes)
133       {
134          if (classes.length==0) return "";
135          StringBuilder builder = new StringBuilder();
136          for (Class<?> clazz: classes)
137          {
138             builder.append(sep).append( clazz.getName() );
139          }
140          return builder.substring(2);
141       }
142       
143       public static String toString(InputStream in) throws IOException {
144          StringBuffer out = new StringBuffer();
145          byte[] b = new byte[4096];
146          for ( int n; (n = in.read(b)) != -1; ) 
147          {
148             out.append(new String(b, 0, n));
149          }
150          return out.toString();
151      }
152    
153    }
154    
155