-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.java
153 lines (153 loc) · 4.22 KB
/
Database.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
/**
*@file Database.java
*This file Contains all the Parsing necessities
*@author Abhinav Khattar 2015120
*@author Tushar Arora 2015107
*/
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
import java.io.*;
/**This Class helps extracting relevant data*/
public class Database{
private SAXParser parser;
private Query13 query;
public Database(String filename, Query13 _query){
try{
query = _query;
SAXParserFactory factory = SAXParserFactory.newInstance();
parser = factory.newSAXParser();
Handler handle = new Handler(this);
parser.parse(filename, handle);
}
catch(Exception e){
e.printStackTrace();
}
}
public void check(DataRecords d){
query.check(d);
}
}
/**This class in the custom handler required for Parsing */
class Handler extends DefaultHandler{
private boolean bAuthor = false;
private boolean bTitle = false;
private boolean bPages = false;
private boolean bYear = false;
private boolean bVolume = false;
private boolean bJournal = false;
private boolean bBookTitle = false;
private boolean bURL = false;
private String author;
private String title;
private String pages;
private String year;
private String volume;
private String journal;
private String booktitle;
private String url;
private Database dbase;
public Handler(Database _dbase){
dbase = _dbase;
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
if (qName.equalsIgnoreCase("author") || qName.equalsIgnoreCase("editor")){
bAuthor = true;
}
else if (qName.equalsIgnoreCase("title")){
bTitle = true;
}
else if (qName.equalsIgnoreCase("pages")){
bPages = true;
}
else if (qName.equalsIgnoreCase("year")){
bYear = true;
}
else if (qName.equalsIgnoreCase("volume")){
bVolume = true;
}
else if (qName.equalsIgnoreCase("journal")){
bJournal = true;
}
else if (qName.equalsIgnoreCase("booktitle")){
bBookTitle = true;
}
else if (qName.equalsIgnoreCase("url")){
bURL = true;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException{
if (qName.equalsIgnoreCase("article") || qName.equalsIgnoreCase("inproceedings") || qName.equalsIgnoreCase("proceedings") || qName.equalsIgnoreCase("book") || qName.equalsIgnoreCase("incollection") || qName.equalsIgnoreCase("phdthesis") || qName.equalsIgnoreCase("mastersthesis") || qName.equalsIgnoreCase("www")){
if (author != null)
author = author.substring(0, author.length()-1);
if (title == null || !title.equalsIgnoreCase("Home Page")){
DataRecords d = new DataRecords(author, title, pages, volume, year, journal, booktitle, url);
dbase.check(d);
}
author = title = pages = year = volume = journal = booktitle = url = null;
}
else if (qName.equalsIgnoreCase("author") || qName.equalsIgnoreCase("editor")){
author = author + ",";
bAuthor = false;
}
else if (qName.equalsIgnoreCase("title")){
bTitle = false;
}
else if (qName.equalsIgnoreCase("journal")){
bJournal = false;
}
else if (qName.equalsIgnoreCase("booktitle")){
bBookTitle = false;
}
else if (qName.equalsIgnoreCase("url")){
bURL = false;
}
}
public void characters(char ch[], int start, int length) throws SAXException{
if (bAuthor){
if (author != null){
author = author + new String(ch, start, length);
}
else{
author = (new String(ch, start, length));
}
}
else if (bTitle){
if (title == null)
title = new String(ch, start, length);
else
title += new String(ch, start, length);
}
else if (bPages){
pages = new String(ch, start, length);
bPages = false;
}
else if (bYear){
year = new String(ch, start, length);
bYear = false;
}
else if (bVolume){
volume = new String(ch, start, length);
bVolume = false;
}
else if (bJournal){
if (journal == null)
journal = new String(ch, start, length);
else
journal += new String(ch, start, length);
}
else if (bBookTitle){
if (booktitle == null)
booktitle = new String(ch, start, length);
else
booktitle += new String(ch, start, length);
}
else if (bURL){
if (url == null)
url = new String(ch, start, length);
else
url += new String(ch, start, length);
}
}
}