| 1 | |
|
| 2 | |
package org.paneris.jammyjoes.util; |
| 3 | |
|
| 4 | |
import java.text.NumberFormat; |
| 5 | |
import java.util.Locale; |
| 6 | |
|
| 7 | |
import org.melati.util.ContextUtil; |
| 8 | |
import org.melati.util.HTMLUtils; |
| 9 | |
import org.melati.util.UTF8URLEncoder; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | 0 | public class JammyJoesContextUtil { |
| 16 | |
|
| 17 | 0 | ContextUtil _melatiUtil = new ContextUtil(); |
| 18 | |
|
| 19 | |
public Integer increment(Integer a) { |
| 20 | 0 | return _melatiUtil.increment(a); |
| 21 | |
} |
| 22 | |
|
| 23 | |
public static String getPriceDisplayWithoutPound(Double price) { |
| 24 | 0 | if (price == null) return ""; |
| 25 | 0 | double a = price.doubleValue(); |
| 26 | 0 | String s = NumberFormat.getCurrencyInstance(Locale.UK).format(a); |
| 27 | 0 | if (s.length() > 1) s = s.substring(1,s.length()); |
| 28 | 0 | return s; |
| 29 | |
} |
| 30 | |
|
| 31 | |
public static String getInteger(Double price) { |
| 32 | 0 | if (price == null) return ""; |
| 33 | 0 | return price.intValue()+""; |
| 34 | |
} |
| 35 | |
|
| 36 | |
public static String getNonZeroInteger(Double price) { |
| 37 | 0 | if (price == null) return ""; |
| 38 | 0 | if (price.doubleValue() == 0) return ""; |
| 39 | 0 | return price.intValue()+""; |
| 40 | |
} |
| 41 | |
|
| 42 | |
public static String getNonZeroPriceDisplay(Double price) { |
| 43 | 0 | if (price == null) return ""; |
| 44 | 0 | if (price.doubleValue() == 0) return ""; |
| 45 | 0 | return getPriceDisplay(price); |
| 46 | |
} |
| 47 | |
|
| 48 | |
public static String getPriceDisplay(Double price) { |
| 49 | 0 | if (price == null) return ""; |
| 50 | 0 | double a = price.doubleValue(); |
| 51 | 0 | if (a < 1) { |
| 52 | 0 | return new String((new Double(a*100)).intValue() + "p"); |
| 53 | |
} else { |
| 54 | 0 | return new String(NumberFormat.getCurrencyInstance(Locale.UK).format(a)); |
| 55 | |
} |
| 56 | |
} |
| 57 | |
|
| 58 | |
public static String getPercentDisplay(Double percent) { |
| 59 | 0 | if (percent == null) return ""; |
| 60 | 0 | double a = percent.doubleValue(); |
| 61 | 0 | return new String(new Double((new Double(a*100)).intValue() / 100) + "%"); |
| 62 | |
} |
| 63 | |
|
| 64 | |
public static String trimTo(String s, int length) { |
| 65 | 0 | s = HTMLUtils.entitied(s,false, null); |
| 66 | 0 | if (s.length() > length) return s.substring(0,length) + "..."; |
| 67 | 0 | return s; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public static String CRLFtoBR(String str) { |
| 71 | 0 | int begin=0, end=0; |
| 72 | 0 | String returnStr=""; |
| 73 | |
while(true) { |
| 74 | 0 | end=str.indexOf("\r\n", begin); |
| 75 | 0 | if(end==-1) return returnStr+=str.substring(begin); |
| 76 | 0 | returnStr+=str.substring(begin, end)+"<br>"; |
| 77 | 0 | begin=end+1; |
| 78 | |
} |
| 79 | |
} |
| 80 | |
|
| 81 | |
public static String getUrlEncoded(String s) { |
| 82 | 0 | return UTF8URLEncoder.encode(s); |
| 83 | |
} |
| 84 | |
|
| 85 | |
public static int getPositionOfSlash(String s, int count) { |
| 86 | 0 | int c = 0; |
| 87 | 0 | int position = 0; |
| 88 | 0 | while (c < count && position > -1) { |
| 89 | 0 | position = s.indexOf("/",position); |
| 90 | 0 | position++; |
| 91 | 0 | c++; |
| 92 | |
} |
| 93 | 0 | return position; |
| 94 | |
} |
| 95 | |
|
| 96 | |
public static String getPathBeforeAge(String s) { |
| 97 | 0 | int p = getPositionOfSlash(s, 3); |
| 98 | 0 | if (p > -1) s = s.substring(0,p); |
| 99 | 0 | return s; |
| 100 | |
} |
| 101 | |
|
| 102 | |
public static String getPathAfterAge(String s) { |
| 103 | 0 | int p = getPositionOfSlash(s, 4); |
| 104 | 0 | if (p > 1) s = s.substring(p-1, s.length()); |
| 105 | 0 | p = getPositionOfSlash(s, 8); |
| 106 | 0 | if (p > 0) s = s.substring(0, p); |
| 107 | 0 | return s; |
| 108 | |
} |
| 109 | |
|
| 110 | |
public static void main(String[] args) { |
| 111 | 0 | System.out.println("path before age: " + getPathBeforeAge("//Dolls+houses/2_2/25_9999/house//////8/")); |
| 112 | 0 | System.out.println("path after age: " + getPathAfterAge("//Dolls+houses/2_2/25_9999/house//////8/")); |
| 113 | 0 | } |
| 114 | |
} |