PROGRAMA LECTOR DE
CODIGO DE PAGINAS WEB
=========== Creamos
un fichero con el nombre URLReader.java
import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
// TODO 1: Importar
los paquetes java.io y java.net
import java.io.*;
import java.net.*;
/**
* Esta clase puede tomar un nœmero variable de
par‡metros en la l’nea de
* de comandos. La
ejecuci—n del programa comienza con el mŽtodo main().
El
* constructor de clase no se invoca a menos
que se cree un objeto del tipo
* 'ReadFile' en el mŽtodo main().
*/
public class ReadURL extends Form
{
public
ReadURL()
{
super();
// Necesario para
el soporte de Visual J++ Form Designer
initForm();
}
/**
* ReadFile omite la colocaci—n, por lo que
puede limpiar la
* lista de componentes.
*/
public
void dispose()
{
super.dispose();
components.dispose();
}
private
void btnRead_click(Object source, Event e)
{
// TODO 3: Abrir
el URL
URL url;
try
{
url = new URL(edtInput.getText());
}
catch(MalformedURLException mu)
{
edtOutput.setForeColor(Color.RED);
edtOutput.setBackColor(Color.WHITE);
edtOutput.setText("*** Error al abrir el URL: "
+
mu.getMessage() + " ***");
return;
}
edtOutput.setForeColor(Color.WINDOWTEXT);
edtOutput.setBackColor(Color.WINDOW);
edtOutput.setText("");
// TODO 4: Matriz
que guarda los caracteres le’dos desde el URL
char[] buffer = new char[256];
// TODO 5: El
nœmero de caracteres le’dos
int length;
// TODO 6: Crea
un InputStreamReader para el URL
InputStreamReader
read;
try
{
read = new InputStreamReader(url.openStream());
}
catch(IOException io)
{
edtOutput.setForeColor(Color.RED);
edtOutput.setBackColor(Color.WHITE);
edtOutput.setText("***Error al abrir flujo de entrada:
"
+
io.getMessage() + " ***");
return;
}
// TODO 7: Leer
el URL en edtOutput
try
{
while((length = read.read(buffer)) != -1)
{
String
s = new String(buffer, 0, length);
edtOutput.setText(edtOutput.getText() + s);
}
}
catch(IOException io)
{
edtOutput.setForeColor(Color.RED);
edtOutput.setBackColor(Color.WHITE);
edtOutput.setText(edtOutput.getText()
+
"\n\n*** Error al leer el archivo ***");
}
// TODO 8: Cerrar
la conexi—n de red
try
{
read.close();
}
catch(IOException io)
{
}
// EJEMPLO Centrar
la atenci—n a edtInput
edtInput.focus();
}
private
void ReadURL_click(Object source, Event e)
{
}
/**
* NOTA: El siguiente c—digo es necesario para
el dise–ador de formularios
* de Visual J++.
Puede modificarse editor de formularios. No lo
* modifique utilizando el editor de c—digo.
*/
Container components = new Container();
Edit edtInput = new Edit();
Button btnRead = new Button();
Edit edtOutput = new Edit();
private
void initForm()
{
this.setText("ReadURL");
this.setAutoScaleBaseSize(new Point(5, 13));
this.setClientSize(new Point(199, 305));
edtInput.setAnchor(ControlAnchor.TOPLEFTRIGHT);
edtInput.setLocation(new Point(8, 8));
edtInput.setSize(new Point(100, 20));
edtInput.setTabIndex(0);
edtInput.setText("http://www.microsoft.com");
btnRead.setAnchor(ControlAnchor.TOPRIGHT);
btnRead.setLocation(new Point(112, 8));
btnRead.setSize(new Point(75, 23));
btnRead.setTabIndex(1);
btnRead.setText("Read");
btnRead.addOnClick(new EventHandler(this.btnRead_click));
edtOutput.setAnchor(ControlAnchor.ALL);
edtOutput.setEnabled(false);
edtOutput.setLocation(new Point(0, 40));
edtOutput.setSize(new Point(200, 264));
edtOutput.setTabIndex(2);
edtOutput.setText("");
edtOutput.setMultiline(true);
this.setNewControls(new Control[] {
edtOutput,
btnRead,
edtInput});
}
/**
* Punto de entrada principal para la
aplicaci—n.
*
* @param args Matriz
de par‡metros pasados a la aplicaci—n
* mediante la l’nea
de comandos.
*/
public
static void main(String args[])
{
Application.run(new ReadURL());
}
}