Chapter 2 - Jetty读取配置文件和Loader (2)

[color=orange]1. XmlConfiguration的value方法[/color]

读取某个节点的值,并进行相应处理:修剪空字符串,解析并拼接结果


private Object value(Object obj, XmlParser.Node node) throws Exception
{
Object value = null;

// Get the type
String type = node.getAttribute("type");

// Try a ref lookup
String ref = node.getAttribute("ref");
if (ref != null)
{
value = _idMap.get(ref);
}
else
{
// handle trivial case
if (node.size() == 0)
{
if ("String".equals(type)) return "";
return null;
}

// Trim values
int first = 0;
int last = node.size() - 1;

// Handle default trim type
if (type == null || !"String".equals(type))
{
// Skip leading white
Object item = null;
while (first <= last)
{
item = node.get(first);
if (!(item instanceof String)) break;
item = ((String) item).trim();
if (((String) item).length() > 0) break;
first++;
}

// Skip trailing white
while (first < last)
{
item = node.get(last);
if (!(item instanceof String)) break;
item = ((String) item).trim();
if (((String) item).length() > 0) break;
last--;
}

// All white, so return null
if (first > last) return null;
}

if (first == last)
// Single Item value
value = itemValue(obj, node.get(first));
else
{
// Get the multiple items as a single string
StringBuffer buf = new StringBuffer();
synchronized (buf)
{
for (int i = first; i <= last; i++)
{
Object item = node.get(i);
buf.append(itemValue(obj, item));
}
value = buf.toString();
}
}
}

// Untyped or unknown
if (value == null)
{
if ("String".equals(type)) return "";
return null;
}

// Try to type the object
if (type == null)
{
if (value != null && value instanceof String) return ((String) value).trim();
return value;
}

if ("String".equals(type) || "java.lang.String".equals(type)) return value.toString();

Class pClass = TypeUtil.fromName(type);
if (pClass != null) return TypeUtil.valueOf(pClass, value.toString());

if ("URL".equals(type) || "java.net.URL".equals(type))
{
if (value instanceof URL) return value;
try
{
return new URL(value.toString());
}
catch (MalformedURLException e)
{
throw new InvocationTargetException(e);
}
}

if ("InetAddress".equals(type) || "java.net.InetAddress".equals(type))
{
if (value instanceof InetAddress) return value;
try
{
return InetAddress.getByName(value.toString());
}
catch (UnknownHostException e)
{
throw new InvocationTargetException(e);
}
}

throw new IllegalStateException("Unknown type " + type);
}


[color=orange]2. XmlConfiguration的itemValue方法[/color]

读取单个节点的值,如果item是String类型,直接返回
如果是其它的tag,还需要调用对应的处理方法


private Object itemValue(Object obj, Object item) throws Exception
{
// String value
if (item instanceof String) return item;

XmlParser.Node node = (XmlParser.Node) item;
String tag = node.getTag();
if ("Call".equals(tag)) return call(obj, node);
if ("Get".equals(tag)) return get(obj, node);
if ("New".equals(tag)) return newObj(obj, node);
if ("Ref".equals(tag)) return refObj(obj, node);
if ("Array".equals(tag)) return newArray(obj, node);
if ("Map".equals(tag)) return newMap(obj, node);
if ("Property".equals(tag)) return propertyObj(obj,node);

if ("SystemProperty".equals(tag))
{
String name = node.getAttribute("name");
String defaultValue = node.getAttribute("default");
return System.getProperty(name, defaultValue);
}

Log.warn("Unknown value tag: " + node, new Throwable());
return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值