1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.parser.xhtml.wikimodel

File XWikiMacroHandler.java

 

Coverage histogram

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

Code metrics

16
51
4
1
176
111
14
0.27
12.75
4
3.5

Classes

Class Line # Actions
XWikiMacroHandler 52 51 0% 14 4
0.94366294.4%
 

Contributing tests

This file is covered by 332 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 javax.inject.Inject;
23   
24    import org.slf4j.Logger;
25    import org.xwiki.component.manager.ComponentLookupException;
26    import org.xwiki.component.manager.ComponentManager;
27    import org.xwiki.rendering.internal.parser.wikimodel.XWikiGeneratorListener;
28    import org.xwiki.rendering.internal.parser.xhtml.XHTMLParser;
29    import org.xwiki.rendering.listener.Listener;
30    import org.xwiki.rendering.listener.MetaData;
31    import org.xwiki.rendering.listener.WrappingListener;
32    import org.xwiki.rendering.renderer.PrintRenderer;
33    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
34    import org.xwiki.rendering.syntax.Syntax;
35    import org.xwiki.rendering.transformation.RenderingContext;
36    import org.xwiki.rendering.wikimodel.WikiParameters;
37    import org.xwiki.rendering.wikimodel.impl.WikiScannerContext;
38    import org.xwiki.rendering.wikimodel.xhtml.impl.MacroInfo;
39    import org.xwiki.rendering.wikimodel.xhtml.impl.TagContext;
40    import org.xwiki.stability.Unstable;
41   
42    import static org.xwiki.rendering.internal.parser.xhtml.wikimodel.XHTMLXWikiGeneratorListener.createMetaData;
43    import static org.xwiki.rendering.internal.parser.xhtml.wikimodel.XHTMLXWikiGeneratorListener.isMetaDataElement;
44   
45    /**
46    * This class aims at handling specific macro metadata.
47    *
48    * @version $Id: c6f4e16d986b6e2e538f75e1ad952b1ba1c01cb4 $
49    * @since 10.10RC1
50    */
51    @Unstable
 
52    public class XWikiMacroHandler implements XWikiWikiModelHandler
53    {
54    private ComponentManager componentManager;
55   
56    private XHTMLParser parser;
57   
58    @Inject
59    private Logger logger;
60   
61    /**
62    * Default constructor for XWikiMacroHandler.
63    * @param componentManager the component manager to retrieve the renderers.
64    * @param parser the current parser.
65    */
 
66  668 toggle public XWikiMacroHandler(ComponentManager componentManager, XHTMLParser parser)
67    {
68  668 this.componentManager = componentManager;
69  668 this.parser = parser;
70    }
71   
 
72  14 toggle private String getSyntax(TagContext previousNodes)
73    {
74  14 if (previousNodes.getTagStack().getStackParameter(CURRENT_SYNTAX) != null) {
75  8 return (String) previousNodes.getTagStack().popStackParameter(CURRENT_SYNTAX);
76   
77    // if the current syntax is not retrieved from the context, we get it from the RenderingContext
78    // target syntax. Now if this one is not set, we fallback on the parser own syntax.
79    } else {
80  6 Syntax syntax = null;
81  6 try {
82  6 RenderingContext renderingContext = this.componentManager.getInstance(RenderingContext.class);
83  6 syntax = renderingContext.getTargetSyntax();
84    } catch (ComponentLookupException e) {
85  0 this.logger.error("Error while retrieving the rendering context", e);
86    }
87  6 if (syntax == null) {
88  0 syntax = this.parser.getSyntax();
89    }
90  6 return syntax.toIdString();
91    }
92    }
93   
94    /**
95    * Handle the begin of a container element (div or span) which could contain a syntax metadata and/or a non
96    * generated content metadata.
97    * @param context the current tag context.
98    * @return true if a non generated content metadata has been found or the macro info is not null.
99    */
 
100  217 toggle public boolean handleBegin(TagContext context)
101    {
102  217 WikiParameters params = context.getParams();
103  217 MacroInfo macroInfo = (MacroInfo) context.getTagStack().getStackParameter(MACRO_INFO);
104   
105  217 boolean withNonGeneratedContent = false;
106  217 if (isMetaDataElement(params)) {
107  18 MetaData metaData = createMetaData(params);
108   
109  18 if (metaData.contains(MetaData.SYNTAX)) {
110  12 String currentSyntax = (String) metaData.getMetaData(MetaData.SYNTAX);
111  12 context.getTagStack().pushStackParameter(CURRENT_SYNTAX, currentSyntax);
112    }
113   
114  18 if (metaData.contains(MetaData.NON_GENERATED_CONTENT)) {
115  14 String currentSyntaxParameter = this.getSyntax(context);
116  14 try {
117  14 PrintRenderer renderer = this.componentManager.getInstance(PrintRenderer.class,
118    currentSyntaxParameter);
119  14 DefaultWikiPrinter printer = new DefaultWikiPrinter();
120  14 renderer.setPrinter(printer);
121  14 Listener listener;
122   
123  14 if (context.getTagStack().isInsideBlockElement()) {
124  3 listener = new InlineFilterListener();
125  3 ((InlineFilterListener) listener).setWrappedListener(renderer);
126    } else {
127  11 listener = renderer;
128    }
129  14 XWikiGeneratorListener xWikiGeneratorListener = this.parser.createXWikiGeneratorListener(listener,
130    null);
131   
132  14 context.getTagStack().pushScannerContext(new WikiScannerContext(xWikiGeneratorListener));
133  14 context.getTagStack().getScannerContext().beginDocument();
134  14 withNonGeneratedContent = true;
135    } catch (ComponentLookupException e) {
136  0 this.logger.error("Error while getting the appropriate renderer for syntax [{}]",
137    currentSyntaxParameter, e);
138    }
139    }
140    }
141   
142  217 context.getTagStack().pushStackParameter(NON_GENERATED_CONTENT_STACK, withNonGeneratedContent);
143  217 return withNonGeneratedContent || macroInfo != null;
144    }
145   
146    /**
147    * Handle the end of a container (div or span) which can contain a non generated content metadata.
148    * @param context the context of the current tag.
149    * @return true if a non generated content tag has been detected or it's in an macro info.
150    */
 
151  217 toggle public boolean handleEnd(TagContext context)
152    {
153  217 boolean nonGeneratedContent = (boolean) context.getTagStack().popStackParameter(NON_GENERATED_CONTENT_STACK);
154  217 MacroInfo macroInfo = (MacroInfo) context.getTagStack().getStackParameter(MACRO_INFO);
155   
156  217 if (nonGeneratedContent) {
157  14 context.getTagStack().getScannerContext().endDocument();
158   
159  14 XWikiGeneratorListener xWikiGeneratorListener =
160    (XWikiGeneratorListener) context.getTagStack().popScannerContext().getfListener();
161   
162  14 PrintRenderer renderer;
163   
164  14 if (context.getTagStack().isInsideBlockElement()) {
165  3 WrappingListener wrappingListener = (WrappingListener) xWikiGeneratorListener.getListener();
166  3 renderer = (PrintRenderer) wrappingListener.getWrappedListener();
167    } else {
168  11 renderer = (PrintRenderer) xWikiGeneratorListener.getListener();
169    }
170  14 String content = renderer.getPrinter().toString();
171  14 macroInfo.setContent(content);
172    }
173   
174  217 return nonGeneratedContent || macroInfo != null;
175    }
176    }