1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.renderer.xwiki21.reference

File XWikiSyntaxResourceRenderer.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
28
3
1
119
66
11
0.39
9.33
3
3.67

Classes

Class Line # Actions
XWikiSyntaxResourceRenderer 39 28 0% 11 2
0.957446895.7%
 

Contributing tests

This file is covered by 63 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.xwiki21.reference;
21   
22    import java.util.Map;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxEscapeWikiPrinter;
26    import org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxListenerChain;
27    import org.xwiki.rendering.listener.reference.AttachmentResourceReference;
28    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
29    import org.xwiki.rendering.listener.reference.ResourceReference;
30    import org.xwiki.rendering.listener.reference.ResourceType;
31    import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer;
32   
33    /**
34    * Logic to render a XWiki Link into XWiki Syntax 2.1.
35    *
36    * @version $Id: e2461da3d8a4c1fa5189d283767ccaf23ce71913 $
37    * @since 2.5M2
38    */
 
39    public class XWikiSyntaxResourceRenderer
40    extends org.xwiki.rendering.internal.renderer.xwiki20.reference.XWikiSyntaxResourceRenderer
41    {
42    /**
43    * Parameter name under which to serialize the query string in XWiki Syntax 2.1.
44    */
45    private static final String QUERY_STRING = "queryString";
46   
47    /**
48    * Parameter name under which to serialize the query string in XWiki Syntax 2.1.
49    */
50    private static final String ANCHOR = "anchor";
51   
52    /**
53    * @param listenerChain the rendering chain
54    * @param referenceSerializer the serializer implementation to use to serialize link references
55    * @since 2.5RC1
56    */
 
57  256 toggle public XWikiSyntaxResourceRenderer(XWikiSyntaxListenerChain listenerChain,
58    ResourceReferenceSerializer referenceSerializer)
59    {
60  256 super(listenerChain, referenceSerializer);
61    }
62   
 
63  41 toggle private boolean isDocument(ResourceType resourceType)
64    {
65  41 return ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType)
66    || ResourceType.PAGE.equals(resourceType);
67    }
68   
 
69  41 toggle @Override
70    protected void printParameters(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference reference,
71    Map<String, String> parameters)
72    {
73    // Print the Query String and Anchor as parameters if they're defined and if the link is a link to a document.
74  41 boolean shouldPrintSeparator = true;
75   
76    // The XWiki Syntax 2.1 supports two special reference parameters for document references:
77    // - queryString and anchor.
78    // The XWiki Syntax 2.1 supports one special reference parameters for attachment references:
79    // - queryString.
80  41 ResourceType resourceType = reference.getType();
81  41 if (isDocument(resourceType)) {
82    // Print first the query string
83  18 String queryString = reference.getParameter(DocumentResourceReference.QUERY_STRING);
84  18 if (!StringUtils.isEmpty(queryString)) {
85  4 printer.print(PARAMETER_SEPARATOR);
86  4 printer.print(this.PARAMETERS_PRINTER.print(QUERY_STRING, queryString, '~'));
87  4 shouldPrintSeparator = false;
88    }
89    // Then print the anchor
90  18 String anchor = reference.getParameter(DocumentResourceReference.ANCHOR);
91  18 if (!StringUtils.isEmpty(anchor)) {
92  4 if (shouldPrintSeparator) {
93  1 printer.print(PARAMETER_SEPARATOR);
94    } else {
95  3 printer.print(" ");
96    }
97  4 printer.print(this.PARAMETERS_PRINTER.print(ANCHOR, anchor, '~'));
98  4 shouldPrintSeparator = false;
99    }
100  23 } else if (ResourceType.ATTACHMENT.equals(resourceType)) {
101  4 String queryString = reference.getParameter(AttachmentResourceReference.QUERY_STRING);
102  4 if (!StringUtils.isEmpty(queryString)) {
103  2 printer.print(PARAMETER_SEPARATOR);
104  2 printer.print(this.PARAMETERS_PRINTER.print(QUERY_STRING, queryString, '~'));
105  2 shouldPrintSeparator = false;
106    }
107    }
108   
109    // Add all Link parameters but only if there isn't a Link Reference parameter of the same name...
110  41 if (!parameters.isEmpty()) {
111  2 if (shouldPrintSeparator) {
112  0 printer.print(PARAMETER_SEPARATOR);
113    } else {
114  2 printer.print(" ");
115    }
116  2 printer.print(this.PARAMETERS_PRINTER.print(parameters, '~'));
117    }
118    }
119    }