⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultconfiguration.java

📁 jConfig,JAVA读取XML的开源项目
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            props = (Properties)cat.getProperties().clone();
            println("+-+ " + cat.getCategoryName());
            if ( debug) props.list(System.err);
            println("+-+ " + cat.getCategoryName());
        }
        if ( baseConfigName != null && includeParent ) {       
            Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
            Properties parentProps = cfg.getProperties(category);
            println("!!!!!");
            if ( debug ) parentProps.list(System.err);
            println("!!!!!");
            Enumeration num = parentProps.keys();
            while ( num.hasMoreElements() ) {
                String name = (String)num.nextElement();
                println("lookin for " + name);
                if ( !props.containsKey(name) ) {
                	println("adding " + name);
                    props.setProperty(name, (String)parentProps.get(name));
                }
            }
        }   
        return props;
    }
    
    private void println(String string) {
    	if ( debug ) {
    		System.err.println( string );
    	}
	}
	/**
     * This method returns all the names of the properties
     * for the specific category
     *
     * @param category the name of the category
     * @return the names as string array
     */
    public String[] getPropertyNames(String category) {
        if ( containsCategory(category) ) {
            // FIXME: include parent properties 
            Properties properties = getProperties(category);            
            if (properties != null) {                
                Enumeration en = properties.propertyNames();
                Vector keys = new Vector();
                while ( en.hasMoreElements() ) {
                    String nm = (String)en.nextElement();                    
                    keys.add(nm);
                }
                /*
                Set keys = properties.keySet();
                 */
                return (String[]) keys.toArray(new String[0]);
            }            
        }
        return null;
    }
    
    /**
     * This method sets a variable inside the configuration.
     *
     * @param name the name of the variable
     * @param value the value of the variable
     */
    public void setVariable(String name, String value) {
        if (name != null && value != null) {
            vm.addVariable(name, value,configName);
        }
    }
    
    /**
     * It returns a HashMap with all variables with the
     * variable name as key
     *
     * @return a HashMap with all variables
     */
    public HashMap getVariables() {
        return vm.getVariables(configName);
    }
    
    public String getVariable(String name) {
        return vm.getVariable(configName, name);
    }
    
    
    /**
     * @param name
     * @param defaultValue
     * @return
     */
    public int getIntProperty(String name, int defaultValue) {
        return getIntProperty(name, defaultValue, mainCategory);
    }
    
    /**
     * @param name
     * @param defaultValue
     * @param category
     * @return
     */
    public int getIntProperty(String name, int defaultValue, String category) {
        String value = getProperty(name,null,category);
        if (value == null) {
            return defaultValue;
        } else {
            try {
                return Integer.parseInt(vm.replaceVariables(value,configName));
            } catch (NumberFormatException nfe) {
                return defaultValue;
            }
        }        
    }
    
    /**
     * Wrapper method to keep API simple
     *
     * @see Category#getBooleanProperty(String, boolean)
     * @param name
     * @param defaultValue
     * @return
     */
    public boolean getBooleanProperty(String name, boolean defaultValue) {
        return getBooleanProperty(name, defaultValue, mainCategory);
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param defaultValue
     * @param categoryName
     * @return
     */
    public boolean getBooleanProperty(String name,boolean defaultValue,String categoryName) {
        String value = getProperty(name,null,categoryName);
        if ( value == null ) {
            return defaultValue;
        } else {
            try {
                return Boolean.valueOf(vm.replaceVariables(value,configName)).booleanValue();
            } catch (Exception e) {
                return defaultValue;
            }
        }
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param key
     * @param value
     */
    public void setBooleanProperty(String key, boolean value) {
        getCategory().setBooleanProperty(key, value);
        
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param key
     * @param value
     * @param category
     */
    public void setBooleanProperty(String key, boolean value, String category) {
        getCategory(category).setBooleanProperty(key, value);
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public long getLongProperty(String name, long defaultValue) {
        return getLongProperty(name, defaultValue, mainCategory);
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param name
     * @param defaultValue
     * @param categoryName
     * @return
     */
    public long getLongProperty(String name,long defaultValue,String categoryName) {
        String value = getProperty(name,null,categoryName);
        if (value == null) {
            return defaultValue;
        } else {
            try {
                return Long.parseLong(vm.replaceVariables(value,configName));
            } catch (NumberFormatException nfe) {
                return defaultValue;
            }
        }     
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public double getDoubleProperty(String name, double defaultValue) {
        return getDoubleProperty(name, defaultValue, mainCategory);
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param name
     * @param defaultValue
     * @param category
     * @return
     */
    public double getDoubleProperty(String name,double defaultValue,String category) {
        String value = getProperty(name,null,category);
        if (value == null) {
            return defaultValue;
        } else {
            try {
                return Double.parseDouble(vm.replaceVariables(value,configName));
            } catch (Exception e) {
                return defaultValue;
            }
        }        
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public char getCharProperty(String name, char defaultValue) {
        return getCharProperty(name, defaultValue, mainCategory);
    }
    
    /**
     * Wrapper method to Category method
     *
     * @param name
     * @param defaultValue
     * @param category
     * @return
     */
    public char getCharProperty(
    String name,
    char defaultValue,
    String category) {
        Category cat = getCategory(category);
        return cat.getCharProperty(name, defaultValue);
    }
    
    /**
     * This method creates a string representation of the configuration.
     *
     * @return a string with the configuration
     */
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        String[] cats = getCategoryNames(false);
        for (int i = 0; i < cats.length; i++) {
            buffer.append("Category=");
            buffer.append(cats[i]);
            Properties props = getProperties(cats[i],false);
            if (props != null) {
                buffer.append("\n");
                Iterator nit = props.keySet().iterator();
                while (nit.hasNext()) {
                    String name = (String) nit.next();
                    String value = props.getProperty(name);
                    buffer.append("  ");
                    buffer.append(name);
                    buffer.append("=");
                    buffer.append(value);
                    buffer.append("\n");
                }
            }
        }
        return buffer.toString();
    }
    
    /**
     * This method converts the Configuration into a String
     * which looks like XML.
     *
     * @return the Configuration as String in XML format
     */
    public String getXMLAsString() {
        StringBuffer buffer = new StringBuffer();        
        // first we will write out the variable block
        // if we have some
        buffer.append("<?xml version=\"1.0\"");
        if ( getEncoding() != null ) {
            buffer.append(" encoding=\""+getEncoding()+"\"");
        }
        buffer.append(" ?>\n");
        buffer.append("<properties");
        if ( baseConfigName != null ) {
            buffer.append(" extends=\"");
            buffer.append(baseConfigName);
            buffer.append("\"");
        }
        buffer.append(">\n");
        addIncludeBlock(buffer);        
        addVariableBlock(buffer);
        // now we are writing out all categories with
        // their properties
        String[] cats = getCategoryNames(false);
        for (int i = 0; i < cats.length; i++) {
            buffer.append("  <category name=\"");
            buffer.append(escapeForXML(cats[i]));
            buffer.append("\">\n");            
            SortedMap sm = getSortedProperties(cats[i],false);
            if (sm != null) {
                Iterator nit = sm.keySet().iterator();
                while (nit.hasNext()) {
                    String name = (String) nit.next();
                    String value = (String)sm.get(name);
                    buffer.append("    <property name=\"");
                    buffer.append(escapeForXML(name));
                    buffer.append("\" value=\"");
                    // do not convert the value
                    buffer.append(escapeForXML(value));
                    buffer.append("\"/>\n");
                }
                buffer.append("  </category>\n");
            }
        }
        buffer.append("</properties>\n");
        return buffer.toString();
    }
    
    protected SortedMap getSortedProperties(String categoryName,boolean includeParent) {
        Properties props = getProperties(categoryName,includeParent);
        SortedMap sm = new TreeMap();
        if (props != null) {
            Iterator nit = props.keySet().iterator();
            while (nit.hasNext()) {
                String name = (String) nit.next();
                String value = props.getProperty(name);
                sm.put(name,value);
            }
            return sm;
        }
        return null;
    }
    
    protected void addIncludeBlock(StringBuffer buffer) {
        if ( includes.size() > 0 ) {
            for ( int i = 0; i < includes.size();i++) {
                IncludeEntry ie = (IncludeEntry)includes.get(i);
                buffer.append("  <include ");
                if ( ie.getType() == IncludeEntry.PROPERTIES ) {
                    buffer.append("properties=\"");
                    buffer.append(ie.getName());
                    buffer.append("\"/>\n");
                }
            }
        }
    }
    
    protected void addVariableBlock(StringBuffer buffer) {
        Iterator it = vm.getVariables(configName).keySet().iterator();
        boolean vars = false;
        if (it.hasNext()) {
            buffer.append("  <variables>\n");
            vars = true;
        }
        while (it.hasNext()) {
            String varName = (String) it.next();
            String varText = (String) vm.getVariables(configName).get(varName);
            buffer.append("    <variable name=\"");
            buffer.append(escapeForXML(varName));
            buffer.append("\" value=\"");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -