Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
50   185   21   8.33
24   122   0.42   6
6     3.5  
1    
 
  AbstractFormatTagHandler       Line # 38 50 0% 21 6 92.5% 0.925
 
  (219)
 
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.XhtmlHandler.TagStack.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: 63136b51e2f811e13de7afd4d0055c6151a6a827 $
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  528 toggle public AbstractFormatTagHandler()
56    {
57  528 this(null);
58    }
59   
 
60  2691 toggle protected AbstractFormatTagHandler(WikiStyle style)
61    {
62  2691 super(false, false, true);
63   
64  2691 this.style = style;
65    }
66   
 
67  172 toggle @Override
68    protected void begin(TagContext context)
69    {
70    // parameters
71  172 WikiParameters currentParameters = (WikiParameters) context
72    .getTagStack().getStackParameter(FORMATPARAMETERS);
73  172 CSSStyleDeclarationImpl currentStyle = (CSSStyleDeclarationImpl) context
74    .getTagStack().getStackParameter(FORMATSTYLEPARAMETER);
75   
76  172 if (currentParameters != null) {
77  21 for (WikiParameter parameter : context.getParams()) {
78  15 WikiParameter currentParameter = currentParameters
79    .getParameter(parameter.getKey());
80   
81  15 String value = parameter.getValue();
82   
83  15 if (currentParameter != null) {
84  5 if (parameter.getKey().equals("style")) {
85  3 CSSStyleDeclarationImpl mergedStyle = mergeStyle(
86    currentStyle, currentParameter.getValue(),
87    parameter.getValue());
88   
89  3 if (mergedStyle != currentStyle) {
90  3 value = mergedStyle.getCssText();
91  3 currentStyle = mergedStyle;
92    }
93  2 } else if (parameter.getKey().equals("class")) {
94  2 value = mergeClass(currentParameter.getValue(),
95    parameter.getValue());
96    }
97    }
98   
99  15 currentParameters = currentParameters.setParameter(
100    parameter.getKey(), value);
101    }
102    } else {
103  151 currentParameters = new WikiParameters(context.getParams());
104    }
105  172 context.getTagStack().pushStackParameter(FORMATPARAMETERS,
106    currentParameters);
107  172 context.getTagStack().pushStackParameter(FORMATSTYLEPARAMETER,
108    currentStyle);
109   
110  172 if (currentParameters.getSize() > 0) {
111  64 context.getScannerContext().beginFormat(currentParameters);
112    }
113   
114    // style
115  172 if (this.style != null) {
116  87 context.getScannerContext().beginFormat(this.style);
117  87 context.getTagStack().pushStackParameter(FORMATWIKISTYLE,
118    this.style);
119    }
120    }
121   
 
122  3 toggle private CSSStyleDeclarationImpl mergeStyle(
123    CSSStyleDeclarationImpl parentStyle, String parentStyleValue,
124    String styleValue)
125    {
126  3 CSSStyleDeclarationImpl currentStyle = new CSSStyleDeclarationImpl();
127   
128  3 if (parentStyle == null) {
129  3 try {
130  3 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  3 try {
140  3 this.cssParser.parseStyleDeclaration(currentStyle, new InputSource(
141    new StringReader(styleValue)));
142    } catch (Exception e) {
143  0 return parentStyle;
144    }
145   
146  3 return currentStyle;
147    }
148   
 
149  2 toggle private String mergeClass(String value1, String value2)
150    {
151  2 return value1 + " " + value2;
152    }
153   
 
154  172 toggle @Override
155    protected void end(TagContext context)
156    {
157    // style
158  172 if (this.style != null) {
159  87 context.getScannerContext().endFormat(this.style);
160  87 context.getTagStack().popStackParameter(FORMATWIKISTYLE);
161    }
162   
163    // parameters
164  172 if (context.getParams().getSize() > 0) {
165  63 context.getScannerContext().endFormat(WikiParameters.EMPTY);
166    }
167   
168  172 context.getTagStack().popStackParameter(FORMATPARAMETERS);
169  172 context.getTagStack().popStackParameter(FORMATSTYLEPARAMETER);
170   
171    // reopen
172   
173  172 WikiStyle currentWikiStyle = (WikiStyle) context.getTagStack()
174    .getStackParameter(FORMATWIKISTYLE);
175  172 WikiParameters currentParameters = (WikiParameters) context
176    .getTagStack().getStackParameter(FORMATPARAMETERS);
177   
178  172 if (currentParameters != null && currentParameters.getSize() > 0) {
179  11 context.getScannerContext().beginFormat(currentParameters);
180  11 if (currentWikiStyle != null) {
181  1 context.getScannerContext().beginFormat(currentWikiStyle);
182    }
183    }
184    }
185    }