杂记(1)

J ive 2.5

从index.jsp

<% String title = JiveGlobals.getJiveProperty("websiteconf.name"); %>

看jdom解析过程

 

jiveGlobals.getJiveProperty(String name)

 

    public static String getJiveProperty(String name) {

        loadProperties(); 
        return properties.getProperty(name);//XMLProperties

    }

    private synchronized static void loadProperties() {

            // If jiveHome is still null, no outside process has set it and

            // we have to attempt to load the value from jive_init.properties,

            // which must be in the classpath.

            if (jiveHome == null) {

                jiveHome = new InitPropLoader().getJiveHome(); //本类中方法,主要是jiveHome的路径

            }

            //Create a manager with the full path to the xml config file.
            //XMLProperties是自己写的类

            properties = new XMLProperties(jiveHome + File.separator +

                    JIVE_CONFIG_FILENAME); 
            //JIVE_CONFIG_FILENAME = "jive_config.xml";

    }

class InitPropLoader {

    public String getJiveHome() {
        String jiveHome = null;
        Properties initProps = new Properties();
        InputStream in = null;
        try {
            in = getClass().getResourceAsStream("/jive_init.properties");
            initProps.load(in);
        }
        catch (Exception e) {
            System.err.println("Error reading Jive properties "
                + "in JiveGlobals");
            e.printStackTrace();
        }
        finally {
            try {
                if (in != null) { in.close(); }
            } catch (Exception e) {}
        }
        if (initProps != null) {
        //用指定的键在此属性列表中搜索属性
            jiveHome = initProps.getProperty("jiveHome"); //Property中的方法
            if (jiveHome != null) {
                jiveHome = jiveHome.trim(); //string 到修剪方法
                //Remove trailing slashes.
                //linux 的/  windows "\\"
                while (jiveHome.endsWith("/") || jiveHome.endsWith("\\")) {
                    jiveHome = jiveHome.substring(0, jiveHome.length()-1);
                }
            }
        }
        return jiveHome;
    }
}

 

 

XMLProperties.java

    public XMLProperties(String file) {

        this.file = new File(file);

        try {

            SAXBuilder builder = new SAXBuilder();

            // Strip formatting

            DataUnformatFilter format = new DataUnformatFilter();

            builder.setXMLFilter(format);

            doc = builder.build(new File(file));

        }

        catch (Exception e) {

            System.err.println("Error creating XML parser in "

                + "PropertyManager.java");

            e.printStackTrace();

        }

    }

       public String getProperty(String name) {

        if (propertyCache.containsKey(name)) { //propertyCache是 HashMap

            return (String)propertyCache.get(name);

        }


        String[] propName = parsePropertyName(name); //parsePropertyName本类中方法
        //是一个将websiteconfig.name 字符串去掉"."号到方法

        // Search for this property by traversing down the XML heirarchy.

        Element element = doc.getRootElement();

        for (int i = 0; i < propName.length; i++) {

            element = element.getChild(propName[i]); //最终将是得到name了,前一个
            //websiteconf会被覆盖掉了吧? 2011-2-15

            if (element == null) {

                // This node doesn't match this part of the property name which

                // indicates this property doesn't exist so return null.

                return null;

            }

        }

        // At this point, we found a matching property, so return its value.

        // Empty strings are returned as null.

        String value = element.getText();

        if ("".equals(value)) {

            return null;

        }

        else {

            // Add to cache so that getting property next time is fast.

            value = value.trim();

            propertyCache.put(name, value); //此处有什么作用?2011-2-15

            return value;

        }

    }

    private String[] parsePropertyName(String name) {

        // Figure out the number of parts of the name (this becomes the size

        // of the resulting array).

        int size = 1;

        for (int i=0; i<name.length(); i++) {

            if (name.charAt(i) == '.') {

                size++;

            }

        }

        String[] propName = new String[size]; 
        // Use a StringTokenizer to tokenize the property name.
        StringTokenizer tokenizer = new StringTokenizer(name, "."); //利用提供了分割符到构造方法

        int i = 0;

        while (tokenizer.hasMoreTokens()) { //判断是否还有分割符

            propName[i] = tokenizer.nextToken(); //返回从当前位置到下一个分隔符的字符串

            i++;

        }

        return propName; 
    }
 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值