-
Notifications
You must be signed in to change notification settings - Fork 1
/
Query2.java
160 lines (131 loc) · 3.86 KB
/
Query2.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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;
public class Query2 {
SAXParserFactory parserFactory;
SAXParser parser;
DefaultHandler handler;
private static Query2 UniqueInstance=null;
private static Map<String, Integer> name_count=null; /*!< Detailed description after the member */
/** An enum type.
* The documentation block cannot be put after the enum!
*/
private Query2() {
try {
parserFactory = SAXParserFactory.newInstance();
parser = parserFactory.newSAXParser();
handler = new CustomHandler();
//parser.parse(new File("dblp.xml"), handler);
}
catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
// TODO Auto-generated constructor stub
/** An enum type.
* The documentation block cannot be put after the enum!
*/
private class CustomHandler extends DefaultHandler {
boolean author;
// String tag;
String aname;
public CustomHandler() {
// TODO Auto-generated constructor stub
author = false;
}
public void startElement(String namespaceURI, String localName, String rawName, Attributes atts)
throws SAXException {
aname="";
// if ((atts.getLength()>0) && ((tag = atts.getValue("key"))!=null));
if (rawName.equalsIgnoreCase("author") || rawName.equalsIgnoreCase("editor")) {
author = true;
}
}
public void characters(char ch[], int start, int length) throws SAXException {
String tempdata = "";
tempdata+=new String(ch, start, length);
if(author)
{
aname+=tempdata;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (author) {
if(name_count.keySet().contains(aname))
{
name_count.put(aname, name_count.get(aname)+1);
}
else{
System.out.println("error");
System.exit(0);
}
//System.out.println(tempdata);
author = false;
}
// if (qName.equals("dblp")) {
// // System.out.println("It rocks");
// remove_www_homepage_count();
// }
}
}
private void remove_www_homepage_count() {
for (Author a : Author.ret_persons()) {
a.increment_no_of_publications(-(a.ret_aliases().size()));
}
}
/** An enum type.
* The documentation block cannot be put after the enum!
*/
public static void execute() {
if(UniqueInstance==null||name_count==null)
{
UniqueInstance=new Query2();
name_count=new HashMap<String, Integer>();
for(Author a : Author.ret_persons()) {
for(String s:a.ret_aliases())
{
name_count.put(s, new Integer(0));
}
}
UniqueInstance.calcPublicationCount();
UniqueInstance.remove_www_homepage_count();
}
//dispay_result(n);
}
/** An enum type.
* The documentation block cannot be put after the enum!
*/
private void calcPublicationCount()
{
try {
parser.parse(new File("dblp.xml"), handler);
for(Author a : Author.ret_persons()) {
for(String s:a.ret_aliases())
{
a.increment_no_of_publications(name_count.get(s));
}
}
} catch (SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void dispay_result(int n)
{
for(Author a : Author.ret_persons()) {
int cur_author_publications=a.ret_no_of_publications();
if(cur_author_publications>n)
System.out.println("Author Name: "+a.ret_name()+ " No of Publications: "+cur_author_publications);
}
}
}