001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.sanitizer.Sanitizer;
019    import com.liferay.portal.kernel.sanitizer.SanitizerUtil;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portlet.dynamicdatamapping.model.Value;
022    
023    import java.util.Locale;
024    
025    /**
026     * @author Marcellus Tavares
027     */
028    public class HTMLSanitizerDDMFormFieldValueTransformer
029            implements DDMFormFieldValueTransformer {
030    
031            public HTMLSanitizerDDMFormFieldValueTransformer(
032                    long companyId, long groupId, long userId) {
033    
034                    _companyId = companyId;
035                    _groupId = groupId;
036                    _userId = userId;
037            }
038    
039            @Override
040            public String getFieldType() {
041                    return "ddm-text-html";
042            }
043    
044            @Override
045            public void transform(Value value) throws PortalException {
046                    for (Locale locale : value.getAvailableLocales()) {
047                            String sanitizedValue = sanitize(value.getString(locale));
048    
049                            value.addString(locale, sanitizedValue);
050                    }
051            }
052    
053            protected String sanitize(String value) throws PortalException {
054                    return SanitizerUtil.sanitize(
055                            _companyId, _groupId, _userId, Value.class.getName(), 0,
056                            ContentTypes.TEXT_HTML, Sanitizer.MODE_ALL, value, null);
057            }
058    
059            private final long _companyId;
060            private final long _groupId;
061            private final long _userId;
062    
063    }