Search in sources :

Example 6 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class XMLAttributes method remove.

@Override
public Object remove(Collection.Key k) throws PageException {
    String key = k.getString();
    Node rtn = null;
    if (!caseSensitive) {
        int len = nodeMap.getLength();
        String nn;
        for (int i = len - 1; i >= 0; i--) {
            nn = nodeMap.item(i).getNodeName();
            if (key.equalsIgnoreCase(nn))
                rtn = nodeMap.removeNamedItem(nn);
        }
    } else
        rtn = nodeMap.removeNamedItem(toName(key));
    if (rtn != null)
        return rtn.getNodeValue();
    throw new ExpressionException("can't remove element with name [" + key + "], element doesn't exist");
}
Also used : Node(org.w3c.dom.Node) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 7 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class XMLCaster method toCommentArray.

/**
 * casts a value to a XML Comment Array
 * @param doc XML Document
 * @param o Object to cast
 * @return XML Comment Array
 * @throws PageException
 */
public static Comment[] toCommentArray(Document doc, Object o) throws PageException {
    // Node[]
    if (o instanceof Node[]) {
        Node[] nodes = (Node[]) o;
        if (_isAllOfSameType(nodes, Node.COMMENT_NODE))
            return (Comment[]) nodes;
        Comment[] comments = new Comment[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            comments[i] = toComment(doc, nodes[i]);
        }
        return comments;
    } else // Collection
    if (o instanceof Collection) {
        Collection coll = (Collection) o;
        Iterator<Object> it = coll.valueIterator();
        List<Comment> comments = new ArrayList<Comment>();
        while (it.hasNext()) {
            comments.add(toComment(doc, it.next()));
        }
        return comments.toArray(new Comment[comments.size()]);
    }
    // Node Map and List
    Node[] nodes = _toNodeArray(doc, o);
    if (nodes != null)
        return toCommentArray(doc, nodes);
    // Single Text Node
    try {
        return new Comment[] { toComment(doc, o) };
    } catch (ExpressionException e) {
        throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Comment Array");
    }
}
Also used : Comment(org.w3c.dom.Comment) XMLException(lucee.runtime.exp.XMLException) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 8 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class XMLCaster method toAttrArray.

/**
 * casts a value to a XML Attr Array
 * @param doc XML Document
 * @param o Object to cast
 * @return XML Attr Array
 * @throws PageException
 */
public static Attr[] toAttrArray(Document doc, Object o) throws PageException {
    // Node[]
    if (o instanceof Node[]) {
        Node[] nodes = (Node[]) o;
        if (_isAllOfSameType(nodes, Node.ATTRIBUTE_NODE))
            return (Attr[]) nodes;
        Attr[] attres = new Attr[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            attres[i] = toAttr(doc, nodes[i]);
        }
        return attres;
    } else // Collection
    if (o instanceof Collection) {
        Collection coll = (Collection) o;
        Iterator<Entry<Key, Object>> it = coll.entryIterator();
        Entry<Key, Object> e;
        List<Attr> attres = new ArrayList<Attr>();
        Attr attr;
        Collection.Key k;
        while (it.hasNext()) {
            e = it.next();
            k = e.getKey();
            attr = doc.createAttribute(Decision.isNumber(k.getString()) ? "attribute-" + k.getString() : k.getString());
            attr.setValue(Caster.toString(e.getValue()));
            attres.add(attr);
        }
        return attres.toArray(new Attr[attres.size()]);
    }
    // Node Map and List
    Node[] nodes = _toNodeArray(doc, o);
    if (nodes != null)
        return toAttrArray(doc, nodes);
    // Single Text Node
    try {
        return new Attr[] { toAttr(doc, o) };
    } catch (ExpressionException e) {
        throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Attributes Array");
    }
}
Also used : Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr) ExpressionException(lucee.runtime.exp.ExpressionException) Entry(java.util.Map.Entry) XMLException(lucee.runtime.exp.XMLException) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Key(lucee.runtime.type.Collection.Key)

Example 9 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class XMLNodeList method insert.

@Override
public boolean insert(int index, Object value) throws PageException {
    // check min Index
    if (index < 1)
        throw new ExpressionException("invalid index [" + index + "] to insert a child node, valid indexes start at 1");
    Node[] nodes = getChildNodesAsArray();
    // if index Greater len append
    if (index > nodes.length) {
        append(value);
        return true;
    }
    // remove all children
    clear();
    // set all children before new Element
    for (int i = 1; i < index; i++) {
        append(nodes[i - 1]);
    }
    // set new Element
    append(XMLCaster.toNode(doc, value, true));
    // set all after new Element
    for (int i = index; i <= nodes.length; i++) {
        append(nodes[i - 1]);
    }
    return true;
}
Also used : Node(org.w3c.dom.Node) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 10 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class XMLNodeList method setE.

@Override
public Object setE(int index, Object value) throws PageException {
    // check min Index
    if (index < 1)
        throw new ExpressionException("invalid index [" + index + "] to set a child node, valid indexes start at 1");
    Node[] nodes = getChildNodesAsArray();
    // if index Greater len append
    if (index > nodes.length)
        return append(value);
    // remove all children
    clear();
    // set all children before new Element
    for (int i = 1; i < index; i++) {
        append(nodes[i - 1]);
    }
    // set new Element
    append(XMLCaster.toNode(doc, value, true));
    // set all after new Element
    for (int i = index; i < nodes.length; i++) {
        append(nodes[i]);
    }
    return value;
}
Also used : Node(org.w3c.dom.Node) ExpressionException(lucee.runtime.exp.ExpressionException)

Aggregations

ExpressionException (lucee.runtime.exp.ExpressionException)136 Element (org.w3c.dom.Element)24 Key (lucee.runtime.type.Collection.Key)15 SecurityException (lucee.runtime.exp.SecurityException)13 ArrayList (java.util.ArrayList)12 Struct (lucee.runtime.type.Struct)12 List (java.util.List)11 Collection (lucee.runtime.type.Collection)11 Entry (java.util.Map.Entry)10 Node (org.w3c.dom.Node)10 Resource (lucee.commons.io.res.Resource)9 Iterator (java.util.Iterator)8 PageException (lucee.runtime.exp.PageException)8 Map (java.util.Map)7 CasterException (lucee.runtime.exp.CasterException)7 NodeList (org.w3c.dom.NodeList)7 PageContextImpl (lucee.runtime.PageContextImpl)6 PageSource (lucee.runtime.PageSource)5 Casting (lucee.runtime.interpreter.ref.cast.Casting)5 ArrayImpl (lucee.runtime.type.ArrayImpl)5