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;
}