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.taglib.aui;
016    
017    import com.liferay.portal.kernel.servlet.taglib.aui.ValidatorTag;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.ModelHintsUtil;
022    import com.liferay.taglib.BaseValidatorTagSupport;
023    import com.liferay.taglib.aui.base.BaseValidatorTagImpl;
024    
025    import javax.servlet.jsp.tagext.BodyContent;
026    import javax.servlet.jsp.tagext.BodyTag;
027    
028    /**
029     * @author Julio Camarero
030     * @author Brian Wing Shun Chan
031     */
032    public class ValidatorTagImpl
033            extends BaseValidatorTagImpl implements BodyTag, ValidatorTag {
034    
035            public ValidatorTagImpl() {
036            }
037    
038            public ValidatorTagImpl(
039                    String name, String errorMessage, String body, boolean custom) {
040    
041                    setName(name);
042                    setErrorMessage(errorMessage);
043    
044                    _body = body;
045                    _custom = custom;
046            }
047    
048            @Override
049            public void cleanUp() {
050                    super.cleanUp();
051    
052                    _body = null;
053                    _custom = false;
054            }
055    
056            @Override
057            public int doAfterBody() {
058                    BodyContent bodyContent = getBodyContent();
059    
060                    if (bodyContent != null) {
061                            _body = bodyContent.getString();
062                    }
063    
064                    return SKIP_BODY;
065            }
066    
067            @Override
068            public int doEndTag() {
069                    BaseValidatorTagSupport baseValidatorTagSupport =
070                            (BaseValidatorTagSupport)findAncestorWithClass(
071                                    this, BaseValidatorTagSupport.class);
072    
073                    String name = getName();
074    
075                    _custom = ModelHintsUtil.isCustomValidator(name);
076    
077                    if (_custom) {
078                            name = ModelHintsUtil.buildCustomValidatorName(name);
079                    }
080    
081                    ValidatorTag validatorTag = new ValidatorTagImpl(
082                            name, getErrorMessage(), _body, _custom);
083    
084                    baseValidatorTagSupport.addValidatorTag(name, validatorTag);
085    
086                    return EVAL_BODY_BUFFERED;
087            }
088    
089            @Override
090            public String getBody() {
091                    if (Validator.isNull(_body)) {
092                            return StringPool.DOUBLE_APOSTROPHE;
093                    }
094    
095                    return _body.trim();
096            }
097    
098            @Override
099            public String getErrorMessage() {
100                    String errorMessage = super.getErrorMessage();
101    
102                    if (errorMessage == null) {
103                            return StringPool.BLANK;
104                    }
105    
106                    return errorMessage;
107            }
108    
109            @Override
110            public boolean isCustom() {
111                    return _custom;
112            }
113    
114            public void setBody(String body) {
115                    _body = body;
116            }
117    
118            protected String processCustom(String name) {
119                    if (name.equals("custom")) {
120                            _custom = true;
121    
122                            return name.concat(StringPool.UNDERLINE).concat(
123                                    StringUtil.randomId());
124                    }
125    
126                    return name;
127            }
128    
129            private String _body;
130            private boolean _custom;
131    
132    }