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.search;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.Writer;
023    
024    import java.util.Map;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class TextSearchEntry extends SearchEntry {
033    
034            @Override
035            public Object clone() {
036                    TextSearchEntry textSearchEntry = new TextSearchEntry();
037    
038                    BeanPropertiesUtil.copyProperties(this, textSearchEntry);
039    
040                    return textSearchEntry;
041            }
042    
043            public Map<String, Object> getData() {
044                    return _data;
045            }
046    
047            public String getHref() {
048                    return _href;
049            }
050    
051            public String getName() {
052                    return _name;
053            }
054    
055            public String getName(HttpServletRequest request) {
056                    return getName();
057            }
058    
059            public String getTarget() {
060                    return _target;
061            }
062    
063            public String getTitle() {
064                    return _title;
065            }
066    
067            @Override
068            public void print(
069                            Writer writer, HttpServletRequest request,
070                            HttpServletResponse response)
071                    throws Exception {
072    
073                    if (Validator.isNull(_href)) {
074                            writer.write(getName(request));
075                    }
076                    else {
077                            StringBundler sb = new StringBundler();
078    
079                            sb.append("<a");
080    
081                            if (_data != null) {
082                                    for (Map.Entry<String, Object> entry : _data.entrySet()) {
083                                            String key = entry.getKey();
084                                            String value = String.valueOf(entry.getValue());
085    
086                                            sb.append(" data-");
087                                            sb.append(key);
088                                            sb.append("=\"");
089                                            sb.append(value);
090                                            sb.append("\"");
091                                    }
092                            }
093    
094                            sb.append(" href=\"");
095    
096                            if (_href.startsWith("javascript:")) {
097                                    sb.append(_href);
098                            }
099                            else {
100                                    sb.append(HtmlUtil.escape(_href));
101                            }
102    
103                            sb.append("\"");
104    
105                            if (Validator.isNotNull(_target)) {
106                                    sb.append(" target=\"");
107                                    sb.append(_target);
108                                    sb.append("\"");
109                            }
110    
111                            if (Validator.isNotNull(_title)) {
112                                    sb.append(" title=\"");
113                                    sb.append(_title);
114                                    sb.append("\"");
115                            }
116    
117                            sb.append(">");
118                            sb.append(getName(request));
119                            sb.append("</a>");
120    
121                            writer.write(sb.toString());
122                    }
123            }
124    
125            public void setData(Map<String, Object> data) {
126                    _data = data;
127            }
128    
129            public void setHref(String href) {
130                    _href = href;
131            }
132    
133            public void setName(String name) {
134                    _name = name;
135            }
136    
137            public void setTarget(String target) {
138                    _target = target;
139            }
140    
141            public void setTitle(String title) {
142                    _title = title;
143            }
144    
145            private Map<String, Object> _data;
146            private String _href;
147            private String _name;
148            private String _target;
149            private String _title;
150    
151    }