You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by lu...@apache.org on 2004/11/08 10:25:02 UTC

cvs commit: jakarta-slide/src/share/org/apache/slide/util/conf Element.java Populate.java

luetzkendorf    2004/11/08 01:25:02

  Modified:    src/share/org/apache/slide/util Configuration.java
               src/share/org/apache/slide/common Domain.java
               src/share/org/apache/slide/util/conf Element.java
                        Populate.java
  Log:
  properties expansion support part II
  
  Revision  Changes    Path
  1.20      +12 -4     jakarta-slide/src/share/org/apache/slide/util/Configuration.java
  
  Index: Configuration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/Configuration.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- Configuration.java	28 Jul 2004 09:34:29 -0000	1.19
  +++ Configuration.java	8 Nov 2004 09:25:02 -0000	1.20
  @@ -308,6 +308,14 @@
           return _principalIdentifiedLocks;
       }
   
  +    /**
  +     * Generic accessor to properties.
  +     * @param name property name
  +     * @return value or <code>null</code>.
  +     */
  +    public static String getProperty(String name) {
  +        return _default.getProperty(name);
  +    }
       
       // ------------------------------------------------------------ Initializer
       
  
  
  
  1.50      +30 -4     jakarta-slide/src/share/org/apache/slide/common/Domain.java
  
  Index: Domain.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/Domain.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- Domain.java	29 Sep 2004 15:28:06 -0000	1.49
  +++ Domain.java	8 Nov 2004 09:25:02 -0000	1.50
  @@ -159,6 +159,11 @@
        */
       private static Hashtable parameters;
       
  +    /**
  +     * Properties set in domain.xml.
  +     */
  +    private static Hashtable properties;
  +    
       // --------------------------------------------------------- Public Methods
       
       
  @@ -188,6 +193,7 @@
               if( namespaces == null ) namespaces = new Hashtable();
               if( activeNamespaces == null ) activeNamespaces = new Hashtable();
               if( parameters == null ) parameters = new Hashtable();
  +            if( properties == null) properties = new Hashtable();
               
               if (logger == null) {
                   logger = new org.apache.slide.util.logger.SimpleLogger();
  @@ -378,6 +384,15 @@
           if (isInitialized())
               return;
           
  +        for(Enumeration e = configuration.getConfigurations("property"); e.hasMoreElements();) {
  +            Configuration property = (Configuration)e.nextElement();
  +            String name = property.getAttribute("name");
  +            String value = property.getValue();
  +            if (name != null && value != null) {
  +                setProperty(name, value);
  +            }
  +        }
  +        
           parameters = new Hashtable();
           
           defaultNamespace = configuration.getAttribute("default", "slide");
  @@ -705,6 +720,17 @@
        */
       static void setParameters( Hashtable parameters ) {
           Domain.parameters = parameters;
  +    }
  +    
  +    public static String getProperty( String name ) {
  +        return (String)properties.get(name);
  +    }
  +    
  +    public static void setProperty( String name, String value) {
  +        if (properties == null) {
  +            properties = new Hashtable();
  +        }
  +        properties.put(name, value);
       }
       
       /**
  
  
  
  1.3       +61 -9     jakarta-slide/src/share/org/apache/slide/util/conf/Element.java
  
  Index: Element.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/conf/Element.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Element.java	28 Jul 2004 09:34:23 -0000	1.2
  +++ Element.java	8 Nov 2004 09:25:02 -0000	1.3
  @@ -10,6 +10,9 @@
   
   import java.util.*;
   
  +import org.apache.slide.common.Domain;
  +import org.apache.slide.util.Configuration;
  +
   /**
    */
   public class Element {
  @@ -30,7 +33,7 @@
       }
   
       public void setName(String s) {
  -        name = new String(s);
  +        name = s;
       }
   
       public boolean hasAttributes() {
  @@ -42,7 +45,12 @@
       }
   
       public String getAttributeValue(String s) {
  -        return (String) attributes.get(s);
  +        String value = (String) attributes.get(s);
  +        
  +        if (value != null) {
  +            return substituteVariables(value);
  +        }
  +        return value;
       }
   
       public void setAttribute(String s, String s1) {
  @@ -100,15 +108,18 @@
       }
   
       public String getData() {
  +        if (data != null) {
  +            return substituteVariables(data);
  +        }
           return data;
       }
   
       public void setData(Object s) {
  -        data = new String(s.toString()).trim();
  +        data = s.toString().trim();
       }
   
       public void clearData() {
  -        data = new String();
  +        data = "";
       }
   
       public String getComment() {
  @@ -116,11 +127,11 @@
       }
   
       public void setComment(String s) {
  -        comment = new String(s);
  +        comment = s;
       }
   
       public void clearComment() {
  -        comment = new String();
  +        comment = "";
       }
   
       public Element() {
  @@ -129,11 +140,11 @@
   
       public Element(String s, Element parent) {
           this.parent = parent;
  -        name = new String(s);
  +        name = s;
           attributes = new Hashtable();
           children = new Vector();
  -        data = new String();
  -        comment = new String();
  +        data = "";
  +        comment = "";
       }
       
       public String toString() {
  @@ -145,5 +156,46 @@
               buffer.append("  " + el.toString());
           }
           return buffer.toString();
  +    }
  +    
  +    private String substituteVariables(String text) {
  +        if (text.indexOf('$') == -1) return text;
  +        
  +        StringTokenizer tokenizer = new StringTokenizer(text, "$");
  +        
  +        StringBuffer b = new StringBuffer();
  +        
  +        for (;tokenizer.hasMoreTokens();) {
  +            String token = tokenizer.nextToken();
  +
  +            if (token.startsWith("{")) {
  +                int endPos = token.indexOf('}');
  +                if (endPos > 0) {
  +                    String tail = token.substring(endPos+1);
  +                    String var = token.substring(1, endPos);
  +                    
  +                    String value = Domain.getProperty(var);
  +                    if (value == null) {
  +                        // try definitions in slide.properties
  +                        value = Configuration.getProperty(var);
  +                    }
  +                    if (value == null) {
  +                        // try system definitions
  +                        value = System.getProperty(var);
  +                    }
  +                    if (value != null) {
  +                        b.append(value);
  +                    } else {
  +                        b.append("${" + var + "}");
  +                    }
  +                    
  +                    b.append(tail);
  +                }
  +            } else {
  +                b.append(token);
  +            }
  +        }
  +        
  +        return b.toString();
       }
   }
  
  
  
  1.9       +6 -44     jakarta-slide/src/share/org/apache/slide/util/conf/Populate.java
  
  Index: Populate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/conf/Populate.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Populate.java	29 Oct 2004 14:03:49 -0000	1.8
  +++ Populate.java	8 Nov 2004 09:25:02 -0000	1.9
  @@ -24,7 +24,6 @@
   package org.apache.slide.util.conf;
   
   import java.io.IOException;
  -import java.util.StringTokenizer;
   
   import org.xml.sax.Attributes;
   import org.xml.sax.ContentHandler;
  @@ -36,6 +35,8 @@
   import org.xml.sax.XMLReader;
   
   /**
  + * Helper class for reading an XML source and populating an 
  + * {@link org.apache.slide.util.conf.Element}.
    */
   public class Populate implements ContentHandler, ErrorHandler {
       
  @@ -101,8 +102,7 @@
           // object tree will be a reflection of the XML document.
           _current.setName( qName );
           for ( i = 0 ; i < attr.getLength() ; ++i )
  -            _current.setAttribute( attr.getQName( i ), 
  -                    substituteVariables(attr.getValue( i )) );
  +            _current.setAttribute( attr.getQName( i ), attr.getValue( i ) );
       }
       
       
  @@ -124,12 +124,6 @@
               throw new SAXParseException( "Attempt to close the element " + 
                   qName + " when the element " + _current.getName() + 
                   " should be closed.", _locator );
  -
  -        // substitute variables in data
  -        String data = _current.getData();
  -        if (data != null) {
  -            _current.setData(substituteVariables(data));
  -        }
           
           // All we have to do is go back to the parent.
           _current = _current.getParent();
  @@ -217,38 +211,6 @@
       
       public void warning( SAXParseException except ) {
           System.out.println( except.getMessage() );
  -    }
  -    
  -    
  -    private String substituteVariables(String text) {
  -        if (text.indexOf('$') == -1) return text;
  -        
  -        StringTokenizer tokenizer = new StringTokenizer(text, "$");
  -        
  -        StringBuffer b = new StringBuffer();
  -        
  -        for (;tokenizer.hasMoreTokens();) {
  -            String token = tokenizer.nextToken();
  -
  -            if (token.startsWith("{")) {
  -                int endPos = token.indexOf('}');
  -                if (endPos > 0) {
  -                    String tail = token.substring(endPos+1);
  -                    String var = token.substring(1, endPos);
  -                    
  -                    String value = System.getProperty(var);
  -                    if (value != null) {
  -                        b.append(value);
  -                    }
  -                    
  -                    b.append(tail);
  -                }
  -            } else {
  -                b.append(token);
  -            }
  -        }
  -        
  -        return b.toString();
       }
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org