Список использованной литературы
1.Документация по языку JAVA, Sun Mycrosystems 2003 г.
2.Материалы лекций по языку JAVA, Дмитриев, А. В., 2005 г.
3.Материалы лекций по WEB дизайну, Цветков, А. С., 2005 г.
4.Материалы статьи сети Интернет с сайта www.microsoft.com
5.Арнольд К., Гослинг Д. Язык программирования Java. С-Пб, Питер, 1997.
6.Хабибуллин И. Самоучитель Java 2. С-Пб, БХВ-Петербург, 2007.
Приложение( листинг программы)
package keytexthighlighting;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ListSelectionListener;
import java.util.StringTokenizer;
import javax.swing.*;
import java.io.*;
public class KeyTextHighlighting extends JFrame{
public static void main(String arg[]){
KeyTextHighlighting t = new
KeyTextHighlighting("Converter from source code to html-page");
}
public KeyTextHighlighting(String s){
super(s);
setBackground(Color.WHITE);
setSize(700, 720);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
JMenuBar mybar = new JMenuBar();
c.add(mybar);
this.setJMenuBar(mybar);
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
mybar.add(file);
mybar.add(edit);
mybar.add(options);
mybar.add(help);
JMenuItem open = file.add("Open source file");
JMenuItem save = file.add("Save html file");
JMenuItem exit = file.add("Exit");
JMenuItem copy = edit.add("Copy");
JMenuItem paste = edit.add("Paste");
JMenuItem cut = edit.add("Cut");
JMenuItem keyWords = options.add("Keywords");
//JMenuItem colorSheme = options.add("Color scheme");
JMenuItem pref = options.add("Preference");
//JMenuItem helpInfo = help.add("Help");
JMenuItem about = help.add("About");
JLabel inpLabel = new JLabel("Source File");
c.add(inpLabel);
final JTextArea inputFile = new JTextArea(18, 62);
JScrollPane spF = new JScrollPane(inputFile);
c.add(spF);
JLabel outLabel = new JLabel("HTML-File");
c.add(outLabel);
String tempString =
"<br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
tempString += tempString;
final JEditorPane outputFile =
new JEditorPane("text/html",tempString);
outputFile.setPreferredSize(new Dimension(680,300));
JScrollPane spS = new JScrollPane(outputFile);
c.add(spS);
setVisible(true);
Settings setgetInfo = new Settings(false);
OpenSource myOpen = new
OpenSource(inputFile,outputFile,setgetInfo.KeyWordsArray);
SaveHtml mySave = new SaveHtml(outputFile);
final KeyWord kw = new KeyWord("Choose Keywords");
kw.getKeyWord(setgetInfo.KeyWordLanguage);
kw.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
int K = kw.getKeyWord();
K+=48;
try{
FileOutputStream fis = new FileOutputStream("KeyWord.txt");
fis.write(K);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
final Preference pr = new Preference();
pr.getIDE(setgetInfo.IDE);
pr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
int K = pr.getIDE();
K+=48;
try{
FileOutputStream fis = new FileOutputStream("IDE.txt");
fis.write(K);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
///////////////////// Events /////////////////
open.addActionListener(myOpen);
save.addActionListener(mySave);
exit.addActionListener(new SaveClass(inputFile));
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputFile.copy();
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputFile.paste();
}
});
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputFile.cut();
}
});
keyWords.addActionListener(kw);
// colorSheme.addActionListener(new ColorSet());
pref.addActionListener(pr);
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog ab = new JDialog();
ab.setTitle("About Converter");
ab.setSize(300, 100);
String temp = "Made by Dryga Anatoly Copyright and etc...";
JLabel sign = new JLabel(temp);
ab.getContentPane().add(sign);
ab.show();
}
});
}
}
class Settings{
public int KeyWordLanguage;
public int IDE;
public String[] KeyWordsArray;
Settings(boolean tf) {
KWIDE();
}
Settings() {
KWIDE();
String t="";
String Keytxt = "";
switch (KeyWordLanguage) {
case 0:
Keytxt = "javaKeywords.txt";
break;
case 1:
Keytxt = "cppKeywords.txt";
break;
case 2:
Keytxt = "cSharpKeywords.txt";
break;
case 3:
Keytxt = "cKeywords.txt";
break;
}
try {
FileInputStream fis = new FileInputStream(Keytxt);
int c;
while((c = fis.read())!=-1){
t+=(char)c;
}
} catch (IOException ex) {
ex.printStackTrace();
}
KeyWordsArray = t.split("\r\n ");
}
private void KWIDE() {
try {
FileInputStream fis = new FileInputStream("KeyWord.txt");
KeyWordLanguage = fis.read();
KeyWordLanguage-=48;
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
FileInputStream fs = new FileInputStream("IDE.txt");
IDE = fs.read();
IDE-=48;
fs.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
class Preference extends JFrame implements ActionListener{
boolean shown;
public int IDE;
public JList A;
public void getIDE(int IDE){
this.IDE = IDE;
}
Preference(){
super("Choose ...");
setSize(200, 400);
shown = false;
}
public int getIDE(){
return A.getSelectedIndex();
}
public void actionPerformed(ActionEvent ae) {
if (shown==true){
setVisible(true);
return;
}
JLabel kw = new JLabel("Form:");
String[] AllInfo =
{"NetBeans5.0","JBuilder2005","VisualC++"};
A = new JList(AllInfo);
A.setVisibleRowCount(3);
A.setSelectedIndex(IDE);
JScrollPane sp = new JScrollPane(A);
sp.setSize(100,100);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
// JLabel kwFrFile = new JLabel("Save current settings:");
// JButton kwOpen = new JButton("Save");
c.add(kw);
c.add(sp);
setVisible(true);
shown = true;
}
}
//class ColorSet extends JFrame implements ActionListener{
// ColorSet(){ unfortunately this class isn't used
// super("Color settings");
// setSize(240, 600);
// Container c = getContentPane();
// c.setLayout(new FlowLayout(FlowLayout.LEFT));
// JLabel jString = new JLabel("String:");
// JLabel jClass = new JLabel("Class:");
// //JLabel jMethod = new JLabel("Methods:");
// JLabel jNumber = new JLabel("Numbers:");
// JLabel jKeyWord = new JLabel("Keywords:");
// JLabel jComment = new JLabel("Comments:");
// c.add(jString);
// write(c);
// c.add(jClass);
// write(c);
//// c.add(jMethod);
//// write(c);
// c.add(jNumber);
// write(c);
// c.add(jKeyWord);
// write(c);
// c.add(jComment);
// write(c);
// }
// void write( Container c){
// JButton colChooser = new JButton("Choose Color");
// String[] jTypeFace= {"Usual","Italics","Bold"};
// JList jLL = new JList(jTypeFace);
// jLL.setVisibleRowCount(3);
// JScrollPane spp = new JScrollPane(jLL);
// spp.setSize(100,100);
// colChooser.addActionListener(new ClrClass());
// c.add(colChooser);
// c.add(spp);
// }
// public void actionPerformed(ActionEvent ae) {
// setVisible(true);
// }
//}
class KeyWord extends JFrame implements ActionListener {
boolean shown;
public int setLanguage;
public JList javaCppCsharpC;
void getKeyWord(int setLanguage){
this.setLanguage = setLanguage;
}
KeyWord(String s){
super(s);
setSize(200, 400);
shown = false;
}
public int getKeyWord(){
return javaCppCsharpC.getSelectedIndex();
}
public void actionPerformed(ActionEvent ae) {
//KeyWord kw = new KeyWord("Choose KeyWords");
if (shown==true){
setVisible(true);
return;
}
JLabel kw = new JLabel("Keywords:");
String[] jCppCshC = {"Java","C++","Csharp","C"};
javaCppCsharpC = new JList(jCppCshC);
javaCppCsharpC.setSelectedIndex(setLanguage);
javaCppCsharpC.setVisibleRowCount(4);
JScrollPane sp = new JScrollPane(javaCppCsharpC);
sp.setSize(100,100);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton kwShow = new JButton("Show KeyWords");
ShowKeyWord KW = new ShowKeyWord(javaCppCsharpC);
kwShow.addActionListener(KW);
c.add(kw);
c.add(sp);
c.add(kwShow);
setVisible(true);
shown = true;
}
}
//class ClrClass implements ActionListener{ only for future :-))))))
// public void actionPerformed(ActionEvent a){
// Color d=JColorChooser.showDialog(null,"Choose",null);
// System.out.println(d.getRed());
// System.out.println(d.getGreen());
// System.out.println(d.getBlue());
// System.out.println(d.toString());
// }
//}
class ShowKeyWord extends JFrame implements ActionListener{
public JList javaCppCsharpC;
ShowKeyWord(JList javaCppCsharpC){
this.javaCppCsharpC = javaCppCsharpC;
}
public void actionPerformed(ActionEvent ae) {
int keyInt = javaCppCsharpC.getSelectedIndex();
JFrame Key = new JFrame("Key Words");
Key.setBackground(Color.WHITE);
Key.setSize(600, 400);
final JTextArea ta = new JTextArea(20, 50);
JScrollPane sp = new JScrollPane(ta);
String temp="";
try{
String Keytxt = "";
switch (keyInt) {
case 0:
Keytxt = "javaKeywords.txt";
break;
case 1:
Keytxt = "cppKeywords.txt";
break;
case 2:
Keytxt = "cSharpKeywords.txt";
break;
case 3:
Keytxt = "cKeywords.txt";
break;
}
FileInputStream fis = new FileInputStream(Keytxt);
int c;
while((c = fis.read())!=-1){
temp+=(char)c;
}
ta.append(temp);
fis.close();
} catch (IOException ex){
System.out.println("Error in SelectionFile section!!!");
}
Key.getContentPane().add(sp);
Key.setVisible(true);
}
}
class SaveHtml extends JFrame implements ActionListener{
JEditorPane in;
SaveHtml(JEditorPane in){
this.in = in;
}
public void actionPerformed(ActionEvent ae) {
JFileChooser fch = new JFileChooser();
String infile = in.getText();
switch(fch.showSaveDialog(null)){
case JFileChooser.APPROVE_OPTION:
File selectedFile = fch.getSelectedFile();
String sf = selectedFile.getAbsolutePath();
sf = sf + ".html";
File sff = new File(sf);
try{
FileWriter fis = new FileWriter(sff);
fis.write(infile);
fis.close();
}catch (IOException ex){
System.out.
println("Error in SelectionFile section!!!");
}
break;
}
}
}
class OpenSource extends JFrame implements ActionListener{
public static String myout;
JTextArea inputFile;
JEditorPane outputFile;
public String[] KeyWords;
OpenSource( JTextArea inputFile,JEditorPane outputFile, String[] KeyWords){
this.inputFile = inputFile;
this.outputFile = outputFile;
this.KeyWords = KeyWords;
}
public void actionPerformed(ActionEvent ae) {
JFileChooser fch = new JFileChooser();
switch (fch.showOpenDialog(null)) {
case JFileChooser.APPROVE_OPTION:
File selectedFile = fch.getSelectedFile();
File directory = fch.getCurrentDirectory();
int maxLength = 30000;
String temp = "";
String temp2 = "";
inputFile.setText("");
int NumChar=1;
try {
InputStream fis = new FileInputStream(selectedFile);
int c;
while ((c = fis.read()) != -1) {
temp += (char) c;
if (NumChar>=maxLength){
outputFile.setText("File is too big for converting"+
"(max length of file <="+maxLength+" characters). Please"+
" break file to pieces");
break;
}
NumChar++;
}
inputFile.append(temp);
if (NumChar<maxLength){
toHTML outStr = new toHTML(temp,KeyWords);
String myout = outStr.getHTML();
outputFile.setText(myout);
fis.close();
}
} catch (IOException ex) {
System.out.println("Error in SelectionFile section!!!");
}
break;
};
}
}
class toHTML{ // Main class
// purpose: xxxxx.java, xxxxx.c,xxxxx.cpp --> HTML
public String myHTML;
static String[] KeyWords;
public String keyFont;
public String keyStyle;
public String numFont;
public String numStyle;
public String stringFont;
public String stringStyle;
public String comFont;
public String comStyle;
public boolean isComment;
public String keyOpen;
public String keyClose;
public String numOpen;
public String numClose;
public String stringOpen;
public String stringClose;
public String comOpen;
public String comClose;
toHTML(String input,String[] KeyWords){
this.myHTML = input;
this.KeyWords = KeyWords;
}
String getHTML(){
String SourceFile="";
begEnd(SourceFile);
return this.myHTML;
}
void begEnd(String FileName){
Settings SS = new Settings();
KeyWords = SS.KeyWordsArray;
String tempIDE = "";
int c;
switch(SS.IDE){
case 0:
try{
InputStream fis = new FileInputStream("NetBeans.txt");
while ((c = fis.read()) != -1) {
tempIDE += (char) c;
}
} catch(IOException ex){
};
break;
case 1:
try{
InputStream fis = new FileInputStream("JBuilder.txt");
while ((c = fis.read()) != -1) {
tempIDE += (char) c;
}
} catch(IOException ex){
};
break;
case 2:
try{
InputStream fis = new FileInputStream("VC++.txt");
while ((c = fis.read()) != -1) {
tempIDE += (char) c;
}
} catch(IOException ex){
};
break;
}
String[] myFormat = tempIDE.split(" ");
keyFont =myFormat[0];
keyStyle = myFormat[1];
keyStyle = (keyStyle.equals("U"))?"":keyStyle;
numFont = myFormat[2];
numStyle = myFormat[3];
numStyle = (numStyle.equals("U"))?"":numStyle;
stringFont = myFormat[4];
stringStyle = myFormat[5];
stringStyle = (stringStyle.equals("U"))?"":stringStyle;
comFont = myFormat[6];
comStyle = myFormat[7];
comStyle = (comStyle.equals("U"))?"":comStyle;
keyOpen = "<font color=" + keyFont +"><"+keyStyle+ ">";
keyClose = "</"+ keyStyle+"><font color=black>";
numOpen = "<font color=" + numFont +"><"+numStyle+ ">";
numClose = "</"+ numStyle+"><font color=black>";;
stringOpen ="<font color="+stringFont+"><"+stringStyle+ ">";
stringClose = "</"+ stringStyle+"><font color=black>";
comOpen = "<font color="+comFont+"><"+comStyle+">";
comClose = "</"+ comStyle+"><font color=black>";
//
String temp="";
String endingComment = "";
String[] myHTMLmas = myHTML.split("\n");
// myHTMLmas[i] - one source line
isComment = false;
for (int i=0;i<myHTMLmas.length;i++){
// replacement "<" ---> "<"
// ">" ---> ">"
String linetemp="";
String[] myHTMLline = myHTMLmas[i].split("<");
for(int j = 0;j<myHTMLline.length-1;j++){
linetemp = linetemp + myHTMLline[j] + "<";
}
myHTMLmas[i] = linetemp + myHTMLline[myHTMLline.length-1];
linetemp="";
myHTMLline = myHTMLmas[i].split(">");
for(int j = 0;j<myHTMLline.length-1;j++){
linetemp = linetemp + myHTMLline[j] + ">";
}
myHTMLmas[i] = linetemp+myHTMLline[myHTMLline.length-1];
int type = isComment?2:0;
myHTMLmas[i] = parsing(myHTMLmas[i],type);
temp = temp+ "<pre>" + myHTMLmas[i]+endingComment + "</pre>" ;
}
temp = "<HTML><HEAD><TITLE>"+FileName+"</TITLE></HEAD>"+
"<BODY> <CODE>"+temp +"</CODE></BODY></HTML>";
myHTML = temp;
}
public String parsing(String line,int Type){
String stringForOut="";
String beginL;
String endL;
String part1;
String part2;
int nElement=0;
int TypeElement=0;
int[] tt;
switch(Type){
//initial line
case 0:
tt = SearchFirstElement(line);
nElement = tt[0];
TypeElement = tt[1];
switch(TypeElement){
case 0:
line = KeyWordSearch(line);
stringForOut=line;
break;
case 1:
beginL = line.substring(0,nElement);
beginL = KeyWordSearch(beginL);
endL = line.substring(nElement);
stringForOut=beginL + parsing(endL,1);
break;
case 2:
beginL = line.substring(0,nElement);
beginL = KeyWordSearch(beginL);
endL = line.substring(nElement);
int NslStr = StarSlashString(endL);
if (NslStr==endL.length()){
isComment = true;
}
part1 = endL.substring(0,NslStr);
part2 = endL.substring(NslStr);
part1 =comOpen + part1 + comClose;
part2 = parsing(part2,0);
stringForOut = beginL + part1 + part2;
break;
case 3:
beginL = line.substring(0,nElement);
beginL = KeyWordSearch(beginL);
endL = line.substring(nElement);
endL =comOpen + endL + comClose;
stringForOut=beginL + endL;
break;
}
break;
// search "
case 1:
nElement = QuotedString(line);
beginL = line.substring(0,++nElement);
beginL = stringOpen + beginL + stringClose;
endL = line.substring(nElement);
stringForOut=beginL + parsing(endL,0);
break;
case 2:
nElement = StarSlashString(line);
beginL = line.substring(0,++nElement);
beginL = comOpen + beginL + comClose;
endL = line.substring(nElement);
stringForOut=beginL + parsing(endL,0);
isComment = false;
break;
case 3:
line = KeyWordSearch(line);
stringForOut=line;
}
return stringForOut;
}
public String KeyWordSearch(String entering){
String linetemp = "";
StringTokenizer st =
new StringTokenizer(entering,";(){}[]. \t\r,.",true);
while(st.hasMoreElements()){
String word = st.nextToken();
boolean tf = search(word);
if(tf==true){
linetemp = linetemp + keyOpen+word+keyClose;
}else{
linetemp = linetemp + word;
}
}
return linetemp;
}
public int[] SearchFirstElement(String entering){
char myQuote = '\"';
char Previous = ' ';
char current = ' ';
int ret[]= new int[2];
for(int i=0;i<entering.length();i++){
current = entering.charAt(i);
if(current==myQuote&(Previous!='\\'&Previous!='\'')){
ret[0] = i;
ret[1] = 1; // found beginning of String
return ret;
}
if(current=='*'&Previous=='/'){
ret[0] = --i;
ret[1] = 2; // found beginning of /*
return ret;
}
if(current=='/'&Previous=='/'){
ret[0] = --i;
ret[1] = 3; // found beginning of //
return ret;
}
Previous = current;
}
return ret;
}
public int StarSlashString(String entering) {
// int NQuote=0;
char myQuote = '/';
char Previous = ' ';
//int DivideChar = '\\';
for(int i=0;i<entering.length();i++){
if(entering.charAt(i)==myQuote&Previous=='*'){
return i;
}
Previous = entering.charAt(i);
}
return entering.length();
}
public int QuotedString(String entering) {
// int NQuote=0;
char myQuote = '"';
char Previous = ' ';
char DivideChar = '\\';
for(int i=1;i<entering.length();i++){
if(entering.charAt(i)==myQuote){
if(Previous!=DivideChar){
return i;
}
}
Previous = entering.charAt(i);
}
return entering.length();
}
static boolean search(String s){
String[] strmas = KeyWords;
int lo = 0;
int hi = strmas.length-1;
while(lo<=hi){
int mid = lo + (hi - lo)/2;
int cmp = s.compareTo(strmas[mid]);
if(cmp==0){
return true;
} else if (cmp<0){
hi = mid - 1;
} else{
lo = mid + 1;
}
}
return false;
}
}
