1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.renderer

File XWikiLinkLabelGenerator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

22
88
10
1
242
177
31
0.35
8.8
10
3.1

Classes

Class Line # Actions
XWikiLinkLabelGenerator 49 88 0% 31 14
0.883333388.3%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.renderer;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.apache.commons.lang3.StringUtils;
27    import org.xwiki.bridge.DocumentAccessBridge;
28    import org.xwiki.bridge.DocumentModelBridge;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.model.reference.EntityReferenceProvider;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.rendering.configuration.RenderingConfiguration;
37    import org.xwiki.rendering.listener.reference.ResourceReference;
38    import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator;
39   
40    /**
41    * Resolve configuration link format using the defined Rendering Configuration
42    * (see {@link org.xwiki.rendering.configuration.RenderingConfiguration}).
43    *
44    * @version $Id: fce0c9197814321bd25b70f1bc236d2fb88bb962 $
45    * @since 2.0M1
46    */
47    @Component
48    @Singleton
 
49    public class XWikiLinkLabelGenerator implements LinkLabelGenerator
50    {
51    @Inject
52    private RenderingConfiguration renderingConfiguration;
53   
54    @Inject
55    private DocumentAccessBridge documentAccessBridge;
56   
57    @Inject
58    private EntityReferenceResolver<ResourceReference> resourceReferenceResolver;
59   
60    @Inject
61    private EntityReferenceProvider defaultEntityReferenceProvider;
62   
63    @Inject
64    @Named("local")
65    private EntityReferenceSerializer<String> localReferenceSerializer;
66   
67    /**
68    * {@inheritDoc}
69    *
70    * @since 2.5RC1
71    */
 
72  27 toggle @Override
73    public String generate(ResourceReference reference)
74    {
75  27 StringBuilder result = new StringBuilder();
76   
77  27 String format = this.renderingConfiguration.getLinkLabelFormat();
78   
79  27 EntityReference resolvedReference = resourceReferenceResolver.resolve(reference, EntityType.DOCUMENT);
80  27 if (resolvedReference == null) {
81  0 throw new IllegalArgumentException(String.valueOf(reference));
82    }
83  27 DocumentReference documentReference = new DocumentReference(resolvedReference);
84   
85  342 for (int i = 0; i < format.length(); i++) {
86  315 char c = format.charAt(i);
87  315 if (c == '%' && i + 1 < format.length()) {
88    // Check first letter after '%'
89  135 i++;
90  135 char cc = format.charAt(i);
91  135 switch (cc) {
92  12 case 's':
93    // Replace %s with the full space name (e.g. space1.space2)
94  12 result.append(getSpacesLabel(documentReference));
95  12 break;
96  14 case 'p':
97    // Replace %p with the page name
98  14 result.append(getPageLabel(documentReference));
99  14 break;
100  12 case 't':
101    // Replace %t with the document title and fall back to %np if the title is null or empty
102  12 result.append(getTitleLabel(documentReference));
103  12 break;
104  12 case 'P':
105    // Replace %P with the page name in camel case + space
106  12 result.append(getCamelPageLabel(documentReference));
107  12 break;
108  12 case 'w':
109    // Replace %w with the wiki name
110  12 result.append(getWikiLabel(documentReference));
111  12 break;
112  18 case 'l':
113  18 if (i + 1 < format.length()) {
114  18 i++;
115  18 char ccc = format.charAt(i);
116  18 if (ccc == 's') {
117    // Replace %ls with the last space name
118  12 result.append(getLastSpaceLabel(documentReference));
119    } else {
120  6 result.append(c);
121  6 result.append(cc);
122  6 result.append(ccc);
123    }
124    } else {
125  0 result.append(c);
126  0 result.append(cc);
127    }
128  18 break;
129  37 case 'n':
130  37 if (i + 1 < format.length()) {
131  37 i++;
132  37 char ccc = format.charAt(i);
133  37 if (ccc == 'p') {
134    // Replace %np with the page name if the name is not the default page name
135    // (e.g. "WebHome") or with the last space name if it is.
136  31 result.append(getNestedPageLabel(documentReference));
137    } else {
138  6 result.append(c);
139  6 result.append(cc);
140  6 result.append(ccc);
141    }
142    } else {
143  0 result.append(c);
144  0 result.append(cc);
145    }
146  37 break;
147  18 case 'N':
148  18 if (i + 1 < format.length()) {
149  18 i++;
150  18 char ccc = format.charAt(i);
151  18 if (ccc == 'P') {
152    // Replace %NP with the nested page name in camel case + space
153  12 result.append(getCamelNestedPageLabel(documentReference));
154    } else {
155  6 result.append(c);
156  6 result.append(cc);
157  6 result.append(ccc);
158    }
159    } else {
160  0 result.append(c);
161  0 result.append(cc);
162    }
163  18 break;
164  0 default:
165  0 result.append(c);
166  0 result.append(cc);
167    }
168    } else {
169  180 result.append(c);
170    }
171    }
172   
173  27 return result.toString();
174    }
175   
 
176  12 toggle private String getCamelNestedPageLabel(DocumentReference documentReference)
177    {
178  12 return convertCamelString(getNestedPageLabel(documentReference));
179    }
180   
 
181  12 toggle private String getLastSpaceLabel(DocumentReference documentReference)
182    {
183  12 return documentReference.getLastSpaceReference().getName();
184    }
185   
 
186  12 toggle private String getWikiLabel(DocumentReference documentReference)
187    {
188  12 return documentReference.getWikiReference().getName();
189    }
190   
 
191  12 toggle private String getCamelPageLabel(DocumentReference documentReference)
192    {
193  12 return convertCamelString(documentReference.getName());
194    }
195   
 
196  24 toggle private String convertCamelString(String value)
197    {
198  24 return value.replaceAll("([a-z])([A-Z])", "$1 $2");
199    }
200   
 
201  14 toggle private String getPageLabel(DocumentReference documentReference)
202    {
203  14 return documentReference.getName();
204    }
205   
 
206  12 toggle private String getSpacesLabel(DocumentReference documentReference)
207    {
208  12 return this.localReferenceSerializer.serialize(documentReference.getParent());
209    }
210   
 
211  47 toggle private String getNestedPageLabel(DocumentReference documentReference)
212    {
213  47 String result;
214  47 if (this.defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName().equals(
215    documentReference.getName()))
216    {
217  22 result = documentReference.getLastSpaceReference().getName();
218    } else {
219  25 result = documentReference.getName();
220    }
221  47 return result;
222    }
223   
 
224  12 toggle private String getTitleLabel(DocumentReference documentReference)
225    {
226  12 String result;
227  12 try {
228  12 DocumentModelBridge document = this.documentAccessBridge.getDocument(documentReference);
229  10 if (StringUtils.isNotBlank(document.getTitle())) {
230  8 result = document.getTitle();
231    } else {
232    // Title is empty, fall back to %np
233  2 result = getNestedPageLabel(documentReference);
234    }
235    } catch (Exception e) {
236    // If there's an error (meaning the document cannot be retrieved from the database for some reason)
237    // the fall back to displaying %np
238  2 result = getNestedPageLabel(documentReference);
239    }
240  12 return result;
241    }
242    }