-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_Body.java
65 lines (58 loc) · 1.58 KB
/
Get_Body.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
//Filter for retrieving body html tag + removing inner tags
package udf_KW;
import java.io.IOException;
import java.util.Arrays;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Get_Body extends EvalFunc<String> {
@Override
public String exec(Tuple input) throws IOException
{
String output = "";
if(input != null && !input.isNull())
{
String _html = (String)input.get(0);
if(_html != null && !_html.isEmpty())
{
output = _html;
int start_index = _html.indexOf("<body");
start_index += 5;
int stop_index = _html.indexOf("</body");
if(start_index >= 0 && start_index < _html.length() && stop_index >= start_index && stop_index < _html.length())
output = _html.substring(start_index,stop_index);
Pattern REMOVE_TAGS = Pattern.compile("<.+?>");
Matcher m = REMOVE_TAGS.matcher(output);
output = m.replaceAll("");
REMOVE_TAGS = Pattern.compile("<!--(.+?)-->");
m = REMOVE_TAGS.matcher(output);
output = m.replaceAll("");
/* boolean intag = false;
String inp = output;
String outp = "";
for (int i=0; i < inp.length(); ++i)
{
if (!intag && inp.charAt(i) == '<')
{
intag = true;
continue;
}
if (intag && inp.charAt(i) == '>')
{
intag = false;
continue;
}
if (!intag)
{
outp = outp + inp.charAt(i);
}
}
output = outp;
// output = output.replaceAll("\\s+","");
*/
}
}
return output;
}
}