costin      01/03/07 17:06:01

  Modified:    src/share/org/apache/tomcat/util IntrospectionUtils.java
  Log:
  Added replaceProperties - cut&paste from ant, with dependencies removed.
  It can be used with a Hasthable storing properties or with a
  simple adapter ( the PropertySource - it's used to support default
  or "dynamic"/runtime values ).
  
  ( before anyone start screaming "duplication" - I wrote part of the code
  that deal with properties in ant, and it'll be moved in commons as soon
  as possible - if ant people also agree to share it.  )
  
  Revision  Changes    Path
  1.6       +57 -2     
jakarta-tomcat/src/share/org/apache/tomcat/util/IntrospectionUtils.java
  
  Index: IntrospectionUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/IntrospectionUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- IntrospectionUtils.java   2001/03/04 03:37:16     1.5
  +++ IntrospectionUtils.java   2001/03/08 01:06:00     1.6
  @@ -197,8 +197,7 @@
        If found, call the method ( if param is int or boolean we'll convert
        value to the right type before) - that means you can have setDebug(1).
       */
  -    public static void setProperty( Object o, String name, String value )
  -    {
  +    public static void setProperty( Object o, String name, String value ) {
        if( dbg > 1 ) d("setProperty(" +
                        o.getClass() + " " +  name + "="  +
                        value  +")" );
  @@ -281,6 +280,52 @@
        }
       }
   
  +    /** Replace ${NAME} with the property value
  +     */
  +    public static String replaceProperties(String value,
  +                                        Object getter )
  +    {
  +        StringBuffer sb=new StringBuffer();
  +        int i=0;
  +        int prev=0;
  +        // assert value!=nil
  +        int pos;
  +        while( (pos=value.indexOf( "$", prev )) >= 0 ) {
  +            if(pos>0) {
  +                sb.append( value.substring( prev, pos ) );
  +            }
  +            if( pos == (value.length() - 1)) {
  +                sb.append('$');
  +                prev = pos + 1;
  +            }
  +            else if (value.charAt( pos + 1 ) != '{' ) {
  +                sb.append( value.charAt( pos + 1 ) );
  +                prev=pos+2; // XXX
  +            } else {
  +                int endName=value.indexOf( '}', pos );
  +                if( endName < 0 ) {
  +                 sb.append( value.substring( pos ));
  +                 prev=value.length();
  +                 continue;
  +                }
  +                String n=value.substring( pos+2, endName );
  +             String v= null;
  +             if( getter instanceof Hashtable ) {
  +                 v=(String)((Hashtable)getter).get(n);
  +             } else if ( getter instanceof PropertySource ) {
  +                 v=((PropertySource)getter).getProperty( n );
  +             }
  +             if( v== null )
  +                 v = "${"+n+"}"; 
  +                
  +                sb.append( v );
  +                prev=endName+1;
  +            }
  +        }
  +        if( prev < value.length() ) sb.append( value.substring( prev ) );
  +        return sb.toString();
  +    }
  +    
       /** Reverse of Introspector.decapitalize
        */
       public static String capitalize(String name) {
  @@ -292,6 +337,16 @@
        return new String(chars);
       }
   
  +    // -------------------- Get property --------------------
  +    // This provides a layer of abstraction
  +
  +    public static interface PropertySource {
  +
  +     public String getProperty( String key );
  +     
  +    }
  +
  +    
       // debug --------------------
       static final int dbg=0;
       static void d(String s ) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to