001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     vpasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.core.exception;
018
019import org.apache.commons.lang.StringEscapeUtils;
020import org.nuxeo.ecm.automation.AutomationFilter;
021import org.nuxeo.ecm.automation.core.AutomationFilterDescriptor;
022import org.nuxeo.ecm.automation.core.scripting.Expression;
023import org.nuxeo.ecm.automation.core.scripting.Scripting;
024
025/**
026 * @since 5.7.3
027 */
028public class ChainExceptionFilter implements AutomationFilter {
029
030    protected String id;
031
032    protected Expression value;
033
034    @Override
035    public Expression getValue() {
036        return value;
037    }
038
039    @Override
040    public String getId() {
041        return id;
042    }
043
044    public ChainExceptionFilter(String id, String value) {
045        this.id = id;
046        if (value.startsWith("expr:")) {
047            value = value.substring(5).trim();
048            // Unescape xml checking
049            value = StringEscapeUtils.unescapeXml(value);
050            if (value.contains("@{")) {
051                this.value = Scripting.newTemplate(value);
052            } else {
053                this.value = Scripting.newExpression(value);
054            }
055        }
056    }
057
058    public ChainExceptionFilter(AutomationFilterDescriptor automationFilterDescriptor) {
059        id = automationFilterDescriptor.getId();
060        String value = automationFilterDescriptor.getValue();
061        if (value.startsWith("expr:")) {
062            value = value.substring(5).trim();
063            // Unescape xml checking
064            value = StringEscapeUtils.unescapeXml(value);
065            if (value.contains("@{")) {
066                this.value = Scripting.newTemplate(value);
067            } else {
068                this.value = Scripting.newExpression(value);
069            }
070        }
071    }
072
073}