📄 parse.java
字号:
/**
* @author LightingMan
*/
import java.util.*;
import javax.swing.*;
import javax.swing.text.html.*;
class Parse
{
Check check;
ArrayList<String> testList =new ArrayList<String>();
int []index;
Stack<String> myStack;
JTextArea jta;
int lineNumber=0;
public Parse(JEditorPane jtp,JTextArea jta){
String s=new String(jtp.getText());
this.jta=jta;
makeParse(s);
this.testMatch();
this.testOrder();
}
public Parse(String s){
String ss=new String(s);
makeParse(ss);
this.testMatch();
this.testOrder();
}
private String dealString(String s){
//s.replaceAll("<"," <");
return s.replaceAll(">","> ");
}
public void makeParse(String htmlString)
{
String tmp;
StringTokenizer lineTokenizer=new StringTokenizer(htmlString,"\n");
index=new int[lineTokenizer.countTokens()+1000];
boolean first=true;
while(lineTokenizer.hasMoreTokens()){
lineNumber++;
tmp=lineTokenizer.nextToken();
tmp=dealString(tmp);
//System.out.println(tmp);
StringTokenizer tokenizer = new StringTokenizer(tmp,">");
StringTokenizer stk;
String a=null,b=null,tag=null;
boolean p;
while(tokenizer.hasMoreTokens())
{
a=tokenizer.nextToken();
p=true;
a=a+">";
a=a.toLowerCase();
//System.out.println(a);
if (a.trim().startsWith("<!doctype")) {
p=false;
first=false;
}
else
if (!(a.trim().startsWith("<!doctype"))&&(first)){
//p=false;
first=false;
//head=false;
new MyException("Line 1 column 0: no document type declaration; implying \"<!DOCTYPE HTML SYSTEM>.\"",jta,lineNumber);
}
if (a.trim().startsWith("<!--")&&(a.trim().endsWith("-->"))) {
p=false;
first=false;
}
else
if((a.trim().startsWith("<!"))&&(!first)&&(p)) {
first=false;
new MyException("注释格式不正确"+a,this.jta,lineNumber);
p=false;
}
if (p){//判断是不是注释
a=a.trim();
if (a.equals(">")) break;
//if (!a.startsWith("<")) break;
if (!a.contains("<")) break;
b=a.substring(a.indexOf("<")+1,a.indexOf(">"));
b=b.trim();
stk=new StringTokenizer(b);
tag=stk.nextToken();
//System.out.println(tag);
if ((HTML.getTag(tag)!=null)||(HTML.getTag(tag.substring(1))!=null)){
if (SingleTag.isSingleTag(tag)||SingleTag.isSingleTag(tag.substring(1)));
else
{
testList.add(tag);
index[testList.size()]=lineNumber;
}
}
if ((!b.trim().startsWith("/"))) {
check = new Check(b,jta,lineNumber);
}
else
{
if (HTML.getTag(tag.substring(1))==null)
new MyException("Unknow end tag:<"+b+">",this.jta,lineNumber);
}
}
}
}
}
//测试标记是否成对出现
public void testMatch()
{
myStack=new Stack<String>();
String tmp;
String startTag;
boolean p;
int index1=0;
Stack<Integer> linNum=new Stack<Integer>();
for (int i=0;i<testList.size();i++){
index1++;
tmp=testList.get(i).toString().trim();
//System.out.println(tmp);
p=true;
if (tmp.startsWith("/")){
if (myStack.empty()){
new MyException("The tag:<"+tmp+"> has not begin tag!",jta,index[index1]);
p=false;
}
if (p){
startTag=myStack.peek();
Integer k=linNum.peek();
if(!tmp.equals("/"+startTag)){
boolean find=false;
//向前查找看是否有配对标记出现
for (int j=myStack.size()-1;j>=0;j--){
if (tmp.equals("/"+myStack.elementAt(j))){
find=true;
break;
}
}
if (find){
while(!myStack.empty()&&(!tmp.equals("/"+myStack.peek()))){
new MyException("The tag "+myStack.peek()+" doesn't have the end tag: "+"</"+myStack.peek()+">",jta,linNum.peek());
myStack.pop();
linNum.pop();
}
myStack.pop();
linNum.pop();
}
else
new MyException("The tag:<"+tmp+"> has not begin tag!",jta,index[index1]);
}
else
{
myStack.pop();
linNum.pop();
}
}
}
else{
myStack.push(tmp);
linNum.push(Integer.valueOf(index[index1]));
}
}
while (!myStack.empty()){
new MyException("The tag "+myStack.peek()+" doesn't have the end tag: "+"</"+myStack.pop()+">",jta,linNum.pop());
}
}
public void testOrder(){
if(!(testList.get(0).equals("html")))
new MyException("<html> is in wrong position or missing!",jta,lineNumber);
for(int index=0;index<testList.size();index ++)
{
if(testList.get(index).equals("head"))
{
if(testList.indexOf("html")>index)
{
new MyException("<head> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("title"))
{
if(testList.indexOf("head")>index)
{
new MyException("<title> is in wrong position or missing",jta,lineNumber);
}
}
if(testList.get(index).equals("/title"))
{
if(testList.indexOf("title")>index)
{
new MyException("</title> is in wrong position or missing",jta,lineNumber);
}
}
if(testList.get(index).equals("/head"))
{
if(testList.indexOf("/title")>index)
{
new MyException("</head> is in wrong position or missing",jta,lineNumber);
}
}
if(testList.get(index).equals("body"))
{
if(testList.indexOf("/head")>index)
{
new MyException("<body> is in wrong position or missing",jta,lineNumber);
}
}
if(testList.get(index).equals("table"))
{
if(!(testList.get(index +1).equals("tr")))
{
new MyException("<tr> is in wrong position! or missing",jta,lineNumber);
}
}
if(testList.get(index).equals("tr"))
{
if(!(testList.get(index +1).equals("td")||testList.get(index+1).equals("th")||testList.get(index+1).equals("/tr")))
new MyException("<tr> is in wrong position! or missing",jta,lineNumber);
}
if(testList.get(index).equals("th"))
{
if(!(testList.get(index+1).equals("/th")))
{
new MyException("</th> is in wrong position! or missing",jta,lineNumber);
}
}
if((testList.get(index).equals("/th")))
{
if(!(testList.get(index+1).equals("/tr")||testList.get(index+1).equals("td")||testList.get(index+1).equals("th")))
{
new MyException("</th> is in wrong position! or missing",jta,lineNumber);
}
}
if(testList.get(index).equals("td"))
{
if(!(testList.get(index+1).equals("/td")))
{
new MyException("</td> is in wrong position or missing",jta,lineNumber);
}
}
if(testList.get(index).equals("/tr"))
{
if(!(testList.get(index-1).equals("/td")||testList.get(index-1).equals("/th")||testList.get(index-1).equals("tr")))
{
new MyException("</tr> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("/table"))
{
if(!(testList.get(index-1).equals("/tr")))
{
new MyException("</table> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("p"))
{
if(!(testList.indexOf("p")>testList.indexOf("body")))
{
new MyException("<p> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("/p"))
{
if(!(testList.indexOf("/p")<testList.indexOf("/body")))
{
new MyException("</p> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("br"))
{
if(!(testList.indexOf("br")>testList.indexOf("body")&&testList.indexOf("br")<testList.indexOf("/body")))
{
new MyException("<br> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("pre"))
{
if(!(testList.indexOf("pre")>testList.indexOf("body")))
{
new MyException("<pre> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("/pre"))
{
if(!(testList.indexOf("/pre")<testList.indexOf("/body")))
{
new MyException("</pre> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("form"))
{
if(!(testList.indexOf("form")>testList.indexOf("body")))
{
new MyException("<form> is in wrong position or missing!",jta,lineNumber);
}
}
if(testList.get(index).equals("/form"))
{
if(!(testList.indexOf("/form")<testList.indexOf("/body")))
{
new MyException("</form> is in wrong posititon or missing!",jta,lineNumber);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -