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

File XWikiReferenceTagHandler.java

 

Coverage histogram

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

Code metrics

18
46
4
1
158
96
13
0.28
11.5
4
3.25

Classes

Class Line # Actions
XWikiReferenceTagHandler 45 46 0% 13 4
0.941176594.1%
 

Contributing tests

This file is covered by 303 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.parser.xhtml.wikimodel;
21   
22    import java.util.Collections;
23   
24    import org.xwiki.rendering.internal.parser.wikimodel.DefaultXWikiGeneratorListener;
25    import org.xwiki.rendering.internal.parser.wikimodel.WikiModelStreamParser;
26    import org.xwiki.rendering.internal.parser.wikimodel.XWikiGeneratorListener;
27    import org.xwiki.rendering.renderer.PrintRenderer;
28    import org.xwiki.rendering.renderer.PrintRendererFactory;
29    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
30    import org.xwiki.rendering.wikimodel.WikiParameter;
31    import org.xwiki.rendering.wikimodel.WikiParameters;
32    import org.xwiki.rendering.wikimodel.WikiReference;
33    import org.xwiki.rendering.wikimodel.impl.WikiScannerContext;
34    import org.xwiki.rendering.wikimodel.xhtml.handler.ReferenceTagHandler;
35    import org.xwiki.rendering.wikimodel.xhtml.impl.TagStack;
36    import org.xwiki.rendering.wikimodel.xhtml.impl.TagContext;
37   
38    /**
39    * Override the default WikiModel Reference handler to handle XWiki references since we store some information in
40    * comments. We also need to handle the span elements introduced by XWiki.
41    *
42    * @version $Id: 5a57ea8f5fa75ea8acf50767becb889ba3865ba7 $
43    * @since 1.7M1
44    */
 
45    public class XWikiReferenceTagHandler extends ReferenceTagHandler implements XWikiWikiModelHandler
46    {
47    private WikiModelStreamParser parser;
48   
49    private PrintRendererFactory xwikiSyntaxPrintRendererFactory;
50   
51    /**
52    * @since 2.2.5
53    * @todo Remove the need to pass a Parser when WikiModel implements support for wiki syntax in links. See
54    * http://code.google.com/p/wikimodel/issues/detail?id=87
55    */
 
56  338 toggle public XWikiReferenceTagHandler(WikiModelStreamParser parser, PrintRendererFactory xwikiSyntaxPrintRendererFactory)
57    {
58  338 this.parser = parser;
59  338 this.xwikiSyntaxPrintRendererFactory = xwikiSyntaxPrintRendererFactory;
60    }
61   
 
62  429 toggle @Override
63    public void initialize(TagStack stack)
64    {
65  429 stack.setStackParameter(IS_IN_LINK, false);
66  429 stack.setStackParameter(IS_FREE_STANDING_LINK, false);
67  429 stack.setStackParameter(LINK_PARAMETERS, WikiParameters.EMPTY);
68    }
69   
 
70  87 toggle @Override
71    protected void begin(TagContext context)
72    {
73  87 boolean isInLink = (Boolean) context.getTagStack().getStackParameter(IS_IN_LINK);
74  87 if (isInLink) {
75  57 XWikiGeneratorListener listener =
76    (XWikiGeneratorListener) context.getTagStack().getStackParameter(LINK_LISTENER);
77  57 context.getTagStack().pushScannerContext(new WikiScannerContext(listener));
78   
79    // Ensure we simulate a new document being parsed
80  57 context.getScannerContext().beginDocument();
81   
82    // Verify if it's a freestanding link and if so save the information so that we can get it in
83    // XWikiCommentHandler.
84  57 if (isFreeStandingReference(context)) {
85  13 context.getTagStack().setStackParameter(IS_FREE_STANDING_LINK, true);
86    } else {
87  44 context.getTagStack().setStackParameter(LINK_PARAMETERS,
88    removeMeaningfulParameters(context.getParams()));
89    }
90   
91  57 setAccumulateContent(false);
92  30 } else if (!isFreeStandingReference(context)) {
93  30 WikiParameter ref = context.getParams().getParameter("href");
94   
95  30 if (ref != null) {
96  22 DefaultWikiPrinter printer = new DefaultWikiPrinter();
97   
98  22 PrintRenderer linkLabelRenderer = this.xwikiSyntaxPrintRendererFactory.createRenderer(printer);
99   
100  22 XWikiGeneratorListener xwikiListener =
101    this.parser.createXWikiGeneratorListener(linkLabelRenderer, null);
102  22 context.getTagStack().pushScannerContext(new WikiScannerContext(xwikiListener));
103   
104    // Ensure we simulate a new document being parsed
105  22 context.getScannerContext().beginDocument();
106    } else {
107  8 WikiParameter idName = context.getParams().getParameter("id");
108   
109  8 if (idName == null) {
110  6 idName = context.getParams().getParameter("name");
111    }
112   
113  8 if (idName != null) {
114  6 WikiParameter parameter = new WikiParameter("name", idName.getValue());
115  6 WikiParameters parameters = new WikiParameters(Collections.singletonList(parameter));
116  6 context.getScannerContext().onExtensionBlock(DefaultXWikiGeneratorListener.EXT_ID, parameters);
117    }
118    }
119    } else {
120  0 super.begin(context);
121    }
122    }
123   
 
124  87 toggle @Override
125    protected void end(TagContext context)
126    {
127  87 boolean isInLink = (Boolean) context.getTagStack().getStackParameter(IS_IN_LINK);
128  87 if (isInLink) {
129    // Ensure we simulate a document parsing end
130  57 context.getScannerContext().endDocument();
131   
132  57 context.getTagStack().popScannerContext();
133  30 } else if (!isFreeStandingReference(context)) {
134  30 WikiParameters parameters = context.getParams();
135   
136  30 WikiParameter ref = parameters.getParameter("href");
137   
138  30 if (ref != null) {
139    // Ensure we simulate a document parsing end
140  22 context.getScannerContext().endDocument();
141   
142  22 WikiScannerContext scannerContext = context.getTagStack().popScannerContext();
143   
144  22 XWikiGeneratorListener xwikiListener = (XWikiGeneratorListener) scannerContext.getfListener();
145  22 PrintRenderer linkLabelRenderer = (PrintRenderer) xwikiListener.getListener();
146   
147  22 String label = linkLabelRenderer.getPrinter().toString();
148   
149  22 WikiReference reference =
150    new WikiReference(ref.getValue(), label, removeMeaningfulParameters(parameters));
151   
152  22 context.getScannerContext().onReference(reference);
153    }
154    } else {
155  0 super.end(context);
156    }
157    }
158    }