-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHtmlReplacer.java
executable file
·115 lines (100 loc) · 2.91 KB
/
HtmlReplacer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gui;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;
/** çàìåíÿåò â ñòðàíèöå HTML óêàçàííûå òýãè íà ïåðåäåííûå çíà÷åíèÿ */
public class HtmlReplacer {
private Page page;
private Lexer lexer;
private Parser parser;
private StringBuffer result;
public HtmlReplacer(String pathToHtml, String charset, String tagForReplace, ArrayList<String> values) throws UnsupportedEncodingException, FileNotFoundException, ParserException{
page=new Page(new FileInputStream(pathToHtml),charset);
lexer=new Lexer(page);
parser=new Parser(lexer);
Visitor visitor=new Visitor(tagForReplace);
visitor.clearList();
parser.visitAllNodesWith(visitor);
ArrayList<Position> listOfBuffer=visitor.getListOfPosition();
result=new StringBuffer();
int lastPosition=0;
for(int counter=0;counter<listOfBuffer.size();counter++){
page.getText(result,lastPosition,listOfBuffer.get(counter).getBegin());
if(counter<values.size()){
result.append(values.get(counter));
}
lastPosition=listOfBuffer.get(counter).getEnd();
}
page.getText(result,lastPosition,page.getText().length());
}
public void printResultToWriter(PrintStream writer){
writer.print(result);
}
public void printResultToOutputStream(OutputStream out) throws IOException{
(new BufferedWriter(new OutputStreamWriter(out))).write(result.toString());
}
}
class Visitor extends NodeVisitor{
private String markTag;
private ArrayList<Position> listOfPosition=new ArrayList<Position>();
/** íàéòè âñå òýãè ñ óêàçàííûì èìåíåì */
public Visitor(String markTag){
this.markTag=markTag;
}
public void clearList(){
this.listOfPosition.clear();
}
public ArrayList<Position> getListOfPosition(){
return this.listOfPosition;
}
@Override
public void visitTag(Tag tag) {
if(tag.getTagName().equalsIgnoreCase(markTag)){
this.listOfPosition.add(new Position(tag.getStartPosition(),tag.getEndPosition()));
}
}
}
class Position{
private int begin;
private int end;
public Position(int begin, int end){
this.begin=begin;
this.end=end;
}
/**
* @return the begin
*/
public int getBegin() {
return begin;
}
/**
* @param begin the begin to set
*/
public void setBegin(int begin) {
this.begin = begin;
}
/**
* @return the end
*/
public int getEnd() {
return end;
}
/**
* @param end the end to set
*/
public void setEnd(int end) {
this.end = end;
}
}