Coverage Report - net.sf.jmatchparser.template.engine.DefaultCallbackFunctions
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultCallbackFunctions
94%
17/18
N/A
3,111
DefaultCallbackFunctions$1
85%
6/7
75%
3/4
3,111
DefaultCallbackFunctions$2
75%
3/4
50%
1/2
3,111
DefaultCallbackFunctions$3
75%
3/4
50%
1/2
3,111
DefaultCallbackFunctions$4
75%
3/4
50%
1/2
3,111
DefaultCallbackFunctions$5
75%
3/4
50%
1/2
3,111
DefaultCallbackFunctions$6
66%
8/12
66%
4/6
3,111
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * All rights reserved.
 4  
  * 
 5  
  * This program is free software: you can redistribute it and/or modify
 6  
  * it under the terms of the GNU General Public License as published by
 7  
  * the Free Software Foundation, either version 2 of the License, or
 8  
  * (at your option) any later version.
 9  
  * 
 10  
  * This program 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
 13  
  * GNU General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 package net.sf.jmatchparser.template.engine;
 19  
 
 20  
 import javax.script.Bindings;
 21  
 import javax.script.ScriptEngine;
 22  
 import net.sf.jmatchparser.template.CallbackFunction;
 23  
 
 24  0
 public class DefaultCallbackFunctions {
 25  
         static void addTo(ParserImpl parser) {
 26  188
                 parser.addCallback("replace", new CallbackFunction() {
 27  
                         @Override
 28  
                         public String call(String[] arguments) {
 29  2
                                 if (arguments.length % 2 != 1)
 30  0
                                         throw new RuntimeException("Replace needs odd number of arguments: " + arguments.length);
 31  2
                                 String result = arguments[0];
 32  6
                                 for (int i = 1; i < arguments.length; i += 2) {
 33  4
                                         result = result.replaceAll(arguments[i], arguments[i + 1]);
 34  
                                 }
 35  2
                                 return result;
 36  
                         }
 37  
                 });
 38  188
                 parser.addCallback("uppercase", new CallbackFunction() {
 39  
                         @Override
 40  
                         public String call(String[] arguments) {
 41  9
                                 if (arguments.length != 1)
 42  0
                                         throw new RuntimeException("Uppercase needs 1 argument: " + arguments.length);
 43  9
                                 return arguments[0].toUpperCase();
 44  
                         }
 45  
                 });
 46  188
                 parser.addCallback("lowercase", new CallbackFunction() {
 47  
                         @Override
 48  
                         public String call(String[] arguments) {
 49  2
                                 if (arguments.length != 1)
 50  0
                                         throw new RuntimeException("Lowercase needs 1 argument: " + arguments.length);
 51  2
                                 return arguments[0].toLowerCase();
 52  
                         }
 53  
                 });
 54  188
                 parser.addCallback("addentities", new CallbackFunction() {
 55  
                         @Override
 56  
                         public String call(String[] arguments) {
 57  2
                                 if (arguments.length != 1)
 58  0
                                         throw new RuntimeException("AddEntities needs 1 argument: " + arguments.length);
 59  2
                                 return addEntities(arguments[0]);
 60  
                         }
 61  
                 });
 62  188
                 parser.addCallback("stripentities", new CallbackFunction() {
 63  
                         @Override
 64  
                         public String call(String[] arguments) {
 65  2
                                 if (arguments.length != 1)
 66  0
                                         throw new RuntimeException("StripEntities needs 1 argument: " + arguments.length);
 67  2
                                 return stripEntities(arguments[0]);
 68  
                         }
 69  
                 });
 70  188
                 parser.addCallback("js", new CallbackFunction() {
 71  
                         @Override
 72  
                         public String call(String[] arguments) {
 73  2
                                 if (arguments.length == 0)
 74  0
                                         throw new RuntimeException("JS needs at least 1 argument: " + arguments.length);
 75  2
                                 ScriptEngine engine = new javax.script.ScriptEngineManager().getEngineByName("rhino");
 76  2
                                 if (engine == null)
 77  0
                                         throw new RuntimeException("Rhino engine not found");
 78  2
                                 Bindings bindings = engine.createBindings();
 79  4
                                 for (int i = 1; i < arguments.length; i++) {
 80  2
                                         bindings.put("param" + i, arguments[i]);
 81  
                                 }
 82  
                                 try {
 83  2
                                         return engine.eval(arguments[0], bindings).toString();
 84  0
                                 } catch (Exception ex) {
 85  0
                                         throw new RuntimeException("Error while running Rhino engine", ex);
 86  
                                 }
 87  
                         }
 88  
                 });
 89  188
         }
 90  
 
 91  
         public static String stripEntities(String string) {
 92  3
                 string = string.replace("&lt;", "<");
 93  3
                 string = string.replace("&gt;", ">");
 94  3
                 string = string.replace("&quot;", "\"");
 95  3
                 string = string.replace("&amp;", "&");
 96  3
                 return string;
 97  
         }
 98  
 
 99  
         public static String addEntities(String string) {
 100  6
                 string = string.replace("&", "&amp;");
 101  6
                 string = string.replace("<", "&lt;");
 102  6
                 string = string.replace(">", "&gt;");
 103  6
                 string = string.replace("\"", "&quot;");
 104  6
                 return string;
 105  
         }
 106  
 }