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.Validator;
020    import com.liferay.portal.model.ModelHintsUtil;
021    import com.liferay.taglib.aui.base.BaseValidatorTagImpl;
022    import com.liferay.util.PwdGenerator;
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                    setName(name);
041                    setErrorMessage(errorMessage);
042    
043                    _body = body;
044                    _custom = custom;
045            }
046    
047            @Override
048            public void cleanUp() {
049                    super.cleanUp();
050    
051                    _body = null;
052                    _custom = false;
053            }
054    
055            @Override
056            public int doAfterBody() {
057                    BodyContent bodyContent = getBodyContent();
058    
059                    if (bodyContent != null) {
060                            _body = bodyContent.getString();
061                    }
062    
063                    return SKIP_BODY;
064            }
065    
066            @Override
067            public int doEndTag() {
068                    InputTag inputTag = (InputTag)findAncestorWithClass(
069                            this, InputTag.class);
070    
071                    String name = getName();
072    
073                    _custom = ModelHintsUtil.isCustomValidator(name);
074    
075                    if (_custom) {
076                            name = ModelHintsUtil.buildCustomValidatorName(name);
077                    }
078    
079                    ValidatorTag validatorTag = new ValidatorTagImpl(
080                            name, getErrorMessage(), _body, _custom);
081    
082                    inputTag.addValidatorTag(name, validatorTag);
083    
084                    return EVAL_BODY_BUFFERED;
085            }
086    
087            @Override
088            public String getBody() {
089                    if (Validator.isNull(_body)) {
090                            return StringPool.DOUBLE_APOSTROPHE;
091                    }
092    
093                    return _body.trim();
094            }
095    
096            @Override
097            public String getErrorMessage() {
098                    String errorMessage = super.getErrorMessage();
099    
100                    if (errorMessage == null) {
101                            return StringPool.BLANK;
102                    }
103    
104                    return errorMessage;
105            }
106    
107            @Override
108            public boolean isCustom() {
109                    return _custom;
110            }
111    
112            public void setBody(String body) {
113                    _body = body;
114            }
115    
116            protected String processCustom(String name) {
117                    if (name.equals("custom")) {
118                            _custom = true;
119    
120                            return name.concat(
121                                    StringPool.UNDERLINE).concat(
122                                            PwdGenerator.getPassword(PwdGenerator.KEY3, 4));
123                    }
124    
125                    return name;
126            }
127    
128            private String _body;
129            private boolean _custom;
130    
131    }