001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.htmlsanitizer;
021
022import org.nuxeo.common.xmap.annotation.XContent;
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XObject;
025
026@XObject(value = "field")
027public class FieldDescriptor {
028
029    @XContent
030    protected String contentField;
031
032    @XNode("@filter")
033    protected String filterField;
034
035    @XNode("@filterValue")
036    protected String filterValue;
037
038    @XNode("@sanitize")
039    protected boolean sanitize = true;
040
041    public String getContentField() {
042        if (contentField != null) {
043            String result = contentField.trim();
044            result = result.replace("\n", "");
045            return result;
046        }
047        return contentField;
048    }
049
050    public String getFilterField() {
051        return filterField;
052    }
053
054    public String getFilterValue() {
055        return filterValue;
056    }
057
058    public String[] getFilterValues() {
059        return filterValue.split(",");
060    }
061
062    public boolean doSanitize() {
063        return sanitize;
064    }
065
066    public boolean match(String fieldValue) {
067        for (String v : getFilterValues()) {
068            if (fieldValue.equals(v)) {
069                return true;
070            }
071        }
072        return false;
073    }
074
075    @Override
076    public String toString() {
077        if (filterField != null) {
078            return getContentField() + " if " + filterField + (sanitize ? "=" : "!=") + filterValue;
079        } else {
080            return getContentField();
081        }
082    }
083
084}