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