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

File JsSkinExtensionPlugin.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
16
6
1
132
55
9
0.56
2.67
6
1.5

Classes

Class Line # Actions
JsSkinExtensionPlugin 35 16 0% 9 5
0.821428682.1%
 

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.plugin.skinx;
21   
22    import org.apache.commons.lang3.BooleanUtils;
23    import org.apache.commons.lang3.StringUtils;
24    import org.xwiki.model.reference.DocumentReference;
25    import org.xwiki.model.reference.LocalDocumentReference;
26   
27    import com.xpn.xwiki.XWikiContext;
28   
29    /**
30    * Skin Extension plugin that allows pulling javascript code stored inside wiki documents as
31    * <code>XWiki.JavaScriptExtension</code> objects.
32    *
33    * @version $Id: 0c74f76d9066293662613eab4411ac8eed408495 $
34    */
 
35    public class JsSkinExtensionPlugin extends AbstractDocumentSkinExtensionPlugin
36    {
37    /** The name of the XClass storing the code for this type of extensions. */
38    public static final String JSX_CLASS_NAME = "XWiki.JavaScriptExtension";
39   
40    /** The local reference of the XClass storing the code for this type of extensions. */
41    public static final LocalDocumentReference JSX_CLASS_REFERENCE = new LocalDocumentReference("XWiki",
42    "JavaScriptExtension");
43   
44    /**
45    * The identifier for this plugin; used for accessing the plugin from velocity, and as the action returning the
46    * extension content.
47    */
48    public static final String PLUGIN_NAME = "jsx";
49   
50    /**
51    * The name of the preference (in the configuration file) specifying what is the default value of the defer, in case
52    * nothing is specified in the parameters of this extension.
53    */
54    public static final String DEFER_DEFAULT_PARAM = "xwiki.plugins.skinx.deferred.default";
55   
56    /**
57    * XWiki plugin constructor.
58    *
59    * @param name The name of the plugin, which can be used for retrieving the plugin API from velocity. Unused.
60    * @param className The canonical classname of the plugin. Unused.
61    * @param context The current request context.
62    * @see com.xpn.xwiki.plugin.XWikiDefaultPlugin#XWikiDefaultPlugin(String,String,com.xpn.xwiki.XWikiContext)
63    */
 
64  32 toggle public JsSkinExtensionPlugin(String name, String className, XWikiContext context)
65    {
66  32 super(PLUGIN_NAME, className, context);
67    }
68   
69    /**
70    * {@inheritDoc}
71    * <p>
72    * We must override this method since the plugin manager only calls it for classes that provide their own
73    * implementation, and not an inherited one.
74    * </p>
75    *
76    * @see com.xpn.xwiki.plugin.XWikiPluginInterface#virtualInit(com.xpn.xwiki.XWikiContext)
77    */
 
78  4 toggle @Override
79    public void virtualInit(XWikiContext context)
80    {
81  4 super.virtualInit(context);
82    }
83   
 
84  6100 toggle @Override
85    public String getLink(String documentName, XWikiContext context)
86    {
87  6100 DocumentReference documentReference = getCurrentDocumentReferenceResolver().resolve(documentName);
88  6100 if (!isAccessible(documentReference, context)) {
89    // No access to view the Skin Extension's document. Don`t generate any link to avoid a useless network
90    // request always leading to a 403 Error.
91  0 return "";
92    }
93   
94  6100 StringBuilder result = new StringBuilder("<script type='text/javascript' src='");
95  6100 result.append(getDocumentSkinExtensionURL(documentReference, documentName, PLUGIN_NAME, context));
96    // check if js should be deferred, defaults to the preference configured in the cfg file, which defaults to true
97  6100 String defaultDeferString = context.getWiki().Param(DEFER_DEFAULT_PARAM);
98  6100 Boolean defaultDefer = (!StringUtils.isEmpty(defaultDeferString)) ? Boolean.valueOf(defaultDeferString) : true;
99  6100 if (BooleanUtils.toBooleanDefaultIfNull((Boolean) getParameter("defer", documentName, context), defaultDefer)) {
100  6091 result.append("' defer='defer");
101    }
102  6100 result.append("'></script>\n");
103  6100 return result.toString();
104    }
105   
 
106  9069 toggle @Override
107    protected String getExtensionClassName()
108    {
109  9069 return JSX_CLASS_NAME;
110    }
111   
 
112  0 toggle @Override
113    protected String getExtensionName()
114    {
115  0 return "Javascript";
116    }
117   
118    /**
119    * {@inheritDoc}
120    * <p>
121    * We must override this method since the plugin manager only calls it for classes that provide their own
122    * implementation, and not an inherited one.
123    * </p>
124    *
125    * @see AbstractSkinExtensionPlugin#endParsing(String, XWikiContext)
126    */
 
127  6893 toggle @Override
128    public String endParsing(String content, XWikiContext context)
129    {
130  6893 return super.endParsing(content, context);
131    }
132    }