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.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                    setName(name);
044                    setErrorMessage(errorMessage);
045    
046                    _body = body;
047                    _custom = custom;
048            }
049    
050            @Override
051            public void cleanUp() {
052                    super.cleanUp();
053    
054                    _body = null;
055                    _custom = false;
056            }
057    
058            @Override
059            public int doAfterBody() {
060                    BodyContent bodyContent = getBodyContent();
061    
062                    if (bodyContent != null) {
063                            _body = bodyContent.getString();
064                    }
065    
066                    return SKIP_BODY;
067            }
068    
069            @Override
070            public int doEndTag() {
071                    BaseValidatorTagSupport baseValidatorTagSupport =
072                            (BaseValidatorTagSupport)findAncestorWithClass(
073                                    this, BaseValidatorTagSupport.class);
074    
075                    String name = getName();
076    
077                    _custom = ModelHintsUtil.isCustomValidator(name);
078    
079                    if (_custom) {
080                            StringBundler sb = new StringBundler(3);
081    
082                            String namespace = baseValidatorTagSupport.getInputName();
083    
084                            sb.append(namespace);
085    
086                            sb.append(StringPool.UNDERLINE);
087    
088                            HttpServletRequest request =
089                                    (HttpServletRequest)pageContext.getRequest();
090    
091                            sb.append(PortalUtil.getUniqueElementId(request, namespace, name));
092    
093                            name = sb.toString();
094                    }
095    
096                    ValidatorTag validatorTag = new ValidatorTagImpl(
097                            name, getErrorMessage(), _body, _custom);
098    
099                    baseValidatorTagSupport.addValidatorTag(name, validatorTag);
100    
101                    return EVAL_BODY_BUFFERED;
102            }
103    
104            @Override
105            public String getBody() {
106                    if (Validator.isNull(_body)) {
107                            return StringPool.DOUBLE_APOSTROPHE;
108                    }
109    
110                    return _body.trim();
111            }
112    
113            @Override
114            public String getErrorMessage() {
115                    String errorMessage = super.getErrorMessage();
116    
117                    if (errorMessage == null) {
118                            return StringPool.BLANK;
119                    }
120    
121                    return errorMessage;
122            }
123    
124            @Override
125            public boolean isCustom() {
126                    return _custom;
127            }
128    
129            public void setBody(String body) {
130                    _body = body;
131            }
132    
133            private String _body;
134            private boolean _custom;
135    
136    }