View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.exorcist.midgard;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.InputStreamReader;
21  import java.io.OutputStream;
22  import java.io.Reader;
23  import java.io.UnsupportedEncodingException;
24  import java.net.URLDecoder;
25  
26  public final class MidgardUtil {
27  
28      public static boolean contains(String haystack, String needle) {
29          haystack = haystack.toLowerCase();
30          needle = needle.toLowerCase();
31          return (haystack.indexOf(needle) != -1);
32      }
33  
34      public static String toLower(String str) {
35          return str.toLowerCase();
36      }
37  
38      public static String urlparam(String url, String param) {
39          int pos = url.indexOf('?');
40          if (pos == -1) {
41              return "";
42          }
43          String query = url.substring(pos + 1);
44  
45          String prefix = param + "=";
46          String[] parts = query.split("&");
47          for (int i = 0; i < parts.length; i++) {
48              if (parts[i].startsWith(prefix)) {
49                  String part = parts[i].substring(prefix.length());
50                  try {
51                      return URLDecoder.decode(part, "UTF-8");
52                  } catch (IllegalArgumentException e) {
53                      return part;
54                  } catch (UnsupportedEncodingException e) {
55                      throw new RuntimeException("UTF-8 not available!", e);
56                  }
57              }
58          }
59  
60          return "";
61      }
62      
63      public static String htmltidy(final String html) throws IOException {
64          final Process process = Runtime.getRuntime().exec("tidy");
65          new Thread(new Runnable() {
66              public void run() {
67                  OutputStream output = process.getOutputStream(); 
68                  try {
69                      output.write(html.getBytes("ISO-8859-1"));
70                  } catch (IOException e) {
71                  } finally {
72                      try { output.close(); } catch (IOException e) {}
73                  }
74              }
75          }).run();
76          StringBuffer buffer = new StringBuffer();
77          InputStream input = process.getInputStream();
78          Reader reader = new InputStreamReader(input);
79          for (int ch = reader.read(); ch != -1; ch = reader.read()) {
80              buffer.append((char) ch);
81          }
82          input.close();
83          try {
84              process.waitFor();
85          } catch (InterruptedException e) {
86          }
87          return buffer.toString();
88      }
89  
90      public static String html2text(final String html) throws IOException {
91          final Process process = Runtime.getRuntime().exec("html2text");
92          new Thread(new Runnable() {
93              public void run() {
94                  OutputStream output = process.getOutputStream(); 
95                  try {
96                      output.write(html.getBytes("UTF-8"));
97                  } catch (IOException e) {
98                  } finally {
99                      try { output.close(); } catch (IOException e) {}
100                 }
101             }
102         }).run();
103         StringBuffer buffer = new StringBuffer();
104         Reader reader = new InputStreamReader(process.getInputStream());
105         for (int ch = reader.read(); ch != -1; ch = reader.read()) {
106             buffer.append((char) ch);
107         }
108         return buffer.toString();
109     }
110 }