|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/mandarintools/pyconv/ |
Upload File : |
import java.lang.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class addpyWeb extends JApplet {
JComboBox urlinput, difficulty, howmany;
JButton addpybutton;
Hashtable pyhash;
public void init() {
loadpy();
urlinput = new JComboBox();
String dataline = null;
try {
InputStream urldata = getClass().getResourceAsStream("addpyurls.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(urldata, "UTF8"));
do {
dataline = in.readLine();
if (dataline != null) {
urlinput.addItem(dataline);
}
} while (dataline != null);
urldata.close();
}
catch (IOException e) {
System.err.println("IOException:"+e);
}
urlinput.setEditable(true);
addpybutton = new JButton("Load and Annotate");
ActionListener urlinputaction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
urlinput.hidePopup();
annotatehtml();
}
};
urlinput.addActionListener(urlinputaction);
ActionListener addpyaction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
annotatehtml();
}
};
addpybutton.addActionListener(addpyaction);
difficulty = new JComboBox();
difficulty.addItem("1");
difficulty.addItem("2");
difficulty.addItem("3");
difficulty.addItem("4");
difficulty.addItem("5");
difficulty.addItem("6");
difficulty.addItem("7");
difficulty.addItem("8");
howmany = new JComboBox();
howmany.addItem("Only Once");
howmany.addItem("Every Time");
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(urlinput);
this.getContentPane().add(addpybutton);
this.getContentPane().add(difficulty);
this.getContentPane().add(howmany);
}
public void annotatehtml() {
URL chineseurl = null;
Hashtable alreadyseen = new Hashtable();
int guess, level, curlevel, insertloc;
boolean onlyonce = false;
String dataline;
String[] hanzipy;
InputStream chinesestream;
String chinesetext = "";
byte[] rawtext = new byte[10000];
int bytesread = 0, byteoffset = 0;
StringBuffer chinesebuffer = new StringBuffer();
SinoDetect sinodetector = new SinoDetect();
char[] tempchar = new char[1];
File storagefile;
update(this.getGraphics());
// Get selected difficulty level
level = Integer.parseInt((String)difficulty.getSelectedItem()) - 1;
// Get how many times to add py
if (howmany.getSelectedIndex() == 0) {
onlyonce = true;
}
try {
// Get URL and figure out encoding
String urlstring = (String)urlinput.getSelectedItem();
if (urlstring.indexOf(":/") == -1) {
urlstring = "http://" + urlstring;
}
chineseurl = new URL(urlstring);
// Put up a working message to let the user know something is happening
File msgfile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "addpyMsg.html");
OutputStreamWriter msgout = new OutputStreamWriter(new FileOutputStream(msgfile));
msgout.write("<HTML><HEAD><TITLE>Adding Pinyin to "+ chineseurl.toString() + "</TITLE></HEAD>\n");
msgout.write("<BODY><CENTER><H2>Loading web page</H2></CENTER>");
msgout.write("Currently loading and annotating the web page located at:<BR> " + chineseurl.toExternalForm() + " .");
msgout.write(" <P>The annotated page will be displayed in this frame. <P>");
msgout.write("<BLINK>LOADING...</BLINK>");
msgout.write("</BODY></HTML>");
msgout.close();
// Show the waiting message file
getAppletContext().showDocument(msgfile.toURL(), "pydisplay");
chinesestream = chineseurl.openStream();
while ((bytesread = chinesestream.read(rawtext, byteoffset, rawtext.length - byteoffset)) > 0) {
byteoffset += bytesread;
};
chinesestream.close();
guess = sinodetector.detectEncoding(rawtext);
// Read in URL
BufferedReader in = new BufferedReader(new InputStreamReader(chineseurl.openStream(), sinodetector.codings[guess]));
do {
dataline = in.readLine();
if (dataline != null) {
chinesebuffer.append(dataline + "\n");
}
} while (dataline != null);
// Add Pinyin to loaded text
for (int i = 0; i < chinesebuffer.length(); i++) {
tempchar[0] = chinesebuffer.charAt(i);
hanzipy = (String[])pyhash.get(new String(tempchar).intern());
if (hanzipy != null) {
curlevel = Integer.parseInt(hanzipy[2]);
if (curlevel >= level) {
if (onlyonce == true && alreadyseen.containsKey(new String(tempchar).intern()) == false) {
chinesebuffer.insert(i+1, hanzipy[0]);
alreadyseen.put(new String(tempchar).intern(), " ");
i += hanzipy[0].length();
} else if (onlyonce == false) {
chinesebuffer.insert(i+1, hanzipy[0]);
i += hanzipy[0].length();
}
}
}
}
// Add URL Base and Charset Information, if needed
// Determine where to insert new data
insertloc = -1;
if ((insertloc = chinesebuffer.toString().indexOf("<HEAD>")) != -1 ||
(insertloc = chinesebuffer.toString().indexOf("<head>")) != -1) {
insertloc += 6;
} else if ((insertloc = chinesebuffer.toString().indexOf("</TITLE>")) != -1 ||
(insertloc = chinesebuffer.toString().indexOf("</title>")) != -1) {
insertloc += 8;
} else if ((insertloc = chinesebuffer.toString().indexOf("<HTML>")) != -1 ||
(insertloc = chinesebuffer.toString().indexOf("<html>")) != -1) {
insertloc += 6;
}
// If one of the above tags was found, insert new info after it, as needed
if (insertloc > -1) {
// Base Information
if (chinesebuffer.toString().indexOf("<base") == -1 &&
chinesebuffer.toString().indexOf("<BASE") == -1) {
String baseinfo = "\n<BASE HREF=\"" + chineseurl.toExternalForm() + "\">\n";
chinesebuffer.insert(insertloc, baseinfo);
}
// Charset information
if (chinesebuffer.toString().indexOf("CHARSET") == -1 &&
chinesebuffer.toString().indexOf("charset") == -1) {
String charsetinfo = "\n<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=" +
sinodetector.codings[guess] + "\">\n";
chinesebuffer.insert(insertloc, charsetinfo);
}
}
// Print to temporary file
storagefile = File.createTempFile("addpyWeb", ".unk");
storagefile.deleteOnExit();
OutputStreamWriter chineseout = new OutputStreamWriter(new FileOutputStream(storagefile), sinodetector.codings[guess]);
chinesetext = new String(chinesebuffer);
chineseout.write(chinesetext, 0, chinesetext.length());
chineseout.close();
// Show the temporary file
getAppletContext().showDocument(storagefile.toURL(), "pydisplay");
// Add User URL to ComboBox list (if not already there)
boolean hasurl = false;
for (int j = 0; j < urlinput.getItemCount(); j++) {
if (chineseurl.toExternalForm().equals((String)urlinput.getItemAt(j)) == true) {
hasurl = true;
break;
};
}
if (hasurl == false) {
urlstring = chineseurl.toExternalForm();
urlinput.addItem(urlstring);
urlinput.setSelectedItem(urlstring);
}
}
catch (Exception ahexception) {
// If problem occurs, put up an error page in display frame
File errorfile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "addpyError.html");
try {
OutputStreamWriter errorout = new OutputStreamWriter(new FileOutputStream(errorfile));
errorout.write("<HTML><HEAD><TITLE>Add Pinyin Error</TITLE></HEAD>\n");
errorout.write("<BODY><CENTER><H2>Error when loading web page</H2></CENTER>");
errorout.write("An error occurred when loading the web page located at <BR>" + chineseurl.toExternalForm() + ".");
errorout.write(" <P>Please check to make sure this is a valid URL (including the http:// prefix) and that ");
errorout.write("it actually exists.<P>");
errorout.write("The error as reported by the program is " + ahexception.toString());
errorout.write("</BODY></HTML>");
errorout.close();
// Show the temporary file
getAppletContext().showDocument(errorfile.toURL(), "pydisplay");
} catch (Exception errorexc) { };
}
}
private void loadpy() {
// Load the Unicode pinyin data file, store in pydata hashtable
String[] pystring;
String dataline;
int charsread = 0;
pyhash = new Hashtable();
try {
InputStream pydata = getClass().getResourceAsStream("uni8py.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(pydata, "UTF8"));
do {
dataline = in.readLine();
if (dataline != null) {
pystring = new String[3];
// Assign difficulty level
pystring[2] = Integer.toString(charsread / 400);
// Record all possible pinyin readings
pystring[1] = dataline.substring(2, dataline.length());
if (dataline.lastIndexOf('\u0020') == -1) {
// Only one pinyin reading
pystring[0] = pystring[1];
} else {
// Get first reading
pystring[0] = pystring[1].substring(0, pystring[1].indexOf('\u0020'));
}
pyhash.put(dataline.substring(0,1).intern(), pystring);
charsread++;
}
} while (dataline != null);
}
catch (IOException e) {
System.err.println("IOException:"+e);
}
}
public static void main(String[] argc) {
JFrame topframe = new JFrame("Add PY to Webpages");
addpyWeb myapp = new addpyWeb();
myapp.init();
topframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
});
topframe.getContentPane().add(myapp);
topframe.pack();
topframe.show();
}
}