Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractTagsDecorator |
|
| 2.75;2.75 |
1 | /* | |
2 | * Copyright (c) 2003-2011, Simon Brown | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * - Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * | |
11 | * - Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in | |
13 | * the documentation and/or other materials provided with the | |
14 | * distribution. | |
15 | * | |
16 | * - Neither the name of Pebble nor the names of its contributors may | |
17 | * be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
30 | * POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package net.sourceforge.pebble.decorator; | |
34 | ||
35 | import java.util.Iterator; | |
36 | ||
37 | import net.sourceforge.pebble.api.decorator.ContentDecoratorContext; | |
38 | import net.sourceforge.pebble.domain.PageBasedContent; | |
39 | import net.sourceforge.pebble.domain.Tag; | |
40 | import net.sourceforge.pebble.util.I18n; | |
41 | ||
42 | /** | |
43 | * Generates tag links for inclusion in the body of blog entries, | |
44 | * when rendered as HTML. | |
45 | * | |
46 | * @author Simon Brown | |
47 | */ | |
48 | public abstract class AbstractTagsDecorator extends ContentDecoratorSupport { | |
49 | private final String resourceKey; | |
50 | private final String target; | |
51 | ||
52 | /** | |
53 | * Extended Parameters for generating Links to different Tagging sites - like Technorati. | |
54 | * @param resourceKey is used to determine the label for the tags from pebbles resource files | |
55 | * @param openLinkInNewWindow set to true to generate links with 'target="_blank"' | |
56 | */ | |
57 | ||
58 | 12 | public AbstractTagsDecorator(String resourceKey, boolean openLinkInNewWindow) { |
59 | 12 | this.resourceKey = resourceKey; |
60 | 12 | target=openLinkInNewWindow ? " target=\"_blank\"":""; |
61 | 12 | } |
62 | ||
63 | /** | |
64 | * Default constructors makes Tags use the standard label (key tag.tags) and open links | |
65 | * in the same browser window. | |
66 | */ | |
67 | ||
68 | 2732 | public AbstractTagsDecorator() { |
69 | 2732 | this.resourceKey = "tag.tags"; |
70 | 2732 | target=""; |
71 | 2732 | } |
72 | ||
73 | protected String generateDecorationHtml(ContentDecoratorContext context, PageBasedContent content) { | |
74 | 52 | StringBuffer buf = new StringBuffer(); |
75 | ||
76 | 52 | if (context.getMedia() == ContentDecoratorContext.HTML_PAGE) { |
77 | 16 | Iterator<Tag> tags = content.getAllTags().iterator(); |
78 | ||
79 | 16 | String baseUrl = getBaseUrl(content); |
80 | ||
81 | 16 | if (tags.hasNext()) { |
82 | 8 | buf.append("<div class=\"tags\"><span>"); |
83 | 8 | buf.append(I18n.getMessage(content.getBlog(), resourceKey)); |
84 | 8 | buf.append(" : </span>"); |
85 | ||
86 | 32 | while (tags.hasNext()) { |
87 | ||
88 | 24 | Tag tag = tags.next(); |
89 | ||
90 | 24 | if (tag.getName() != null && !tag.getName().equals("")) { |
91 | ||
92 | 24 | buf.append("<a href=\""); |
93 | 24 | buf.append(baseUrl); |
94 | 24 | buf.append(tag.getName() + "\""); |
95 | 24 | buf.append(target); |
96 | 24 | buf.append(" rel=\"tag\">"); |
97 | 24 | buf.append(tag.getName()); |
98 | 24 | buf.append("</a>"); |
99 | ||
100 | 24 | if (tags.hasNext()) { |
101 | 16 | buf.append(", "); |
102 | } | |
103 | } | |
104 | 24 | } |
105 | 8 | buf.append("</div>"); |
106 | ||
107 | } | |
108 | } | |
109 | ||
110 | 52 | return buf.toString(); |
111 | } | |
112 | ||
113 | /** | |
114 | * Gets the base URL for tag links, complete with trailing slash. | |
115 | * | |
116 | * @param content the owning content | |
117 | * @return a URL as a String | |
118 | */ | |
119 | public abstract String getBaseUrl(PageBasedContent content); | |
120 | } |