001/*
002 * (C) Copyright 2010 Nuxeo SAS (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.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 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.platform.htmlsanitizer;
019
020import org.nuxeo.common.xmap.annotation.XContent;
021import org.nuxeo.common.xmap.annotation.XNode;
022import org.nuxeo.common.xmap.annotation.XObject;
023
024@XObject(value = "field")
025public class FieldDescriptor {
026
027    @XContent
028    protected String contentField;
029
030    @XNode("@filter")
031    protected String filterField;
032
033    @XNode("@filterValue")
034    protected String filterValue;
035
036    @XNode("@sanitize")
037    protected boolean sanitize = true;
038
039    public String getContentField() {
040        if (contentField != null) {
041            String result = contentField.trim();
042            result = result.replace("\n", "");
043            return result;
044        }
045        return contentField;
046    }
047
048    public String getFilterField() {
049        return filterField;
050    }
051
052    public String getFilterValue() {
053        return filterValue;
054    }
055
056    public String[] getFilterValues() {
057        return filterValue.split(",");
058    }
059
060    public boolean doSanitize() {
061        return sanitize;
062    }
063
064    public boolean match(String fieldValue) {
065        for (String v : getFilterValues()) {
066            if (fieldValue.equals(v)) {
067                return true;
068            }
069        }
070        return false;
071    }
072
073    @Override
074    public String toString() {
075        if (filterField != null) {
076            return getContentField() + " if " + filterField + (sanitize ? "=" : "!=") + filterValue;
077        } else {
078            return getContentField();
079        }
080    }
081
082}