-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.java
More file actions
193 lines (168 loc) · 6.54 KB
/
parser.java
File metadata and controls
193 lines (168 loc) · 6.54 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Parser {
private final int maxAuthorsPerPaper = 200;
private class ConfigHandler extends DefaultHandler {
private Locator locator;
private String Value;
private String key;
private String recordTag;
private Person[] persons= new Person[maxAuthorsPerPaper];
private int numberOfPersons = 0;
private boolean insidePerson;
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
/*The startElement recognizes two situations:
if the parser is located at the beginning of an author or
editor, the boolean variable 'insidePerson' is true
if the parser is on another kind of field, this method
just look for 'key'-attributes and stores the key and the
recordTag*/
public void startElement(String namespaceURI, String localName,
String rawName, Attributes atts) throws SAXException {
String k;
if (insidePerson = (rawName.equals("author") || rawName
.equals("editor"))) {
Value = "";
return;
}
if ((atts.getLength()>0) && ((k = atts.getValue("key"))!=null)) {
key = k;
recordTag = rawName;
}
}
/*The EndElement stores the name of an author/editor field
in the temporary array "persons". When we see the end of a
publication record, we copy the information from the 'persons'
array into a new array of the required size and call the
constructor of the Publication class*/
public void endElement(String namespaceURI, String localName,
String rawName) throws SAXException {
if (rawName.equals("author") || rawName.equals("editor")) {
Person p;
if ((p = Person.searchPerson(Value)) == null) {
p = new Person(Value);
}
p.increment();
if (numberOfPersons<maxAuthorsPerPaper)
persons[numberOfPersons++] = p;
return;
}
if (rawName.equals(recordTag)) {
if (numberOfPersons == 0)
return;
Person pa[] = new Person[numberOfPersons];
for (int i=0; i<numberOfPersons; i++) {
pa[i] = persons[i];
persons[i] = null;
}
Publication p = new Publication(key,pa);
numberOfPersons = 0;
}
}
/*this method appends the input text to the Value string*/
public void characters(char[] ch, int start, int length)
throws SAXException {
if (insidePerson)
Value += new String(ch, start, length);
}
private void Message(String mode, SAXParseException exception) {
System.out.println(mode + " Line: " + exception.getLineNumber()
+ " URI: " + exception.getSystemId() + "\n" + " Message: "
+ exception.getMessage());
}
public void warning(SAXParseException exception) throws SAXException {
Message("**Parsing Warning**\n", exception);
throw new SAXException("Warning encountered");
}
public void error(SAXParseException exception) throws SAXException {
Message("**Parsing Error**\n", exception);
throw new SAXException("Error encountered");
}
public void fatalError(SAXParseException exception) throws SAXException {
Message("**Parsing Fatal Error**\n", exception);
throw new SAXException("Fatal Error encountered");
}
}
private void nameLengthStatistics() {
Iterator i = Person.iterator();
Person p;
int l = Person.getMaxNameLength();
int lengthTable[] = new int[l+1];
int j;
System.out.println();
System.out.println("Name length: Number of persons");
while (i.hasNext()) {
p = (Person) i.next();
lengthTable[p.getName().length()]++;
}
for (j=1; j <= l; j++) {
System.out.print(j + ": " + lengthTable[j]+ " ");
if (j%5 == 0)
System.out.println();
}
System.out.println();
}
private void publicationCountStatistics() {
Iterator i = Person.iterator();
Person p;
int l = Person.getMaxPublCount();
int countTable[] = new int[l+1];
int j, n;
System.out.println();
System.out.println("Number of publications: Number of persons");
while (i.hasNext()) {
p = (Person) i.next();
countTable[p.getCount()]++;
}
n = 0;
for (j=1; j <= l; j++) {
if (countTable[j] == 0)
continue;
n++;
System.out.print(j + ": " + countTable[j]+ " ");
if (n%5 == 0)
System.out.println();
}
System.out.println();
}
Parser(String uri) {
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
ConfigHandler handler = new ConfigHandler();
parser.getXMLReader().setFeature(
"http://xml.org/sax/features/validation", true);
parser.parse(new File(uri), handler);
} catch (IOException e) {
System.out.println("Error reading URI: " + e.getMessage());
} catch (SAXException e) {
System.out.println("Error in parsing: " + e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println("Error in XML parser configuration: " +
e.getMessage());
}
System.out.println("Number of Persons : " + Person.numberOfPersons());
nameLengthStatistics();
System.out.println("Number of Publications with authors/editors: " +
Publication.getNumberOfPublications());
System.out.println("Maximum number of authors/editors in a publication: " +
Publication.getMaxNumberOfAuthors());
publicationCountStatistics();
Person.enterPublications();
Person.printCoauthorTable();
Person.printNamePartTable();
Person.findSimilarNames();
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java Parser [input]");
System.exit(0);
}
Parser p = new Parser(args[0]);
}
}