View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.services.util;
20  
21  import java.text.DateFormat;
22  import java.text.SimpleDateFormat;
23  import java.util.StringTokenizer;
24  
25  import org.apache.log4j.Logger;
26  import org.apache.oro.text.regex.MalformedPatternException;
27  import org.apache.oro.text.regex.Pattern;
28  import org.apache.oro.text.regex.PatternCompiler;
29  import org.apache.oro.text.regex.Perl5Compiler;
30  import org.apache.oro.text.regex.Perl5Matcher;
31  import org.apache.oro.text.regex.Perl5Substitution;
32  import org.apache.oro.text.regex.Util;
33  
34  public class HTMLUtilities {
35      
36      private static final Logger logger = Logger.getLogger(HTMLUtilities.class);
37      private static Perl5Matcher matcher = new Perl5Matcher();
38      private static PatternCompiler compiler = new Perl5Compiler();
39      private static Pattern pattern = null;
40  
41      static {
42          try {
43              pattern = compiler.compile("<[\\w/].*?>", Perl5Compiler.CASE_INSENSITIVE_MASK);
44          } catch(MalformedPatternException mpe) {
45              logger.error("Invalid pattern in HTMLUtilities. " + mpe.getMessage());
46          }
47      }
48  
49      public static String removeQuotes(String input) {
50          StringBuffer sb = new StringBuffer(input.length());
51          int len = input.length();
52          char c;
53  
54          for(int i = 0; i < len; i++) {
55              c = input.charAt(i);
56              if (c == '\'') {
57                  sb.append("''");
58              } else {
59                  sb.append(c);
60              }
61          }
62          return sb.toString();
63      }
64  
65      public static String handleQuotes(String input) {
66          if(input == null || "".equals(input) || input.indexOf('"') == -1) {
67              return input;
68          }
69  
70          StringBuffer buf = new StringBuffer();
71  
72          char[] chars = input.toCharArray();
73          for(int i = 0; i < chars.length; i++) {
74              if(chars[i] == '"') {
75                  buf.append("&quot;");
76              } else {
77                  buf.append(chars[i]);
78              }
79          }
80  
81          return buf.toString();
82      }
83  
84       public static String escapeNewlines(String input) {
85          if(input == null || "".equals(input) || input.indexOf('\n') == -1) {
86              return input;
87          }
88  
89          StringBuffer buf = new StringBuffer();
90          char[] chars = input.toCharArray();
91          for(int i = 0; i < chars.length; i++) {
92              if(chars[i] == '\r') {
93                  continue;
94              } else if(chars[i] == '\n') {
95                  buf.append("\\n");
96              } else {
97                  buf.append(chars[i]);
98              }
99          }
100         return buf.toString();
101     }
102 
103      public static String newlinesToBreaks(String input) {
104         if(input == null || "".equals(input) || input.indexOf('\n') == -1) {
105             return input;
106         }
107 
108         StringBuffer buf = new StringBuffer();
109         char[] chars = input.toCharArray();
110         for(int i = 0; i < chars.length; i++) {
111             if(chars[i] == '\r') {
112                 continue;
113             } else if(chars[i] == '\n') {
114                 buf.append("<br>");
115             } else {
116                 buf.append(chars[i]);
117             }
118         }
119         return buf.toString();
120     }
121 
122     public static String removeMarkup(String input) {
123         String output = (input == null ? "" : input);
124 
125         if(pattern != null && matcher != null && output != null && ! output.equals("")) {
126             output = Util.substitute(matcher, pattern, new Perl5Substitution(), output, Util.SUBSTITUTE_ALL);
127         } else {
128             logger.debug("Failed removing markup.  Pattern = " + pattern + "   Output = " + output);
129         }
130 
131         return output;
132     }
133 
134     public static String escapeTags(String input) {
135         StringBuffer sb = new StringBuffer(input.length());
136         int len = input.length();
137         char c;
138 
139         for(int i = 0; i < len; i++) {
140             c = input.charAt(i);
141             if (c == '"') {
142                 sb.append("&quot;");
143             } else if (c == '&') {
144                 sb.append("&amp;");
145             } else if (c == '<') {
146                 sb.append("&lt;");
147             } else if (c == '>') {
148                 sb.append("&gt;");
149             } else {
150                 int ci = 0xffff & c;
151                 if (ci < 160 ) {
152                     // nothing special only 7 Bit
153                     sb.append(c);
154                 } else {
155                     // Not 7 Bit use the unicode system
156                     sb.append("&#");
157                     sb.append(new Integer(ci).toString());
158                     sb.append(';');
159                 }
160             }
161         }
162 
163         return sb.toString();
164     }
165     
166     /**
167      * format a itracker date format for scal datepicker
168      * 
169      * @see http://scal.fieldguidetoprogrammers.com
170      * 
171      * @param format
172      * @return
173      */
174     public static final String getJSDateFormat(String format) {
175     	
176     	
177     	String f = format.replace('m', 'n'); 
178     		
179     		
180     	f = format.toLowerCase();
181     	
182     	return f;
183 
184     	
185     }
186     
187 }
188 
189