-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityResolver.java
123 lines (102 loc) · 2.94 KB
/
EntityResolver.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
116
117
118
119
120
121
122
123
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
//! Entity Resolver
/*!
Used for Resolving different aliases for Author with 2 different name but same author
*/
public class EntityResolver {
private SAXParser parser;
private DefaultHandler handler;
private File dblp_file;
//! Default Constructor
/*!
EntityResolver parsing
*/
public EntityResolver() {
try {
dblp_file = new File("dblp.xml");
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parser = parserFactory.newSAXParser();
handler = new EntityHandler();
parser.parse(dblp_file, handler);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
//! custom handler for parsing
/*! */
private class EntityHandler extends DefaultHandler {
boolean author;
boolean is_person_record;
String temp_authorName;
String key;
Author tempauthor;
public EntityHandler() {
author = false;
is_person_record = false;
tempauthor = null;
}
//parsing functions
@Override
public void startElement(String namespaceURI, String localName, String rawName, Attributes atts)
throws SAXException {
if (rawName.equalsIgnoreCase("www")) {
is_person_record = false;
if ((atts.getLength() > 0) && ((key = atts.getValue("key")) != null)) {
String[] parts = key.split("/");
if ((parts[0]).equals("homepages")) {
// System.out.println(key);
tempauthor = new Author();
is_person_record = true;
temp_authorName = "";
}
}
} else if (is_person_record == true) {
if (rawName.equalsIgnoreCase("author") || rawName.equalsIgnoreCase("editor")) {
author = true;
}
}
}
//parsing functions
@Override
public void characters(char ch[], int start, int length) throws SAXException {
String tempdata = "";
tempdata += new String(ch, start, length);
if (author) {
temp_authorName += tempdata;
}
}
//parsing functions
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (author) {
if (temp_authorName == "") {
System.out.println("some error");
}
tempauthor.add_name(temp_authorName);
temp_authorName = "";
author = false;
}
if (qName.equalsIgnoreCase("www") && (is_person_record == true)) {
if (tempauthor.ret_aliases().size() == 0) {
// cross ref to other homepages
} else {
tempauthor.set_key(key);
Author.add_person(tempauthor);
}
tempauthor = null;
is_person_record = false;
}
}
}
}