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

File AbstractFormatTagHandler.java

 

Coverage histogram

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

Code metrics

24
50
6
1
185
122
21
0.42
8.33
6
3.5

Classes

Class Line # Actions
AbstractFormatTagHandler 38 50 0% 21 6
0.92592.5%
 

Contributing tests

This file is covered by 322 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.wikimodel.xhtml.handler;
21   
22    import java.io.StringReader;
23   
24    import org.w3c.css.sac.InputSource;
25    import org.xwiki.rendering.wikimodel.WikiParameter;
26    import org.xwiki.rendering.wikimodel.WikiParameters;
27    import org.xwiki.rendering.wikimodel.WikiStyle;
28    import org.xwiki.rendering.wikimodel.xhtml.impl.TagContext;
29   
30    import com.steadystate.css.dom.CSSStyleDeclarationImpl;
31    import com.steadystate.css.parser.CSSOMParser;
32    import com.steadystate.css.parser.SACParserCSS21;
33   
34    /**
35    * @version $Id: 653c5a9385b9abfeb161abeb45750b0e4f072c5e $
36    * @since 4.0M1
37    */
 
38    public abstract class AbstractFormatTagHandler extends TagHandler
39    {
40    public static final String FORMATWIKISTYLE = "formatWikiStyle";
41   
42    public static final String FORMATPARAMETERS = "formatParameters";
43   
44    public static final String FORMATSTYLEPARAMETER = "formatStyleParameter";
45   
46    private final WikiStyle style;
47   
48    /**
49    * The object used to parse the style attribute. Explicitly specify the
50    * parser to use, since otherwise cssparser overrides the default parser
51    * used in the JVM, breaking css4j.
52    */
53    private final CSSOMParser cssParser = new CSSOMParser(new SACParserCSS21());
54   
 
55  731 toggle public AbstractFormatTagHandler()
56    {
57  731 this(null);
58    }
59   
 
60  3790 toggle protected AbstractFormatTagHandler(WikiStyle style)
61    {
62  3790 super(true);
63   
64  3790 this.style = style;
65    }
66   
 
67  410 toggle @Override
68    protected void begin(TagContext context)
69    {
70    // parameters
71  410 WikiParameters currentParameters = (WikiParameters) context
72    .getTagStack().getStackParameter(FORMATPARAMETERS);
73  410 CSSStyleDeclarationImpl currentStyle = (CSSStyleDeclarationImpl) context
74    .getTagStack().getStackParameter(FORMATSTYLEPARAMETER);
75   
76  410 if (currentParameters != null) {
77  58 for (WikiParameter parameter : context.getParams()) {
78  37 WikiParameter currentParameter = currentParameters
79    .getParameter(parameter.getKey());
80   
81  37 String value = parameter.getValue();
82   
83  37 if (currentParameter != null) {
84  21 if (parameter.getKey().equals("style")) {
85  17 CSSStyleDeclarationImpl mergedStyle = mergeStyle(
86    currentStyle, currentParameter.getValue(),
87    parameter.getValue());
88   
89  17 if (mergedStyle != currentStyle) {
90  17 value = mergedStyle.getCssText();
91  17 currentStyle = mergedStyle;
92    }
93  4 } else if (parameter.getKey().equals("class")) {
94  4 value = mergeClass(currentParameter.getValue(),
95    parameter.getValue());
96    }
97    }
98   
99  37 currentParameters = currentParameters.setParameter(
100    parameter.getKey(), value);
101    }
102    } else {
103  352 currentParameters = new WikiParameters(context.getParams());
104    }
105  410 context.getTagStack().pushStackParameter(FORMATPARAMETERS,
106    currentParameters);
107  410 context.getTagStack().pushStackParameter(FORMATSTYLEPARAMETER,
108    currentStyle);
109   
110  410 if (currentParameters.getSize() > 0) {
111  193 context.getScannerContext().beginFormat(currentParameters);
112    }
113   
114    // style
115  410 if (this.style != null) {
116  136 context.getScannerContext().beginFormat(this.style);
117  136 context.getTagStack().pushStackParameter(FORMATWIKISTYLE,
118    this.style);
119    }
120    }
121   
 
122  17 toggle private CSSStyleDeclarationImpl mergeStyle(
123    CSSStyleDeclarationImpl parentStyle, String parentStyleValue,
124    String styleValue)
125    {
126  17 CSSStyleDeclarationImpl currentStyle = new CSSStyleDeclarationImpl();
127   
128  17 if (parentStyle == null) {
129  17 try {
130  17 this.cssParser.parseStyleDeclaration(currentStyle,
131    new InputSource(new StringReader(parentStyleValue)));
132    } catch (Exception e) {
133  0 return parentStyle;
134    }
135    } else {
136  0 currentStyle.setProperties(parentStyle.getProperties());
137    }
138   
139  17 try {
140  17 this.cssParser.parseStyleDeclaration(currentStyle, new InputSource(
141    new StringReader(styleValue)));
142    } catch (Exception e) {
143  0 return parentStyle;
144    }
145   
146  17 return currentStyle;
147    }
148   
 
149  4 toggle private String mergeClass(String value1, String value2)
150    {
151  4 return value1 + " " + value2;
152    }
153   
 
154  410 toggle @Override
155    protected void end(TagContext context)
156    {
157    // style
158  410 if (this.style != null) {
159  136 context.getScannerContext().endFormat(this.style);
160  136 context.getTagStack().popStackParameter(FORMATWIKISTYLE);
161    }
162   
163    // parameters
164  410 if (context.getParams().getSize() > 0) {
165  183 context.getScannerContext().endFormat(WikiParameters.EMPTY);
166    }
167   
168  410 context.getTagStack().popStackParameter(FORMATPARAMETERS);
169  410 context.getTagStack().popStackParameter(FORMATSTYLEPARAMETER);
170   
171    // reopen
172   
173  410 WikiStyle currentWikiStyle = (WikiStyle) context.getTagStack()
174    .getStackParameter(FORMATWIKISTYLE);
175  410 WikiParameters currentParameters = (WikiParameters) context
176    .getTagStack().getStackParameter(FORMATPARAMETERS);
177   
178  410 if (currentParameters != null && currentParameters.getSize() > 0) {
179  36 context.getScannerContext().beginFormat(currentParameters);
180  36 if (currentWikiStyle != null) {
181  1 context.getScannerContext().beginFormat(currentWikiStyle);
182    }
183    }
184    }
185    }