Forum - Java
- Mi servirebbe un consiglio
lorenzk16 (Normal User)
Newbie
Messaggi: 2
Iscritto: 31/12/2008
Ciao a tutti
Ho un piccolo problema con un programma statistico realizzato parecchi anni fa.
Il problema consiste nell'intrpretazione di una pagina html da cui devo prendere dei dati ( pubblici), attualmente il programma interpreta una pagina non più esestente e va sostituito o corretto.
-------------------------------
Ecco la pagina da interpretare:
<html>
<head><meta http-equiv="Content-Type" content="text/xml; charset=ISO-8859-1"><link type="text/css" rel="stylesheet" href="http://davidcasino.net/dcgame/includes/css/lotto_cells.css ... ;
<title>Lot O Balls Draw History</title>
<link rel="stylesheet" href="http://davidcasino.net/dcgame/includes/css/LotOBalls.css"& ... ;
<body style="margin-right:0px;margin-left:0px;margin-top:0px;margin-bottom:0px;margin-width:0px;margin-height:0px;">
<table style="border-collapse:collapse;padding:0px;" align="center">
<tr>
<td>
<div id="xData"><br>
<div id="reportBody">
<table class="Report" width="700px">
<tr>
<th class="Header">Estrazione N�</th>
<th class="Header" colspan="6">Numeri</th>
<th class="Header" colspan="3">Pronostico Colore</th>
<th class="Header">Range Total</th>
<th class="Header">Pari contro Dispari</th>
<th class="Header">Colore Preferito</th>
<th class="Header">Pari o Dispari</th>
<th class="Header">Ball Wars</th>
<th class="Header">6� Numero</th>
</tr>
<tr>
<td align="center">
<b>189327</b>
</td>
<td width="19px" align="center">
<font class="">20</font>
</td>
<td width="19px" align="center">
<font class="">15</font>
</td>
<td width="19px" align="center">
<font class="">34</font>
</td>
<td width="19px" align="center">
<font class="">21</font>
</td>
<td width="19px" align="center">
<font class="">11</font>
</td>
<td width="19px" align="center">
<font class="">1</font>
</td>
<td width="21px" align="center">
<b>
<font class="resultColorA">2</font>
</b>
</td>
<td width="21px" align="center">
<b>
<font class="resultColorB">2</font>
</b>
</td>
<td width="21px" align="center">
<b>
<font class="resultColorC">2</font>
</b>
</td>
<td align="center"><img src="http://davidcasino.net/dcgame/graphics/softgames/lotto_med ... ;
</td>
<td align="center"><img src="http://davidcasino.net/dcgame/graphics/softgames/lotto_odd ... ;
</td>
<td align="center"><img src="http://davidcasino.net/dcgame/graphics/softgames/lotto_bal ... ;
</td>
<td align="center"><img src="http://davidcasino.net/dcgame/graphics/softgames/lotto_eve ... ;
</td>
<td align="center"><img src="http://davidcasino.net/dcgame/graphics/softgames/lotto_bal ... ;
</td>
<td align="center">
<b>
<font class="resultColorA">1</font>
</b>
</td>
</tr>
</table>
<table width="160" border="0" cellspacing="1" align="center" cellpadding="12">
<tr align="center">
<td>
<a href="http://davidcasino.net/dcgame/"><img '>http://www.priscillacasino.com/servlet/com.cwh.gdk.servlet ... border="0" src="http://davidcasino.net/dcgame/graphics/softgames/back.gif"> ;
</a>
</td>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table><br><br>
<div id="extraContent">
<span> </span>
</div>
</body>
</html>
----------------------------------------
Ecco il mio codice:
import java.io.*;
import java.util.*;
/*******************************/
/*/ AUTHOR: ecs /*/
/*/ Copyright 2002 /*/
/*******************************/
/*/ See license for /*/
/*/ further information /*/
/*******************************/
/**
* Classe wrapper che costruisce un oggetto che rispecchia la struttura di una pagina HTML passata al costruttore
*/
public class HTMLobject extends Object
{
private Properties props;
private Properties defaults;
private Vector children;
private String value;
private HTMLobject parent;
HTMLobject(String html, HTMLobject par)
{
defaults = HTMLUtil.getPropDefaults();
String firstTag = "", openTag = "", closeTag = "";
html = prepareString(html);
int init, end, temp;
if (par == null)
{
firstTag = "HTML";
if (html.indexOf(firstTag) < 0)
firstTag = firstTag.toLowerCase();
if (html.indexOf(firstTag) < 0)
return;
if(html.indexOf("</"+firstTag)>0)
html = html.substring(html.indexOf(firstTag)-1, html.indexOf("</"+firstTag)+firstTag.length()+3);
else
html = html.substring(html.indexOf(firstTag)-1, html.length());
}
else if ( (firstTag = getFirstTag(html)) == "")
{
children = new Vector(0);
props = new Properties();
props.setProperty("type", "text");
if (html != null)
value = html;
else
value = " ";
return;
}
parent = par;
openTag = "<"+firstTag;
closeTag = "</"+firstTag;
if( !( (init = html.indexOf(openTag)) >= 0 ) )
error(0);
if ( !( (end = html.indexOf(">", init)) >= 0 ) )
error(0);
init += openTag.length();
props = getProps(html.substring( init, end ));
props.setProperty("type", firstTag);
if ( (temp = html.lastIndexOf(closeTag)) >=0 )
html = html.substring(end+1, temp);
else
html = html.substring(end+1, html.length());
children = getKids(html);
}
public int hasChild(String type)
{
return 0;
}
public int childNum()
{
return children.size();
}
public HTMLobject getChild(int off)
{
if(off < children.size())
return (HTMLobject)children.get(off);
else
return null;
}
public HTMLobject[] getChildren(String type)
{
Vector tmp = new Vector();
for(int i=0; i<children.size(); i++)
{
HTMLobject obj = (HTMLobject)children.get(i);
if(obj.getType().equalsIgnoreCase(type))
tmp.add(obj);
}
return (HTMLobject[])tmp.toArray(new HTMLobject[tmp.size()]);
}
public String getType()
{
try
{
return props.getProperty("type");
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println("HTMLobject::getType() -> !!!!!!!! property -type- not found !!!!!!!");
return "text";
}
}
public HTMLobject getParent(){
return parent;
}
public boolean isRoot(){
return (parent == null);
}
public boolean isText(){
return (props.getProperty("type").equalsIgnoreCase("text"));
}
public String getValue(){
return value;
}
public String getProperty(String name){
return props.getProperty(name, defaults.getProperty(name.toUpperCase()));
}
public Enumeration getPropertyNames(){
return props.propertyNames();
}
/**
* Individua il primo tag
*/
private String getFirstTag(String html)
{
html = html.trim();
int init = html.indexOf("<"), space, a;
String tmp = "";
if(init < 0)
return tmp;
if( (a = html.indexOf(">")) > (init+1) )
tmp = html.substring(init+1, a).trim();
if( (space = tmp.indexOf(" ")) >=0 )
return tmp.substring(0, space+1 ).trim();
else
return tmp;
}
/**
* Smazza i tag
*/
private Vector getKids(String html)
{
Vector ret = new Vector(0);
int off = 0, pr = 0, count = 0, a, b;
String tmp = html.trim(), tag;
while (tmp.length()>0)
{
tag = getFirstTag(tmp);
if (tag.equalsIgnoreCase(""))
{
ret.add(new HTMLobject(tmp, this));
return ret;
}
else if( (pr = tmp.indexOf("<")) != 0)
{
String temp = "";
if ( (temp = tmp.substring(0,pr).trim()).length() > 0 )
ret.add(new HTMLobject(temp, this));
tmp = tmp.substring(pr, tmp.length());
}
off = tmp.indexOf(">", tmp.indexOf("<"+tag))+1;
String openTag = "<"+tag;
String closTag = "</"+tag;
if (tag.equalsIgnoreCase("!--"))
{
closTag = "-->";
tmp = tmp.substring(tmp.indexOf(closTag)+closTag.length(), tmp.length());
continue;
}
count = 1;
while (count != 0)
{
a = tmp.indexOf(openTag, off);
b = tmp.indexOf(closTag, off);
if ( (a >= 0) && (b >= 0) && (a<b))
{
off = tmp.indexOf(">", a)+1;
count++;
}
else if ( b >= 0 )
{
off = tmp.indexOf(">", b)+1;
count--;
}
else
break;
}
ret.add(new HTMLobject(tmp.substring(0,off), this));
tmp = tmp.substring(off, tmp.length());
}
return ret;
}
private Properties getProps(String html)
{
int offSp = 0, offEq = 0;
String tmp = html.trim(), propN = "", propV = "";
Properties pro = new Properties();
while (true)
{
if ((offSp = tmp.indexOf(" ")) >= 0)
{
String temp = tmp.substring(0, offSp);
propN = temp;
propV = "true";
if (temp.indexOf("=") > 0)
{
propN = temp.substring(0,temp.indexOf("="));
propV = temp.substring(temp.indexOf("=")+1, temp.length()).trim();
int off = temp.length()+1;
propV = getVal(off, propV, tmp);
}
else
{
int off = offSp;
while ( (off < tmp.length()) && (tmp.charAt(off) == 32) )
{
if (tmp.charAt(++off) == '=')
{
propV = getVal(++off, "", tmp);
break;
}
}
}
}
else if ((offEq = tmp.indexOf("=")) >= 0)
{
String temp = tmp.substring(0, tmp.length());
propN = temp.substring(0,temp.indexOf("="));
propV = temp.substring(temp.indexOf("=")+1, temp.length()).trim();
}
else if(tmp.length() > 0)
{
propN = tmp;
propV = "true";
}
else
{
return pro;
}
pro.setProperty(propN, propV);
int end = 0;
if(propV.equalsIgnoreCase("true"))
end = tmp.indexOf(propN)+propN.length()+1;
else
end = tmp.indexOf(propV)+propV.length()+1;
if(end >= tmp.length())
break;
else
tmp = tmp.substring(end, tmp.length()).trim();
} // END WHILE
return pro;
}
private String getVal(int off, String propV, String html) {
boolean entered = false;
while (propV.length() == 0) {
entered = true;
int end = html.indexOf(" ", off);
if(end < 0)
propV = html.substring(off, html.length()).trim();
else
propV = html.substring(off, end).trim();
off++;
}
return propV;
}
private String prepareString(String html){
html = html.trim();
html = html.replace((char)13, ' ');
return html.replace((char)10, ' ');
}
private void error(int value){
System.err.println("error "+value);
System.exit(value);
}
public String toString(){
return props.getProperty("type");
}
}
------------------------------
In sintesi devo leggere i campi:
Estrazione e
Numeri
Premetto che non sono un programmatore, ma da autodidatta comprendo discretamente il codice.
Secondo voi devo far riscrivere tutto il codice o è sufficente apportare alcune modifiche ( premetto che qualcosa ho già tentato ma con scarsi risultati )
Grazie e Buon Anno
Lorenz
pierotofy (Admin )
Guru^2
Messaggi: 6230
Iscritto: 04/12/2003
Mha, io personalmente riscriverei il tutto usando le Regex. L'autore di questo sorgente probabilmente non ne ha mai sentito parlare, ma penso possano ridurre il codice fino a 1/5 della dimensione attuale.
lorenzk16 (Normal User)
Newbie
Messaggi: 2
Iscritto: 31/12/2008
Ciao
Molto interessante!
L'utilizzo delle regex comporterebbe il dover riscrivere tutto il codice, o può essere applicato solo a questa funzione?
Il programma completo e stato sviluppato con jbuilder 5, ed è veramente complesso, specialmente nella parte di elaborazione statistica, se devo far rifare tutto il programma mi costa una cifra!
In più dovrei trovare un programmatore che conosca bene le regex
Grazia molte del consiglio.
Lorenz