Search in sources :

Example 6 with XMLException

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

the class XMLCaster method toElementArray.

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

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

the class XMLValidator method validate.

public Struct validate(InputSource xml) throws XMLException {
    warnings = new ArrayImpl();
    errors = new ArrayImpl();
    fatals = new ArrayImpl();
    try {
        XMLReader parser = XMLUtil.createXMLReader();
        parser.setContentHandler(this);
        parser.setErrorHandler(this);
        parser.setEntityResolver(this);
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setFeature("http://apache.org/xml/features/validation/schema", true);
        parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        // if(!validateNamespace)
        if (!StringUtil.isEmpty(strSchema))
            parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", strSchema);
        parser.parse(xml);
    } catch (SAXException e) {
    } catch (IOException e) {
        throw new XMLException(e.getMessage());
    }
    // result
    Struct result = new StructImpl();
    result.setEL("warnings", warnings);
    result.setEL("errors", errors);
    result.setEL("fatalerrors", fatals);
    result.setEL("status", Caster.toBoolean(!hasErrors));
    release();
    return result;
}
Also used : XMLException(lucee.runtime.exp.XMLException) StructImpl(lucee.runtime.type.StructImpl) ArrayImpl(lucee.runtime.type.ArrayImpl) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) Struct(lucee.runtime.type.Struct)

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