001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.ModelHintsUtil;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.taglib.BaseValidatorTagSupport;
024    import com.liferay.taglib.aui.base.BaseValidatorTagImpl;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.tagext.BodyContent;
028    import javax.servlet.jsp.tagext.BodyTag;
029    
030    /**
031     * @author Julio Camarero
032     * @author Brian Wing Shun Chan
033     */
034    public class ValidatorTagImpl
035            extends BaseValidatorTagImpl implements BodyTag, ValidatorTag {
036    
037            public ValidatorTagImpl() {
038            }
039    
040            public ValidatorTagImpl(
041                    String name, String errorMessage, String body, boolean custom) {
042    
043                    this(name, errorMessage, body, custom, true);
044            }
045    
046            public ValidatorTagImpl(
047                    String name, String errorMessage, String body, boolean custom,
048                    boolean customValidatorRequired) {
049    
050                    setName(name);
051                    setErrorMessage(errorMessage);
052    
053                    _body = body;
054                    _custom = custom;
055                    _customValidatorRequired = customValidatorRequired;
056            }
057    
058            @Override
059            public void cleanUp() {
060                    super.cleanUp();
061    
062                    _body = null;
063                    _custom = false;
064                    _customValidatorRequired = true;
065            }
066    
067            @Override
068            public int doAfterBody() {
069                    BodyContent bodyContent = getBodyContent();
070    
071                    if (bodyContent != null) {
072                            _body = bodyContent.getString();
073                    }
074    
075                    return SKIP_BODY;
076            }
077    
078            @Override
079            public int doEndTag() {
080                    BaseValidatorTagSupport baseValidatorTagSupport =
081                            (BaseValidatorTagSupport)findAncestorWithClass(
082                                    this, BaseValidatorTagSupport.class);
083    
084                    String name = getName();
085    
086                    _custom = ModelHintsUtil.isCustomValidator(name);
087    
088                    if (_custom) {
089                            StringBundler sb = new StringBundler(3);
090    
091                            String namespace = baseValidatorTagSupport.getInputName();
092    
093                            sb.append(namespace);
094    
095                            sb.append(StringPool.UNDERLINE);
096    
097                            HttpServletRequest request =
098                                    (HttpServletRequest)pageContext.getRequest();
099    
100                            sb.append(PortalUtil.getUniqueElementId(request, namespace, name));
101    
102                            name = sb.toString();
103                    }
104    
105                    ValidatorTag validatorTag = new ValidatorTagImpl(
106                            name, getErrorMessage(), _body, _custom, _customValidatorRequired);
107    
108                    baseValidatorTagSupport.addValidatorTag(name, validatorTag);
109    
110                    return EVAL_BODY_BUFFERED;
111            }
112    
113            @Override
114            public String getBody() {
115                    if (Validator.isNull(_body)) {
116                            return StringPool.DOUBLE_APOSTROPHE;
117                    }
118    
119                    return _body.trim();
120            }
121    
122            @Override
123            public String getErrorMessage() {
124                    String errorMessage = super.getErrorMessage();
125    
126                    if (errorMessage == null) {
127                            return StringPool.BLANK;
128                    }
129    
130                    return errorMessage;
131            }
132    
133            @Override
134            public boolean isCustom() {
135                    return _custom;
136            }
137    
138            @Override
139            public boolean isCustomValidatorRequired() {
140                    return _customValidatorRequired;
141            }
142    
143            public void setCustomValidatorRequired(boolean customValidatorRequired) {
144                    _customValidatorRequired = customValidatorRequired;
145            }
146    
147            public void setBody(String body) {
148                    _body = body;
149            }
150    
151            private String _body;
152            private boolean _custom;
153            private boolean _customValidatorRequired = true;
154    
155    }