Coverage Report - net.admin4j.util.GuiUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
GuiUtils
90%
30/33
86%
19/22
3.571
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.util;
 15  
 
 16  
 import java.text.DecimalFormat;
 17  
 import java.util.Date;
 18  
 
 19  
 import net.admin4j.deps.commons.lang3.StringEscapeUtils;
 20  
 import net.admin4j.deps.commons.lang3.StringUtils;
 21  
 
 22  
 /**
 23  
  * Generic utilities needed for generating page displays.
 24  
  * @author D. Ashmore
 25  
  * @since 1.0
 26  
  */
 27  81
 public class GuiUtils {
 28  
     
 29  
     public static String htmlEscape(Object obj) {
 30  15
         String str = null;
 31  15
         if (obj instanceof String) {
 32  9
             str = (String)obj;
 33  
         }
 34  6
         else if (obj == null) {
 35  3
             str = "";
 36  
         }
 37  3
         else str = obj.toString();
 38  
         
 39  
         return StringEscapeUtils.escapeHtml4(str);
 40  
     }
 41  
     
 42  
     public static String javascriptEscape(Object obj) {
 43  12
         String str = null;
 44  12
         if (obj instanceof String) {
 45  6
             str = (String)obj;
 46  
         }
 47  6
         else if (obj == null) {
 48  3
             str = "";
 49  
         }
 50  3
         else str = obj.toString();
 51  
         
 52  
         str = StringUtils.replace(str, "'", "\\'");
 53  
         str = StringUtils.replace(str, "\"", "\\\"");
 54  
         
 55  12
         return str;
 56  
     }
 57  
     
 58  
     public static String objectId(Object obj) {
 59  6
         if (obj == null)  return "-1";
 60  3
         DecimalFormat format = new DecimalFormat("#");
 61  3
         return format.format(System.identityHashCode(obj));
 62  
     }
 63  
     
 64  
     public static Date toDate(Object timeInMillis) {
 65  141
         if (timeInMillis == null)  return null;
 66  138
         if (timeInMillis instanceof Number) {
 67  138
             return new Date( ((Number)timeInMillis).longValue());
 68  
         }
 69  
         
 70  0
         throw new Admin4jRuntimeException("Invalid Long date")
 71  
             .addContextValue("timeInMillis", timeInMillis);
 72  
     }
 73  
     
 74  
     public static Double bytes2Mb(Number bytes) {
 75  156
         if (bytes == null) {
 76  3
             return null;
 77  
         }
 78  
         
 79  153
         return bytes.doubleValue() / 1024000.000;
 80  
     }
 81  
     
 82  
     public static String abbreviate(String text, Number maxNbrChars) {
 83  6
         return abbreviate(text, maxNbrChars, "...");
 84  
     }
 85  
     
 86  
     public static String abbreviate(String text, Number maxNbrChars, String suffix) {
 87  9
         if (text == null) {
 88  3
             return text;
 89  
         }
 90  6
         String localSuffix = suffix;
 91  6
         if (localSuffix == null) {
 92  0
             localSuffix = "";
 93  
         }
 94  
         
 95  6
         if (text.length() > maxNbrChars.intValue()) {
 96  6
             return text.substring(0, maxNbrChars.intValue()) + localSuffix;
 97  
         }
 98  0
         return text;
 99  
     }
 100  
 
 101  
 }