Search in sources :

Example 1 with CasterException

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

the class CFTag method validateAttributes.

private static void validateAttributes(Component cfc, StructImpl attributesScope, String tagName) throws ApplicationException, ExpressionException {
    TagLibTag tag = getAttributeRequirments(cfc, false);
    if (tag == null)
        return;
    if (tag.getAttributeType() == TagLibTag.ATTRIBUTE_TYPE_FIXED || tag.getAttributeType() == TagLibTag.ATTRIBUTE_TYPE_MIXED) {
        Iterator<Entry<String, TagLibTagAttr>> it = tag.getAttributes().entrySet().iterator();
        int count = 0;
        Collection.Key key;
        TagLibTagAttr attr;
        Object value;
        Entry<String, TagLibTagAttr> entry;
        // check existing attributes
        while (it.hasNext()) {
            entry = it.next();
            count++;
            key = KeyImpl.toKey(entry.getKey(), null);
            attr = entry.getValue();
            value = attributesScope.get(key, null);
            // check alias
            if (value == null) {
                String[] alias = attr.getAlias();
                if (!ArrayUtil.isEmpty(alias))
                    for (int i = 0; i < alias.length; i++) {
                        value = attributesScope.get(KeyImpl.toKey(alias[i], null), null);
                        if (value != null)
                            break;
                    }
            }
            if (value == null) {
                if (attr.getDefaultValue() != null) {
                    value = attr.getDefaultValue();
                    attributesScope.setEL(key, value);
                } else if (attr.isRequired())
                    throw new ApplicationException("attribute [" + key.getString() + "] is required for tag [" + tagName + "]");
            }
            if (value != null) {
                if (!Decision.isCastableTo(attr.getType(), value, true, true, -1))
                    throw new CasterException(createMessage(attr.getType(), value));
            }
        }
        // check if there are attributes not supported
        if (tag.getAttributeType() == TagLibTag.ATTRIBUTE_TYPE_FIXED && count < attributesScope.size()) {
            Collection.Key[] keys = attributesScope.keys();
            for (int i = 0; i < keys.length; i++) {
                if (tag.getAttribute(keys[i].getLowerString(), true) == null)
                    throw new ApplicationException("attribute [" + keys[i].getString() + "] is not supported for tag [" + tagName + "]");
            }
        // Attribute susi is not allowed for tag cfmail
        }
    }
}
Also used : TagLibTagAttr(lucee.transformer.library.tag.TagLibTagAttr) TagLibTag(lucee.transformer.library.tag.TagLibTag) CasterException(lucee.runtime.exp.CasterException) Key(lucee.runtime.type.Collection.Key) Entry(java.util.Map.Entry) ApplicationException(lucee.runtime.exp.ApplicationException) Collection(lucee.runtime.type.Collection) Key(lucee.runtime.type.Collection.Key)

Example 2 with CasterException

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

the class Caster method toArray.

/**
 * cast a Object to a Array Object
 * @param o Object to cast
 * @return casted Array
 * @throws PageException
 */
public static Array toArray(Object o) throws PageException {
    if (o instanceof Array)
        return (Array) o;
    else if (o instanceof Object[]) {
        return new ArrayImpl((Object[]) o);
    } else if (o instanceof List) {
        // new ArrayImpl(((List) o).toArray());
        return ListAsArray.toArray((List) o);
    } else if (o instanceof Set) {
        // new ArrayImpl(((List) o).toArray());
        return toArray(((Set) o).toArray());
    } else if (o instanceof XMLStruct) {
        XMLMultiElementStruct xmes;
        if (o instanceof XMLMultiElementStruct) {
            xmes = (XMLMultiElementStruct) o;
        } else {
            XMLStruct sct = (XMLStruct) o;
            Array a = new ArrayImpl();
            a.append(o);
            xmes = new XMLMultiElementStruct(a, sct.getCaseSensitive());
        }
        return new XMLMultiElementArray(xmes);
    } else if (o instanceof ObjectWrap) {
        return toArray(((ObjectWrap) o).getEmbededObject());
    } else if (o instanceof Struct) {
        // function _toArray
        if (o instanceof Component) {
            Component c = (Component) o;
            PageContext pc = ThreadLocalPageContext.get();
            if (pc != null) {
                Member member = c.getMember(Component.ACCESS_PRIVATE, KeyConstants.__toArray, false, false);
                // Object o = get(pc,"_toString",null);
                if (member instanceof UDFPlus) {
                    UDFPlus udf = (UDFPlus) member;
                    if (udf.getReturnType() == CFTypes.TYPE_ARRAY && udf.getFunctionArguments().length == 0) {
                        return Caster.toArray(c.call(pc, KeyConstants.__toArray, new Object[0]));
                    }
                }
            }
        }
        Struct sct = (Struct) o;
        Array arr = new ArrayImpl();
        Iterator<Entry<Key, Object>> it = sct.entryIterator();
        Entry<Key, Object> e = null;
        try {
            while (it.hasNext()) {
                e = it.next();
                arr.setE(toIntValue(e.getKey().getString()), e.getValue());
            }
        } catch (ExpressionException ee) {
            throw new ExpressionException("can't cast struct to a array, key [" + e.getKey().getString() + "] is not a number");
        }
        return arr;
    } else if (o instanceof boolean[])
        return new ArrayImpl(ArrayUtil.toReferenceType((boolean[]) o));
    else if (o instanceof byte[])
        return new ArrayImpl(ArrayUtil.toReferenceType((byte[]) o));
    else if (o instanceof char[])
        return new ArrayImpl(ArrayUtil.toReferenceType((char[]) o));
    else if (o instanceof short[])
        return new ArrayImpl(ArrayUtil.toReferenceType((short[]) o));
    else if (o instanceof int[])
        return new ArrayImpl(ArrayUtil.toReferenceType((int[]) o));
    else if (o instanceof long[])
        return new ArrayImpl(ArrayUtil.toReferenceType((long[]) o));
    else if (o instanceof float[])
        return new ArrayImpl(ArrayUtil.toReferenceType((float[]) o));
    else if (o instanceof double[])
        return new ArrayImpl(ArrayUtil.toReferenceType((double[]) o));
    throw new CasterException(o, "Array");
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) ArrayImpl(lucee.runtime.type.ArrayImpl) ExpressionException(lucee.runtime.exp.ExpressionException) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) ObjectStruct(lucee.runtime.type.scope.ObjectStruct) MapAsStruct(lucee.runtime.type.wrap.MapAsStruct) Struct(lucee.runtime.type.Struct) CollectionStruct(lucee.runtime.type.CollectionStruct) Entry(java.util.Map.Entry) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) ArrayList(java.util.ArrayList) ArrayAsList(lucee.runtime.type.wrap.ArrayAsList) List(java.util.List) NodeList(org.w3c.dom.NodeList) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) Component(lucee.runtime.Component) Member(lucee.runtime.component.Member) CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) UDFPlus(lucee.runtime.type.UDFPlus) Array(lucee.runtime.type.Array) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ListAsArray(lucee.runtime.type.wrap.ListAsArray) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) JavaObject(lucee.runtime.java.JavaObject) Key(lucee.runtime.type.Collection.Key)

Example 3 with CasterException

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

the class Caster method toList.

/**
 * cast a Object to a Array Object
 * @param o Object to cast
 * @param duplicate
 * @return casted Array
 * @throws PageException
 */
public static List toList(Object o, boolean duplicate) throws PageException {
    if (o instanceof List) {
        if (duplicate) {
            List src = (List) o;
            int size = src.size();
            ArrayList trg = new ArrayList();
            for (int i = 0; i < size; i++) {
                trg.add(i, src.get(i));
            }
            return trg;
        }
        return (List) o;
    } else if (o instanceof Object[]) {
        ArrayList list = new ArrayList();
        Object[] arr = (Object[]) o;
        for (int i = 0; i < arr.length; i++) list.add(i, arr[i]);
        return list;
    } else if (o instanceof Array) {
        if (!duplicate)
            return ArrayAsList.toList((Array) o);
        ArrayList list = new ArrayList();
        Array arr = (Array) o;
        for (int i = 0; i < arr.size(); i++) list.add(i, arr.get(i + 1, null));
        return list;
    } else if (o instanceof Iterator) {
        Iterator it = (Iterator) o;
        ArrayList list = new ArrayList();
        while (it.hasNext()) {
            list.add(it.next());
        }
        return list;
    } else if (o instanceof XMLStruct) {
        XMLStruct sct = ((XMLStruct) o);
        if (sct instanceof XMLMultiElementStruct)
            return toList(new XMLMultiElementArray((XMLMultiElementStruct) o));
        ArrayList list = new ArrayList();
        list.add(sct);
        return list;
    } else if (o instanceof ObjectWrap) {
        return toList(((ObjectWrap) o).getEmbededObject());
    } else if (o instanceof Struct) {
        Struct sct = (Struct) o;
        ArrayList arr = new ArrayList();
        Iterator<Entry<Key, Object>> it = sct.entryIterator();
        Entry<Key, Object> e = null;
        try {
            while (it.hasNext()) {
                e = it.next();
                arr.add(toIntValue(e.getKey().getString()), e.getValue());
            }
        } catch (ExpressionException ee) {
            throw new ExpressionException("can't cast struct to a array, key [" + (e != null ? e.getKey() : "") + "] is not a number");
        }
        return arr;
    } else if (o instanceof boolean[])
        return toList(ArrayUtil.toReferenceType((boolean[]) o));
    else if (o instanceof byte[])
        return toList(ArrayUtil.toReferenceType((byte[]) o));
    else if (o instanceof char[])
        return toList(ArrayUtil.toReferenceType((char[]) o));
    else if (o instanceof short[])
        return toList(ArrayUtil.toReferenceType((short[]) o));
    else if (o instanceof int[])
        return toList(ArrayUtil.toReferenceType((int[]) o));
    else if (o instanceof long[])
        return toList(ArrayUtil.toReferenceType((long[]) o));
    else if (o instanceof float[])
        return toList(ArrayUtil.toReferenceType((float[]) o));
    else if (o instanceof double[])
        return toList(ArrayUtil.toReferenceType((double[]) o));
    throw new CasterException(o, "List");
}
Also used : CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ArrayList(java.util.ArrayList) ExpressionException(lucee.runtime.exp.ExpressionException) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) ObjectStruct(lucee.runtime.type.scope.ObjectStruct) MapAsStruct(lucee.runtime.type.wrap.MapAsStruct) Struct(lucee.runtime.type.Struct) CollectionStruct(lucee.runtime.type.CollectionStruct) Array(lucee.runtime.type.Array) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ListAsArray(lucee.runtime.type.wrap.ListAsArray) Entry(java.util.Map.Entry) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) Iterator(java.util.Iterator) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) ArrayList(java.util.ArrayList) ArrayAsList(lucee.runtime.type.wrap.ArrayAsList) List(java.util.List) NodeList(org.w3c.dom.NodeList) JavaObject(lucee.runtime.java.JavaObject) Key(lucee.runtime.type.Collection.Key)

Example 4 with CasterException

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

the class Caster method _castTo.

private static Object _castTo(PageContext pc, String strType, Object o) throws PageException {
    if (o instanceof Component) {
        Component comp = ((Component) o);
        if (comp.instanceOf(strType))
            return o;
        throw new ExpressionException("can't cast Component of Type [" + comp.getAbsName() + "] to [" + strType + "]");
    }
    if (o instanceof Pojo) {
        Component cfc = AxisCaster.toComponent(pc, ((Pojo) o), strType, null);
        if (cfc != null)
            return cfc;
        throw new ExpressionException("can't cast Pojo of Type [" + o.getClass().getName() + "] to [" + strType + "]");
    }
    if (strType.endsWith("[]") && Decision.isArray(o)) {
        String _strType = strType.substring(0, strType.length() - 2);
        short _type = CFTypes.toShort(_strType, false, (short) -1);
        Array arr = Caster.toArray(o, null);
        if (arr != null) {
            // convert the values
            Iterator<Entry<Key, Object>> it = arr.entryIterator();
            Array _arr = new ArrayImpl();
            Entry<Key, Object> e;
            Object src, trg;
            boolean hasChanged = false;
            while (it.hasNext()) {
                e = it.next();
                src = e.getValue();
                trg = castTo(pc, _type, _strType, src);
                _arr.setEL(e.getKey(), trg);
                if (src != trg)
                    hasChanged = true;
            }
            if (!hasChanged)
                return arr;
            return _arr;
        }
    }
    throw new CasterException(o, strType);
}
Also used : Pojo(lucee.runtime.net.rpc.Pojo) CasterException(lucee.runtime.exp.CasterException) ArrayImpl(lucee.runtime.type.ArrayImpl) ExpressionException(lucee.runtime.exp.ExpressionException) Array(lucee.runtime.type.Array) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ListAsArray(lucee.runtime.type.wrap.ListAsArray) Entry(java.util.Map.Entry) JavaObject(lucee.runtime.java.JavaObject) Component(lucee.runtime.Component) Key(lucee.runtime.type.Collection.Key)

Example 5 with CasterException

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

the class DateFormat method _call.

private static String _call(PageContext pc, Object object, String mask, TimeZone tz) throws PageException {
    Locale locale = Locale.US;
    DateTime datetime = DateCaster.toDateAdvanced(object, tz, null);
    // Caster.toDate(object,true,tz,null);
    if (datetime == null) {
        if (StringUtil.isEmpty(object, true))
            return "";
        throw new CasterException(object, "datetime");
    // if(!Decision.isSimpleValue(object))
    // throw new ExpressionException("can't convert object of type "+Type.getName(object)+" to a datetime value");
    // throw new ExpressionException("can't convert value "+object+" to a datetime value");
    }
    return new lucee.runtime.format.DateFormat(locale).format(datetime, mask, tz);
}
Also used : Locale(java.util.Locale) CasterException(lucee.runtime.exp.CasterException) DateTime(lucee.runtime.type.dt.DateTime)

Aggregations

CasterException (lucee.runtime.exp.CasterException)12 ExpressionException (lucee.runtime.exp.ExpressionException)6 Key (lucee.runtime.type.Collection.Key)5 Entry (java.util.Map.Entry)4 ObjectWrap (lucee.runtime.type.ObjectWrap)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JavaObject (lucee.runtime.java.JavaObject)3 XMLMultiElementArray (lucee.runtime.text.xml.struct.XMLMultiElementArray)3 Array (lucee.runtime.type.Array)3 ArrayAsList (lucee.runtime.type.wrap.ArrayAsList)3 ListAsArray (lucee.runtime.type.wrap.ListAsArray)3 NodeList (org.w3c.dom.NodeList)3 Locale (java.util.Locale)2 Component (lucee.runtime.Component)2 XMLMultiElementStruct (lucee.runtime.text.xml.struct.XMLMultiElementStruct)2 XMLStruct (lucee.runtime.text.xml.struct.XMLStruct)2 ArrayImpl (lucee.runtime.type.ArrayImpl)2 CollectionStruct (lucee.runtime.type.CollectionStruct)2 Struct (lucee.runtime.type.Struct)2