XmlDataParser.java 文件源码

java
阅读 21 收藏 0 点赞 0 评论 0

项目:easycode 作者:
public static Map parseMapElement(Element mapEle) throws XMLParseException{
    String defaultKeyTypeClassName = mapEle.attributeValue(KEY_TYPE_ATTRIBUTE);
    String defaultValueTypeClassName = mapEle.attributeValue(VALUE_TYPE_ATTRIBUTE);

    List entryEles = mapEle.elements(ENTRY_ELEMENT);
    Map map = new HashMap(entryEles.size());
    for (Iterator it = entryEles.iterator(); it.hasNext();) {
        Element entryEle = (Element) it.next();
        // Should only have one value child element: value, list, etc.
        // Optionally, there might be a key child element.
        List<Element> el = entryEle.elements();

        Element keyEle = null;
        Element valueEle = null;
        for (Element candidateEle : el) {
            if (KEY_ELEMENT.equals(candidateEle.getName())) {
                if (keyEle != null) {
                    throw new XMLParseException(entryEle.attributeValue("name") +
                            "<entry> element is only allowed to contain one <key> sub-element");
                } else {
                    keyEle = candidateEle;
                }
            } else {
                if (valueEle != null) {
                    throw new XMLParseException(entryEle.attributeValue("name") +
                            "<entry> element must not contain more than one value sub-element");
                } else {
                    valueEle = candidateEle;
                }
            }
        }

        // Extract key from attribute or sub-element.
        Object key;
        Attribute keyAttribute = entryEle.attribute(KEY_ATTRIBUTE);
        if (keyAttribute != null && keyEle != null) {
            throw new XMLParseException(entryEle + "<entry> element is only allowed to contain either " +
                    "a 'key' attribute OR a <key> sub-element");
        }
        if (keyAttribute != null) {
            key = buildTypedStringValueForMap(
                    entryEle.attributeValue(KEY_ATTRIBUTE), defaultKeyTypeClassName, entryEle);
        } else if (keyEle != null) {
            key = parseKeyElement(keyEle,defaultKeyTypeClassName);
        } else {
            throw new XMLParseException("<entry> element must specify a key" + entryEle);
        }

        // Extract value from attribute or sub-element.
        Object value;
        Attribute valueAttribute = entryEle.attribute(VALUE_ATTRIBUTE);
        if (valueAttribute != null && valueEle != null) {
            throw new XMLParseException("<entry> element is only allowed to contain either " +
                    "'value' attribute OR <value> sub-element" + entryEle);
        }
        if (valueAttribute != null) {
            value = buildTypedStringValueForMap(
                    entryEle.attributeValue(VALUE_ATTRIBUTE), defaultValueTypeClassName, entryEle);
        }
        else if (valueEle != null) {
            value = parseDataSubElement(valueEle,defaultValueTypeClassName);
        }
        else {
            throw new XMLParseException("<entry> element must specify a value" + entryEle);
        }
        map.put(key, value);
    }

    return map;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号