001    /**
002     * Copyright (c) 2000-2012 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            public String getBody() {
088                    if (Validator.isNull(_body)) {
089                            return StringPool.DOUBLE_APOSTROPHE;
090                    }
091    
092                    return _body.trim();
093            }
094    
095            @Override
096            public String getErrorMessage() {
097                    String errorMessage = super.getErrorMessage();
098    
099                    if (errorMessage == null) {
100                            return StringPool.BLANK;
101                    }
102    
103                    return errorMessage;
104            }
105    
106            public boolean isCustom() {
107                    return _custom;
108            }
109    
110            public void setBody(String body) {
111                    _body = body;
112            }
113    
114            protected String processCustom(String name) {
115                    if (name.equals("custom")) {
116                            _custom = true;
117    
118                            return name.concat(
119                                    StringPool.UNDERLINE).concat(
120                                            PwdGenerator.getPassword(PwdGenerator.KEY3, 4));
121                    }
122    
123                    return name;
124            }
125    
126            private String _body;
127            private boolean _custom;
128    
129    }