Jump to content

Conveter Java para vb ou C#


sabing

Recommended Posts

Pessoal como faço para converter um código em java para vb ou c# ? sei lá que funciona no visual studio eu estou com dificuldade por causa das bibliotecas por exemplo 

//Essse codigo eu não consigo converter para vs ! a biblioteca Pattern por exemplo o resto eu entendo ! mais 

o Url não consigo etc...

" private final String patternTable = "<table  border.*?</TABLE>";"
 " Pattern pattern = Pattern.compile(patternTable);"
 " Matcher matcher = pattern.matcher(html);"

package rast;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Codigo implements Serializable {

    private String codigo;
    private List<Info> infoList;
    private final String urlBase = "http://websro.correios.com.br/sro_bin/txect" +
            "01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI=";
    private final String patternString = "<tr><td rowspan=\\d>(.*)</td><td>(.*)</td><td>" +
            "<FONT COLOR=\".{6}\">(.*)</font></td></tr>|<tr><td colspan=\\d>(.*)</td></tr>";
    private final String patternTable = "<table  border.*?</TABLE>";
    private static final Pattern patternCodigo = Pattern.compile("\\w{2}\\d{9}\\w{2}");
    private static final Pattern patternCodigoInvalido = Pattern.compile("<FONT face=" +
            "Arial size=2 color=black><b></b><p>(.*)<input type=hidden name=\"Z_ACTION\">");
    private long serialVersionUID = 1l;

    public Codigo(String codigo) {
        this.codigo = codigo;
    }

   
    public String getHtmlInfo() throws CodigoException {
        String saida = null;
        String html = getHtml().replace('\n', ' ');
        html = html.replace('\t', ' ');
        Pattern pattern = Pattern.compile(patternTable);
        Matcher matcher = pattern.matcher(html);

        while (matcher.find()) {
            saida = matcher.group();
        }
        
        if (saida == null) {
            matcher = patternCodigoInvalido.matcher(html);
            if (matcher.find()) {
                saida = matcher.group(1);
            }
        }
        return saida;
    }

    public List<Info> getInfoList() throws CodigoException {
        generateInfoFromHtml();
        return infoList;
    }

    private String getHtml() throws CodigoException {
        StringBuffer newData = new StringBuffer(10000);
        try {
        newData.append("");

        URL url = new URL(urlBase + codigo);
        HttpURLConnection connection =
                (HttpURLConnection) url.openConnection();

        connection.setRequestProperty("Request-Method", "GET");
        connection.setDoInput(true);
        connection.setDoOutput(false);

        connection.connect();

        // abre a conexão pra input
        BufferedReader br =
                new BufferedReader(new InputStreamReader(connection.getInputStream()));

        // le ate o final
        newData = new StringBuffer(10000);
        String s = "";
        while (null != ((s = br.readLine()))) {
            newData.append(s).append('\n');
        }
        br.close();
        String result = connection.getResponseCode() + "/" + connection.getResponseMessage();
        if (!result.equals("200/OK")) {
            throw new CodigoException("Erro ao tentar obter dados " +
                    "do correio: " + result);
        } 

        } catch (UnknownHostException uHostE) {
            throw new CodigoException("Erro ao conectar a pagina dos correios.");
        } catch (IOException ioEx) {
            throw new CodigoException("Erro: " + ioEx.getMessage());
        }

        return newData.toString();
    }
    
    /*
    private void getInfo() {
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(getHtml());
        
        Vector<Info> infoTemp = new Vector<Info>();

        while (matcher.find()) {
            if (matcher.group(1) != null) {
                infoTemp.add(new Info(matcher.group(1), matcher.group(2), matcher.group(3)));
            } else {
                infoTemp.add(new Info(null, matcher.group(4), null));
            }
            
        }

        infoList = new ArrayList<Info>();
        for (int i = 0; i < infoTemp.size(); i++) {
            Info info = infoTemp.get(i);
            if (info.getHorario() != null) {
                infoList.add(info);
            } else {
                infoList.add(info);
            }
        }
    }
     */ 
    
    private void generateInfoFromHtml() throws CodigoException {
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(getHtml());
        Vector<Info> infoTemp = new Vector<Info>();
        while (matcher.find()) {
            if (matcher.group(1) != null) {
                infoTemp.add(new Info(matcher.group(1), matcher.group(2), matcher.group(3)));
            } else {
                infoTemp.add(new Info(null, matcher.group(4), null));
            }

        }
        infoList = new ArrayList<Info>();
        for (int i = 0; i < infoTemp.size(); i++) {
            Info info = infoTemp.get(i);
            if (info.getHorario() != null) {
                infoList.add(info);
            } else {
                infoList.add(info);
            }
        }
    }

    public static List<Codigo> parseCodigos(String texto) {
        List<Codigo> lista = null;
        Matcher matcher = patternCodigo.matcher(texto);
        while (matcher.find()) {
            if (lista == null) {
                lista = new ArrayList<Codigo>();
            }
            lista.add(new Codigo(matcher.group()));
        }
        return lista;
    }

    @Override
    public String toString() {
        return codigo;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj instanceof Codigo) {
            Codigo c = (Codigo) obj;
            return this.codigo.equals(c.codigo);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 23 * hash + (this.codigo != null ? this.codigo.hashCode() : 0);
        return hash;
    }
    
    public static void main(String args[]) {
        try{
        	 Codigo c = new Codigo("PN613314389BR");
             for (int i = 0; i < c.getInfoList().size(); i++) {
                System.out.println(c.getInfoList().get(i));
             }
        }catch(Exception ex){
        	System.err.println(ex.getMessage());
        }
    }
    
}


 

Link to comment
Share on other sites

Eu estava tentando transcrever o código java para c# , eu estou com muita dificuldade de transcrever o regex que é o mesmo parten do java, porém as string que verifica os carácter é diferente ! eu estou perdidão! 

Tipo aqui String patternTable = "<table border.*?</TABLE>"; 

Edited by sabing
Link to comment
Share on other sites

Os RegExp são, normalmente, compatíveis, embora haja algumas linguagens com algumas diferenças.

Tens de ver a sintaxe no manual de ambas as linguagens para saber qual a expressão correta.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.