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

File XWikiSyntaxResourceRenderer.java

 

Coverage histogram

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

Code metrics

10
29
10
1
170
101
16
0.55
2.9
10
1.6

Classes

Class Line # Actions
XWikiSyntaxResourceRenderer 42 29 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 295 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.xwiki20.reference;
21   
22    import java.util.ArrayDeque;
23    import java.util.Deque;
24    import java.util.Map;
25   
26    import org.apache.commons.lang3.StringUtils;
27    import org.xwiki.rendering.internal.parser.plain.PlainTextStreamParser;
28    import org.xwiki.rendering.internal.renderer.ParametersPrinter;
29    import org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxEscapeWikiPrinter;
30    import org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxListenerChain;
31    import org.xwiki.rendering.listener.QueueListener.Event;
32    import org.xwiki.rendering.listener.chaining.EventType;
33    import org.xwiki.rendering.listener.reference.ResourceReference;
34    import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer;
35   
36    /**
37    * Logic to render a Resource Reference into XWiki Syntax 2.0.
38    *
39    * @version $Id: 77c3736a39dc71140ad7039645b746d45593f312 $
40    * @since 2.0M3
41    */
 
42    public class XWikiSyntaxResourceRenderer
43    {
44    /**
45    * Separator to use between the link reference and link parameters.
46    */
47    protected static final String PARAMETER_SEPARATOR = "||";
48   
49    protected static final ParametersPrinter PARAMETERS_PRINTER = new ParametersPrinter('~', "||", "]]", ">>");
50   
51    private Deque<Boolean> forceFullSyntaxDeque = new ArrayDeque<Boolean>();
52   
53    private XWikiSyntaxListenerChain listenerChain;
54   
55    private ResourceReferenceSerializer referenceSerializer;
56   
57    /**
58    * @since 2.5RC1
59    */
 
60  916 toggle public XWikiSyntaxResourceRenderer(XWikiSyntaxListenerChain listenerChain,
61    ResourceReferenceSerializer referenceSerializer)
62    {
63  916 this.listenerChain = listenerChain;
64  916 this.referenceSerializer = referenceSerializer;
65  916 this.forceFullSyntaxDeque.push(false);
66    }
67   
 
68  128 toggle public String serialize(ResourceReference reference, boolean isFreeStanding)
69    {
70  128 String result = this.referenceSerializer.serialize(reference);
71   
72  128 if (!isFreeStanding) {
73  92 result = result.replace("~", "~~").replace(">>", "~>~>").replace(PARAMETER_SEPARATOR, "~|~|");
74    }
75   
76  128 return result;
77    }
78   
 
79  127 toggle public void beginRenderLink(XWikiSyntaxEscapeWikiPrinter printer,
80    boolean freestanding, Map<String, String> parameters)
81    {
82    // find if the last printed char is part of a syntax (i.e. consumed by the parser before starting to parse the
83    // link)
84  127 boolean isLastSyntax = printer.getBuffer().length() == 0;
85   
86  127 printer.flush();
87   
88  127 if (forceFullSyntax(printer, isLastSyntax, freestanding, parameters)) {
89  95 this.forceFullSyntaxDeque.push(true);
90   
91  95 printer.print("[[");
92    } else {
93  32 this.forceFullSyntaxDeque.push(false);
94    }
95    }
96   
 
97  91 toggle public boolean forceFullSyntax(XWikiSyntaxEscapeWikiPrinter printer, boolean freestanding,
98    Map<String, String> parameters)
99    {
100  91 return forceFullSyntax(printer, true, freestanding, parameters);
101    }
102   
 
103  218 toggle public boolean forceFullSyntax(XWikiSyntaxEscapeWikiPrinter printer, boolean isLastSyntax,
104    boolean freestanding, Map<String, String> parameters)
105    {
106  218 Event nextEvent = this.listenerChain.getLookaheadChainingListener().getNextEvent();
107   
108    // force full syntax if
109    // 1: it's not a free standing URI
110    // 2: there is parameters
111    // 3: it follows a character which is not a white space (newline/space) and is not consumed by the parser (like
112    // a another link)
113    // 4: it's followed by a character which is not a white space (TODO: find a better way than this endless list of
114    // EventType test but it probably need some big refactoring of the printer and XWikiSyntaxResourceRenderer)
115  218 return !freestanding
116    || !parameters.isEmpty()
117    || isNonWhiteSpaceAndConsumed(isLastSyntax, printer)
118    || isNotAWhiteSpace(nextEvent);
119    }
120   
 
121  49 toggle private boolean isNonWhiteSpaceAndConsumed(boolean isLastSyntax, XWikiSyntaxEscapeWikiPrinter printer)
122    {
123  49 return !isLastSyntax && !printer.isAfterWhiteSpace() && (!PlainTextStreamParser.SPECIALSYMBOL_PATTERN.matcher(
124    String.valueOf(printer.getLastPrinted().charAt(printer.getLastPrinted().length() - 1))).matches());
125    }
126   
 
127  47 toggle private boolean isNotAWhiteSpace(Event nextEvent)
128    {
129  47 return nextEvent != null && nextEvent.eventType != EventType.ON_SPACE
130    && nextEvent.eventType != EventType.ON_NEW_LINE && nextEvent.eventType != EventType.END_PARAGRAPH
131    && nextEvent.eventType != EventType.END_LINK && nextEvent.eventType != EventType.END_LIST_ITEM
132    && nextEvent.eventType != EventType.END_DEFINITION_DESCRIPTION
133    && nextEvent.eventType != EventType.END_DEFINITION_TERM
134    && nextEvent.eventType != EventType.END_QUOTATION_LINE && nextEvent.eventType != EventType.END_SECTION;
135    }
136   
 
137  90 toggle public void renderLinkContent(XWikiSyntaxEscapeWikiPrinter printer, String label)
138    {
139    // If there was some link content specified then output the character separator ">>".
140  90 if (!StringUtils.isEmpty(label)) {
141  28 printer.print(label);
142  28 printer.print(">>");
143    }
144    }
145   
 
146  127 toggle public void endRenderLink(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference reference,
147    boolean freestanding, Map<String, String> parameters)
148    {
149  127 printer.print(serialize(reference, freestanding));
150   
151    // If there were parameters specified, print them
152  127 printParameters(printer, reference, parameters);
153   
154  127 if (this.forceFullSyntaxDeque.peek() || !freestanding) {
155  95 printer.print("]]");
156    }
157   
158  127 this.forceFullSyntaxDeque.pop();
159    }
160   
 
161  86 toggle protected void printParameters(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference resourceReference,
162    Map<String, String> parameters)
163    {
164    // If there were parameters specified, output them separated by the "||" characters
165  86 if (!parameters.isEmpty()) {
166  15 printer.print(PARAMETER_SEPARATOR);
167  15 printer.print(PARAMETERS_PRINTER.print(parameters));
168    }
169    }
170    }