Search in sources :

Example 1 with XMLException

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

the class XMLCaster method toAttr.

/**
 * casts a value to a XML Attribute Object
 * @param doc XML Document
 * @param o Object to cast
 * @return XML Comment Object
 * @throws PageException
 */
public static Attr toAttr(Document doc, Object o) throws PageException {
    if (o instanceof Attr)
        return (Attr) o;
    if (o instanceof Struct && ((Struct) o).size() == 1) {
        Struct sct = (Struct) o;
        Entry<Key, Object> e = sct.entryIterator().next();
        Attr attr = doc.createAttribute(e.getKey().getString());
        attr.setValue(Caster.toString(e.getValue()));
        return attr;
    }
    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Attribute");
}
Also used : XMLException(lucee.runtime.exp.XMLException) Attr(org.w3c.dom.Attr) Key(lucee.runtime.type.Collection.Key) Struct(lucee.runtime.type.Struct) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct)

Example 2 with XMLException

use of lucee.runtime.exp.XMLException 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 3 with XMLException

use of lucee.runtime.exp.XMLException 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 4 with XMLException

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

the class XMLAttributes method set.

@Override
public Object set(Collection.Key key, Object value) throws PageException {
    if (owner == null)
        return value;
    try {
        Attr attr = owner.createAttribute(toName(key.getString()));
        attr.setValue(Caster.toString(value));
        nodeMap.setNamedItem(attr);
    } catch (DOMException de) {
        throw new XMLException(de);
    }
    return value;
}
Also used : DOMException(org.w3c.dom.DOMException) XMLException(lucee.runtime.exp.XMLException) Attr(org.w3c.dom.Attr)

Example 5 with XMLException

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

the class XMLCaster method toTextArray.

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

Aggregations

XMLException (lucee.runtime.exp.XMLException)7 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 List (java.util.List)4 ExpressionException (lucee.runtime.exp.ExpressionException)4 Collection (lucee.runtime.type.Collection)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 Attr (org.w3c.dom.Attr)3 Key (lucee.runtime.type.Collection.Key)2 Struct (lucee.runtime.type.Struct)2 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 XMLStruct (lucee.runtime.text.xml.struct.XMLStruct)1 ArrayImpl (lucee.runtime.type.ArrayImpl)1 StructImpl (lucee.runtime.type.StructImpl)1 Comment (org.w3c.dom.Comment)1 DOMException (org.w3c.dom.DOMException)1 Element (org.w3c.dom.Element)1 Text (org.w3c.dom.Text)1