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");
}
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");
}
}
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");
}
}
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;
}
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");
}
}
Aggregations