1. Project Clover database Tue Dec 20 2016 21:24:09 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
27
8
1
160
93
14
0.52
3.38
8
1.75

Classes

Class Line # Actions
XWikiSyntaxResourceRenderer 42 27 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 278 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: 15f88638109c75d6d332c3277e19cca391899742 $
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> forceFullSyntax = new ArrayDeque<Boolean>();
52   
53    private XWikiSyntaxListenerChain listenerChain;
54   
55    private ResourceReferenceSerializer referenceSerializer;
56   
57    /**
58    * @since 2.5RC1
59    */
 
60  726 toggle public XWikiSyntaxResourceRenderer(XWikiSyntaxListenerChain listenerChain,
61    ResourceReferenceSerializer referenceSerializer)
62    {
63  726 this.listenerChain = listenerChain;
64  726 this.referenceSerializer = referenceSerializer;
65  726 this.forceFullSyntax.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  129 toggle public void beginRenderLink(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference reference,
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  129 boolean isLastSyntax = printer.getBuffer().length() == 0;
85   
86  129 printer.flush();
87   
88  129 if (forceFullSyntax(printer, isLastSyntax, freestanding, parameters)) {
89  97 this.forceFullSyntax.push(true);
90   
91  97 printer.print("[[");
92    } else {
93  32 this.forceFullSyntax.push(false);
94    }
95    }
96   
 
97  93 toggle public boolean forceFullSyntax(XWikiSyntaxEscapeWikiPrinter printer, boolean freestanding,
98    Map<String, String> parameters)
99    {
100  93 return forceFullSyntax(printer, true, freestanding, parameters);
101    }
102   
 
103  222 toggle public boolean forceFullSyntax(XWikiSyntaxEscapeWikiPrinter printer, boolean isLastSyntax,
104    boolean freestanding, Map<String, String> parameters)
105    {
106  222 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  222 return !freestanding
116    || !parameters.isEmpty()
117    || (!isLastSyntax && !printer.isAfterWhiteSpace() && (!PlainTextStreamParser.SPECIALSYMBOL_PATTERN.matcher(
118    String.valueOf(printer.getLastPrinted().charAt(printer.getLastPrinted().length() - 1))).matches()))
119    || (nextEvent != null && nextEvent.eventType != EventType.ON_SPACE
120    && nextEvent.eventType != EventType.ON_NEW_LINE && nextEvent.eventType != EventType.END_PARAGRAPH
121    && nextEvent.eventType != EventType.END_LINK && nextEvent.eventType != EventType.END_LIST_ITEM
122    && nextEvent.eventType != EventType.END_DEFINITION_DESCRIPTION
123    && nextEvent.eventType != EventType.END_DEFINITION_TERM
124    && nextEvent.eventType != EventType.END_QUOTATION_LINE && nextEvent.eventType != EventType.END_SECTION);
125    }
126   
 
127  90 toggle public void renderLinkContent(XWikiSyntaxEscapeWikiPrinter printer, String label)
128    {
129    // If there was some link content specified then output the character separator ">>".
130  90 if (!StringUtils.isEmpty(label)) {
131  28 printer.print(label);
132  28 printer.print(">>");
133    }
134    }
135   
 
136  127 toggle public void endRenderLink(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference reference,
137    boolean freestanding, Map<String, String> parameters)
138    {
139  127 printer.print(serialize(reference, freestanding));
140   
141    // If there were parameters specified, print them
142  127 printParameters(printer, reference, parameters);
143   
144  127 if (this.forceFullSyntax.peek() || !freestanding) {
145  95 printer.print("]]");
146    }
147   
148  127 this.forceFullSyntax.pop();
149    }
150   
 
151  86 toggle protected void printParameters(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference resourceReference,
152    Map<String, String> parameters)
153    {
154    // If there were parameters specified, output them separated by the "||" characters
155  86 if (!parameters.isEmpty()) {
156  15 printer.print(PARAMETER_SEPARATOR);
157  15 printer.print(PARAMETERS_PRINTER.print(parameters));
158    }
159    }
160    }