个人工具

“用户:Wikibot”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第10行: 第10行:
 
  * and open the template in the editor.
 
  * and open the template in the editor.
 
  */
 
  */
 
 
package wiki;
 
package wiki;
  
import java.io.BufferedInputStream;
 
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
 
 
import java.io.File;
 
import java.io.File;
 
import java.io.FileInputStream;
 
import java.io.FileInputStream;
 +
import java.io.FileNotFoundException;
 
import java.io.FileOutputStream;
 
import java.io.FileOutputStream;
import java.io.FileReader;
 
 
import java.io.IOException;
 
import java.io.IOException;
 
import java.io.InputStream;
 
import java.io.InputStream;
 
import java.io.InputStreamReader;
 
import java.io.InputStreamReader;
 
import java.io.OutputStream;
 
import java.io.OutputStream;
import java.io.OutputStreamWriter;
 
import java.io.PrintWriter;
 
 
import java.io.UnsupportedEncodingException;
 
import java.io.UnsupportedEncodingException;
 
import java.net.HttpURLConnection;
 
import java.net.HttpURLConnection;
第33行: 第28行:
 
import java.net.URLEncoder;
 
import java.net.URLEncoder;
 
import java.security.GeneralSecurityException;
 
import java.security.GeneralSecurityException;
import java.security.Security;
 
 
import java.security.cert.X509Certificate;
 
import java.security.cert.X509Certificate;
 +
import java.util.ArrayList;
 +
import java.util.Date;
 
import java.util.HashMap;
 
import java.util.HashMap;
 
import java.util.Iterator;
 
import java.util.Iterator;
 
import java.util.List;
 
import java.util.List;
 
import java.util.Map;
 
import java.util.Map;
import java.util.Vector;
+
import java.util.logging.Level;
 +
import java.util.logging.Logger;
 
import java.util.regex.Matcher;
 
import java.util.regex.Matcher;
 
import java.util.regex.Pattern;
 
import java.util.regex.Pattern;
第53行: 第50行:
 
  */
 
  */
 
public class Main {
 
public class Main {
     List <String> addDict= new Vector<String>();
+
 
     List <String> oldDict= new Vector<String>();
+
     List<String> addDict = new ArrayList<String>();
     String cookie=getCookie();
+
     List<String> oldDict = new ArrayList<String>();
      
+
     String cookie = "NEED INPUT";
     private String getCookie(){
+
     int readOneDictPayTime = 2500;
         String cookie = "";
+
 
        try{
+
     public String getHtmlFromStream(InputStream in) throws IOException {
            URL httpurl = new URL("http://wiki.ubuntu.org.cn/index.php?title=Special:Userlogin");
+
         StringBuffer html = new StringBuffer();
            HttpURLConnection httpConn = (HttpURLConnection)httpurl.openConnection();
+
        int no = 0;
            httpConn.addRequestProperty("Cookie",cookie);
+
        while ((no = in.read()) > -1) {
            httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
+
             html.append((char) no);
            httpConn.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");
+
            httpConn.setRequestProperty("Connection", "Keep-Alive");
+
            httpConn.setUseCaches(false);
+
            cookie=httpConn.getHeaderField("Set-Cookie");
+
           
+
            String data="wpName=wikibot&wpPassword=********&wpRemember=1";
+
            httpurl = new URL("http://wiki.ubuntu.org.cn/index.php?title=Special:Userlogin&action=submitlogin&type=login");
+
            httpConn = (HttpURLConnection)httpurl.openConnection();
+
            httpConn.setRequestMethod("POST");
+
            httpConn.addRequestProperty("Cookie",cookie);
+
            httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
+
            httpConn.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");
+
            httpConn.setRequestProperty("Content-Language","UTF-8" );
+
            httpConn.setRequestProperty("Content-Length", ""+data.getBytes().length);
+
            httpConn.setRequestProperty("Connection", "Keep-Alive");
+
            httpConn.setDoOutput(true);
+
            httpConn.setDoInput(true);
+
            httpConn.setUseCaches(false);
+
           
+
            DataOutputStream outStream = new DataOutputStream(httpConn.getOutputStream());
+
            outStream.writeBytes(data);
+
            outStream.flush();
+
            outStream.close();
+
           
+
            List <String> list=httpConn.getHeaderFields().get("Set-Cookie");
+
             for (int i=0;i<list.size();i++){
+
                cookie=cookie+"; "+list.get(i);
+
            }
+
        } catch (Exception ex){
+
            ex.printStackTrace();
+
 
         }
 
         }
         return cookie;
+
        in.close();
 +
         return new String(html.toString().getBytes("iso8859-1"), "utf-8");
 
     }
 
     }
   
+
 
     private void addDict(String dict){
+
     private void addDict(String dict) {
         String str=dict.trim();
+
         String str = dict.trim();
       
+
 
         if (dict.startsWith("/")){
+
         if (dict.startsWith("/")) {
             str=dict.substring(1);
+
             str = dict.substring(1);
        }else if (dict.indexOf("#")>0){
+
            str=dict.substring(0,dict.indexOf("#"));
+
        }else if (dict.indexOf("?")>0){
+
            str=dict.substring(0,dict.indexOf("?"));
+
        }else if (dict.startsWith("./") || dict.startsWith("//")){
+
            str=dict.substring(2);
+
        }else if (dict.startsWith("../")){
+
            str=dict.substring(3);
+
 
         }
 
         }
         if (str.toLowerCase().indexOf("team")>0) return;
+
         if (dict.indexOf("#") > 0) {
         if (str.trim().length()==0) return;
+
            str = dict.substring(0, dict.indexOf("#"));
         if (str.trim().length()>=256) return;
+
        }
 +
        if (dict.indexOf("?") > 0) {
 +
            str = dict.substring(0, dict.indexOf("?"));
 +
        }
 +
        if (dict.startsWith("./") || dict.startsWith("//")) {
 +
            str = dict.substring(2);
 +
        }
 +
        if (dict.startsWith("../")) {
 +
            str = dict.substring(3);
 +
        }
 +
        if (dict.endsWith(".") || dict.endsWith(")") || dict.endsWith("]") ||
 +
                dict.endsWith("}") || dict.endsWith(";") || dict.endsWith("+") ||
 +
                dict.endsWith("'") || dict.endsWith("\"") || dict.endsWith("/") ||
 +
                dict.endsWith(":") || dict.endsWith(",") || dict.endsWith(">")) {
 +
            str = dict.substring(0, str.length() - 1);
 +
        }
 +
//        if (str.toLowerCase().indexOf("team")>0) return;
 +
         if (str.trim().length() == 0) {
 +
            return;
 +
        }
 +
         if (str.trim().length() >= 256) {
 +
            return;
 
         //../CommandLine
 
         //../CommandLine
          
+
         //Community => community
         if (oldDict.contains(str)) return;
+
        }
         if (addDict.contains(str)) return;
+
        if ("Community".equals(str)) {
          
+
            return;
 +
        }
 +
         if (oldDict.contains(str)) {
 +
            return;
 +
        }
 +
         if (addDict.contains(str)) {
 +
            return;
 +
         }
 
         addDict.add(str);
 
         addDict.add(str);
 
     }
 
     }
   
+
 
     private void delDict(int dictindex){
+
     private void delDict(int dictindex) {
 
         oldDict.add(addDict.get(dictindex));
 
         oldDict.add(addDict.get(dictindex));
 
         addDict.remove(dictindex);
 
         addDict.remove(dictindex);
 
     }
 
     }
      
+
 
     private void clearDict(){
+
     private void delDict(String dict) {
 +
        oldDict.add(dict);
 +
        addDict.remove(dict);
 +
    }
 +
 
 +
     private void clearDict() {
 
         addDict.clear();
 
         addDict.clear();
 
         oldDict.clear();
 
         oldDict.clear();
 
     }
 
     }
      
+
 
     private void getDicts(String html){
+
     private boolean findDict(String dict) {
         Pattern pattern= Pattern.compile("\\[UbuntuHelp:(.*?)\\]");
+
        for (String d : addDict) {
         Matcher matcher=pattern.matcher(html);
+
            if (d.equals(dict)) {
         while(matcher.find()) {
+
                return true;
             String line=matcher.group(1);
+
            }
             if (line.indexOf("|")>0){
+
        }
                 addDict(line.substring(0,line.indexOf("|")));
+
        for (String d : oldDict) {
             }else{
+
            if (d.equals(dict)) {
 +
                return true;
 +
            }
 +
        }
 +
        return false;
 +
    }
 +
 
 +
     private void getDicts(String html) {
 +
         Pattern pattern = Pattern.compile("\\[UbuntuHelp:(.*?)\\]");
 +
         Matcher matcher = pattern.matcher(html);
 +
         while (matcher.find()) {
 +
             String line = matcher.group(1);
 +
             if (line.indexOf("|") > 0) {
 +
                 addDict(line.substring(0, line.indexOf("|")));
 +
             } else {
 
                 addDict(line);
 
                 addDict(line);
 
             }
 
             }
 
         }
 
         }
         pattern= Pattern.compile("\\[UbuntuWiki:(.*?)\\]");
+
         pattern = Pattern.compile("\\[UbuntuWiki:(.*?)\\]");
         matcher=pattern.matcher(html);
+
         matcher = pattern.matcher(html);
         while(matcher.find()) {
+
         while (matcher.find()) {
             String line=matcher.group(1);
+
             String line = matcher.group(1);
             if (line.indexOf("|")>0){
+
             if (line.indexOf("|") > 0) {
                 addDict(line.substring(0,line.indexOf("|")));
+
                 addDict(line.substring(0, line.indexOf("|")));
             }else{
+
             } else {
 
                 addDict(line);
 
                 addDict(line);
 
             }
 
             }
 
         }
 
         }
 
     }
 
     }
   
+
 
 
     /** Creates a new instance of Main */
 
     /** Creates a new instance of Main */
 
     public Main() {
 
     public Main() {
第161行: 第168行:
 
         try {
 
         try {
 
             sslContext = SSLContext.getInstance("TLS");
 
             sslContext = SSLContext.getInstance("TLS");
             X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
+
             X509TrustManager[] xtmArray = new X509TrustManager[]{xtm};
 
             sslContext.init(null, xtmArray, new java.security.SecureRandom());
 
             sslContext.init(null, xtmArray, new java.security.SecureRandom());
         } catch(GeneralSecurityException gse) {
+
         } catch (GeneralSecurityException gse) {
 
         }
 
         }
         if(sslContext != null) {
+
         if (sslContext != null) {
 
             HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
 
             HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
 
         }
 
         }
 
         HttpsURLConnection.setDefaultHostnameVerifier(hnv);
 
         HttpsURLConnection.setDefaultHostnameVerifier(hnv);
 
     }
 
     }
   
 
   
 
 
     private X509TrustManager xtm = new X509TrustManager() {
 
     private X509TrustManager xtm = new X509TrustManager() {
         public void checkClientTrusted(X509Certificate[] chain, String authType) {}
+
 
         public void checkServerTrusted(X509Certificate[] chain, String authType) {}
+
        @Override
          
+
         public void checkClientTrusted(X509Certificate[] chain, String authType) {
 +
        }
 +
 
 +
        @Override
 +
         public void checkServerTrusted(X509Certificate[] chain, String authType) {
 +
        }
 +
 
 +
         @Override
 
         public X509Certificate[] getAcceptedIssuers() {
 
         public X509Certificate[] getAcceptedIssuers() {
 
             return null;
 
             return null;
 
         }
 
         }
 
     };
 
     };
   
 
 
     private HostnameVerifier hnv = new HostnameVerifier() {
 
     private HostnameVerifier hnv = new HostnameVerifier() {
 +
 +
        @Override
 
         public boolean verify(String hostname, SSLSession session) {
 
         public boolean verify(String hostname, SSLSession session) {
 
             return true;
 
             return true;
 
         }
 
         }
 
     };
 
     };
   
+
 
     public String getUrl(String urladdress,String dict) throws IOException{
+
     public String getUrl(String urladdress, String dict) throws IOException, InterruptedException {
         URL url=new URL(urladdress);
+
         URL url = new URL(urladdress);
         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
+
         HttpURLConnection httpConn;
        httpConn.setReadTimeout(60000);
+
        int SleepTime = 1;
        httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
+
        while (true) {
        httpConn.setRequestProperty("Content-Language","UTF-8" );
+
            httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestProperty("Connection", "Keep-Alive");
+
            httpConn.setReadTimeout(60000);
        InputStream in=httpConn.getInputStream();
+
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
        BufferedReader read=new BufferedReader(new InputStreamReader(in));
+
            httpConn.setRequestProperty("Content-Language", "UTF-8");
        StringBuffer sb=new StringBuffer();
+
            httpConn.setRequestProperty("Connection", "Keep-Alive");
         while (true){
+
 
             String line=read.readLine();
+
            if (httpConn.getResponseCode() > 500) {
             if (line==null) break;
+
                System.out.println("read " + httpConn.getResponseCode() + " from " + urladdress + " sleep " + SleepTime + " minute.");
             sb.append(line+"\r\n");
+
                readOneDictPayTime = readOneDictPayTime + 100;
 +
                System.out.println("adjust readOneDictPayTime to " + readOneDictPayTime);
 +
                if (readOneDictPayTime > 10000) {
 +
                    readOneDictPayTime = 2500;
 +
                }
 +
                Thread.sleep(60000 * SleepTime);
 +
                SleepTime = SleepTime + 1;
 +
            } else if (httpConn.getResponseCode() > 400) {
 +
                return "";
 +
            } else {
 +
                break;
 +
            }
 +
         }
 +
 
 +
        InputStream in = httpConn.getInputStream();
 +
        try {
 +
             String html = getHtmlFromStream(in);
 +
             return moin2wm(html, urladdress, dict);
 +
        } finally {
 +
             in.close();
 
         }
 
         }
        return moin2wm(sb.toString(),urladdress,dict);
 
 
     }
 
     }
   
+
 
     public String moin2wm(String html,String url,String dict) throws UnsupportedEncodingException{
+
     public String moin2wm(String html, String url, String dict) throws UnsupportedEncodingException {
         String text=html;
+
         String text = html;
        String ex="UbuntuHelp";
+
         if (text.indexOf("This page does not exist yet.") > 0) {
        String turl="https://help.ubuntu.com/community/";
+
             return "";
         if (url.startsWith("https://wiki")){
+
             ex="UbuntuWiki";
+
            turl="https://wiki.ubuntu.com/";
+
 
         }
 
         }
          
+
         String ex = "UbuntuHelp";
         String head="{{From|"+url.substring(0,url.indexOf("?"))+"}}\r\n{{Languages|"+ex+":"+dict+"}}\r\n";
+
        String turl = "https://help.ubuntu.com/community/";
       
+
        if (url.startsWith("https://wiki")) {
 +
            ex = "UbuntuWiki";
 +
            turl = "https://wiki.ubuntu.com/";
 +
        }
 +
 
 +
         String head = "{{From|" + url.substring(0, url.indexOf("?")) + "}}\r\n{{Languages|" + ex + ":" + dict + "}}\r\n";
 +
 
 
         //删除注释
 
         //删除注释
         text=text.replaceAll("\r\n##(.*)","");
+
         text = text.replaceAll("\r\n##(.*)", "");
         text=text.replaceAll("\r\n#format(.*)","");
+
         text = text.replaceAll("\r\n#format(.*)", "");
         text=text.replaceAll("\r\n#language(.*)","");
+
         text = text.replaceAll("\r\n#language(.*)", "");
         text=text.replaceAll("\r\n#pragma(.*)","");
+
         text = text.replaceAll("\r\n#pragma(.*)", "");
         text=text.replaceAll("\r\n#acl(.*)","");
+
         text = text.replaceAll("\r\n#acl(.*)", "");
         text=text.replaceAll("^##(.*)\r\n","");
+
         text = text.replaceAll("^##(.*)\r\n", "");
         text=text.replaceAll("^#format(.*)\r\n","");
+
         text = text.replaceAll("^#format(.*)\r\n", "");
         text=text.replaceAll("^#language(.*)\r\n","");
+
         text = text.replaceAll("^#language(.*)\r\n", "");
         text=text.replaceAll("^#pragma(.*)\r\n","");
+
         text = text.replaceAll("^#pragma(.*)\r\n", "");
         text=text.replaceAll("^#acl(.*)\r\n","");
+
         text = text.replaceAll("^#acl(.*)\r\n", "");
 
         //替换#REDIRECT PDFPrinting => #REDIRECT [[PDFPrinting]]
 
         //替换#REDIRECT PDFPrinting => #REDIRECT [[PDFPrinting]]
         text=text.replaceAll("#REDIRECT (\\S*)","#REDIRECT "+"[["+ex+":$1]]");
+
         text = text.replaceAll("#REDIRECT (\\S*)", "#REDIRECT " + "[[" + ex + ":$1]]");
         text=text.replaceAll("#redirect (\\S*)","#REDIRECT "+"[["+ex+":$1]]");
+
         text = text.replaceAll("#redirect (\\S*)", "#REDIRECT " + "[[" + ex + ":$1]]");
 
         //#refresh 0 https://wiki.ubuntu.com/ASUS_A3H_5010_Laptop_with_Ubuntu
 
         //#refresh 0 https://wiki.ubuntu.com/ASUS_A3H_5010_Laptop_with_Ubuntu
         text=text.replaceAll("#REFRESH (.*?) (\\S*)","#REDIRECT "+"[["+ex+":$2]]");
+
         text = text.replaceAll("#REFRESH (.*?) (\\S*)", "#REDIRECT " + "[[" + ex + ":$2]]");
         text=text.replaceAll("#refresh (.*?) (\\S*)","#REDIRECT "+"[["+ex+":$2]]");
+
         text = text.replaceAll("#refresh (.*?) (\\S*)", "#REDIRECT " + "[[" + ex + ":$2]]");
          
+
 
 +
         //删除不要的下拉列表
 +
        text = text.replaceAll("\\[\\[Navigation\\(.*?\\)\\]\\]", "");
 +
        text = text.replaceAll("Navigation\\(.*?\\)", "");
 
         //删除主题
 
         //删除主题
         text=text.replaceAll(".*TableOfContents.*","");
+
         text = text.replaceAll(".*TableOfContents.*", "");
 +
 
 +
        //分离{{{ }}}
 +
        Pattern pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}");
 +
        Matcher matcher = pattern.matcher(text);
 +
        List<String> codes = new ArrayList();
 +
        while (matcher.find()) {
 +
            String code = matcher.group(1);
 +
            codes.add("<code><nowiki>" + code + "</nowiki></code>");
 +
            int index = text.indexOf(matcher.group(0));
 +
            int len = matcher.group(0).length();
 +
            text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len);
 +
        }
 +
 
 +
        pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}", Pattern.DOTALL);
 +
        String html2 = text;
 +
        matcher = pattern.matcher(html2);
 +
        while (matcher.find()) {
 +
            String code = matcher.group(1);
 +
            codes.add("<pre><nowiki>" + code + "</nowiki></pre>");
 +
            int index = text.indexOf("{{{" + matcher.group(1) + "}}}");
 +
            int len = matcher.group(1).length() + 6;
 +
            text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len);
 +
        }
 +
 
 +
        //转化表格
 +
        text = tableConv(text);
 +
 
 
         //标题从二开始
 
         //标题从二开始
         text=text.replaceAll("= (.*?) =","== $1 ==");
+
         text = text.replaceAll("= (.*?) =", "== $1 ==");
 
         //转化List
 
         //转化List
         text=replaceList2(text);
+
         text = replaceList(text);
 
         //[[BR]] -> <BR>
 
         //[[BR]] -> <BR>
         text=text.replaceAll("\\[\\[BR\\]\\]","<br>");
+
         text = text.replaceAll("\\[\\[BR\\]\\]", "<br>");
 
         //link convert superscripted - ^ * ^ -> <sup> * </sup>
 
         //link convert superscripted - ^ * ^ -> <sup> * </sup>
         text=text.replaceAll("\\^(.*)\\^","<sup>$1</sup>");
+
         text = text.replaceAll("\\^(.*)\\^", "<sup>$1</sup>");
 
         //link convert subscripted - ,, * ,, -> <sub> * </sub>
 
         //link convert subscripted - ,, * ,, -> <sub> * </sub>
         text=text.replaceAll(",,(.*?),,","<sub>$1</sub>");
+
         text = text.replaceAll(",,(.*?),,", "<sub>$1</sub>");
       
+
 
         //link convert - ["/*"] -> [[ex:dict/*]]
+
         //link convert - ["/*"] -> [dict/*]
         text=text.replaceAll("\\[//(.*?)\"\\]","[["+ex+":"+dict+"/$1]]");
+
        text = text.replaceAll("\\[\\[\"//(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
         text=text.replaceAll("\\[/(.*?)\"\\]","[["+ex+":"+dict+"/$1]]");
+
        text = text.replaceAll("\\[\\[\"/(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
 +
         text = text.replaceAll("\\[\\[\"\\./(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":$1|$2]]");
 +
        text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]");
 +
 
 +
         text = text.replaceAll("\\[\\[\"//(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
 +
        text = text.replaceAll("\\[\\[\"/(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
 +
        text = text.replaceAll("\\[\\[\"\\./(.*?)\"\\]\\]", "[[" + ex + ":$1|$1]]");
 +
        text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]");
 +
        //link convert - [[/*]] -> [[dict/*]]
 +
        //[[/Introduction|Introduction]]
 +
        text = text.replaceAll("\\[\\[//(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
 +
        text = text.replaceAll("\\[\\[/(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
 +
        text = text.replaceAll("\\[\\[\\./(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":$1|$2]]");
 +
        text = text.replaceAll("\\[\\[\\.\\./(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]");
 +
 
 +
        text = text.replaceAll("\\[\\[//(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
 +
        text = text.replaceAll("\\[\\[/(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
 +
        text = text.replaceAll("\\[\\[\\./(.*?)\\]\\]", "[[" + ex + ":$1|$1]]");
 +
        text = text.replaceAll("\\[\\[\\.\\./(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]");
 +
 
 
         //link convert - [" * "] -> [[ UbuntuHelp: * ]]
 
         //link convert - [" * "] -> [[ UbuntuHelp: * ]]
         text=text.replaceAll("\\[\"(.*?)\"\\]","[["+ex+":$1]]");
+
         text = text.replaceAll("\\[\"(.*?)\"\\]", "[[" + ex + ":$1|$1]]");
 +
 
 
         //link convert - [# * ] -> [[ * ]]
 
         //link convert - [# * ] -> [[ * ]]
         text=text.replaceAll("\\[#(.*?)\\]","[[$1]]");
+
         text = text.replaceAll("\\[#(.*?)\\]", "[[$1]]");
 
         //link convert - [: / * : * ] -> [[ UbuntuHelp:dict\ * | * ]]
 
         //link convert - [: / * : * ] -> [[ UbuntuHelp:dict\ * | * ]]
         text=text.replaceAll("\\[:/(.*?):(.*?)\\]","[["+ex+":"+dict+"/$1|$2]]");
+
         text = text.replaceAll("\\[:/(.*?):(.*?)\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
 
         //link convert - [: * : * ] -> [[ UbuntuHelp: * | * ]]
 
         //link convert - [: * : * ] -> [[ UbuntuHelp: * | * ]]
         text=text.replaceAll("\\[:(.*?):(.*?)\\]","[["+ex+":$1|$2]]");
+
         text = text.replaceAll("\\[:(.*?):(.*?)\\]", "[[" + ex + ":$1|$2]]");
 
         //link convert - [: / * ] -> [[ UbuntuHelp: dict * ]]
 
         //link convert - [: / * ] -> [[ UbuntuHelp: dict * ]]
         text=text.replaceAll("\\[:/(.*?)\\]","[["+ex+":"+dict+"/$1]]");
+
         text = text.replaceAll("\\[:/(.*?)\\]", "[[" + ex + ":" + dict + "/$1]]");
 
         //link convert - [: * ] -> [[ UbuntuHelp: * ]]
 
         //link convert - [: * ] -> [[ UbuntuHelp: * ]]
         text=text.replaceAll("\\[:(.*?)\\]","[["+ex+":$1]]");
+
         text = text.replaceAll("\\[:(.*?)\\]", "[[" + ex + ":$1]]");
 
         //link convert - wiki:cat  -> [[UbuntuWiki:cat]]
 
         //link convert - wiki:cat  -> [[UbuntuWiki:cat]]
         text=text.replaceAll(" wiki:(\\S*)"," [[UbuntuWiki:$1]]");
+
        //link convert - wiki:Ubuntu/cat  -> [[UbuntuWiki:cat]]
         text=text.replaceAll("\r\nwiki:(\\S*)","\r\n[[UbuntuWiki:$1]]");
+
        text = text.replaceAll(" wiki:Ubuntu/(\\S*)", " [[UbuntuWiki:$1]]");
 +
        text = text.replaceAll("\r\nwiki:Ubuntu/(\\S*)", "\r\n[[UbuntuWiki:$1]]");
 +
         text = text.replaceAll(" wiki:(\\S*)", " [[UbuntuWiki:$1]]");
 +
         text = text.replaceAll("\r\nwiki:(\\S*)", "\r\n[[UbuntuWiki:$1]]");
 
         //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]]
 
         //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]]
         text=text.replaceAll("\\[wiki:(.*?)\\ (.*?)\\]","[[UbuntuWiki:$1|$2]]");
+
        text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\ (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
 +
         text = text.replaceAll("\\[wiki:(.*?)\\ (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
 
         //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]]
 
         //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]]
         text=text.replaceAll("\\[wiki:(.*?)\\]","[[UbuntuWiki:$1]]");
+
        text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\]", "[[UbuntuWiki:$1]]");
 +
         text = text.replaceAll("\\[wiki:(.*?)\\]", "[[UbuntuWiki:$1]]");
 
         //link convert - [UbuntuWiki:\*] -> [UbuntuWiki:dict\*]
 
         //link convert - [UbuntuWiki:\*] -> [UbuntuWiki:dict\*]
         text=text.replaceAll("\\[UbuntuWiki:\\\\(.*?)\\]","[UbuntuWiki:"+dict+"\\$1]");
+
         text = text.replaceAll("\\[UbuntuWiki:\\\\(.*?)\\]", "[UbuntuWiki:" + dict + "\\$1]");
 
         //link convert - [UbuntuHelp:\*] -> [UbuntuHelp:dict\*]
 
         //link convert - [UbuntuHelp:\*] -> [UbuntuHelp:dict\*]
         text=text.replaceAll("\\[UbuntuHelp:\\\\(.*?)\\]","[UbuntuHelp:"+dict+"\\$1]");
+
         text = text.replaceAll("\\[UbuntuHelp:\\\\(.*?)\\]", "[UbuntuHelp:" + dict + "\\$1]");
          
+
 
 +
         //Self:/ddd=dict/ Self:ddd=dict
 +
        text = text.replaceAll(":Self:(.*?)//", ":" + dict + "/$1");
 +
        text = text.replaceAll(":Self:(.*?)/", ":" + dict + "/$1");
 +
        text = text.replaceAll(":Self:(.*?)", ":$1");
 +
        text = text.replaceAll(":self:(.*?)//", ":" + dict + "/$1");
 +
        text = text.replaceAll(":self:(.*?)/", ":" + dict + "/$1");
 +
        text = text.replaceAll(":self:(.*?)", ":$1");
 +
 
 
         //link convert - __ * __ -> <u> * </u>
 
         //link convert - __ * __ -> <u> * </u>
         text=text.replaceAll("__(.*?)__","<u>$1</u>");
+
         text = text.replaceAll("__(.*?)__", "<u>$1</u>");
       
+
 
        //link convert - {{{ * }}} -> <code><nowiki> * </nowiki></code>
+
        text=text.replaceAll("\\{\\{\\{(.*?)\\}\\}\\}","<code><nowiki>$1</nowiki></code>");
+
       
+
        //link convert - \r\n {{{ *  -> \r\n<pre><nowiki> *
+
        text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","\r\n<pre><nowiki>$2");
+
       
+
        //link convert - {{{ *  -> <pre><nowiki> *
+
        text=text.replaceAll("\\{\\{\\{(.*)","\r\n<pre><nowiki>$1");
+
       
+
        //link convert -  * }}} ->  * <\pre><\nowiki>
+
        text=text.replaceAll("(.*?)\\}\\}\\}","$1</nowiki></pre>");
+
 
         //CategoryHomepage =>[[category:"+ex+"]];
 
         //CategoryHomepage =>[[category:"+ex+"]];
         text=text.replaceAll("Category(\\S*)","[[category:Category$1]]");
+
         text = text.replaceAll("Category(\\S*)", "[[category:Category$1]]");
       
+
 
         text=text.replaceAll("\r\n( *)","\r\n");
+
         text = text.replaceAll("\r\n( *)", "\r\n");
         text=replaceUrl(text,turl,dict);
+
         text = attachmentUrl(text, turl, dict);
        text=tableConv(text);
+
 
         //[[xxx:http]] => [[http:]]
 
         //[[xxx:http]] => [[http:]]
         text=text.replaceAll("\\[\\[(.*?):http(.*?)\\]\\]","[[http$2]]");
+
         text = text.replaceAll("\\[\\[(.*?):http(.*?)\\]\\]", "[[http$2]]");
 
         //[[xxx:ftp]] => [[ftp:]]
 
         //[[xxx:ftp]] => [[ftp:]]
         text=text.replaceAll("\\[\\[(.*?):ftp(.*?)\\]\\]","[[ftp$2]]");
+
         text = text.replaceAll("\\[\\[(.*?):ftp(.*?)\\]\\]", "[[ftp$2]]");
          
+
 
 +
         //[[Ubuntu:HardwareSupport]] =>[[UbuntuWiki:HardwareSupport]]
 +
        text = text.replaceAll("\\[\\[Ubuntu:(.*?)\\]\\]", "[[UbuntuWiki:$1]]");
 
         //[[https://wiki.ubuntu.com/*]] => [[UbuntuWiki:]]
 
         //[[https://wiki.ubuntu.com/*]] => [[UbuntuWiki:]]
         text=text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?)\\]\\]","[[UbuntuWiki:$1]]");
+
        text = text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]\\]", "[[UbuntuWiki:$1|$2]]");
 +
         text = text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?)\\]\\]", "[[UbuntuWiki:$1]]");
 
         //[[https://help.ubuntu.com/community/*]] => [[UbuntuHelp:]]
 
         //[[https://help.ubuntu.com/community/*]] => [[UbuntuHelp:]]
         text=text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?)\\]\\]","[[UbuntuHelp:$1]]");
+
        text = text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]\\]", "[[UbuntuHelp:$1|$2]]");
 +
         text = text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?)\\]\\]", "[[UbuntuHelp:$1]]");
 
         //[[http://wiki.ubuntu.com/*]] => [[UbuntuWiki:]]
 
         //[[http://wiki.ubuntu.com/*]] => [[UbuntuWiki:]]
         text=text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?)\\]\\]","[[UbuntuWiki:$1]]");
+
        text = text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]\\]", "[[UbuntuWiki:$1|$2]]");
 +
         text = text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?)\\]\\]", "[[UbuntuWiki:$1]]");
 
         //[[http://help.ubuntu.com/community/*]] => [[UbuntuHelp:]]
 
         //[[http://help.ubuntu.com/community/*]] => [[UbuntuHelp:]]
         text=text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?)\\]\\]","[[UbuntuHelp:$1]]");
+
         text = text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]\\]", "[[UbuntuHelp:$1|$2]]");
          
+
         text = text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?)\\]\\]", "[[UbuntuHelp:$1]]");
          
+
 
         String foot="\r\n[[category:"+ex+"]]";
+
         text = text.replaceAll("\\[http://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
       
+
        text = text.replaceAll("\\[https://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
         if (text.trim().startsWith("#REDIRECT")){
+
        text = text.replaceAll("\\[https://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]", "[[UbuntuHelp:$1|$2]]");
            System.out.print(" "+text.trim());
+
        text = text.replaceAll("\\[http://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]", "[[UbuntuHelp:$1|$2]]");
             Pattern pattern= Pattern.compile("\\[\\["+ex+":(.*?)\\]\\]");
+
        text = text.replaceAll("\\[http://wiki\\.ubuntu\\.com/(.*?)\\]", "[[UbuntuWiki:$1]]");
             Matcher matcher=pattern.matcher(text);
+
        text = text.replaceAll("\\[https://wiki\\.ubuntu\\.com/(.*?)\\]", "[[UbuntuWiki:$1]]");
             if (matcher.find()){
+
        text = text.replaceAll("\\[https://help\\.ubuntu\\.com/community/(.*?)\\]", "[[UbuntuHelp:$1]]");
 +
        text = text.replaceAll("\\[http://help\\.ubuntu\\.com/community/(.*?)\\]", "[[UbuntuHelp:$1]]");
 +
 
 +
        text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "[[$1/$3]]");
 +
        text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "[[$1/$3]]");
 +
        text = text.replaceAll("\\[\\[(.*?)/\\./(.*?)\\]\\]", "[[$1/$2]]");
 +
 
 +
        //[[http://redhat.com|RedHat]] => [http://redhat.com RedHat]
 +
        text = text.replaceAll("\\[\\[(http://.*?)\\|(.*?)\\]\\]", "[$1 $2]");
 +
        //[[https://redhat.com|RedHat]] => [https://redhat.com RedHat]
 +
        text = text.replaceAll("\\[\\[(https://.*?)\\|(.*?)\\]\\]", "[$1 $2]");
 +
        //[[ftp://redhat.com|RedHat]] => [http://redhat.com RedHat]
 +
        text = text.replaceAll("\\[\\[(ftp://.*?)\\|(.*?)\\]\\]", "[$1 $2]");
 +
        //[[http://redhat.com]] => [http://redhat.com]
 +
        text = text.replaceAll("\\[\\[(http://.*?)\\]\\]", "[$1]");
 +
        //[[https://redhat.com]] => [https://redhat.com]
 +
        text = text.replaceAll("\\[\\[(https://.*?)\\]\\]", "[$1]");
 +
        //[[ftp://redhat.com]] => [http://redhat.com]
 +
        text = text.replaceAll("\\[\\[(ftp://.*?)\\]\\]", "[$1]");
 +
 
 +
        //[[RedHat|redhat]] => [[UbuntuHelp:RedHat|readhat]]
 +
        text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":$1|$2]]");
 +
 
 +
        //[[RedHat]] => [[UbuntuHelp:RedHat|ReadHat]]
 +
        text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\]\\]", "[[" + ex + ":$1|$1]]");
 +
 
 +
 
 +
        //转化%
 +
        text = formatdict(text);
 +
 
 +
        //转化单词
 +
        text = format2(text, ex, dict);
 +
 
 +
        //需要还原$code$
 +
        for (int i = 0; i < codes.size(); i++) {
 +
            String str = codes.get(i);
 +
            String s = "@@code" + String.valueOf(i + 1) + "@@";
 +
            int m = text.indexOf(s);
 +
            int n = s.length();
 +
            //text = text.substring(0, m) + str + text.substring(m + n);
 +
            text = text.replace(s, str);
 +
        }
 +
 
 +
         String foot = "\r\n[[category:" + ex + "]]";
 +
 
 +
         if (text.trim().startsWith("#REDIRECT")) {
 +
//            System.out.print(" "+text.trim());
 +
             pattern = Pattern.compile("\\[\\[" + ex + ":(.*?)\\]\\]");
 +
             matcher = pattern.matcher(text);
 +
             if (matcher.find()) {
 
                 //如果仅仅是大小写的重定向,就不用考虑直接忽略。
 
                 //如果仅仅是大小写的重定向,就不用考虑直接忽略。
                 if (matcher.group(1).toLowerCase().equals(dict.toLowerCase())){
+
                 if (matcher.group(1).toLowerCase().equals(dict.toLowerCase())) {
                     System.out.println("\r\n"+dict+" redirect to "+matcher.group(1)+" , 忽略. ");
+
                     System.out.println(dict + " redirect to " + matcher.group(1) + " , ignore. ");
 
                     return "";
 
                     return "";
 
                 }
 
                 }
 
             }
 
             }
             return text+head+foot;
+
             return text + head + foot;
 
         }
 
         }
         if (text.trim().length()<10){
+
         if (text.trim().length() < 10) {
 
             return "";
 
             return "";
 
         }
 
         }
         return head+text+foot;
+
         return head + text + foot;
 
     }
 
     }
      
+
 
     public String replaceList2(String text){
+
     public String formatdict(String text) {
         String[] lines=text.split("\r\n");
+
        Pattern pattern = Pattern.compile("\\[\\[(.*?)\\]\\]");
 +
        Matcher matcher = pattern.matcher(text);
 +
        String html = text;
 +
        while (matcher.find()) {
 +
            String dict = matcher.group(1);
 +
            if (dict.indexOf("%") > 0) {
 +
                int index = html.indexOf("[[" + dict + "]]");
 +
                int len = dict.length() + 4;
 +
                html = html.substring(0, index) + "[[" + getDict(dict) + "]]" + html.substring(index + len);
 +
            }
 +
        }
 +
        return html;
 +
    }
 +
 
 +
    public String getDict(String dict) {
 +
        String str = "";
 +
        str = dict.replaceAll("%..", "_");
 +
        str = str.replaceAll("%.", "");
 +
        return str;
 +
    }
 +
 
 +
    //检查有没有至少两个大写单词存在,如果存在检查是不是词汇,如果是,转为链接。
 +
    public boolean arrayhas(char[] c, char b) {
 +
        for (int i = 0; i < c.length; i++) {
 +
            if (c[i] == b) {
 +
                return true;
 +
            }
 +
        }
 +
        return false;
 +
    }
 +
 
 +
    public String replaceBySpace(String text, String match) {
 +
        Pattern pattern = Pattern.compile(match);
 +
        Matcher matcher = pattern.matcher(text);
 +
        String html = text;
 +
        while (matcher.find()) {
 +
            String str = matcher.group(0);
 +
            int index = text.indexOf(str);
 +
            int length = str.length();
 +
            String space = String.format("%-" + length + "s", "");
 +
            html = html.substring(0, index) + space + html.substring(index + length);
 +
        }
 +
        return html;
 +
    }
 +
 
 +
    public String format2(String html, String ex, String dict) {
 +
        String result = html;
 +
        int n_count = 0;
 +
        int m_count = 0;
 +
        char[] o = {':', '%', '?', '&', '=', '\\', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'};
 +
        char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
 +
        char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
 +
        String temp = html;
 +
        //删除现有的所有的链接
 +
        temp = replaceBySpace(temp, "\\{\\{.*?\\}\\}");
 +
        temp = replaceBySpace(temp, "=====.*?=====");
 +
        temp = replaceBySpace(temp, "====.*?====");
 +
        temp = replaceBySpace(temp, "===.*?===");
 +
        temp = replaceBySpace(temp, "==.*?==");
 +
        temp = replaceBySpace(temp, "=.*?=");
 +
        temp = replaceBySpace(temp, "\\[\\[.*?\\]\\]");
 +
        temp = replaceBySpace(temp, "\\[.*?\\]");
 +
        temp = replaceBySpace(temp, "http:.*? ");
 +
        temp = replaceBySpace(temp, "https:.*? ");
 +
        temp = replaceBySpace(temp, "<pre>.*?</pre>");
 +
        temp = replaceBySpace(temp, "<code>.*?</code>");
 +
        String word = "";
 +
        for (int i = 0; i < temp.length(); i++) {
 +
            char c = temp.charAt(i);
 +
            if (arrayhas(m, c)) {
 +
                word = word + String.valueOf(c);
 +
                m_count++;
 +
            } else if (arrayhas(n, c)) {
 +
                word = word + String.valueOf(c);
 +
                n_count++;
 +
            } else if (arrayhas(o, c)) {
 +
                word = word + String.valueOf(c);
 +
            } else {
 +
                //单词截取完成
 +
                if (n_count > 1 && m_count > 0) {
 +
                    if (!word.equals("IconsPage") && !word.equals("AttachFile") &&
 +
                            word.indexOf("\\") < 0 && word.indexOf("/") < 0 && findDict(word) && !word.equals(dict)) {
 +
                        try {
 +
                            int index = temp.indexOf(word);
 +
                            int len = word.length();
 +
                            String replace = "[[" + ex + ":" + word + "|" + word + "]]";
 +
                            result = result.substring(0, index) + replace + result.substring(index + len);
 +
                            temp = temp.substring(0, index) + String.format("%-" + replace.length() + "s", "") + temp.substring(index + len);
 +
                        } catch (Exception e) {
 +
                            e.printStackTrace();
 +
                        }
 +
                    }
 +
                }
 +
                n_count = 0;
 +
                m_count = 0;
 +
                word = "";
 +
            }
 +
        }
 +
        return result;
 +
    }
 +
 
 +
    public String urlEncode(String dict) {
 +
        String str = dict;
 +
        str = str.replaceAll(" ", "%20");
 +
        if (str.indexOf("#") > 0) {
 +
            str = str.substring(0, str.indexOf("#"));
 +
        }
 +
        if (str.indexOf("?") > 0) {
 +
            str = str.substring(0, str.indexOf("?"));
 +
        }
 +
        return str;
 +
    }
 +
 
 +
    public boolean checkListStart(String line) {
 +
        String str = line;
 +
        if (str.length() < 4) {
 +
            return false;
 +
        }
 +
        if (str.charAt(0) == '.' && str.charAt(0) == ' ') {
 +
            return true;
 +
        }
 +
        if (str.charAt(0) == '*' && str.charAt(0) == ' ') {
 +
            return true;
 +
        }
 +
        char[] o = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
 +
        char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
 +
        char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
 +
 
 +
        if (arrayhas(o, str.charAt(0)) || arrayhas(m, str.charAt(0)) || arrayhas(n, str.charAt(0))) {
 +
            if (str.charAt(1) == '.' && str.charAt(2) == ' ') {
 +
                return true;
 +
            }
 +
        }
 +
        return false;
 +
    }
 +
 
 +
     public String replaceList(String text) {
 +
         String[] lines = text.split("\r\n");
 
         String block = "";
 
         String block = "";
         StringBuffer bf= new StringBuffer();
+
         StringBuffer bf = new StringBuffer();
         boolean start=false;
+
         boolean start = false;
         for (int i=0;i<lines.length;i++){
+
         for (int i = 0; i < lines.length; i++) {
             String line=lines[i];
+
             String line = lines[i];
             String linetrim=line.trim();
+
             String linetrim = line.trim();
             if (linetrim.equals("")) continue;
+
             if (linetrim.equals("")) {
             if (linetrim.startsWith(". ")||linetrim.startsWith("* ")||linetrim.startsWith("1. ")
+
                continue;
             ||linetrim.startsWith("a. ")||linetrim.startsWith("A. ")||linetrim.startsWith("i. ")
+
             }
            ||linetrim.startsWith("I. ")){
+
             if (checkListStart(linetrim)) {
                 if (! start){
+
                 if (!start) {
                     start=true;
+
                     start = true;
                     block=line;
+
                     block = line;
                 }else{
+
                 } else {
                     block=block+"\r\n"+line;
+
                     block = block + "\r\n" + line;
 
                 }
 
                 }
             }else{
+
             } else {
                 if (start){
+
                 if (start) {
                     block=formatList(block);
+
                     block = formatList2(block);
                   
+
 
                     bf.append(block);
+
                     bf.append(block + "\r\n");
                     start=false;
+
                     start = false;
 
                 }
 
                 }
                 bf.append(lines[i]+"\r\n");
+
                 bf.append(lines[i] + "\r\n");
 
             }
 
             }
 
         }
 
         }
         if (start){
+
         if (start) {
             block=formatList(block);
+
             block = formatList2(block);
             bf.append(block);
+
             bf.append(block + "\r\n");
             start=false;
+
             start = false;
 
         }
 
         }
 
         return bf.toString();
 
         return bf.toString();
 
     }
 
     }
      
+
 
     public String formatList(String text){
+
     public static int formatList_getSpaceCount(String s) {
 +
        int num = 0;
 +
        for (int i = 0; i < s.length(); i++) {
 +
            if (s.charAt(i) == ' ') {
 +
                num++;
 +
            } else {
 +
                break;
 +
            }
 +
        }
 +
        return num;
 +
    }
 +
 
 +
    public static String formatList_clear(String mstr) {
 +
        String str = mstr.trim();
 +
        if (str.startsWith(".") || str.startsWith("*")) {
 +
            return str.substring(1).trim();
 +
        } else {
 +
            return str.substring(2).trim();
 +
        }
 +
    }
 +
 
 +
    public String formatList2(String text) {
 +
        String[] mlist = text.split("\r\n");
 +
//        if (mlist.length<2) return text;
 +
        while (true) {
 +
            boolean done = true;
 +
            for (String line : mlist) {
 +
                if (formatList_getSpaceCount(line) > 0) {
 +
                    done = false;
 +
                    break;
 +
                }
 +
            }
 +
            if (done) {
 +
                break;
 +
            }
 +
            int lay = 0;
 +
            String lType = "ol";
 +
            for (int i = 0; i < mlist.length; i++) {
 +
                int space = formatList_getSpaceCount(mlist[i]);
 +
                if (space > 0 && lay == 0) {
 +
                    String str = mlist[i].trim();
 +
                    if (str.startsWith("*")) {
 +
                        lType = "ul";
 +
                    }
 +
                    mlist[i] = "<" + lType + "><li>" + formatList_clear(str);
 +
                    lay = space;
 +
                    if (i == mlist.length - 1) {
 +
                        mlist[i] = mlist[i] + "</li></" + lType + ">";
 +
                    }
 +
                } else if (space > 0 && lay == space) {
 +
                    mlist[i] = "</li><li>" + formatList_clear(mlist[i]);
 +
                    if (i == mlist.length - 1) {
 +
                        mlist[i] = mlist[i] + "</li></" + lType + ">";
 +
                    }
 +
                } else if (lay > 0 && space == 0) {
 +
                    mlist[i] = "</li></" + lType + ">" + mlist[i];
 +
                    break;
 +
                } else {
 +
                    continue;
 +
                }
 +
            }
 +
        }
 +
 
 +
        StringBuffer sb = new StringBuffer();
 +
        for (String line : mlist) {
 +
            sb.append(line + "\r\n");
 +
        }
 +
        return sb.toString();
 +
    }
 +
 
 +
     public String formatList(String text) {
 
         /*
 
         /*
  1. one
+
        1. one
  1. two
+
        1. two
    1. one
+
        1. one
 
         * bullet 1
 
         * bullet 1
 
         * bullet 2
 
         * bullet 2
    1. two
+
        1. two
  1. three
+
        1. three
 
         * bullet
 
         * bullet
      1. one
+
        1. one
 +
        *最好是转为*=> <ul><li></li></ul> #=> <ol><li></li></ol>
 
         */
 
         */
 
         //获得步进长度
 
         //获得步进长度
         text=text.replaceAll(" 1\\. "," # ");
+
         text = text.replaceAll(" 1\\. ", " # ");
         text=text.replaceAll(" a\\. "," # ");
+
         text = text.replaceAll(" a\\. ", " # ");
         text=text.replaceAll(" A\\. "," # ");
+
         text = text.replaceAll(" A\\. ", " # ");
         text=text.replaceAll(" i\\. "," # ");
+
         text = text.replaceAll(" i\\. ", " # ");
         text=text.replaceAll(" I\\. "," # ");
+
         text = text.replaceAll(" I\\. ", " # ");
         text=text.replaceAll(" \\. "," # ");
+
         text = text.replaceAll(" \\. ", " # ");
         String step="";
+
         String step = "";
         for (int i=0;i<text.length();i++){
+
         for (int i = 0; i < text.length(); i++) {
             if (text.charAt(i)==' '){
+
             if (text.charAt(i) == ' ') {
                 step=step+" ";
+
                 step = step + " ";
             }else break;
+
             } else {
 +
                break;
 +
            }
 
         }
 
         }
       
+
 
 
         //修改步长为" "
 
         //修改步长为" "
         if (step.length()>0)
+
         if (step.length() > 0) {
             text=text.replaceAll(step," ");
+
             text = text.replaceAll(step, " ");
        String[] lines=text.split("\r\n");
+
        for (int i=0;i<lines.length;i++){
+
            if (lines[i].charAt(0)!=' ')break;
+
            lines[i]=lines[i].substring(1);
+
 
         }
 
         }
          
+
         String[] lines = text.split("\r\n");
         StringBuffer bf= new StringBuffer();
+
        for (int i = 0; i < lines.length; i++) {
         bf.append(lines[0]+"\r\n");
+
            if (lines[i].charAt(0) != ' ') {
         for (int i=1;i<lines.length;i++){
+
                break;
             if (lines[i].startsWith(" ")){
+
            }
                 char[] s=lines[i].toCharArray();
+
            lines[i] = lines[i].substring(1);
                 for (int j=0;j<s.length;j++){
+
        }
                     if (s[j]==' '){
+
 
                         if (j>=lines[i-1].length()){
+
         StringBuffer bf = new StringBuffer();
                             s[j]=0;
+
         bf.append(lines[0] + "\r\n");
                        }if (lines[i-1].charAt(j)!='*' && lines[i-1].charAt(j)!='#'){
+
         for (int i = 1; i < lines.length; i++) {
                            s[j]=0;
+
             if (lines[i].startsWith(" ")) {
                        }else{
+
                 char[] s = lines[i].toCharArray();
                            s[j]=lines[i-1].charAt(j);
+
                 for (int j = 0; j < s.length; j++) {
 +
                     if (s[j] == ' ') {
 +
                         if (j >= lines[i - 1].length()) {
 +
                             s[j] = 0;
 
                         }
 
                         }
                     }else break;
+
                        if (lines[i - 1].charAt(j) != '*' && lines[i - 1].charAt(j) != '#') {
 +
                            s[j] = 0;
 +
                        } else {
 +
                            s[j] = lines[i - 1].charAt(j);
 +
                        }
 +
                     } else {
 +
                        break;
 +
                    }
 
                 }
 
                 }
                 String l="";
+
                 String l = "";
                 for (int j=0;j<s.length;j++){
+
                 for (int j = 0; j < s.length; j++) {
                     if (s[j]==0)continue;
+
                     if (s[j] == 0) {
                     l=l+s[j];
+
                        continue;
 +
                    }
 +
                     l = l + s[j];
 
                 }
 
                 }
                 lines[i]=l;
+
                 lines[i] = l;
 
             }
 
             }
             bf.append(lines[i]+"\r\n");
+
             bf.append(lines[i] + "\r\n");
 
         }
 
         }
       
 
 
         return bf.toString();
 
         return bf.toString();
 
     }
 
     }
   
+
 
    public String replaceList(String text){
+
     public String attachmentUrl(String text, String baseurl, String dict) throws UnsupportedEncodingException {
        text=text.replaceAll("\r\n \\. (.*?)","\r\n* $1");
+
        text=text.replaceAll("\r\n  \\. (.*?)","\r\n** $1");
+
        text=text.replaceAll("\r\n  \\. (.*?)","\r\n*** $1");
+
        text=text.replaceAll("\r\n    \\. (.*?)","\r\n**** $1");
+
        text=text.replaceAll("\r\n    \\. (.*?)","\r\n***** $1");
+
        text=text.replaceAll("\r\n      \\. (.*?)","\r\n****** $1");
+
        text=text.replaceAll("\r\n \\*(.*?)","\r\n*$1");
+
        text=text.replaceAll("\r\n  \\*(.*?)","\r\n**$1");
+
        text=text.replaceAll("\r\n  \\*(.*?)","\r\n***$1");
+
        text=text.replaceAll("\r\n    \\*(.*?)","\r\n****$1");
+
        text=text.replaceAll("\r\n    \\*(.*?)","\r\n*****$1");
+
        text=text.replaceAll("\r\n      \\*(.*?)","\r\n******$1");
+
//        text=text.replaceAll("\r\n \\. (.*?)","\r\n#: $1");
+
//        text=text.replaceAll("\r\n  \\. (.*?)","\r\n##: $1");
+
//        text=text.replaceAll("\r\n  \\. (.*?)","\r\n###: $1");
+
//        text=text.replaceAll("\r\n    \\. (.*?)","\r\n####: $1");
+
//        text=text.replaceAll("\r\n    \\. (.*?)","\r\n#####: $1");
+
//        text=text.replaceAll("\r\n      \\. (.*?)","\r\n######: $1");
+
//        text=text.replaceAll("\r\n \\*(.*?)","\r\n#$1");
+
//        text=text.replaceAll("\r\n  \\*(.*?)","\r\n##$1");
+
//        text=text.replaceAll("\r\n  \\*(.*?)","\r\n###$1");
+
//        text=text.replaceAll("\r\n    \\*(.*?)","\r\n####$1");
+
//        text=text.replaceAll("\r\n    \\*(.*?)","\r\n#####$1");
+
//        text=text.replaceAll("\r\n      \\*(.*?)","\r\n######$1");
+
       
+
        for (int i=1;i<20;i++){
+
//            text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n#$1");
+
//            text=text.replaceAll("\r\n  "+i+"\\.(.*?)","\r\n##$1");
+
//            text=text.replaceAll("\r\n  "+i+"\\.(.*?)","\r\n###$1");
+
//            text=text.replaceAll("\r\n    "+i+"\\.(.*?)","\r\n####$1");
+
//            text=text.replaceAll("\r\n    "+i+"\\.(.*?)","\r\n#####$1");
+
//            text=text.replaceAll("\r\n      "+i+"\\.(.*?)","\r\n######$1");
+
            text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n*$1");
+
            text=text.replaceAll("\r\n  "+i+"\\.(.*?)","\r\n**$1");
+
            text=text.replaceAll("\r\n  "+i+"\\.(.*?)","\r\n***$1");
+
            text=text.replaceAll("\r\n    "+i+"\\.(.*?)","\r\n****$1");
+
            text=text.replaceAll("\r\n    "+i+"\\.(.*?)","\r\n*****$1");
+
            text=text.replaceAll("\r\n      "+i+"\\.(.*?)","\r\n******$1");
+
        }
+
        return text;
+
    }
+
   
+
     public String replaceUrl(String text,String baseurl,String dict) throws UnsupportedEncodingException{
+
 
         //attachment:IconsPage/info.png -> https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png
 
         //attachment:IconsPage/info.png -> https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png
         while (true){
+
         while (true) {
             Pattern pattern= Pattern.compile("attachment:(.*?)/(\\S*)");
+
             Pattern pattern = Pattern.compile("\\{\\{attachment:(.*?)/(.*?)\\}\\}");
             Matcher matcher=pattern.matcher(text);
+
             Matcher matcher = pattern.matcher(text);
 
             String replace;
 
             String replace;
 
             if (matcher.find()) {
 
             if (matcher.find()) {
                 replace=baseurl+matcher.group(1)+"?action=AttachFile&do=get&target="+URLEncoder.encode(matcher.group(2), "UTF-8");
+
                 replace = baseurl + matcher.group(1) + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(2), "UTF-8");
                 text=text.substring(0,matcher.start(0))+replace+text.substring(matcher.end(0));
+
                 text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0));
 
                 continue;
 
                 continue;
 
             }
 
             }
           
+
 
           
+
 
             pattern= Pattern.compile("attachment:(\\S*)");
+
             pattern = Pattern.compile("\\{\\{attachment:(.*?)\\}\\}");
             matcher=pattern.matcher(text);
+
             matcher = pattern.matcher(text);
             if(matcher.find()) {
+
             if (matcher.find()) {
                 replace=baseurl+dict+"?action=AttachFile&do=get&target="+URLEncoder.encode(matcher.group(1), "UTF-8");
+
                 replace = baseurl + dict + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(1), "UTF-8");
                 text=text.substring(0,matcher.start(0))+replace+text.substring(matcher.end(0));
+
                 text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0));
 
                 continue;
 
                 continue;
 
             }
 
             }
           
+
 
 
             break;
 
             break;
 
         }
 
         }
 
         return text;
 
         return text;
 
     }
 
     }
   
+
 
     public String tableConv(String html){
+
     public String tableConv(String html) {
 
         //||a||b||c|| -> {|
 
         //||a||b||c|| -> {|
 
         //||d||e||f||    |a||b||c
 
         //||d||e||f||    |a||b||c
第505行: 第818行:
 
         //              |d||e||f
 
         //              |d||e||f
 
         //              |}
 
         //              |}
       
+
//        System.out.println(html);
         String[] lines=html.split("\r\n");
+
 
 +
         String[] lines = html.split("\r\n");
 
         String block = "";
 
         String block = "";
         StringBuffer bf= new StringBuffer();
+
         StringBuffer bf = new StringBuffer();
         boolean start=false;
+
         boolean start = false;
         for (int i=0;i<lines.length;i++){
+
         for (int i = 0; i < lines.length; i++) {
             String line=lines[i].trim();
+
             String line = lines[i].trim();
             if (line.startsWith("||")){
+
             if (line.startsWith("||")) {
                 if (! start){
+
                 if (line.length() < 4) {
                    start=true;
+
                     continue; //line like ||(.*?)||
                    String str=line.substring(1,line.length()-2);
+
                    str=str.replaceAll("<bgcolor.*?>","");
+
                     str=str.replaceAll("<style.*?>","");
+
                    str=str.replaceAll("<rowbgcolor.*?>","");
+
                    str=str.replaceAll("<tablestyle.*?>","");
+
                    block="{|border=\"1\" cellspacing=\"0\"\r\n"+str;
+
                }else{
+
                    String str=line.substring(1,line.length()-2);
+
                    str=str.replaceAll("<bgcolor.*?>","");
+
                    str=str.replaceAll("<style.*?>","");
+
                    str=str.replaceAll("<rowbgcolor.*?>","");
+
                    block=block+"\r\n|-\r\n"+str;
+
 
                 }
 
                 }
             }else{
+
                String str = line.substring(1, line.length() - 2);
                 if (start){
+
 
                     block=block+"\r\n|}\r\n";
+
                Pattern pattern = Pattern.compile("<(.*?)>");
 +
                Matcher matcher = pattern.matcher(line);
 +
                while (matcher.find()) {
 +
                    String x = matcher.group(1).trim();
 +
                    String replace = "";
 +
                    if (x.startsWith("-")) {
 +
                        if (x.indexOf(" ") > 0) {
 +
                            replace = "colspan=" + x.substring(1, x.indexOf(" ")) + "|";
 +
                        } else {
 +
                            replace = "colspan=" + x.substring(1) + "|";
 +
                        }
 +
                    } else if (x.startsWith("|")) {
 +
                        if (x.indexOf(" ") > 0) {
 +
                            replace = "rowspan=" + x.substring(1, x.indexOf(" ")) + "|";
 +
                        } else {
 +
                            replace = "rowspan=" + x.substring(1) + "|";
 +
                        }
 +
                    }
 +
//                    System.out.println("match:"+matcher.group(1)+" to:"+replace);
 +
                    int m = str.indexOf("<" + matcher.group(1) + ">");
 +
                    int n = ("<" + matcher.group(1) + ">").length();
 +
                    if (m == -1 || (m + n) > str.length()) {
 +
                        continue;
 +
                    }
 +
                    str = str.substring(0, m) + replace + str.substring(m + n);
 +
//                    str=str.replaceFirst("<"+matcher.group(1)+">",replace);
 +
                }
 +
 
 +
                if (!start) {
 +
                    start = true;
 +
                    block = "{|border=\"1\" cellspacing=\"0\"\r\n" + str;
 +
                } else {
 +
                    block = block + "\r\n|-\r\n" + str;
 +
                }
 +
             } else {
 +
                 if (start) {
 +
                     block = block + "\r\n|}\r\n";
 
                     bf.append(block);
 
                     bf.append(block);
                     start=false;
+
                     start = false;
 
                 }
 
                 }
                 bf.append(lines[i]+"\r\n");
+
                 bf.append(lines[i] + "\r\n");
 
             }
 
             }
 
         }
 
         }
         if (start){
+
         if (start) {
             block=block+"\r\n|}\r\n";
+
             block = block + "\r\n|}\r\n";
 
             bf.append(block);
 
             bf.append(block);
             start=false;
+
             start = false;
 
         }
 
         }
       
+
 
 
         return bf.toString();
 
         return bf.toString();
 
     }
 
     }
   
+
 
      
+
     public String getWikiHtml(String url) throws MalformedURLException, IOException {
      
+
        URL httpurl = new URL(url);
     public void putText(String dict,String html,String surl) throws MalformedURLException, IOException{
+
        HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection();
 +
        httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10");
 +
        httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
 +
        httpConn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
 +
        httpConn.setRequestProperty("Accept-Charset", "gb2312,utf-8;q=0.7,*;q=0.7");
 +
        httpConn.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/");
 +
        httpConn.setRequestProperty("Cookie", cookie);
 +
        httpConn.setUseCaches(false);
 +
        httpConn.setDoInput(true);
 +
        InputStream in = httpConn.getInputStream();
 +
        String wikihtml = getHtmlFromStream(in);
 +
        return wikihtml;
 +
     }
 +
 
 +
     public void putText(String dict, String html, String surl) throws MalformedURLException, IOException {
 
         URL url;
 
         URL url;
 
         URLConnection conn;
 
         URLConnection conn;
第554行: 第906行:
 
         BufferedReader read;
 
         BufferedReader read;
 
         StringBuffer sb;
 
         StringBuffer sb;
         if (html.length()<5){
+
         if (html.length() < 5) {
             System.out.print(" is short:"+html);
+
             System.out.println(dict + " is short , ignore. " + html);
 
             return;
 
             return;
 
         }
 
         }
第568行: 第920行:
 
//            }
 
//            }
 
//        }
 
//        }
         String ex="UbuntuHelp";
+
         String ex = "UbuntuHelp";
         if (surl.startsWith("https://wiki")){
+
         if (surl.startsWith("https://wiki")) {
             ex="UbuntuWiki";
+
             ex = "UbuntuWiki";
 
         }
 
         }
          
+
 
         try{
+
         String sdict = dict;
             url=new URL("http://wiki.ubuntu.org.cn/"+ex+":"+dict+"?action=raw");
+
        if (sdict.indexOf("%") > 0) {
 +
            sdict = getDict(dict);
 +
        }
 +
 
 +
 
 +
         try {
 +
             url = new URL("http://wiki.ubuntu.org.cn/" + ex + ":" + sdict + "?action=raw");
 
             conn = url.openConnection();
 
             conn = url.openConnection();
 
             conn.setReadTimeout(60000);
 
             conn.setReadTimeout(60000);
 
             conn.setRequestProperty("Cookie", cookie);
 
             conn.setRequestProperty("Cookie", cookie);
             in=conn.getInputStream();
+
             in = conn.getInputStream();
             read=new BufferedReader(new InputStreamReader(in));
+
             String wikihtml = getHtmlFromStream(in);
             sb=new StringBuffer();
+
             int wikilen = (wikihtml.replaceAll("\r", "")).replaceAll("\n", "").length();
            while (true){
+
             int htmllen = (html.replaceAll("\r", "")).replaceAll("\n", "").length();
                String line=read.readLine();
+
            if (wikilen == htmllen) {
                if (line==null) break;
+
                 System.out.println(dict + " no changes.");
                sb.append(line+"\r\n");
+
             }
+
            if (sb.toString().trim().length()==html.trim().length()) {
+
                 System.out.print(" no changes");
+
 
                 return;
 
                 return;
 +
            } else {
 +
                System.out.println(dict + " length: " + html.length() + " oldlength: " + wikihtml.length());
 
             }
 
             }
         }catch(Exception ex0){
+
         } catch (Exception ex0) {
            //nothing
+
 
         }
 
         }
       
+
 
         url=new URL("http://wiki.ubuntu.org.cn/"+ex+":"+dict+"?action=edit");
+
         String tempdict = sdict.replaceAll("%23", "");
         conn = url.openConnection();
+
        String address = "http://wiki.ubuntu.org.cn/" + ex + ":" + tempdict + "?action=edit";
         conn.setReadTimeout(60000);
+
         String wikihtml = null;
          
+
         String from = null;
         conn.setRequestProperty("Connection", "Keep-Alive");
+
         Boolean flag = false;
        conn.setRequestProperty("Cookie", cookie);
+
         for (int i = 0; i < 5; i++) {
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)");
+
            wikihtml = getWikiHtml(address);
        conn.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
+
            int start = wikihtml.indexOf("<form id=\"editform\"");
        in=conn.getInputStream();
+
            int end = wikihtml.indexOf("<div class=\"printfooter\">");
        read=new BufferedReader(new InputStreamReader(in));
+
            try {
        sb=new StringBuffer();
+
                from = wikihtml.substring(start, end);
        while (true){
+
                flag = true;
            String line=read.readLine();
+
                break;
            if (line==null) break;
+
            } catch (Exception ex0) {
             sb.append(line+"\r\n");
+
                //cookie = getCookie();
 +
                if (!wikihtml.equals("")) {
 +
                    System.out.println(wikihtml);
 +
                } else {
 +
                    System.out.println(dict + " read error!");
 +
                }
 +
                try {
 +
                    Thread.sleep(60000);
 +
                } catch (InterruptedException ex1) {
 +
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
 +
                }
 +
             }
 
         }
 
         }
        int start=sb.indexOf("<form id=\"editform\"");
+
 
         int end=sb.indexOf("<div class=\"printfooter\">");
+
         if (!flag) {
        String from;
+
             System.out.println("read " + address + " error,please set cookie!");
        try{
+
             System.exit(0);
            from=sb.substring(start,end);
+
        }catch(Exception ex0){
+
             System.out.println(dict+" error,please set cookie!");
+
             System.out.println(sb);
+
            return;
+
 
         }
 
         }
       
+
 
         Map<String,String> map=getPostDate(from);
+
         Map<String, String> map = getPostDate(from);
         Iterator<String> iterator=map.keySet().iterator();
+
         Iterator<String> iterator = map.keySet().iterator();
       
+
 
         url = new URL("http://wiki.ubuntu.org.cn/index.php?title="+ex+":"+URLEncoder.encode(dict, "UTF-8")+"&action=submit");
+
         url = new URL("http://wiki.ubuntu.org.cn/index.php?title=" + ex + ":" + URLEncoder.encode(sdict, "UTF-8") + "&action=submit");
       
+
 
 
         HttpURLConnection conn2 = (HttpURLConnection) url.openConnection();
 
         HttpURLConnection conn2 = (HttpURLConnection) url.openConnection();
         String boundary="---------------------------167593640336579986891120154";
+
         String boundary = "---------------------------167593640336579986891120154";
 
         conn2.setReadTimeout(60000);
 
         conn2.setReadTimeout(60000);
       
+
 
 
         conn2.setDoOutput(true);
 
         conn2.setDoOutput(true);
 
         conn2.setAllowUserInteraction(false);
 
         conn2.setAllowUserInteraction(false);
 
         conn2.setRequestMethod("POST");
 
         conn2.setRequestMethod("POST");
 
         conn2.setRequestProperty("Cookie", cookie);
 
         conn2.setRequestProperty("Cookie", cookie);
         conn2.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
+
         conn2.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
 
         conn2.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)");
 
         conn2.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)");
         conn2.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
+
         conn2.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
 
         conn2.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
 
         conn2.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
         conn2.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/index.php?title=UbuntuHelp:"+URLEncoder.encode(dict, "UTF-8")+"&action=edit");
+
         conn2.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/index.php?title=UbuntuHelp:" + URLEncoder.encode(dict, "UTF-8") + "&action=edit");
 
         conn2.setRequestProperty("Accept-Charset", "UTF-8,*");
 
         conn2.setRequestProperty("Accept-Charset", "UTF-8,*");
 
         conn2.setRequestProperty("Connection", "Keep-Alive");
 
         conn2.setRequestProperty("Connection", "Keep-Alive");
       
+
 
         StringBuffer content=new StringBuffer();
+
         StringBuffer content = new StringBuffer();
       
+
 
         while (iterator.hasNext()){
+
         while (iterator.hasNext()) {
             String name=iterator.next();
+
             String name = iterator.next();
             if (name.equals("wpPreview")) continue;
+
             if (name.equals("wpPreview")) {
            if (name.equals("wpDiff")) continue;
+
                 continue;
            if (name.equals("wpWatchthis")) continue;
+
            String value="";
+
           
+
            if (name.equals("wpTextbox1")){
+
                 value=html;
+
            }else {
+
                value=map.get(name);
+
 
             }
 
             }
             content.append("--"+boundary+"\r\n");
+
            if (name.equals("wpDiff")) {
             content.append("Content-Disposition: form-data; name=\""+name+"\"\r\n\r\n");
+
                continue;
             content.append(value+"\r\n");
+
            }
 +
            if (name.equals("wpWatchthis")) {
 +
                continue;
 +
            }
 +
            String value = "";
 +
 
 +
            if (name.equals("wpTextbox1")) {
 +
                value = html;
 +
            } else {
 +
                value = map.get(name);
 +
            }
 +
             content.append("--" + boundary + "\r\n");
 +
             content.append("Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n");
 +
             content.append(value + "\r\n");
 
         }
 
         }
         content.append("--"+boundary+"--\r\n\r\n");
+
         content.append("--" + boundary + "--\r\n\r\n");
         byte[] data=content.toString().getBytes();
+
         byte[] data = content.toString().getBytes();
       
+
 
 
         conn2.setRequestProperty("Content-Length", String.valueOf(data.length));
 
         conn2.setRequestProperty("Content-Length", String.valueOf(data.length));
       
+
 
 
         OutputStream out = conn2.getOutputStream();
 
         OutputStream out = conn2.getOutputStream();
 
         out.write(data);
 
         out.write(data);
 
         out.flush();
 
         out.flush();
       
+
 
 
         // Get the response
 
         // Get the response
         try{
+
         try {
             BufferedReader rd = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
+
             String wikihtml2 = getHtmlFromStream(conn2.getInputStream());
            String line;
+
//           System.out.println(wikihtml2);
            while ((line = rd.readLine()) != null) {
+
         } catch (Exception ex0) {
                //System.out.println(line);
+
             System.out.println(dict + " add. but has a error:" + ex0.getMessage());
                // Nothing
+
                break;
+
            }
+
            rd.close();
+
         }catch(Exception ex0){
+
             System.out.println(" add. but has a error:"+ex0.getMessage());
+
 
             out.close();
 
             out.close();
 
             return;
 
             return;
 
         }
 
         }
 
         out.close();
 
         out.close();
//        System.out.println(dict+" add.");
+
        System.out.println(dict + " put end .");
    };
+
        delDict(dict);
      
+
     }
      
+
 
     private Map<String,String> getPostDate(String from){
+
     @SuppressWarnings("empty-statement")
         Map<String,String> map=new HashMap<String,String>();
+
     private Map<String, String> getPostDate(String from) {
         Pattern pattern= Pattern.compile("<input(.*?)/>");
+
         Map<String, String> map = new HashMap<String, String>();
         Matcher matcher=pattern.matcher(from);
+
         Pattern pattern = Pattern.compile("<input(.*?)/>");
         Pattern pname=Pattern.compile("name=[\"|'](.*?)[\"|']");
+
         Matcher matcher = pattern.matcher(from);
         Pattern pvalue=Pattern.compile("value=[\"|'](.*?)[\"|']");
+
         Pattern pname = Pattern.compile("name=[\"|'](.*?)[\"|']");
       
+
         Pattern pvalue = Pattern.compile("value=[\"|'](.*?)[\"|']");
         while(matcher.find()) {
+
 
             String name,value;
+
         while (matcher.find()) {
             String input=matcher.group(1);
+
             String name, value;
             Matcher mname=pname.matcher(input);
+
             String input = matcher.group(1);
             Matcher mvalue=pvalue.matcher(input);
+
             Matcher mname = pname.matcher(input);
             if (mname.find()){
+
             Matcher mvalue = pvalue.matcher(input);
                 name=mname.group(1);
+
             if (mname.find()) {
             }else{
+
                 name = mname.group(1);
 +
             } else {
 
                 continue;
 
                 continue;
             };
+
             }
             if (mvalue.find()){
+
            ;
                 value=mvalue.group(1);
+
             if (mvalue.find()) {
             }else{
+
                 value = mvalue.group(1);
                 if (input.indexOf("checkbox")>0){
+
             } else {
                     value="0";
+
                 if (input.indexOf("checkbox") > 0) {
                 }else{
+
                     value = "0";
                     value="";
+
                 } else {
 +
                     value = "";
 
                 }
 
                 }
             };
+
             }
             map.put(name,value);
+
            ;
 +
             map.put(name, value);
 
         }
 
         }
       
+
 
 
//        int start=from.indexOf("cols='80' style=\"width:100%\" >");
 
//        int start=from.indexOf("cols='80' style=\"width:100%\" >");
 
//        int end=from.indexOf("</textarea>");
 
//        int end=from.indexOf("</textarea>");
 
//
 
//
 
//        map.put("wpTextbox1",from.substring(start,end));
 
//        map.put("wpTextbox1",from.substring(start,end));
         map.put("wpTextbox1","");
+
         map.put("wpTextbox1", "");
 
         return map;
 
         return map;
 
     }
 
     }
      
+
 
     public void helpstart() throws IOException{
+
     private void saveProgress(String file, String dict) throws FileNotFoundException, IOException {
 +
        FileOutputStream out = new FileOutputStream(file, true);
 +
        out.write(dict.getBytes());
 +
        out.write("\r\n".getBytes());
 +
        out.flush();
 +
        out.close();
 +
    }
 +
 
 +
    private void readProgress(String file) throws FileNotFoundException, IOException {
 +
        if (!new File(file).exists()) {
 +
            return;
 +
        }
 +
        FileInputStream in = new FileInputStream(file);
 +
        String lines = getHtmlFromStream(in);
 +
        String[] dicts = lines.split("\r\n");
 +
        for (String dict : dicts) {
 +
            oldDict.add(dict);
 +
        }
 +
    }
 +
 
 +
     public void helpstart() throws IOException, InterruptedException {
 
         clearDict();
 
         clearDict();
 
//        String dict="community/";
 
//        String dict="community/";
第732行: 第1,116行:
 
//        getDicts(out);
 
//        getDicts(out);
 
//        putText(dict,out,"https://help.ubuntu.com/community/");
 
//        putText(dict,out,"https://help.ubuntu.com/community/");
       
+
 
         InputStream in=null;
+
         InputStream in = null;
         File saveFile=new File("/tmp/helpindex.html");
+
         File saveFile = new File("help.ubuntu.com.index.html");
         if (saveFile.exists()){
+
         if (saveFile.exists()) {
             in=new FileInputStream(saveFile);
+
             in = new FileInputStream(saveFile);
         }else{
+
         } else {
             URL url=new URL("https://help.ubuntu.com/community/TitleIndex");
+
             URL url = new URL("https://help.ubuntu.com/community/TitleIndex");
             in=url.openConnection().getInputStream();
+
             in = url.openConnection().getInputStream();
 
         }
 
         }
         BufferedReader read=new BufferedReader(new InputStreamReader(in));
+
         String html = getHtmlFromStream(in);
        StringBuffer sb=new StringBuffer();
+
         if (!saveFile.exists()) {
        while (true){
+
             FileOutputStream out = new FileOutputStream(saveFile);
            String line=read.readLine();
+
             out.write(html.getBytes());
            if (line==null) break;
+
            sb.append(line+"\r\n");
+
        }
+
        read.close();
+
         if (!saveFile.exists()){
+
             FileOutputStream out=new FileOutputStream(saveFile);
+
             out.write(sb.toString().getBytes());
+
 
             out.flush();
 
             out.flush();
 
             out.close();
 
             out.close();
 
         }
 
         }
       
+
 
 
//        URL url=new URL("https://help.ubuntu.com/community/TitleIndex");
 
//        URL url=new URL("https://help.ubuntu.com/community/TitleIndex");
 
//        InputStream in=url.openConnection().getInputStream();
 
//        InputStream in=url.openConnection().getInputStream();
第765行: 第1,142行:
 
//            sb.append(line+"\r\n");
 
//            sb.append(line+"\r\n");
 
//        }
 
//        }
       
+
 
         String html=sb.substring(sb.indexOf("<a name=\"3\">"),sb.indexOf("<a name=\"%5b\">"));
+
         html = html.substring(html.indexOf("<a href=\"/community/TitleIndex?allpages=1\">") + 50, html.indexOf("<div id=\"footer\">"));
         Pattern pattern= Pattern.compile("<a href=\"/community/(.*?)\">");
+
         Pattern pattern = Pattern.compile("<a href=\"/community/(.*?)\">");
         Matcher matcher=pattern.matcher(html);
+
         Matcher matcher = pattern.matcher(html);
       
+
 
         while(matcher.find()) {
+
         while (matcher.find()) {
             String input=matcher.group(1);
+
             String input = matcher.group(1);
 
             addDict(input);
 
             addDict(input);
            System.out.println(input);
 
 
         }
 
         }
 
         String dict;
 
         String dict;
         String out;
+
         for (String d : addDict) {
          
+
            System.out.println(d);
         System.out.println("一共需要转换 "+String.valueOf(addDict.size())+" 篇文章。");
+
         }
       
+
 
         while (addDict.size()>0){
+
//        String out;
//            if (addDict.size()==0) break;
+
 
//            dict=addDict.get(addDict.size()-1);
+
         System.out.println("一共需要转换 " + String.valueOf(addDict.size()) + " 篇文章。");
             dict=addDict.get(0);
+
 
            System.out.print(String.valueOf(addDict.size())+" "+dict);
+
         readProgress("helpProgress.txt");
             try{
+
 
//                if (addDict.size()>1500) continue;
+
        ThreadPool pool = new ThreadPool();
                 try {
+
        for (int i = 0; i < addDict.size(); i++) {
                    System.out.print(" read");
+
            final String dict1 = addDict.get(i);
                     out=getUrl("https://help.ubuntu.com/community/"+dict+"?action=raw",dict);
+
             if (oldDict.contains(dict1)) {
                     System.out.print(" .");
+
                System.out.println(dict1 + " already read.");
                }catch(Exception ex){
+
                continue;
 +
             }
 +
            Runnable run = new Runnable() {
 +
 
 +
                 public void run() {
 +
                     long starttime = new Date().getTime();
 +
                     String html = "";
 
                     try {
 
                     try {
                         System.out.print(" read again");
+
                         System.out.println(dict1 + " read ...");
                         out=getUrl("https://help.ubuntu.com/community/"+dict+"?action=raw",dict);
+
                         html = getUrl("https://help.ubuntu.com/community/" + urlEncode(dict1) + "?action=raw", dict1);
                         System.out.print(" .");
+
                         System.out.println(dict1 + " read end.");
                     }catch(Exception ex2){
+
                     } catch (Exception ex) {
                         continue;
+
                         ex.printStackTrace();
 +
                        return;
 +
                    }
 +
                    try {
 +
//                        System.out.println(dict1 + " conver ...");
 +
//                        getDicts(html);
 +
                        System.out.println(dict1 + " put ...");
 +
                        putText(dict1, html, "https://help.ubuntu.com/community/" + dict1);
 +
                        saveProgress("helpProgress.txt", dict1);
 +
 
 +
                        long needwaitTime = readOneDictPayTime - (new Date().getTime() - starttime);
 +
                        if (needwaitTime > 0) {
 +
                            System.out.println("sleep " + needwaitTime);
 +
                            Thread.sleep(needwaitTime);
 +
                        }
 +
 
 +
//                        System.out.println(dict1+" put end.");
 +
                    } catch (Exception ex) {
 +
                        System.out.println(dict1 + " error:" + ex.getMessage());
 
                     }
 
                     }
 
                 }
 
                 }
                try {
+
            };
                    System.out.print(" get dict");
+
 
                    getDicts(out);
+
             pool.addTask(run);
                    System.out.print(" . put");
+
                    putText(dict,out,"https://help.ubuntu.com/community/"+dict);
+
                    System.out.print(" .\r\n");
+
                }catch(Exception ex){
+
                    System.out.println(dict+" error:"+ex.getMessage());
+
                    continue;
+
                }
+
             }finally{
+
                delDict(0);
+
            }
+
 
         }
 
         }
          
+
         pool.start();
 +
 
 +
//        while (addDict.size()>0){
 +
//            dict=addDict.get(0);
 +
//            System.out.print(String.valueOf(addDict.size())+" "+dict);
 +
//            try{
 +
////                if (addDict.size()>1377 && dict.indexOf("%")<0 ) continue;
 +
//                try {
 +
//                    System.out.print(" read");
 +
//                    out=getUrl("https://help.ubuntu.com/community/"+urlEncode(dict)+"?action=raw",dict);
 +
//                    System.out.print(" .");
 +
//                }catch(Exception ex){
 +
//                    try {
 +
//                        System.out.print(" read again");
 +
//                        out=getUrl("https://help.ubuntu.com/community/"+urlEncode(dict)+"?action=raw",dict);
 +
//                        System.out.print(" .");
 +
//                    }catch(Exception ex2){
 +
//                        continue;
 +
//                    }
 +
//                }
 +
//                try {
 +
//                    System.out.print(" get dict");
 +
//                    getDicts(out);
 +
//                    System.out.print(" . put");
 +
//                    putText(dict,out,"https://help.ubuntu.com/community/"+dict);
 +
//                    System.out.print(" .\r\n");
 +
//                }catch(Exception ex){
 +
//                    System.out.println(dict+" error:"+ex.getMessage());
 +
//                    continue;
 +
//                }
 +
//            }finally{
 +
//                delDict(0);
 +
//            }
 +
//        }
 +
 
 +
        String out = getUrl("https://help.ubuntu.com/community?action=raw", "community");
 +
        putText("community", out, "https://help.ubuntu.com/community/");
 
     }
 
     }
   
+
 
   
+
     public void wikistart() throws MalformedURLException, IOException {
     public void wikistart() throws MalformedURLException, IOException{
+
 
         clearDict();
 
         clearDict();
       
+
 
         InputStream in=null;
+
         InputStream in = null;
         File saveFile=new File("/tmp/wikiindex.html");
+
         File saveFile = new File("wiki.ubuntu.com.index.html");
         if (saveFile.exists()){
+
         if (saveFile.exists()) {
             in=new FileInputStream(saveFile);
+
             in = new FileInputStream(saveFile);
         }else{
+
         } else {
             URL url=new URL("https://wiki.ubuntu.com/TitleIndex");
+
             URL url = new URL("https://wiki.ubuntu.com/TitleIndex");
             in=url.openConnection().getInputStream();
+
             in = url.openConnection().getInputStream();
 
         }
 
         }
         BufferedReader read=new BufferedReader(new InputStreamReader(in));
+
         BufferedReader read = new BufferedReader(new InputStreamReader(in));
         StringBuffer sb=new StringBuffer();
+
         StringBuffer sb = new StringBuffer();
         while (true){
+
         while (true) {
             String line=read.readLine();
+
             String line = read.readLine();
             if (line==null) break;
+
             if (line == null) {
             sb.append(line+"\r\n");
+
                break;
 +
            }
 +
             sb.append(line + "\r\n");
 
         }
 
         }
 
         read.close();
 
         read.close();
         if (!saveFile.exists()){
+
         if (!saveFile.exists()) {
             FileOutputStream out=new FileOutputStream(saveFile);
+
             FileOutputStream out = new FileOutputStream(saveFile);
 
             out.write(sb.toString().getBytes());
 
             out.write(sb.toString().getBytes());
 
             out.flush();
 
             out.flush();
 
             out.close();
 
             out.close();
 
         }
 
         }
       
+
 
         String html=sb.substring(sb.indexOf("<a name=\"0\">"),sb.indexOf("<a name=\"%5b\">"));
+
         String html = sb.substring(sb.indexOf("<a href=\"/TitleIndex?allpages=1\">Include system pages</a>"), sb.indexOf("<div id=\"footer\">"));
         Pattern pattern= Pattern.compile("<a href=\"/(.*?)\">");
+
         Pattern pattern = Pattern.compile("<a href=\"/(.*?)\">");
         Matcher matcher=pattern.matcher(html);
+
         Matcher matcher = pattern.matcher(html);
       
+
 
         while(matcher.find()) {
+
         while (matcher.find()) {
             String input=matcher.group(1);
+
             String input = matcher.group(1);
 +
            if (input.indexOf("?") > 0) {
 +
                continue;
 +
            }
 +
            if (input.indexOf("ubuntu.com") > 0) {
 +
                continue;
 +
            }
 +
            if (input.equals("Jan_Mate")) {
 +
                continue;
 +
            }
 
             addDict(input);
 
             addDict(input);
 
             System.out.println(input);
 
             System.out.println(input);
第855行: 第1,291行:
 
         String dict;
 
         String dict;
 
         String out;
 
         String out;
         System.out.println("一共需要转换 "+String.valueOf(addDict.size())+" 篇文章。");
+
         System.out.println("一共需要转换 " + String.valueOf(addDict.size()) + " 篇文章。");
          
+
         readProgress("wikiProgress.txt");
         while (addDict.size()>0){
+
 
             dict=addDict.get(0);
+
         ThreadPool pool = new ThreadPool();
            System.out.print(String.valueOf(addDict.size())+" "+dict);
+
        for (int i = 0; i < addDict.size(); i++) {
            try{
+
            final String dict1 = addDict.get(i);
//                if (addDict.size()>12285) continue;
+
            if (oldDict.contains(dict1)) {
 +
                System.out.println(dict1 + " already read.");
 +
                continue;
 +
            }
 +
 
 +
 
 +
            Runnable run = new Runnable() {
 +
 
 +
                public void run() {
 +
                    long starttime = new Date().getTime();
 +
                    String html = "";
 +
                    try {
 +
                        System.out.println(dict1 + " read ...");
 +
                        html = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict1) + "?action=raw", dict1);
 +
                        System.out.println(dict1 + " read end .");
 +
                    } catch (Exception ex) {
 +
                        ex.printStackTrace();
 +
                        return;
 +
                    }
 +
                    try {
 +
//                        System.out.println(dict1 + " conver ...");
 +
//                        getDicts(html);
 +
                        System.out.println(dict1 + " put ...");
 +
                        putText(dict1, html, "https://wiki.ubuntu.com/" + dict1);
 +
                        saveProgress("wikiProgress.txt", dict1);
 +
 
 +
                        long needwaitTime = readOneDictPayTime - (new Date().getTime() - starttime);
 +
                        if (needwaitTime > 0) {
 +
                            System.out.println("sleep " + needwaitTime);
 +
                            Thread.sleep(needwaitTime);
 +
                        }
 +
 
 +
//                        System.out.println(dict1+" put end .");
 +
                    } catch (Exception ex) {
 +
                        System.out.println(dict1 + " error:" + ex.getMessage());
 +
                        ex.printStackTrace();
 +
                    }
 +
 
 +
                }
 +
             };
 +
 
 +
            pool.addTask(run);
 +
        }
 +
        pool.start();
 +
 
 +
//        while (addDict.size()>0){
 +
//            dict=addDict.get(0);
 +
//            System.out.print(String.valueOf(addDict.size())+" "+dict);
 +
//            try{
 +
////                if (addDict.size()>12285) continue;
 +
//                try {
 +
//                    System.out.print(" read");
 +
//                    out=getUrl("https://wiki.ubuntu.com/"+urlEncode(dict)+"?action=raw",dict);
 +
//                    System.out.print(" .");
 +
//                }catch(Exception ex){
 +
//                    ex.printStackTrace();
 +
//                    try {
 +
//                        System.out.print(" read again");
 +
//                        out=getUrl("https://wiki.ubuntu.com/"+urlEncode(dict)+"?action=raw",dict);
 +
//                        System.out.print(" .");
 +
//                    }catch(Exception ex2){
 +
//                        ex2.printStackTrace();
 +
//                        continue;
 +
//                    }
 +
//                }
 +
//                try {
 +
//                    System.out.print(" get dict");
 +
//                    getDicts(out);
 +
//                    System.out.print(" . put");
 +
//                    putText(dict,out,"https://wiki.ubuntu.com/"+dict);
 +
//                    System.out.print(" .\r\n");
 +
//                }catch(Exception ex){
 +
//                    System.out.println(dict+" error:"+ex.getMessage());
 +
//                }
 +
//            }finally{
 +
//                delDict(0);
 +
//            }
 +
//        }
 +
    }
 +
 
 +
    public void laptopTestingTeam() throws FileNotFoundException, IOException {
 +
        InputStream in = null;
 +
        File saveFile = new File("/home/wangpian/Desktop/index.php");
 +
        in = new FileInputStream(saveFile);
 +
        BufferedReader read = new BufferedReader(new InputStreamReader(in));
 +
        StringBuffer sb = new StringBuffer();
 +
        while (true) {
 +
            String line = read.readLine();
 +
            if (line == null) {
 +
                break;
 +
            }
 +
            sb.append(line + "\r\n");
 +
        }
 +
        read.close();
 +
        in.close();
 +
        Pattern pattern = Pattern.compile("\\[\\[UbuntuWiki:LaptopTestingTeam/(.*?)\\]\\]");
 +
        Matcher matcher = pattern.matcher(sb.toString());
 +
 
 +
        while (matcher.find()) {
 +
            String input = "LaptopTestingTeam/" + matcher.group(1);
 +
            addDict(input);
 +
            System.out.println(input);
 +
        }
 +
        String out = "";
 +
        while (addDict.size() > 0) {
 +
            String dict = addDict.get(0);
 +
            System.out.print(String.valueOf(addDict.size()) + " " + dict);
 +
            try {
 
                 try {
 
                 try {
 
                     System.out.print(" read");
 
                     System.out.print(" read");
                     out=getUrl("https://wiki.ubuntu.com/"+dict+"?action=raw",dict);
+
                     out = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict) + "?action=raw", dict);
 
                     System.out.print(" .");
 
                     System.out.print(" .");
                 }catch(Exception ex){
+
                 } catch (Exception ex) {
 
                     ex.printStackTrace();
 
                     ex.printStackTrace();
 
                     try {
 
                     try {
 
                         System.out.print(" read again");
 
                         System.out.print(" read again");
                         out=getUrl("https://wiki.ubuntu.com/"+dict+"?action=raw",dict);
+
                         out = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict) + "?action=raw", dict);
 
                         System.out.print(" .");
 
                         System.out.print(" .");
                     }catch(Exception ex2){
+
                     } catch (Exception ex2) {
 
                         ex2.printStackTrace();
 
                         ex2.printStackTrace();
 
                         continue;
 
                         continue;
第881行: 第1,424行:
 
                     getDicts(out);
 
                     getDicts(out);
 
                     System.out.print(" . put");
 
                     System.out.print(" . put");
                     putText(dict,out,"https://wiki.ubuntu.com/"+dict);
+
                     putText(dict, out, "https://wiki.ubuntu.com/" + dict);
 
                     System.out.print(" .\r\n");
 
                     System.out.print(" .\r\n");
                 }catch(Exception ex){
+
                 } catch (Exception ex) {
                     System.out.println(dict+" error:"+ex.getMessage());
+
                     System.out.println(dict + " error:" + ex.getMessage());
 
                 }
 
                 }
             }finally{
+
             } finally {
 
                 delDict(0);
 
                 delDict(0);
 
             }
 
             }
 
         }
 
         }
 
     }
 
     }
   
+
 
     public void test() throws IOException{
+
     public void test() throws IOException, InterruptedException {
 
//        String sss="\r\ndd attachment:IconsPage/info.png ClamAV can only\r\n";
 
//        String sss="\r\ndd attachment:IconsPage/info.png ClamAV can only\r\n";
 
//        sss=sss.replaceAll("attachment:(.*?)/(.*?) ","https://help.ubuntu.com/community/$1?action=AttachFile&do=get&target=$2 ");
 
//        sss=sss.replaceAll("attachment:(.*?)/(.*?) ","https://help.ubuntu.com/community/$1?action=AttachFile&do=get&target=$2 ");
第914行: 第1,457行:
 
         //  System.out.println(text);
 
         //  System.out.println(text);
 
//        text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","<pre><nowiki>$2");
 
//        text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","<pre><nowiki>$2");
       
+
 
 
         //link convert - {{{ *  -> <pre><nowiki> *
 
         //link convert - {{{ *  -> <pre><nowiki> *
 
//        text=text.replaceAll("\\{\\{\\{(.*)","\r\n<pre><nowiki>$1");
 
//        text=text.replaceAll("\\{\\{\\{(.*)","\r\n<pre><nowiki>$1");
第922行: 第1,465行:
 
//        System.out.println(text);
 
//        System.out.println(text);
 
//        getCookie();
 
//        getCookie();
       
+
 
 
//        String url="http://bingniu.3322.org/mywiki/OpenLDAPAdminGuide/SecurityConsideration";
 
//        String url="http://bingniu.3322.org/mywiki/OpenLDAPAdminGuide/SecurityConsideration";
 
//        String out=getUrl(url+"?action=raw","UbuntuManual");
 
//        String out=getUrl(url+"?action=raw","UbuntuManual");
 
//        System.out.println(out);
 
//        System.out.println(out);
       
+
 
 
//        BufferedReader read=new BufferedReader(new FileReader("/home/wangpian/a1.txt"));
 
//        BufferedReader read=new BufferedReader(new FileReader("/home/wangpian/a1.txt"));
 
//        String s="";
 
//        String s="";
第939行: 第1,482行:
 
//
 
//
 
//        System.out.println(s.replaceAll("\\[\\[UbuntuHelp(.*?)\\|(.*?)\\]\\]","$2"));
 
//        System.out.println(s.replaceAll("\\[\\[UbuntuHelp(.*?)\\|(.*?)\\]\\]","$2"));
        String html="== Design ==\r\n"+
+
//        String html="== Design ==\r\n"+
                "* Choosing the \"About UMC\" menu item should open the About window, visually centered on the screen. It should behave properly with respect to startup notification and session saving. The window should be closable using either Alt-F4 or the close button.\r\n"+
+
//                "* Choosing the \"About UMC\" menu item should open the About window, visually centered on the screen. It should behave properly with respect to startup notification and session saving. The window should be closable using either Alt-F4 or the close button.\r\n"+
                "\r\n"+
+
//                "\r\n"+
                " * The About window should include:\r\n"+
+
//                " * The About window should include:\r\n"+
                "  * UMC logo and version number (most required!)\r\n"+
+
//                "  * UMC logo and version number (most required!)\r\n"+
                "  * computer model, if available\r\n"+
+
//                "  * computer model, if available\r\n"+
                "  * CPU speed and amount of RAM on the system\r\n"+
+
//                "  * CPU speed and amount of RAM on the system\r\n"+
                "  * a \"Hardware Details\" button that opens the Device Manager\r\n"+
+
//                "  * a \"Hardware Details\" button that opens the Device Manager\r\n"+
                "  * Linux kernel version number\r\n"+
+
//                "  * Linux kernel version number\r\n"+
                "  * KDE/Gnome version number.\r\n"+
+
//                "  * KDE/Gnome version number.\r\n"+
                "  * This \"About Box\" could also include a button \"Generate System Report\". As newbies often are asked to add system details like their sources.list, lspci, lsmod and the like(in launchpad answers or ubuntu forums), they tend to have a hard time gathering these information. Maybe they could select/unselect the appropriate  buttons for the config file they want to be exported..?\r\n"+
+
//                "  * This \"About Box\" could also include a button \"Generate System Report\". As newbies often are asked to add system details like their sources.list, lspci, lsmod and the like(in launchpad answers or ubuntu forums), they tend to have a hard time gathering these information. Maybe they could select/unselect the appropriate  buttons for the config file they want to be exported..?\r\n"+
                "\r\n"+
+
//                "\r\n"+
                " * Any of the informative text should be drag-and-droppable as plain text into another program."
+
//                " * Any of the informative text should be drag-and-droppable as plain text into another program."
                ;
+
//                ;
        //System.out.println(replaceList2(html));
+
//        //System.out.println(replaceList2(html));
        String text="#REDIRECT [[UbuntuWiki:BelgianTeam/Leuven_2007-01-06]]";
+
//        String text="#REDIRECT [[UbuntuWiki:BelgianTeam/Leuven_2007-01-06]]";
        Pattern pattern= Pattern.compile("\\[\\[UbuntuWiki:(.*?)\\]\\]");
+
//        Pattern pattern= Pattern.compile("\\[\\[UbuntuWiki:(.*?)\\]\\]");
        Matcher matcher=pattern.matcher(text);
+
//        Matcher matcher=pattern.matcher(text);
        if (matcher.find()){
+
//        if (matcher.find()){
            System.out.println(matcher.group(1));
+
//            System.out.println(matcher.group(1));
        }
+
//        }
 +
//        String dict="community";
 +
//        System.out.println(dict);
 +
//        String out=getUrl("https://help.ubuntu.com/"+dict+"?action=raw",dict);
 +
//        String out =formatdict("ddrer%34[[24%s]]%343242");
 +
//        System.out.println(out);
 +
 
 +
//        for (int i=0 ;i<255;i++){
 +
//            System.out.print(",'"+String.valueOf((char)i)+"'");
 +
//        }
 +
//        String out=getUrl("https://help.ubuntu.com/community/3DChessboard?action=raw","3DChessboard");
 +
        String out = getUrl("https://wiki.ubuntu.com/HardwareSupport?action=raw", "HardwareSupport");
 +
 
 +
        System.out.println(out);
 +
 
 
     }
 
     }
   
+
 
   
+
   
+
 
     /**
 
     /**
 
     * @param args the command line arguments
 
     * @param args the command line arguments
第969行: 第1,524行:
 
     public static void main(String[] args) {
 
     public static void main(String[] args) {
 
         // TODO code application logic here
 
         // TODO code application logic here
         Main main=new Main();
+
         Main main = new Main();
 
         try {
 
         try {
 
//            main.test();
 
//            main.test();
//            main.helpstart();
+
//            main.laptopTestingTeam();
 +
            main.helpstart();
 
             main.wikistart();
 
             main.wikistart();
         }catch (Exception ex){
+
         } catch (Exception ex) {
 
             ex.printStackTrace();
 
             ex.printStackTrace();
 
         }
 
         }
 
     }
 
     }
      
+
}
      
+
 
      
+
class ThreadPool {
 +
 
 +
     List<Thread> taskList = new ArrayList();
 +
     int count = 1;
 +
 
 +
    void addTask(Thread obj) {
 +
        taskList.add(obj);
 +
    }
 +
 
 +
    void addTask(Runnable obj) {
 +
        Thread thread = new Thread(obj);
 +
        taskList.add(thread);
 +
    }
 +
 
 +
    @SuppressWarnings("static-access")
 +
    void start() {
 +
        while (true) {
 +
            try {
 +
                Thread.currentThread().sleep(1000);
 +
            } catch (InterruptedException ex) {
 +
                ex.printStackTrace();
 +
            }
 +
            int j = 0;
 +
            for (int i = 0; i < taskList.size(); i++) {
 +
                if (taskList.get(i).getState() != Thread.State.TERMINATED && taskList.get(i).getState() != Thread.State.NEW) {
 +
                    j++;
 +
                    if (j >= count) {
 +
                        break;
 +
                    }
 +
                }
 +
            }
 +
            if (j >= count) {
 +
                continue;
 +
            }
 +
 
 +
            for (int i = 0; i < taskList.size(); i++) {
 +
                if (taskList.get(i).getState() == Thread.State.NEW) {
 +
                    taskList.get(i).start();
 +
                    j++;
 +
                    if (j >= count) {
 +
                        break;
 +
                    }
 +
                }
 +
            }
 +
            boolean end = true;
 +
            for (int i = 0; i < taskList.size(); i++) {
 +
                if (taskList.get(i).getState() != Thread.State.TERMINATED) {
 +
                    end = false;
 +
                    break;
 +
                }
 +
            }
 +
            if (end) {
 +
                break;
 +
            }
 +
        }
 +
     }
 
}
 
}
  
 
</source>
 
</source>

2010年5月19日 (三) 17:32的版本

机器人,自动将 http://help.ubuntu.comhttp://wiki.ubuntu.com 由 monimoni 格式转换到 mediawiki 格式,并自动更新和发布的小程序。 由java写成。


/*
 * Main.java
 *
 * Created on 2007年5月12日, 下午1:31
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package wiki;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

/**
 *
 * @author oneleaf
 */
public class Main {

    List<String> addDict = new ArrayList<String>();
    List<String> oldDict = new ArrayList<String>();
    String cookie = "NEED INPUT";
    int readOneDictPayTime = 2500;

    public String getHtmlFromStream(InputStream in) throws IOException {
        StringBuffer html = new StringBuffer();
        int no = 0;
        while ((no = in.read()) > -1) {
            html.append((char) no);
        }
        in.close();
        return new String(html.toString().getBytes("iso8859-1"), "utf-8");
    }

    private void addDict(String dict) {
        String str = dict.trim();

        if (dict.startsWith("/")) {
            str = dict.substring(1);
        }
        if (dict.indexOf("#") > 0) {
            str = dict.substring(0, dict.indexOf("#"));
        }
        if (dict.indexOf("?") > 0) {
            str = dict.substring(0, dict.indexOf("?"));
        }
        if (dict.startsWith("./") || dict.startsWith("//")) {
            str = dict.substring(2);
        }
        if (dict.startsWith("../")) {
            str = dict.substring(3);
        }
        if (dict.endsWith(".") || dict.endsWith(")") || dict.endsWith("]") ||
                dict.endsWith("}") || dict.endsWith(";") || dict.endsWith("+") ||
                dict.endsWith("'") || dict.endsWith("\"") || dict.endsWith("/") ||
                dict.endsWith(":") || dict.endsWith(",") || dict.endsWith(">")) {
            str = dict.substring(0, str.length() - 1);
        }
//        if (str.toLowerCase().indexOf("team")>0) return;
        if (str.trim().length() == 0) {
            return;
        }
        if (str.trim().length() >= 256) {
            return;
        //../CommandLine
        //Community => community 
        }
        if ("Community".equals(str)) {
            return;
        }
        if (oldDict.contains(str)) {
            return;
        }
        if (addDict.contains(str)) {
            return;
        }
        addDict.add(str);
    }

    private void delDict(int dictindex) {
        oldDict.add(addDict.get(dictindex));
        addDict.remove(dictindex);
    }

    private void delDict(String dict) {
        oldDict.add(dict);
        addDict.remove(dict);
    }

    private void clearDict() {
        addDict.clear();
        oldDict.clear();
    }

    private boolean findDict(String dict) {
        for (String d : addDict) {
            if (d.equals(dict)) {
                return true;
            }
        }
        for (String d : oldDict) {
            if (d.equals(dict)) {
                return true;
            }
        }
        return false;
    }

    private void getDicts(String html) {
        Pattern pattern = Pattern.compile("\\[UbuntuHelp:(.*?)\\]");
        Matcher matcher = pattern.matcher(html);
        while (matcher.find()) {
            String line = matcher.group(1);
            if (line.indexOf("|") > 0) {
                addDict(line.substring(0, line.indexOf("|")));
            } else {
                addDict(line);
            }
        }
        pattern = Pattern.compile("\\[UbuntuWiki:(.*?)\\]");
        matcher = pattern.matcher(html);
        while (matcher.find()) {
            String line = matcher.group(1);
            if (line.indexOf("|") > 0) {
                addDict(line.substring(0, line.indexOf("|")));
            } else {
                addDict(line);
            }
        }
    }

    /** Creates a new instance of Main */
    public Main() {
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("TLS");
            X509TrustManager[] xtmArray = new X509TrustManager[]{xtm};
            sslContext.init(null, xtmArray, new java.security.SecureRandom());
        } catch (GeneralSecurityException gse) {
        }
        if (sslContext != null) {
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        }
        HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    }
    private X509TrustManager xtm = new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    private HostnameVerifier hnv = new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    public String getUrl(String urladdress, String dict) throws IOException, InterruptedException {
        URL url = new URL(urladdress);
        HttpURLConnection httpConn;
        int SleepTime = 1;
        while (true) {
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setReadTimeout(60000);
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
            httpConn.setRequestProperty("Content-Language", "UTF-8");
            httpConn.setRequestProperty("Connection", "Keep-Alive");

            if (httpConn.getResponseCode() > 500) {
                System.out.println("read " + httpConn.getResponseCode() + " from " + urladdress + " sleep " + SleepTime + " minute.");
                readOneDictPayTime = readOneDictPayTime + 100;
                System.out.println("adjust readOneDictPayTime to " + readOneDictPayTime);
                if (readOneDictPayTime > 10000) {
                    readOneDictPayTime = 2500;
                }
                Thread.sleep(60000 * SleepTime);
                SleepTime = SleepTime + 1;
            } else if (httpConn.getResponseCode() > 400) {
                return "";
            } else {
                break;
            }
        }

        InputStream in = httpConn.getInputStream();
        try {
            String html = getHtmlFromStream(in);
            return moin2wm(html, urladdress, dict);
        } finally {
            in.close();
        }
    }

    public String moin2wm(String html, String url, String dict) throws UnsupportedEncodingException {
        String text = html;
        if (text.indexOf("This page does not exist yet.") > 0) {
            return "";
        }
        String ex = "UbuntuHelp";
        String turl = "https://help.ubuntu.com/community/";
        if (url.startsWith("https://wiki")) {
            ex = "UbuntuWiki";
            turl = "https://wiki.ubuntu.com/";
        }

        String head = "{{From|" + url.substring(0, url.indexOf("?")) + "}}\r\n{{Languages|" + ex + ":" + dict + "}}\r\n";

        //删除注释
        text = text.replaceAll("\r\n##(.*)", "");
        text = text.replaceAll("\r\n#format(.*)", "");
        text = text.replaceAll("\r\n#language(.*)", "");
        text = text.replaceAll("\r\n#pragma(.*)", "");
        text = text.replaceAll("\r\n#acl(.*)", "");
        text = text.replaceAll("^##(.*)\r\n", "");
        text = text.replaceAll("^#format(.*)\r\n", "");
        text = text.replaceAll("^#language(.*)\r\n", "");
        text = text.replaceAll("^#pragma(.*)\r\n", "");
        text = text.replaceAll("^#acl(.*)\r\n", "");
        //替换#REDIRECT PDFPrinting => #REDIRECT [[PDFPrinting]]
        text = text.replaceAll("#REDIRECT (\\S*)", "#REDIRECT " + "[[" + ex + ":$1]]");
        text = text.replaceAll("#redirect (\\S*)", "#REDIRECT " + "[[" + ex + ":$1]]");
        //#refresh 0 https://wiki.ubuntu.com/ASUS_A3H_5010_Laptop_with_Ubuntu
        text = text.replaceAll("#REFRESH (.*?) (\\S*)", "#REDIRECT " + "[[" + ex + ":$2]]");
        text = text.replaceAll("#refresh (.*?) (\\S*)", "#REDIRECT " + "[[" + ex + ":$2]]");

        //删除不要的下拉列表
        text = text.replaceAll("\\[\\[Navigation\\(.*?\\)\\]\\]", "");
        text = text.replaceAll("Navigation\\(.*?\\)", "");
        //删除主题
        text = text.replaceAll(".*TableOfContents.*", "");

        //分离{{{ }}}
        Pattern pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}");
        Matcher matcher = pattern.matcher(text);
        List<String> codes = new ArrayList();
        while (matcher.find()) {
            String code = matcher.group(1);
            codes.add("<code><nowiki>" + code + "</nowiki></code>");
            int index = text.indexOf(matcher.group(0));
            int len = matcher.group(0).length();
            text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len);
        }

        pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}", Pattern.DOTALL);
        String html2 = text;
        matcher = pattern.matcher(html2);
        while (matcher.find()) {
            String code = matcher.group(1);
            codes.add("<pre><nowiki>" + code + "</nowiki></pre>");
            int index = text.indexOf("{{{" + matcher.group(1) + "}}}");
            int len = matcher.group(1).length() + 6;
            text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len);
        }

        //转化表格
        text = tableConv(text);

        //标题从二开始
        text = text.replaceAll("= (.*?) =", "== $1 ==");
        //转化List
        text = replaceList(text);
        //[[BR]] -> <BR>
        text = text.replaceAll("\\[\\[BR\\]\\]", "<br>");
        //link convert superscripted - ^ * ^ -> <sup> * </sup>
        text = text.replaceAll("\\^(.*)\\^", "<sup>$1</sup>");
        //link convert subscripted - ,, * ,, -> <sub> * </sub>
        text = text.replaceAll(",,(.*?),,", "<sub>$1</sub>");

        //link convert - ["/*"] -> [dict/*]
        text = text.replaceAll("\\[\\[\"//(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
        text = text.replaceAll("\\[\\[\"/(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
        text = text.replaceAll("\\[\\[\"\\./(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":$1|$2]]");
        text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]");

        text = text.replaceAll("\\[\\[\"//(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
        text = text.replaceAll("\\[\\[\"/(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
        text = text.replaceAll("\\[\\[\"\\./(.*?)\"\\]\\]", "[[" + ex + ":$1|$1]]");
        text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]");
        //link convert - [[/*]] -> [[dict/*]]
        //[[/Introduction|Introduction]]
        text = text.replaceAll("\\[\\[//(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
        text = text.replaceAll("\\[\\[/(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
        text = text.replaceAll("\\[\\[\\./(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":$1|$2]]");
        text = text.replaceAll("\\[\\[\\.\\./(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]");

        text = text.replaceAll("\\[\\[//(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
        text = text.replaceAll("\\[\\[/(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]");
        text = text.replaceAll("\\[\\[\\./(.*?)\\]\\]", "[[" + ex + ":$1|$1]]");
        text = text.replaceAll("\\[\\[\\.\\./(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]");

        //link convert - [" * "] -> [[ UbuntuHelp: * ]]
        text = text.replaceAll("\\[\"(.*?)\"\\]", "[[" + ex + ":$1|$1]]");

        //link convert - [# * ] -> [[ * ]]
        text = text.replaceAll("\\[#(.*?)\\]", "[[$1]]");
        //link convert - [: / * : * ] -> [[ UbuntuHelp:dict\ * | * ]]
        text = text.replaceAll("\\[:/(.*?):(.*?)\\]", "[[" + ex + ":" + dict + "/$1|$2]]");
        //link convert - [: * : * ] -> [[ UbuntuHelp: * | * ]]
        text = text.replaceAll("\\[:(.*?):(.*?)\\]", "[[" + ex + ":$1|$2]]");
        //link convert - [: / * ] -> [[ UbuntuHelp: dict * ]]
        text = text.replaceAll("\\[:/(.*?)\\]", "[[" + ex + ":" + dict + "/$1]]");
        //link convert - [: * ] -> [[ UbuntuHelp: * ]]
        text = text.replaceAll("\\[:(.*?)\\]", "[[" + ex + ":$1]]");
        //link convert - wiki:cat  -> [[UbuntuWiki:cat]]
        //link convert - wiki:Ubuntu/cat  -> [[UbuntuWiki:cat]]
        text = text.replaceAll(" wiki:Ubuntu/(\\S*)", " [[UbuntuWiki:$1]]");
        text = text.replaceAll("\r\nwiki:Ubuntu/(\\S*)", "\r\n[[UbuntuWiki:$1]]");
        text = text.replaceAll(" wiki:(\\S*)", " [[UbuntuWiki:$1]]");
        text = text.replaceAll("\r\nwiki:(\\S*)", "\r\n[[UbuntuWiki:$1]]");
        //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]]
        text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\ (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
        text = text.replaceAll("\\[wiki:(.*?)\\ (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
        //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]]
        text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\]", "[[UbuntuWiki:$1]]");
        text = text.replaceAll("\\[wiki:(.*?)\\]", "[[UbuntuWiki:$1]]");
        //link convert - [UbuntuWiki:\*] -> [UbuntuWiki:dict\*]
        text = text.replaceAll("\\[UbuntuWiki:\\\\(.*?)\\]", "[UbuntuWiki:" + dict + "\\$1]");
        //link convert - [UbuntuHelp:\*] -> [UbuntuHelp:dict\*]
        text = text.replaceAll("\\[UbuntuHelp:\\\\(.*?)\\]", "[UbuntuHelp:" + dict + "\\$1]");

        //Self:/ddd=dict/ Self:ddd=dict
        text = text.replaceAll(":Self:(.*?)//", ":" + dict + "/$1");
        text = text.replaceAll(":Self:(.*?)/", ":" + dict + "/$1");
        text = text.replaceAll(":Self:(.*?)", ":$1");
        text = text.replaceAll(":self:(.*?)//", ":" + dict + "/$1");
        text = text.replaceAll(":self:(.*?)/", ":" + dict + "/$1");
        text = text.replaceAll(":self:(.*?)", ":$1");

        //link convert - __ * __ -> <u> * </u>
        text = text.replaceAll("__(.*?)__", "<u>$1</u>");

        //CategoryHomepage =>[[category:"+ex+"]];
        text = text.replaceAll("Category(\\S*)", "[[category:Category$1]]");

        text = text.replaceAll("\r\n( *)", "\r\n");
        text = attachmentUrl(text, turl, dict);
        //[[xxx:http]] => [[http:]]
        text = text.replaceAll("\\[\\[(.*?):http(.*?)\\]\\]", "[[http$2]]");
        //[[xxx:ftp]] => [[ftp:]]
        text = text.replaceAll("\\[\\[(.*?):ftp(.*?)\\]\\]", "[[ftp$2]]");

        //[[Ubuntu:HardwareSupport]] =>[[UbuntuWiki:HardwareSupport]]
        text = text.replaceAll("\\[\\[Ubuntu:(.*?)\\]\\]", "[[UbuntuWiki:$1]]");
        //[[https://wiki.ubuntu.com/*]] => [[UbuntuWiki:]]
        text = text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]\\]", "[[UbuntuWiki:$1|$2]]");
        text = text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?)\\]\\]", "[[UbuntuWiki:$1]]");
        //[[https://help.ubuntu.com/community/*]] => [[UbuntuHelp:]]
        text = text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]\\]", "[[UbuntuHelp:$1|$2]]");
        text = text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?)\\]\\]", "[[UbuntuHelp:$1]]");
        //[[http://wiki.ubuntu.com/*]] => [[UbuntuWiki:]]
        text = text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]\\]", "[[UbuntuWiki:$1|$2]]");
        text = text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?)\\]\\]", "[[UbuntuWiki:$1]]");
        //[[http://help.ubuntu.com/community/*]] => [[UbuntuHelp:]]
        text = text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]\\]", "[[UbuntuHelp:$1|$2]]");
        text = text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?)\\]\\]", "[[UbuntuHelp:$1]]");

        text = text.replaceAll("\\[http://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
        text = text.replaceAll("\\[https://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]", "[[UbuntuWiki:$1|$2]]");
        text = text.replaceAll("\\[https://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]", "[[UbuntuHelp:$1|$2]]");
        text = text.replaceAll("\\[http://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]", "[[UbuntuHelp:$1|$2]]");
        text = text.replaceAll("\\[http://wiki\\.ubuntu\\.com/(.*?)\\]", "[[UbuntuWiki:$1]]");
        text = text.replaceAll("\\[https://wiki\\.ubuntu\\.com/(.*?)\\]", "[[UbuntuWiki:$1]]");
        text = text.replaceAll("\\[https://help\\.ubuntu\\.com/community/(.*?)\\]", "[[UbuntuHelp:$1]]");
        text = text.replaceAll("\\[http://help\\.ubuntu\\.com/community/(.*?)\\]", "[[UbuntuHelp:$1]]");

        text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "[[$1/$3]]");
        text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "[[$1/$3]]");
        text = text.replaceAll("\\[\\[(.*?)/\\./(.*?)\\]\\]", "[[$1/$2]]");

        //[[http://redhat.com|RedHat]] => [http://redhat.com RedHat]
        text = text.replaceAll("\\[\\[(http://.*?)\\|(.*?)\\]\\]", "[$1 $2]");
        //[[https://redhat.com|RedHat]] => [https://redhat.com RedHat]
        text = text.replaceAll("\\[\\[(https://.*?)\\|(.*?)\\]\\]", "[$1 $2]");
        //[[ftp://redhat.com|RedHat]] => [http://redhat.com RedHat]
        text = text.replaceAll("\\[\\[(ftp://.*?)\\|(.*?)\\]\\]", "[$1 $2]");
        //[[http://redhat.com]] => [http://redhat.com]
        text = text.replaceAll("\\[\\[(http://.*?)\\]\\]", "[$1]");
        //[[https://redhat.com]] => [https://redhat.com]
        text = text.replaceAll("\\[\\[(https://.*?)\\]\\]", "[$1]");
        //[[ftp://redhat.com]] => [http://redhat.com]
        text = text.replaceAll("\\[\\[(ftp://.*?)\\]\\]", "[$1]");

        //[[RedHat|redhat]] => [[UbuntuHelp:RedHat|readhat]]
        text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":$1|$2]]");

        //[[RedHat]] => [[UbuntuHelp:RedHat|ReadHat]]
        text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\]\\]", "[[" + ex + ":$1|$1]]");


        //转化%
        text = formatdict(text);

        //转化单词
        text = format2(text, ex, dict);

        //需要还原$code$
        for (int i = 0; i < codes.size(); i++) {
            String str = codes.get(i);
            String s = "@@code" + String.valueOf(i + 1) + "@@";
            int m = text.indexOf(s);
            int n = s.length();
            //text = text.substring(0, m) + str + text.substring(m + n);
            text = text.replace(s, str);
        }

        String foot = "\r\n[[category:" + ex + "]]";

        if (text.trim().startsWith("#REDIRECT")) {
//            System.out.print(" "+text.trim());
            pattern = Pattern.compile("\\[\\[" + ex + ":(.*?)\\]\\]");
            matcher = pattern.matcher(text);
            if (matcher.find()) {
                //如果仅仅是大小写的重定向,就不用考虑直接忽略。
                if (matcher.group(1).toLowerCase().equals(dict.toLowerCase())) {
                    System.out.println(dict + " redirect to " + matcher.group(1) + " , ignore. ");
                    return "";
                }
            }
            return text + head + foot;
        }
        if (text.trim().length() < 10) {
            return "";
        }
        return head + text + foot;
    }

    public String formatdict(String text) {
        Pattern pattern = Pattern.compile("\\[\\[(.*?)\\]\\]");
        Matcher matcher = pattern.matcher(text);
        String html = text;
        while (matcher.find()) {
            String dict = matcher.group(1);
            if (dict.indexOf("%") > 0) {
                int index = html.indexOf("[[" + dict + "]]");
                int len = dict.length() + 4;
                html = html.substring(0, index) + "[[" + getDict(dict) + "]]" + html.substring(index + len);
            }
        }
        return html;
    }

    public String getDict(String dict) {
        String str = "";
        str = dict.replaceAll("%..", "_");
        str = str.replaceAll("%.", "");
        return str;
    }

    //检查有没有至少两个大写单词存在,如果存在检查是不是词汇,如果是,转为链接。
    public boolean arrayhas(char[] c, char b) {
        for (int i = 0; i < c.length; i++) {
            if (c[i] == b) {
                return true;
            }
        }
        return false;
    }

    public String replaceBySpace(String text, String match) {
        Pattern pattern = Pattern.compile(match);
        Matcher matcher = pattern.matcher(text);
        String html = text;
        while (matcher.find()) {
            String str = matcher.group(0);
            int index = text.indexOf(str);
            int length = str.length();
            String space = String.format("%-" + length + "s", "");
            html = html.substring(0, index) + space + html.substring(index + length);
        }
        return html;
    }

    public String format2(String html, String ex, String dict) {
        String result = html;
        int n_count = 0;
        int m_count = 0;
        char[] o = {':', '%', '?', '&', '=', '\\', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'};
        char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        String temp = html;
        //删除现有的所有的链接
        temp = replaceBySpace(temp, "\\{\\{.*?\\}\\}");
        temp = replaceBySpace(temp, "=====.*?=====");
        temp = replaceBySpace(temp, "====.*?====");
        temp = replaceBySpace(temp, "===.*?===");
        temp = replaceBySpace(temp, "==.*?==");
        temp = replaceBySpace(temp, "=.*?=");
        temp = replaceBySpace(temp, "\\[\\[.*?\\]\\]");
        temp = replaceBySpace(temp, "\\[.*?\\]");
        temp = replaceBySpace(temp, "http:.*? ");
        temp = replaceBySpace(temp, "https:.*? ");
        temp = replaceBySpace(temp, "<pre>.*?</pre>");
        temp = replaceBySpace(temp, "<code>.*?</code>");
        String word = "";
        for (int i = 0; i < temp.length(); i++) {
            char c = temp.charAt(i);
            if (arrayhas(m, c)) {
                word = word + String.valueOf(c);
                m_count++;
            } else if (arrayhas(n, c)) {
                word = word + String.valueOf(c);
                n_count++;
            } else if (arrayhas(o, c)) {
                word = word + String.valueOf(c);
            } else {
                //单词截取完成
                if (n_count > 1 && m_count > 0) {
                    if (!word.equals("IconsPage") && !word.equals("AttachFile") &&
                            word.indexOf("\\") < 0 && word.indexOf("/") < 0 && findDict(word) && !word.equals(dict)) {
                        try {
                            int index = temp.indexOf(word);
                            int len = word.length();
                            String replace = "[[" + ex + ":" + word + "|" + word + "]]";
                            result = result.substring(0, index) + replace + result.substring(index + len);
                            temp = temp.substring(0, index) + String.format("%-" + replace.length() + "s", "") + temp.substring(index + len);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                n_count = 0;
                m_count = 0;
                word = "";
            }
        }
        return result;
    }

    public String urlEncode(String dict) {
        String str = dict;
        str = str.replaceAll(" ", "%20");
        if (str.indexOf("#") > 0) {
            str = str.substring(0, str.indexOf("#"));
        }
        if (str.indexOf("?") > 0) {
            str = str.substring(0, str.indexOf("?"));
        }
        return str;
    }

    public boolean checkListStart(String line) {
        String str = line;
        if (str.length() < 4) {
            return false;
        }
        if (str.charAt(0) == '.' && str.charAt(0) == ' ') {
            return true;
        }
        if (str.charAt(0) == '*' && str.charAt(0) == ' ') {
            return true;
        }
        char[] o = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

        if (arrayhas(o, str.charAt(0)) || arrayhas(m, str.charAt(0)) || arrayhas(n, str.charAt(0))) {
            if (str.charAt(1) == '.' && str.charAt(2) == ' ') {
                return true;
            }
        }
        return false;
    }

    public String replaceList(String text) {
        String[] lines = text.split("\r\n");
        String block = "";
        StringBuffer bf = new StringBuffer();
        boolean start = false;
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            String linetrim = line.trim();
            if (linetrim.equals("")) {
                continue;
            }
            if (checkListStart(linetrim)) {
                if (!start) {
                    start = true;
                    block = line;
                } else {
                    block = block + "\r\n" + line;
                }
            } else {
                if (start) {
                    block = formatList2(block);

                    bf.append(block + "\r\n");
                    start = false;
                }
                bf.append(lines[i] + "\r\n");
            }
        }
        if (start) {
            block = formatList2(block);
            bf.append(block + "\r\n");
            start = false;
        }
        return bf.toString();
    }

    public static int formatList_getSpaceCount(String s) {
        int num = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                num++;
            } else {
                break;
            }
        }
        return num;
    }

    public static String formatList_clear(String mstr) {
        String str = mstr.trim();
        if (str.startsWith(".") || str.startsWith("*")) {
            return str.substring(1).trim();
        } else {
            return str.substring(2).trim();
        }
    }

    public String formatList2(String text) {
        String[] mlist = text.split("\r\n");
//        if (mlist.length<2) return text;
        while (true) {
            boolean done = true;
            for (String line : mlist) {
                if (formatList_getSpaceCount(line) > 0) {
                    done = false;
                    break;
                }
            }
            if (done) {
                break;
            }
            int lay = 0;
            String lType = "ol";
            for (int i = 0; i < mlist.length; i++) {
                int space = formatList_getSpaceCount(mlist[i]);
                if (space > 0 && lay == 0) {
                    String str = mlist[i].trim();
                    if (str.startsWith("*")) {
                        lType = "ul";
                    }
                    mlist[i] = "<" + lType + "><li>" + formatList_clear(str);
                    lay = space;
                    if (i == mlist.length - 1) {
                        mlist[i] = mlist[i] + "</li></" + lType + ">";
                    }
                } else if (space > 0 && lay == space) {
                    mlist[i] = "</li><li>" + formatList_clear(mlist[i]);
                    if (i == mlist.length - 1) {
                        mlist[i] = mlist[i] + "</li></" + lType + ">";
                    }
                } else if (lay > 0 && space == 0) {
                    mlist[i] = "</li></" + lType + ">" + mlist[i];
                    break;
                } else {
                    continue;
                }
            }
        }

        StringBuffer sb = new StringBuffer();
        for (String line : mlist) {
            sb.append(line + "\r\n");
        }
        return sb.toString();
    }

    public String formatList(String text) {
        /*
        1. one
        1. two
        1. one
         * bullet 1
         * bullet 2
        1. two
        1. three
         * bullet
        1. one
         *最好是转为*=> <ul><li></li></ul> #=> <ol><li></li></ol>
         */
        //获得步进长度
        text = text.replaceAll(" 1\\. ", " # ");
        text = text.replaceAll(" a\\. ", " # ");
        text = text.replaceAll(" A\\. ", " # ");
        text = text.replaceAll(" i\\. ", " # ");
        text = text.replaceAll(" I\\. ", " # ");
        text = text.replaceAll(" \\. ", " # ");
        String step = "";
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == ' ') {
                step = step + " ";
            } else {
                break;
            }
        }

        //修改步长为" "
        if (step.length() > 0) {
            text = text.replaceAll(step, " ");
        }
        String[] lines = text.split("\r\n");
        for (int i = 0; i < lines.length; i++) {
            if (lines[i].charAt(0) != ' ') {
                break;
            }
            lines[i] = lines[i].substring(1);
        }

        StringBuffer bf = new StringBuffer();
        bf.append(lines[0] + "\r\n");
        for (int i = 1; i < lines.length; i++) {
            if (lines[i].startsWith(" ")) {
                char[] s = lines[i].toCharArray();
                for (int j = 0; j < s.length; j++) {
                    if (s[j] == ' ') {
                        if (j >= lines[i - 1].length()) {
                            s[j] = 0;
                        }
                        if (lines[i - 1].charAt(j) != '*' && lines[i - 1].charAt(j) != '#') {
                            s[j] = 0;
                        } else {
                            s[j] = lines[i - 1].charAt(j);
                        }
                    } else {
                        break;
                    }
                }
                String l = "";
                for (int j = 0; j < s.length; j++) {
                    if (s[j] == 0) {
                        continue;
                    }
                    l = l + s[j];
                }
                lines[i] = l;
            }
            bf.append(lines[i] + "\r\n");
        }
        return bf.toString();
    }

    public String attachmentUrl(String text, String baseurl, String dict) throws UnsupportedEncodingException {
        //attachment:IconsPage/info.png -> https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png
        while (true) {
            Pattern pattern = Pattern.compile("\\{\\{attachment:(.*?)/(.*?)\\}\\}");
            Matcher matcher = pattern.matcher(text);
            String replace;
            if (matcher.find()) {
                replace = baseurl + matcher.group(1) + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(2), "UTF-8");
                text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0));
                continue;
            }


            pattern = Pattern.compile("\\{\\{attachment:(.*?)\\}\\}");
            matcher = pattern.matcher(text);
            if (matcher.find()) {
                replace = baseurl + dict + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(1), "UTF-8");
                text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0));
                continue;
            }

            break;
        }
        return text;
    }

    public String tableConv(String html) {
        //||a||b||c|| -> {|
        //||d||e||f||    |a||b||c
        //               |-
        //               |d||e||f
        //               |}
//        System.out.println(html);

        String[] lines = html.split("\r\n");
        String block = "";
        StringBuffer bf = new StringBuffer();
        boolean start = false;
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if (line.startsWith("||")) {
                if (line.length() < 4) {
                    continue; //line like ||(.*?)||
                }
                String str = line.substring(1, line.length() - 2);

                Pattern pattern = Pattern.compile("<(.*?)>");
                Matcher matcher = pattern.matcher(line);
                while (matcher.find()) {
                    String x = matcher.group(1).trim();
                    String replace = "";
                    if (x.startsWith("-")) {
                        if (x.indexOf(" ") > 0) {
                            replace = "colspan=" + x.substring(1, x.indexOf(" ")) + "|";
                        } else {
                            replace = "colspan=" + x.substring(1) + "|";
                        }
                    } else if (x.startsWith("|")) {
                        if (x.indexOf(" ") > 0) {
                            replace = "rowspan=" + x.substring(1, x.indexOf(" ")) + "|";
                        } else {
                            replace = "rowspan=" + x.substring(1) + "|";
                        }
                    }
//                    System.out.println("match:"+matcher.group(1)+" to:"+replace);
                    int m = str.indexOf("<" + matcher.group(1) + ">");
                    int n = ("<" + matcher.group(1) + ">").length();
                    if (m == -1 || (m + n) > str.length()) {
                        continue;
                    }
                    str = str.substring(0, m) + replace + str.substring(m + n);
//                    str=str.replaceFirst("<"+matcher.group(1)+">",replace);
                }

                if (!start) {
                    start = true;
                    block = "{|border=\"1\" cellspacing=\"0\"\r\n" + str;
                } else {
                    block = block + "\r\n|-\r\n" + str;
                }
            } else {
                if (start) {
                    block = block + "\r\n|}\r\n";
                    bf.append(block);
                    start = false;
                }
                bf.append(lines[i] + "\r\n");
            }
        }
        if (start) {
            block = block + "\r\n|}\r\n";
            bf.append(block);
            start = false;
        }

        return bf.toString();
    }

    public String getWikiHtml(String url) throws MalformedURLException, IOException {
        URL httpurl = new URL(url);
        HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection();
        httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10");
        httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpConn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
        httpConn.setRequestProperty("Accept-Charset", "gb2312,utf-8;q=0.7,*;q=0.7");
        httpConn.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/");
        httpConn.setRequestProperty("Cookie", cookie);
        httpConn.setUseCaches(false);
        httpConn.setDoInput(true);
        InputStream in = httpConn.getInputStream();
        String wikihtml = getHtmlFromStream(in);
        return wikihtml;
    }

    public void putText(String dict, String html, String surl) throws MalformedURLException, IOException {
        URL url;
        URLConnection conn;
        InputStream in;
        BufferedReader read;
        StringBuffer sb;
        if (html.length() < 5) {
            System.out.println(dict + " is short , ignore. " + html);
            return;
        }
//        if (html.length()<300){
//            if (html.toUpperCase().trim().indexOf("REFRESH")>0) {
//                System.out.println(dict+" is REFRESH.");
//                return;
//            }
//            if (html.toUpperCase().trim().indexOf("REDIRECT")>0) {
//                System.out.println(dict+" is REDIRECT.");
//                return;
//            }
//        }
        String ex = "UbuntuHelp";
        if (surl.startsWith("https://wiki")) {
            ex = "UbuntuWiki";
        }

        String sdict = dict;
        if (sdict.indexOf("%") > 0) {
            sdict = getDict(dict);
        }


        try {
            url = new URL("http://wiki.ubuntu.org.cn/" + ex + ":" + sdict + "?action=raw");
            conn = url.openConnection();
            conn.setReadTimeout(60000);
            conn.setRequestProperty("Cookie", cookie);
            in = conn.getInputStream();
            String wikihtml = getHtmlFromStream(in);
            int wikilen = (wikihtml.replaceAll("\r", "")).replaceAll("\n", "").length();
            int htmllen = (html.replaceAll("\r", "")).replaceAll("\n", "").length();
            if (wikilen == htmllen) {
                System.out.println(dict + " no changes.");
                return;
            } else {
                System.out.println(dict + " length: " + html.length() + " oldlength: " + wikihtml.length());
            }
        } catch (Exception ex0) {
        }

        String tempdict = sdict.replaceAll("%23", "");
        String address = "http://wiki.ubuntu.org.cn/" + ex + ":" + tempdict + "?action=edit";
        String wikihtml = null;
        String from = null;
        Boolean flag = false;
        for (int i = 0; i < 5; i++) {
            wikihtml = getWikiHtml(address);
            int start = wikihtml.indexOf("<form id=\"editform\"");
            int end = wikihtml.indexOf("<div class=\"printfooter\">");
            try {
                from = wikihtml.substring(start, end);
                flag = true;
                break;
            } catch (Exception ex0) {
                //cookie = getCookie();
                if (!wikihtml.equals("")) {
                    System.out.println(wikihtml);
                } else {
                    System.out.println(dict + " read error!");
                }
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException ex1) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }
        }

        if (!flag) {
            System.out.println("read " + address + " error,please set cookie!");
            System.exit(0);
        }

        Map<String, String> map = getPostDate(from);
        Iterator<String> iterator = map.keySet().iterator();

        url = new URL("http://wiki.ubuntu.org.cn/index.php?title=" + ex + ":" + URLEncoder.encode(sdict, "UTF-8") + "&action=submit");

        HttpURLConnection conn2 = (HttpURLConnection) url.openConnection();
        String boundary = "---------------------------167593640336579986891120154";
        conn2.setReadTimeout(60000);

        conn2.setDoOutput(true);
        conn2.setAllowUserInteraction(false);
        conn2.setRequestMethod("POST");
        conn2.setRequestProperty("Cookie", cookie);
        conn2.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        conn2.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)");
        conn2.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        conn2.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
        conn2.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/index.php?title=UbuntuHelp:" + URLEncoder.encode(dict, "UTF-8") + "&action=edit");
        conn2.setRequestProperty("Accept-Charset", "UTF-8,*");
        conn2.setRequestProperty("Connection", "Keep-Alive");

        StringBuffer content = new StringBuffer();

        while (iterator.hasNext()) {
            String name = iterator.next();
            if (name.equals("wpPreview")) {
                continue;
            }
            if (name.equals("wpDiff")) {
                continue;
            }
            if (name.equals("wpWatchthis")) {
                continue;
            }
            String value = "";

            if (name.equals("wpTextbox1")) {
                value = html;
            } else {
                value = map.get(name);
            }
            content.append("--" + boundary + "\r\n");
            content.append("Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n");
            content.append(value + "\r\n");
        }
        content.append("--" + boundary + "--\r\n\r\n");
        byte[] data = content.toString().getBytes();

        conn2.setRequestProperty("Content-Length", String.valueOf(data.length));

        OutputStream out = conn2.getOutputStream();
        out.write(data);
        out.flush();

        // Get the response
        try {
            String wikihtml2 = getHtmlFromStream(conn2.getInputStream());
//            System.out.println(wikihtml2);
        } catch (Exception ex0) {
            System.out.println(dict + " add. but has a error:" + ex0.getMessage());
            out.close();
            return;
        }
        out.close();
        System.out.println(dict + " put end .");
        delDict(dict);
    }

    @SuppressWarnings("empty-statement")
    private Map<String, String> getPostDate(String from) {
        Map<String, String> map = new HashMap<String, String>();
        Pattern pattern = Pattern.compile("<input(.*?)/>");
        Matcher matcher = pattern.matcher(from);
        Pattern pname = Pattern.compile("name=[\"|'](.*?)[\"|']");
        Pattern pvalue = Pattern.compile("value=[\"|'](.*?)[\"|']");

        while (matcher.find()) {
            String name, value;
            String input = matcher.group(1);
            Matcher mname = pname.matcher(input);
            Matcher mvalue = pvalue.matcher(input);
            if (mname.find()) {
                name = mname.group(1);
            } else {
                continue;
            }
            ;
            if (mvalue.find()) {
                value = mvalue.group(1);
            } else {
                if (input.indexOf("checkbox") > 0) {
                    value = "0";
                } else {
                    value = "";
                }
            }
            ;
            map.put(name, value);
        }

//        int start=from.indexOf("cols='80' style=\"width:100%\" >");
//        int end=from.indexOf("</textarea>");
//
//        map.put("wpTextbox1",from.substring(start,end));
        map.put("wpTextbox1", "");
        return map;
    }

    private void saveProgress(String file, String dict) throws FileNotFoundException, IOException {
        FileOutputStream out = new FileOutputStream(file, true);
        out.write(dict.getBytes());
        out.write("\r\n".getBytes());
        out.flush();
        out.close();
    }

    private void readProgress(String file) throws FileNotFoundException, IOException {
        if (!new File(file).exists()) {
            return;
        }
        FileInputStream in = new FileInputStream(file);
        String lines = getHtmlFromStream(in);
        String[] dicts = lines.split("\r\n");
        for (String dict : dicts) {
            oldDict.add(dict);
        }
    }

    public void helpstart() throws IOException, InterruptedException {
        clearDict();
//        String dict="community/";
//        String out = getUrl("https://help.ubuntu.com/"+dict+"?action=raw",dict);
//        getDicts(out);
//        putText(dict,out,"https://help.ubuntu.com/community/");

        InputStream in = null;
        File saveFile = new File("help.ubuntu.com.index.html");
        if (saveFile.exists()) {
            in = new FileInputStream(saveFile);
        } else {
            URL url = new URL("https://help.ubuntu.com/community/TitleIndex");
            in = url.openConnection().getInputStream();
        }
        String html = getHtmlFromStream(in);
        if (!saveFile.exists()) {
            FileOutputStream out = new FileOutputStream(saveFile);
            out.write(html.getBytes());
            out.flush();
            out.close();
        }

//        URL url=new URL("https://help.ubuntu.com/community/TitleIndex");
//        InputStream in=url.openConnection().getInputStream();
//        BufferedReader read=new BufferedReader(new InputStreamReader(in));
//        StringBuffer sb=new StringBuffer();
//        while (true){
//            String line=read.readLine();
//            if (line==null) break;
//            sb.append(line+"\r\n");
//        }

        html = html.substring(html.indexOf("<a href=\"/community/TitleIndex?allpages=1\">") + 50, html.indexOf("<div id=\"footer\">"));
        Pattern pattern = Pattern.compile("<a href=\"/community/(.*?)\">");
        Matcher matcher = pattern.matcher(html);

        while (matcher.find()) {
            String input = matcher.group(1);
            addDict(input);
        }
        String dict;
        for (String d : addDict) {
            System.out.println(d);
        }

//        String out;

        System.out.println("一共需要转换 " + String.valueOf(addDict.size()) + " 篇文章。");

        readProgress("helpProgress.txt");

        ThreadPool pool = new ThreadPool();
        for (int i = 0; i < addDict.size(); i++) {
            final String dict1 = addDict.get(i);
            if (oldDict.contains(dict1)) {
                System.out.println(dict1 + " already read.");
                continue;
            }
            Runnable run = new Runnable() {

                public void run() {
                    long starttime = new Date().getTime();
                    String html = "";
                    try {
                        System.out.println(dict1 + " read ...");
                        html = getUrl("https://help.ubuntu.com/community/" + urlEncode(dict1) + "?action=raw", dict1);
                        System.out.println(dict1 + " read end.");
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        return;
                    }
                    try {
//                        System.out.println(dict1 + " conver ...");
//                        getDicts(html);
                        System.out.println(dict1 + " put ...");
                        putText(dict1, html, "https://help.ubuntu.com/community/" + dict1);
                        saveProgress("helpProgress.txt", dict1);

                        long needwaitTime = readOneDictPayTime - (new Date().getTime() - starttime);
                        if (needwaitTime > 0) {
                            System.out.println("sleep " + needwaitTime);
                            Thread.sleep(needwaitTime);
                        }

//                        System.out.println(dict1+" put end.");
                    } catch (Exception ex) {
                        System.out.println(dict1 + " error:" + ex.getMessage());
                    }
                }
            };

            pool.addTask(run);
        }
        pool.start();

//        while (addDict.size()>0){
//            dict=addDict.get(0);
//            System.out.print(String.valueOf(addDict.size())+" "+dict);
//            try{
////                if (addDict.size()>1377 && dict.indexOf("%")<0 ) continue;
//                try {
//                    System.out.print(" read");
//                    out=getUrl("https://help.ubuntu.com/community/"+urlEncode(dict)+"?action=raw",dict);
//                    System.out.print(" .");
//                }catch(Exception ex){
//                    try {
//                        System.out.print(" read again");
//                        out=getUrl("https://help.ubuntu.com/community/"+urlEncode(dict)+"?action=raw",dict);
//                        System.out.print(" .");
//                    }catch(Exception ex2){
//                        continue;
//                    }
//                }
//                try {
//                    System.out.print(" get dict");
//                    getDicts(out);
//                    System.out.print(" . put");
//                    putText(dict,out,"https://help.ubuntu.com/community/"+dict);
//                    System.out.print(" .\r\n");
//                }catch(Exception ex){
//                    System.out.println(dict+" error:"+ex.getMessage());
//                    continue;
//                }
//            }finally{
//                delDict(0);
//            }
//        }

        String out = getUrl("https://help.ubuntu.com/community?action=raw", "community");
        putText("community", out, "https://help.ubuntu.com/community/");
    }

    public void wikistart() throws MalformedURLException, IOException {
        clearDict();

        InputStream in = null;
        File saveFile = new File("wiki.ubuntu.com.index.html");
        if (saveFile.exists()) {
            in = new FileInputStream(saveFile);
        } else {
            URL url = new URL("https://wiki.ubuntu.com/TitleIndex");
            in = url.openConnection().getInputStream();
        }
        BufferedReader read = new BufferedReader(new InputStreamReader(in));
        StringBuffer sb = new StringBuffer();
        while (true) {
            String line = read.readLine();
            if (line == null) {
                break;
            }
            sb.append(line + "\r\n");
        }
        read.close();
        if (!saveFile.exists()) {
            FileOutputStream out = new FileOutputStream(saveFile);
            out.write(sb.toString().getBytes());
            out.flush();
            out.close();
        }

        String html = sb.substring(sb.indexOf("<a href=\"/TitleIndex?allpages=1\">Include system pages</a>"), sb.indexOf("<div id=\"footer\">"));
        Pattern pattern = Pattern.compile("<a href=\"/(.*?)\">");
        Matcher matcher = pattern.matcher(html);

        while (matcher.find()) {
            String input = matcher.group(1);
            if (input.indexOf("?") > 0) {
                continue;
            }
            if (input.indexOf("ubuntu.com") > 0) {
                continue;
            }
            if (input.equals("Jan_Mate")) {
                continue;
            }
            addDict(input);
            System.out.println(input);
        }
        String dict;
        String out;
        System.out.println("一共需要转换 " + String.valueOf(addDict.size()) + " 篇文章。");
        readProgress("wikiProgress.txt");

        ThreadPool pool = new ThreadPool();
        for (int i = 0; i < addDict.size(); i++) {
            final String dict1 = addDict.get(i);
            if (oldDict.contains(dict1)) {
                System.out.println(dict1 + " already read.");
                continue;
            }


            Runnable run = new Runnable() {

                public void run() {
                    long starttime = new Date().getTime();
                    String html = "";
                    try {
                        System.out.println(dict1 + " read ...");
                        html = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict1) + "?action=raw", dict1);
                        System.out.println(dict1 + " read end .");
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        return;
                    }
                    try {
//                        System.out.println(dict1 + " conver ...");
//                        getDicts(html);
                        System.out.println(dict1 + " put ...");
                        putText(dict1, html, "https://wiki.ubuntu.com/" + dict1);
                        saveProgress("wikiProgress.txt", dict1);

                        long needwaitTime = readOneDictPayTime - (new Date().getTime() - starttime);
                        if (needwaitTime > 0) {
                            System.out.println("sleep " + needwaitTime);
                            Thread.sleep(needwaitTime);
                        }

//                        System.out.println(dict1+" put end .");
                    } catch (Exception ex) {
                        System.out.println(dict1 + " error:" + ex.getMessage());
                        ex.printStackTrace();
                    }

                }
            };

            pool.addTask(run);
        }
        pool.start();

//        while (addDict.size()>0){
//            dict=addDict.get(0);
//            System.out.print(String.valueOf(addDict.size())+" "+dict);
//            try{
////                if (addDict.size()>12285) continue;
//                try {
//                    System.out.print(" read");
//                    out=getUrl("https://wiki.ubuntu.com/"+urlEncode(dict)+"?action=raw",dict);
//                    System.out.print(" .");
//                }catch(Exception ex){
//                    ex.printStackTrace();
//                    try {
//                        System.out.print(" read again");
//                        out=getUrl("https://wiki.ubuntu.com/"+urlEncode(dict)+"?action=raw",dict);
//                        System.out.print(" .");
//                    }catch(Exception ex2){
//                        ex2.printStackTrace();
//                        continue;
//                    }
//                }
//                try {
//                    System.out.print(" get dict");
//                    getDicts(out);
//                    System.out.print(" . put");
//                    putText(dict,out,"https://wiki.ubuntu.com/"+dict);
//                    System.out.print(" .\r\n");
//                }catch(Exception ex){
//                    System.out.println(dict+" error:"+ex.getMessage());
//                }
//            }finally{
//                delDict(0);
//            }
//        }
    }

    public void laptopTestingTeam() throws FileNotFoundException, IOException {
        InputStream in = null;
        File saveFile = new File("/home/wangpian/Desktop/index.php");
        in = new FileInputStream(saveFile);
        BufferedReader read = new BufferedReader(new InputStreamReader(in));
        StringBuffer sb = new StringBuffer();
        while (true) {
            String line = read.readLine();
            if (line == null) {
                break;
            }
            sb.append(line + "\r\n");
        }
        read.close();
        in.close();
        Pattern pattern = Pattern.compile("\\[\\[UbuntuWiki:LaptopTestingTeam/(.*?)\\]\\]");
        Matcher matcher = pattern.matcher(sb.toString());

        while (matcher.find()) {
            String input = "LaptopTestingTeam/" + matcher.group(1);
            addDict(input);
            System.out.println(input);
        }
        String out = "";
        while (addDict.size() > 0) {
            String dict = addDict.get(0);
            System.out.print(String.valueOf(addDict.size()) + " " + dict);
            try {
                try {
                    System.out.print(" read");
                    out = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict) + "?action=raw", dict);
                    System.out.print(" .");
                } catch (Exception ex) {
                    ex.printStackTrace();
                    try {
                        System.out.print(" read again");
                        out = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict) + "?action=raw", dict);
                        System.out.print(" .");
                    } catch (Exception ex2) {
                        ex2.printStackTrace();
                        continue;
                    }
                }
                try {
                    System.out.print(" get dict");
                    getDicts(out);
                    System.out.print(" . put");
                    putText(dict, out, "https://wiki.ubuntu.com/" + dict);
                    System.out.print(" .\r\n");
                } catch (Exception ex) {
                    System.out.println(dict + " error:" + ex.getMessage());
                }
            } finally {
                delDict(0);
            }
        }
    }

    public void test() throws IOException, InterruptedException {
//        String sss="\r\ndd attachment:IconsPage/info.png ClamAV can only\r\n";
//        sss=sss.replaceAll("attachment:(.*?)/(.*?) ","https://help.ubuntu.com/community/$1?action=AttachFile&do=get&target=$2 ");
//        System.out.println(sss);
//        String dict="RestrictedFormats";
//        String out=getUrl("https://help.ubuntu.com/community/"+dict+"?action=raw",dict);
//        System.out.println(out);
//        String text="d CategoryHome dd";
//        text=text.replaceAll("Category(\\S*)","[[category:Category$1]]");
//        System.out.println(text);
//        String out=tableConv("dddd\r\n||xxx||nnn||ddd||\r\n||dd||xxdee||dd||\r\nxdd");
//        System.out.println(out);
//        String text="#title User Documentation\r\n##Please discuss major/structural changes to this page on the Documentation team mailing list at: http://lists.ubuntu.com/mailman/listinfo/ubuntu-doc\r\n##If you want to get involved with editing and organising the Wiki please visit DocumentationTeam.\r\n## This page is designed to remain mostly static - make and propose changes to the pages that are linked to from this page\r\n## For help on contributing to the wiki, see the WikiGuide\r\n||<tablestyl";
//        text=text.replaceAll("\r\n#(.*)","");
//        text=text.replaceAll("^#(.*?)\r\n","");
//        System.out.println(text);
//        String text="sss\r\ndddf{{{dxx\r\n}}}\r\n . {{{ddd}}}\r\n .{{{ddd}}}\r\n{{{de}}}";
//        //link convert - \r\n {{{ *  -> \r\n<pre><nowiki> *
//        text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","\r\n<pre><nowiki>$2");
        //link convert - {{{ *  -> <pre><nowiki> *
        //  System.out.println(text);
//        text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","<pre><nowiki>$2");

        //link convert - {{{ *  -> <pre><nowiki> *
//        text=text.replaceAll("\\{\\{\\{(.*)","\r\n<pre><nowiki>$1");
//        //link convert -  * }}} ->  * <\pre><\nowiki>
//        text=text.replaceAll("(.*?)\\}\\}\\}","$1</nowiki></pre>");
//
//        System.out.println(text);
//        getCookie();

//        String url="http://bingniu.3322.org/mywiki/OpenLDAPAdminGuide/SecurityConsideration";
//        String out=getUrl(url+"?action=raw","UbuntuManual");
//        System.out.println(out);

//        BufferedReader read=new BufferedReader(new FileReader("/home/wangpian/a1.txt"));
//        String s="";
//        StringBuffer str=new StringBuffer();
//        while (true){
//            s=read.readLine();
//            if (s==null)break;
//            str.append(s+"\r\n");
//        }
//        s=str.toString();
//        s=s.replaceAll("\\[\\[\\[.*?\\]\\]\\]","");
//
//        System.out.println(s.replaceAll("\\[\\[UbuntuHelp(.*?)\\|(.*?)\\]\\]","$2"));
//        String html="== Design ==\r\n"+
//                "* Choosing the \"About UMC\" menu item should open the About window, visually centered on the screen. It should behave properly with respect to startup notification and session saving. The window should be closable using either Alt-F4 or the close button.\r\n"+
//                "\r\n"+
//                " * The About window should include:\r\n"+
//                "  * UMC logo and version number (most required!)\r\n"+
//                "  * computer model, if available\r\n"+
//                "  * CPU speed and amount of RAM on the system\r\n"+
//                "  * a \"Hardware Details\" button that opens the Device Manager\r\n"+
//                "  * Linux kernel version number\r\n"+
//                "  * KDE/Gnome version number.\r\n"+
//                "  * This \"About Box\" could also include a button \"Generate System Report\". As newbies often are asked to add system details like their sources.list, lspci, lsmod and the like(in launchpad answers or ubuntu forums), they tend to have a hard time gathering these information. Maybe they could select/unselect the appropriate  buttons for the config file they want to be exported..?\r\n"+
//                "\r\n"+
//                " * Any of the informative text should be drag-and-droppable as plain text into another program."
//                ;
//        //System.out.println(replaceList2(html));
//        String text="#REDIRECT [[UbuntuWiki:BelgianTeam/Leuven_2007-01-06]]";
//        Pattern pattern= Pattern.compile("\\[\\[UbuntuWiki:(.*?)\\]\\]");
//        Matcher matcher=pattern.matcher(text);
//        if (matcher.find()){
//            System.out.println(matcher.group(1));
//        }
//        String dict="community";
//        System.out.println(dict);
//        String out=getUrl("https://help.ubuntu.com/"+dict+"?action=raw",dict);
//        String out =formatdict("ddrer%34[[24%s]]%343242");
//        System.out.println(out);

//        for (int i=0 ;i<255;i++){
//            System.out.print(",'"+String.valueOf((char)i)+"'");
//        }
//        String out=getUrl("https://help.ubuntu.com/community/3DChessboard?action=raw","3DChessboard");
        String out = getUrl("https://wiki.ubuntu.com/HardwareSupport?action=raw", "HardwareSupport");

        System.out.println(out);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Main main = new Main();
        try {
//            main.test();
//            main.laptopTestingTeam();
            main.helpstart();
            main.wikistart();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

class ThreadPool {

    List<Thread> taskList = new ArrayList();
    int count = 1;

    void addTask(Thread obj) {
        taskList.add(obj);
    }

    void addTask(Runnable obj) {
        Thread thread = new Thread(obj);
        taskList.add(thread);
    }

    @SuppressWarnings("static-access")
    void start() {
        while (true) {
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            int j = 0;
            for (int i = 0; i < taskList.size(); i++) {
                if (taskList.get(i).getState() != Thread.State.TERMINATED && taskList.get(i).getState() != Thread.State.NEW) {
                    j++;
                    if (j >= count) {
                        break;
                    }
                }
            }
            if (j >= count) {
                continue;
            }

            for (int i = 0; i < taskList.size(); i++) {
                if (taskList.get(i).getState() == Thread.State.NEW) {
                    taskList.get(i).start();
                    j++;
                    if (j >= count) {
                        break;
                    }
                }
            }
            boolean end = true;
            for (int i = 0; i < taskList.size(); i++) {
                if (taskList.get(i).getState() != Thread.State.TERMINATED) {
                    end = false;
                    break;
                }
            }
            if (end) {
                break;
            }
        }
    }
}