1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.api

File Util.java

 

Coverage histogram

../../../../img/srcFileCovDistChart3.png
82% of files have more coverage

Code metrics

0
17
14
1
220
73
14
0.82
1.21
14
1

Classes

Class Line # Actions
Util 36 17 0% 14 22
0.2903225729%
 

Contributing tests

This file is covered by 23 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 com.xpn.xwiki.api;
21   
22    import java.awt.image.BufferedImage;
23    import java.io.IOException;
24    import java.io.OutputStream;
25    import java.util.Date;
26   
27    import com.xpn.xwiki.XWikiContext;
28    import com.xpn.xwiki.plugin.image.ImageProcessor;
29    import com.xpn.xwiki.web.Utils;
30   
31    /**
32    * Utility APIs, available to scripting environments under the {@code util} variable.
33    *
34    * @version $Id: a581bc36e2fa571ef7793f8d2b730a8217edb2c9 $
35    */
 
36    public class Util extends Api
37    {
38    /** The internal object wrapped by this API. */
39    private com.xpn.xwiki.XWiki xwiki;
40   
41    /**
42    * Simple constructor, initializes a new utility API with the current {@link com.xpn.xwiki.XWikiContext context} and
43    * the current global {@link com.xpn.xwiki.XWiki XWiki} object.
44    *
45    * @param xwiki the current global XWiki object
46    * @param context the current context
47    * @see Api#Api(com.xpn.xwiki.XWikiContext)
48    */
 
49  168322 toggle public Util(com.xpn.xwiki.XWiki xwiki, XWikiContext context)
50    {
51  168187 super(context);
52  168562 this.xwiki = xwiki;
53    }
54   
55    /**
56    * Decodes a <code>application/x-www-form-urlencoded</code> string.
57    *
58    * @param text the encoded text
59    * @return decoded text
60    * @since 1.3 Milestone 2
61    */
 
62  0 toggle public String decodeURI(String text)
63    {
64  0 return com.xpn.xwiki.util.Util.decodeURI(text, this.context);
65    }
66   
67    /**
68    * Creates a new {@link Date} object corresponding to the current time. This is useful from Velocity since new
69    * objects cannot be created.
70    *
71    * @return the current date
72    * @since 1.3 Milestone 2
73    * @deprecated use <code>$datetool.date</code> instead
74    */
 
75  0 toggle @Deprecated
76    public Date getDate()
77    {
78  0 return new Date();
79    }
80   
81    /**
82    * Creates a new {@link Date} object corresponding to the specified time. This is useful from Velocity since new
83    * objects cannot be created.
84    *
85    * @param time time in milliseconds since 1970, 00:00:00 GMT
86    * @return Date a date from a time in milliseconds since 01/01/1970 as a Java {@link Date} Object
87    * @since 1.3 Milestone 2
88    * @deprecated use <code>$datetool.toDate(time)</code> instead
89    */
 
90  0 toggle @Deprecated
91    public Date getDate(long time)
92    {
93  0 return new Date(time);
94    }
95   
96    /**
97    * Compute the elapsed time, in milliseconds, since the specified unix-epoch timestamp. This is useful from Velocity
98    * since new objects cannot be created.
99    *
100    * @param time the time in milliseconds
101    * @return the time delta in milliseconds between the current date and the time passed as parameter
102    * @since 1.3 Milestone 2
103    */
 
104  0 toggle public int getTimeDelta(long time)
105    {
106  0 return this.xwiki.getTimeDelta(time);
107    }
108   
109    /**
110    * Get a stack trace as a String.
111    *
112    * @param e the exception to convert to a String
113    * @return the exception stack trace as a String
114    * @since 1.3 Milestone 2
115    */
 
116  0 toggle public String printStrackTrace(Throwable e)
117    {
118  0 return this.xwiki.printStrackTrace(e);
119    }
120   
121    /**
122    * Generate a random string, containing only alpha-numeric characters.
123    *
124    * @param size the desired size of the string
125    * @return the randomly generated string
126    * @since 1.3 Milestone 2
127    */
 
128  20 toggle public String generateRandomString(int size)
129    {
130  20 return this.xwiki.generateRandomString(size);
131    }
132   
133    /**
134    * Output a BufferedImage object into the response outputstream. Once this method has been called, no further action
135    * is possible. Users should set {@code $xcontext.setFinished(true)} to avoid template output. The image is served
136    * as image/jpeg.
137    *
138    * @param image the BufferedImage to output
139    * @throws java.io.IOException if the output fails
140    * @since 1.3 Milestone 2
141    */
 
142  0 toggle public void outputImage(BufferedImage image) throws IOException
143    {
144  0 OutputStream ostream = getXWikiContext().getResponse().getOutputStream();
145  0 Utils.getComponent(ImageProcessor.class).writeImage(image, "image/jpeg", (float) 0.8, ostream);
146  0 ostream.flush();
147    }
148   
149    /**
150    * Get a Null value. This is useful in Velocity where there is no real {@code null} object for comparisons.
151    *
152    * @return a {@code null} Object
153    * @since 1.3 Milestone 2
154    */
 
155  26 toggle public Object getNull()
156    {
157  26 return null;
158    }
159   
160    /**
161    * Get a New Line character. This is useful in Velocity where there is no real new line character for inclusion in
162    * texts.
163    *
164    * @return a new line character
165    * @since 1.3 Milestone 2
166    */
 
167  564 toggle public String getNewline()
168    {
169  564 return "\n";
170    }
171   
172    /**
173    * Convert a {@code String} to a {@code Boolean} object.
174    *
175    * @param str the String containing the boolean representation to be parsed
176    * @return the boolean represented by the string argument, {@code false} if the string is not representing a boolean
177    * @since 1.8 Milestone 2
178    */
 
179  0 toggle public Boolean parseBoolean(String str)
180    {
181  0 return Boolean.parseBoolean(str);
182    }
183   
184    /**
185    * Replace all accented characters by their ASCII equivalent.
186    *
187    * @param text the text to parse
188    * @return a string with accents replaced with their alpha equivalent
189    * @since 1.3 Milestone 2
190    */
 
191  0 toggle public String clearAccents(String text)
192    {
193  0 return com.xpn.xwiki.util.Util.noaccents(text);
194    }
195   
196    /**
197    * Cleans up the passed text by removing all accents and special characters to make it a valid page name.
198    *
199    * @param documentName the document name to normalize
200    * @return the equivalent valid document name
201    * @since 1.3 Milestone 2
202    */
 
203  0 toggle public String clearName(String documentName)
204    {
205  0 return this.xwiki.clearName(documentName, getXWikiContext());
206    }
207   
208    /**
209    * Removes all non alpha numerical characters from the passed text. First tries to convert accented chars to their
210    * ASCII representation. Then it removes all the remaining non-alphanumeric non-ASCII characters.
211    *
212    * @param text the text to convert
213    * @return the alpha numeric equivalent
214    * @since 1.3 Milestone 2
215    */
 
216  0 toggle public String convertToAlphaNumeric(String text)
217    {
218  0 return com.xpn.xwiki.util.Util.convertToAlphaNumeric(text);
219    }
220    }