1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web.sx

File JsExtension.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

0
17
7
3
111
72
10
0.59
2.43
2.33
1.43

Classes

Class Line # Actions
JsExtension 40 3 0% 3 0
1.0100%
JsExtension.JsCompressor 64 10 0% 4 4
0.636363663.6%
JsExtension.JsCompressor.CustomErrorReporter 86 4 0% 3 7
0.00%
 

Contributing tests

No tests hitting this source file were found.

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 com.xpn.xwiki.web.sx;
21   
22    import java.io.IOException;
23    import java.io.StringReader;
24    import java.io.StringWriter;
25    import java.text.MessageFormat;
26   
27    import org.slf4j.Logger;
28    import org.slf4j.LoggerFactory;
29   
30    import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
31    import com.yahoo.platform.yui.compressor.javascript.ErrorReporter;
32    import com.yahoo.platform.yui.compressor.javascript.EvaluatorException;
33   
34    /**
35    * JavaScript extension.
36    *
37    * @version $Id: 6b412cc9910669689acb175a69f393cc61ef9140 $
38    * @since 1.7M2
39    */
 
40    public class JsExtension implements Extension
41    {
42    /** Logging helper. */
43    private static final Logger LOGGER = LoggerFactory.getLogger(JsExtension.class);
44   
 
45  759 toggle @Override
46    public String getClassName()
47    {
48  759 return "XWiki.JavaScriptExtension";
49    }
50   
 
51  252 toggle @Override
52    public String getContentType()
53    {
54  252 return "text/javascript; charset=UTF-8";
55    }
56   
 
57  252 toggle @Override
58    public SxCompressor getCompressor()
59    {
60  252 return new JsCompressor();
61    }
62   
63    /** The JavaScript compressor which is returned by getCompressor. Currently implemented using YUI Compressor. */
 
64    private static class JsCompressor implements SxCompressor
65    {
 
66  252 toggle @Override
67    public String compress(String source)
68    {
69  252 try {
70  252 ErrorReporter reporter = new CustomErrorReporter();
71  252 JavaScriptCompressor compressor = new JavaScriptCompressor(new StringReader(source), reporter);
72  252 StringWriter out = new StringWriter();
73  252 compressor.compress(out, -1, true, false, false, false);
74  252 return out.toString();
75    } catch (IOException ex) {
76  0 LOGGER.info("Failed to write the compressed output: " + ex.getMessage());
77    } catch (EvaluatorException ex) {
78  0 LOGGER.info("Failed to parse the JS extension: " + ex.getMessage());
79    } catch (Exception ex) {
80  0 LOGGER.warn("Failed to compress JS extension: " + ex.getMessage());
81    }
82  0 return source;
83    }
84   
85    /** A Javascript error reporter which logs errors with the XWiki logging system. */
 
86    private static class CustomErrorReporter implements ErrorReporter
87    {
 
88  0 toggle @Override
89    public void error(String message, String filename, int lineNumber, String context, int column)
90    {
91  0 LOGGER.warn(MessageFormat.format("Error at line {2}, column {3}: {0}. Caused by: [{1}]",
92    message, context, lineNumber, column));
93    }
94   
 
95  0 toggle @Override
96    public EvaluatorException runtimeError(String message, String filename, int lineNumber,
97    String context, int column)
98    {
99  0 LOGGER.error(MessageFormat.format("Runtime error minimizing JSX object: {0}", message));
100  0 return null;
101    }
102   
 
103  0 toggle @Override
104    public void warning(String message, String filename, int lineNumber, String context, int column)
105    {
106  0 LOGGER.info(MessageFormat.format("Warning at line {2}, column {3}: {0}. Caused by: [{1}]",
107    message, context, lineNumber, column));
108    }
109    }
110    }
111    }