1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.renderer.xhtml.link; |
21 |
|
|
22 |
|
import java.util.LinkedHashMap; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
|
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
31 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
32 |
|
import org.xwiki.component.manager.ComponentLookupException; |
33 |
|
import org.xwiki.component.phase.Initializable; |
34 |
|
import org.xwiki.component.phase.InitializationException; |
35 |
|
import org.xwiki.rendering.listener.reference.DocumentResourceReference; |
36 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
37 |
|
import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator; |
38 |
|
import org.xwiki.rendering.wiki.WikiModel; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
46 |
|
@Component |
47 |
|
@Named("doc") |
48 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 90.6% |
Uncovered Elements: 5 (53) |
Complexity: 14 |
Complexity Density: 0.41 |
|
49 |
|
public class DocumentXHTMLLinkTypeRenderer extends AbstractXHTMLLinkTypeRenderer implements Initializable |
50 |
|
{ |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
private static final String WIKILINK = "wikilink"; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private WikiModel wikiModel; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@Inject |
65 |
|
private LinkLabelGenerator linkLabelGenerator; |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 2 |
Complexity Density: 1 |
|
67 |
686 |
@Override... |
68 |
|
public void initialize() throws InitializationException |
69 |
|
{ |
70 |
|
|
71 |
|
|
72 |
686 |
try { |
73 |
686 |
this.wikiModel = this.componentManager.getInstance(WikiModel.class); |
74 |
|
} catch (ComponentLookupException e) { |
75 |
|
|
76 |
|
} |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
79 |
343 |
@Override... |
80 |
|
public void beginLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters) |
81 |
|
{ |
82 |
343 |
if (this.wikiModel == null) { |
83 |
10 |
super.beginLink(reference, freestanding, parameters); |
84 |
|
} else { |
85 |
333 |
beginInternalLink(reference, freestanding, parameters); |
86 |
|
} |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
32 |
@Override... |
90 |
|
protected String computeLabel(ResourceReference reference) |
91 |
|
{ |
92 |
32 |
return this.linkLabelGenerator.generate(reference); |
93 |
|
} |
94 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
95 |
10 |
@Override... |
96 |
|
protected void beginLinkExtraAttributes(ResourceReference reference, Map<String, String> spanAttributes, |
97 |
|
Map<String, String> anchorAttributes) |
98 |
|
{ |
99 |
10 |
if (StringUtils.isEmpty(reference.getReference())) { |
100 |
10 |
renderAutoLink(reference, spanAttributes, anchorAttributes); |
101 |
|
} else { |
102 |
0 |
anchorAttributes.put(XHTMLLinkRenderer.HREF, reference.getReference()); |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@param |
110 |
|
@param |
111 |
|
@param |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
113 |
333 |
private void beginInternalLink(ResourceReference reference, boolean freestanding,... |
114 |
|
Map<String, String> parameters) |
115 |
|
{ |
116 |
333 |
Map<String, String> spanAttributes = new LinkedHashMap<String, String>(); |
117 |
333 |
Map<String, String> anchorAttributes = new LinkedHashMap<String, String>(); |
118 |
|
|
119 |
|
|
120 |
333 |
anchorAttributes.putAll(parameters); |
121 |
|
|
122 |
333 |
if (StringUtils.isEmpty(reference.getReference())) { |
123 |
28 |
spanAttributes.put(CLASS, WIKILINK); |
124 |
28 |
renderAutoLink(reference, spanAttributes, anchorAttributes); |
125 |
305 |
} else if (this.wikiModel.isDocumentAvailable(reference)) { |
126 |
267 |
spanAttributes.put(CLASS, WIKILINK); |
127 |
267 |
anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentViewURL(reference)); |
128 |
|
} else { |
129 |
|
|
130 |
38 |
spanAttributes.put(CLASS, "wikicreatelink"); |
131 |
38 |
anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentEditURL(reference)); |
132 |
|
} |
133 |
|
|
134 |
333 |
getXHTMLWikiPrinter().printXMLStartElement(SPAN, spanAttributes); |
135 |
333 |
getXHTMLWikiPrinter().printXMLStartElement(XHTMLLinkRenderer.ANCHOR, anchorAttributes); |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@param |
141 |
|
@param |
142 |
|
|
|
|
| 80% |
Uncovered Elements: 3 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
143 |
38 |
private void renderAutoLink(ResourceReference reference, Map<String, String> spanAttributes,... |
144 |
|
Map<String, String> aAttributes) |
145 |
|
{ |
146 |
38 |
spanAttributes.put(CLASS, WIKILINK); |
147 |
|
|
148 |
38 |
StringBuilder buffer = new StringBuilder(); |
149 |
38 |
String queryString = reference.getParameter(DocumentResourceReference.QUERY_STRING); |
150 |
38 |
if (queryString != null) { |
151 |
0 |
buffer.append('?'); |
152 |
0 |
buffer.append(queryString); |
153 |
|
} |
154 |
38 |
buffer.append('#'); |
155 |
38 |
String anchor = reference.getParameter(DocumentResourceReference.ANCHOR); |
156 |
38 |
if (anchor != null) { |
157 |
36 |
buffer.append(anchor); |
158 |
|
} |
159 |
|
|
160 |
38 |
aAttributes.put(XHTMLLinkRenderer.HREF, buffer.toString()); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
10 |
@Override... |
164 |
|
protected boolean isExternalLink(ResourceReference reference) |
165 |
|
{ |
166 |
10 |
return false; |
167 |
|
} |
168 |
|
} |