User:TLULbot/jBot/Utils.java

/* * jBot - Java bot framework, for editing a wiki powered by MediaWiki. * Copyright (c) 2009-2010 The last username left and others. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;. * * This framework was based on AmauriceBot, created by Maurice Abraham. * * Contributors: * The last username left - Task framework, GUI * Maurice Abraham - Core utilities *--*/

package jBot;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.text.NumberFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern;

/** * Class for providing utilities to the other classes. * @author Maurice Abraham */ public class Utils {   private static ArrayList&lt;String&gt; s_logList = new ArrayList&lt;String&gt;;

private static Pattern m_cookiePattern = Pattern.compile("([^=]+)=([^;]*).*");

public static String getInternetPage(String url, String post, HashMap&lt;String,String&gt; cookieStore, Core.Printer p) throws IOException {       // In case of briefly lost connection, try 10 times for (int tries = 0; true; tries++) {           try {               URL theURL = new URL(url); HttpURLConnection connection = (HttpURLConnection)theURL.openConnection; connection.setReadTimeout(30 * 1000); // 30 seconds prepareInternetRequest(connection, post, cookieStore); return processInternetReply(connection, cookieStore); }           catch (IOException e)            { if (tries &gt;= 10) throw e; // too many tries, so give up

p.errString("Network error: " + e.toString + " (retrying)"); try {                   int delay = (tries+1)*1000; Thread.sleep(delay); }               catch (Exception ex) {                   // ignore exception }           }        }    }    private static void prepareInternetRequest(HttpURLConnection connection, String post, HashMap&lt;String,String&gt; cookieStore) throws IOException {       connection.setRequestProperty("Content-Type",        "application/x-www-form-urlencoded; charset=utf-8"); if (cookieStore != null) {       StringBuffer cookieList = new StringBuffer; for (String name : cookieStore.keySet) {       if (cookieList.length &gt; 0) cookieList.append("; "); cookieList.append(name).append("=").append(cookieStore.get(name)); }       connection.setRequestProperty("Cookie", cookieList.toString); }       if (post != null) {       byte[] postBytes = post.getBytes("utf-8"); String postLen = Integer.toString(postBytes.length); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-length", postLen); connection.setDoOutput(true); OutputStream out = connection.getOutputStream; try {       out.write(postBytes); }       finally {       out.close; }       }    }    private static String processInternetReply(HttpURLConnection connection, HashMap&lt;String,String&gt; cookieStore) throws IOException {       if (cookieStore != null) {           for (int i = 0; true; i ++) {               String fieldName = connection.getHeaderFieldKey(i); String fieldValue = connection.getHeaderField(i); if (fieldName == null &amp;&amp; fieldValue == null) break; if ("Set-Cookie".equalsIgnoreCase(fieldName)) {                   String[] toks = Utils.extract(m_cookiePattern, fieldValue); if (toks != null) cookieStore.put(toks[1], toks[2]); }           }        }        String reply = ""; InputStream in = connection.getInputStream; try {           int replyLen = connection.getContentLength; if (replyLen &gt;= 0) {       //we know the length, so could be reusing connection byte[] replyBuf = new byte[replyLen]; int numRead = 0; while (numRead &lt; replyLen) {                   numRead += in.read(replyBuf, numRead, replyLen - numRead); }               reply = new String(replyBuf, "utf-8"); }           else {       //web server didn't send content length StringBuffer replyBuf = new StringBuffer; BufferedReader reader = new BufferedReader(                       new InputStreamReader(in, "utf-8")); int c;               while ((c = reader.read) != -1) {                   replyBuf.append((char)c); }               reply = replyBuf.toString; }       }        finally {           in.close; }       return reply; }

public static String[] extract(Pattern pattern, String str) {       Matcher matcher = pattern.matcher(str); if (matcher.find) {           int totalGroups = matcher.groupCount + 1; String[] subStrs = new String[totalGroups]; for (int i = 0; i &lt; totalGroups; i++) subStrs[i] = matcher.group(i); return subStrs; }       return null; }

public static String decodeEntities(String str) {       if (str == null || !str.contains("&amp;")) return str;

return str.replaceAll("&amp;apos;","'") .replaceAll("&amp;lt;","&lt;") .replaceAll("&amp;gt;","&gt;") .replaceAll("&amp;quot;","\"")                 .replaceAll("&amp;amp;","&amp;"); //&amp; should be last    }

public static long parseNum(String numStr, String unitStr) {       if (numStr == null | numStr.length == 0) return 0; float num = Float.parseFloat(numStr.replaceAll(",",""));

if (unitStr != null) {           if (unitStr.contains("k")) num *= 1000; if (unitStr.contains("m")) num *= 1000000; }       return (long)(num + 0.5); }   public static String formatNum(long num) {       NumberFormat fmt = NumberFormat.getIntegerInstance(Locale.UK); return fmt.format(num); }

public static void log(String msg) {       if (s_logList.size &lt; 100) s_logList.add(msg); }

public static List&lt;String&gt; getLogList {       return s_logList; } }