public TemplateModel get()

in freemarker-core/src/main/java/freemarker/ext/dom/ElementModel.java [60:129]


    public TemplateModel get(String key) throws TemplateModelException {
        if (key.equals("*")) {
            NodeListModel ns = new NodeListModel(this);
            TemplateSequenceModel children = getChildNodes();
            int size = children.size();
            for (int i = 0; i < size; i++) {
                NodeModel child = (NodeModel) children.get(i);
                if (child.node.getNodeType() == Node.ELEMENT_NODE) {
                    ns.add(child);
                }
            }
            return ns;
        } else if (key.equals("**")) {
            return new NodeListModel(((Element) node).getElementsByTagName("*"), this);    
        } else if (key.startsWith("@")) {
            if (key.startsWith("@@")) {
                if (key.equals(AtAtKey.ATTRIBUTES.getKey())) {
                    return new NodeListModel(node.getAttributes(), this);
                } else if (key.equals(AtAtKey.START_TAG.getKey())) {
                    NodeOutputter nodeOutputter = new NodeOutputter(node);
                    return new SimpleScalar(nodeOutputter.getOpeningTag((Element) node));
                } else if (key.equals(AtAtKey.END_TAG.getKey())) {
                    NodeOutputter nodeOutputter = new NodeOutputter(node);
                    return new SimpleScalar(nodeOutputter.getClosingTag((Element) node));
                } else if (key.equals(AtAtKey.ATTRIBUTES_MARKUP.getKey())) {
                    StringBuilder buf = new StringBuilder();
                    NodeOutputter nu = new NodeOutputter(node);
                    nu.outputContent(node.getAttributes(), buf);
                    return new SimpleScalar(buf.toString().trim());
                } else if (key.equals(AtAtKey.PREVIOUS_SIBLING_ELEMENT.getKey())) {
                    Node previousSibling = node.getPreviousSibling();
                    while (previousSibling != null && !this.isSignificantNode(previousSibling)) {
                        previousSibling = previousSibling.getPreviousSibling();
                    }
                    return previousSibling != null && previousSibling.getNodeType() == Node.ELEMENT_NODE
                            ? wrap(previousSibling) : new NodeListModel(Collections.emptyList(), null);  
                } else if (key.equals(AtAtKey.NEXT_SIBLING_ELEMENT.getKey())) {
                    Node nextSibling = node.getNextSibling();
                    while (nextSibling != null && !this.isSignificantNode(nextSibling)) {
                        nextSibling = nextSibling.getNextSibling();
                    }
                    return nextSibling != null && nextSibling.getNodeType() == Node.ELEMENT_NODE
                            ? wrap(nextSibling) : new NodeListModel(Collections.emptyList(), null);  
                } else {
                    // We don't know anything like this that's element-specific; fall back 
                    return super.get(key);
                }
            } else { // Starts with "@", but not with "@@"
                if (DomStringUtil.isXMLNameLike(key, 1)) {
                    Attr att = getAttribute(key.substring(1));
                    if (att == null) { 
                        return new NodeListModel(this);
                    }
                    return wrap(att);
                } else if (key.equals("@*")) {
                    return new NodeListModel(node.getAttributes(), this);
                } else {
                    // We don't know anything like this that's element-specific; fall back 
                    return super.get(key);
                }
            }
        } else if (DomStringUtil.isXMLNameLike(key)) {
            // We interpret key as an element name
            NodeListModel result = ((NodeListModel) getChildNodes()).filterByName(key);
            return result.size() != 1 ? result : result.get(0);
        } else {
            // We don't anything like this that's element-specific; fall back 
            return super.get(key);
        }
    }