-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
EMLParserServlet.java
477 lines (420 loc) · 17.6 KB
/
EMLParserServlet.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* '$RCSfile: EMLParserServlet.java,v $'
* Copyright: 1997-2002 Regents of the University of California,
* University of New Mexico, and
* Arizona State University
* Sponsors: National Center for Ecological Analysis and Synthesis and
* Partnership for Interdisciplinary Studies of Coastal Oceans,
* University of California Santa Barbara
* Long-Term Ecological Research Network Office,
* University of New Mexico
* Center for Environmental Studies, Arizona State University
* Other funding: National Science Foundation (see README for details)
* The David and Lucile Packard Foundation
* For Details: http://knb.ecoinformatics.org/
*
* '$Author: tao $'
* '$Date: 2008-10-09 00:00:40 $'
* '$Revision: 1.11 $'
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.ecoinformatics.eml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.xml.sax.SAXException;
import com.oreilly.servlet.multipart.FilePart;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.ParamPart;
import com.oreilly.servlet.multipart.Part;
/**
* Servlet interface for the EMLValidator
*/
public class EMLParserServlet extends HttpServlet {
private ServletConfig servletconfig = null;
private ServletContext context = null;
private HttpServletRequest request;
private static HttpServletResponse response;
private static PrintWriter out = null;
private Hashtable params = new Hashtable();
private static final String NAMESPACEKEYWORD = "xmlns";
/**
* Initialize the servlet
*/
public void init(ServletConfig servletconfig) throws ServletException {
try {
super.init(servletconfig);
this.servletconfig = servletconfig;
this.context = servletconfig.getServletContext();
System.out.println("Starting EMLParserServlet");
} catch (ServletException ex) {
throw ex;
}
}
/**
* Destroy the servlet
*/
public void destroy() {
System.out.println("Destroying EMLParserServlet");
}
/** Handle "GET" method requests from HTTP clients */
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Process the data and send back the response
handleGetOrPost(request, response);
}
/** Handle "POST" method requests from HTTP clients */
public void doPost( HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Process the data and send back the response
handleGetOrPost(request, response);
}
/**
* Control servlet response depending on the action parameter specified
*/
private void handleGetOrPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
this.request = request;
this.response = response;
StringBuffer html = new StringBuffer();
out = response.getWriter();
String ctype = request.getContentType();
InputStream fileToParse = null;
File tempfile = null;
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD html 4.0//EN\">");
html.append("<html>");
html.append("<head>");
html.append("<title>EML Parser</title>");
html.append("</head>");
html.append("<body>");
html.append("<h3>EML Parser</h3>");
HttpSession sess = request.getSession(true);
String sess_id = "";
try {
//get the cookie for the session.
sess_id = (String)sess.getId();
} catch(IllegalStateException ise) {
System.out.println("error in handleGetOrPost: this shouldn't " +
"happen: the session should be valid: " +
ise.getMessage());
}
tempfile = File.createTempFile(".emlparser", ".tmp");
if (ctype != null && ctype.startsWith("multipart/form-data")) {
//deal with multipart encoding of the package zip file
try {
fileToParse = handleGetFile(request, response);
int c = fileToParse.read();
FileOutputStream fos = new FileOutputStream(tempfile);
while(c != -1) {
fos.write(c);
c = fileToParse.read();
}
fos.flush();
fos.close();
} catch(Exception e) {
out.println("<html><body><h1>Error handling multipart data: " +
e.getMessage() + "</h1></body></html>");
System.out.println("Error handling multipart data: " + e.getMessage());
e.printStackTrace();
}
} else {
Enumeration paramlist = request.getParameterNames();
while (paramlist.hasMoreElements()) {
String name = (String)paramlist.nextElement();
Object value = request.getParameterValues(name);
params.put(name,value);
}
}
String action = ((String[])params.get("action"))[0];
if(action.equals("parse")) {
//parse action
html.append(parse(tempfile));
} else if(action.equals("textparse")) {
String doctext = ((String[])params.get("doctext"))[0];
if(doctext == null || doctext.trim().equals("")) {
html.append("<h4>Error. Submitted document is null.</h4>");
} else {
StringReader sr = new StringReader(doctext);
FileWriter fw = new FileWriter(tempfile);
int c = sr.read();
while(c != -1) {
fw.write(c);
c = sr.read();
}
fw.flush();
fw.close();
html.append(parse(tempfile));
}
} else {
html.append("<h4>Error. Action '").append(action);
html.append("' not registered</h4>");
}
tempfile.delete();
html.append("<hr><a href=\"/emlparser\">Back</a> to the previous page.");
html.append("</body></html>");
response.setContentType("text/html");
out.println(html.toString());
out.flush();
}
private String parse(File tempfile) {
StringBuffer html = new StringBuffer();
try {
if(tempfile != null) {
EMLValidator val = new EMLValidator(tempfile.getAbsolutePath());
boolean isValid = val.validate();
if (isValid) {
html.append("<h4>EML specific tests: Passed.</h4><p>The tests which ");
html.append("are specific to EML, including validation that IDs are ");
html.append("present and properly referenced, have passed.</p>");
} else {
html.append("<h4>EML specific tests: Failed.</h4><p>The following errors were found:<ul>");
for(String e : val.getErrors()) {
html.append("<li>" + e + "</e>");
}
html.append("</ul></p>");
}
} else {
html.append("<h4>Error: The file sent to the parser was null.</h4>");
}
} catch(Exception e) {
html.append("<h4>EML specific tests: Failed.</h4><p>The following errors were found:");
html.append("</p><p>").append(e.getMessage()).append("</p>");
}
try {
// TODO: handle UTF-8
Reader xmlReader = new FileReader(tempfile);
String namespaceInDoc = findNamespace(xmlReader);
xmlReader.close();
System.out.println("The namespace in xml is "+namespaceInDoc);
SAXValidate validator = new SAXValidate(true);
validator.runTest(new FileReader(tempfile), "DEFAULT", namespaceInDoc);
html.append("<hr><h4>XML specific tests: Passed.</h4>");
html.append("<p>Document is XML-schema valid. There were no XML errors found in your document.</p>");
} catch(IOException ioe) {
html.append("<hr><h4>IOException: Error reading file</h4>");
html.append("<p>").append(ioe.getMessage()).append("</p>");
} catch(ClassNotFoundException cnfe) {
html.append("<hr><h4>Parser class not found</h4>");
html.append("<p>").append(cnfe.getMessage()).append("</p>");
} catch(SAXException se) {
if(se.getMessage().indexOf("WARNING") != -1) {
html.append("<hr><h4>XML-Schema Warning</h4><p>The following warnings ");
html.append("were issued about your document: </p><p>");
html.append(se.getMessage()).append("</p>");
} else {
html.append("<hr><h4>XML specific tests: Failed</h4><p>");
html.append("The following errors were ");
html.append("found:</p><p>").append(se.getMessage()).append("</p>");
}
}
return html.toString();
}
/**
* This method deals with getting the zip file from the client, unzipping
* it, then running the process on it.
*/
private InputStream handleGetFile(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Hashtable fileList = new Hashtable();
try {
MultipartParser mp = new MultipartParser(request, 1024 * 1024 * 8);
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) {
// it's a parameter part
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue();
String[] s = {value};
params.put(name, s);
} else if (part.isFile()) {
// it's a file part
FilePart filePart = (FilePart) part;
fileList.put(name, filePart);
// Stop once the first file part is found, otherwise going onto the
// next part prevents access to the file contents. So...for upload
// to work, the datafile must be the last part
break;
}
}
} catch (Exception ioe) {
throw ioe;
}
//now that we have the file, do some checking and get the files we need
//out of the zip file.
FilePart fp = (FilePart)fileList.get("filename");
return fp.getInputStream();
}
/**
* Gets namespace from the xml source
*/
public static String findNamespace(Reader xml) throws IOException {
String namespace = null;
if (xml == null) {
//System.out.println("Validation for schema is "+ namespace);
return namespace;
}
String targetLine = getSchemaLine(xml);
if (targetLine != null) {
// find if the root element has prefix
String prefix = getPrefix(targetLine);
//System.out.println("prefix is:" + prefix);
int startIndex = 0;
if(prefix != null) {
// if prefix found then look for xmlns:prefix
// element to find the ns
String namespaceWithPrefix = NAMESPACEKEYWORD
+ ":" + prefix;
startIndex = targetLine.indexOf(namespaceWithPrefix);
//System.out.println("namespaceWithPrefix is:" + namespaceWithPrefix+":");
//System.out.println("startIndex is:" + startIndex);
} else {
// if prefix not found then look for xmlns
// attribute to find the ns
startIndex = targetLine.indexOf(NAMESPACEKEYWORD);
//System.out.println("startIndex is:" + startIndex);
}
int start = 1;
int end = 1;
String namespaceString = null;
int count = 0;
if (startIndex != -1) {
for (int i = startIndex; i < targetLine.length(); i++) {
if (targetLine.charAt(i) == '"') {
count++;
}
if (targetLine.charAt(i) == '"' && count == 1) {
start = i;
}
if (targetLine.charAt(i) == '"' && count == 2) {
end = i;
break;
}
}
}
// else: xmlns not found. namespace = null will be returned
//System.out.println("targetLine is " + targetLine);
//System.out.println("start is " + end);
//System.out.println("end is " + end);
if(start < end) {
namespaceString = targetLine.substring(start + 1, end);
//System.out.println("namespaceString is " + namespaceString);
}
namespace = namespaceString;
}
System.out.println("Validation for eml is " + namespace);
return namespace;
}
/*
* Gets the string which contains schema declaration info
*/
private static String getSchemaLine(Reader xml) throws IOException {
// find the line
String secondLine = null;
int count = 0;
int endIndex = 0;
int startIndex = 0;
final int TARGETNUM = 1;
StringBuffer buffer = new StringBuffer();
boolean comment = false;
boolean processingInstruction = false;
char thirdPreviousCharacter = '?';
char secondPreviousCharacter = '?';
char previousCharacter = '?';
char currentCharacter = '?';
int tmp = xml.read();
while (tmp != -1) {
currentCharacter = (char)tmp;
//in a comment
if (currentCharacter == '-' && previousCharacter == '-'
&& secondPreviousCharacter == '!'
&& thirdPreviousCharacter == '<') {
comment = true;
}
//out of comment
if (comment && currentCharacter == '>' && previousCharacter == '-'
&& secondPreviousCharacter == '-') {
comment = false;
}
//in a processingInstruction
if (currentCharacter == '?' && previousCharacter == '<') {
processingInstruction = true;
}
//out of processingInstruction
if (processingInstruction && currentCharacter == '>'
&& previousCharacter == '?') {
processingInstruction = false;
}
//this is not comment or a processingInstruction
if (currentCharacter != '!' && previousCharacter == '<'
&& !comment && !processingInstruction) {
count++;
}
// get target line
if (count == TARGETNUM && currentCharacter != '>') {
buffer.append(currentCharacter);
}
if (count == TARGETNUM && currentCharacter == '>') {
break;
}
thirdPreviousCharacter = secondPreviousCharacter;
secondPreviousCharacter = previousCharacter;
previousCharacter = currentCharacter;
tmp = xml.read();
}
secondLine = buffer.toString();
//System.out.println("the second line string is: " + secondLine);
//xml.reset();
return secondLine;
}
/*
* Gets the prefix of this eml document. E.g eml for eml:eml
*/
private static String getPrefix(String schemaLine) {
String prefix = null;
if(schemaLine.indexOf(" ") > 0) {
String rootElement = "";
try {
rootElement = schemaLine.substring(0, schemaLine.indexOf(" "));
} catch (StringIndexOutOfBoundsException sioobe) {
rootElement = schemaLine;
}
//System.out.println("rootElement:" + rootElement);
if(rootElement.indexOf(":") > 0) {
prefix = rootElement.substring(0, rootElement.indexOf(":"));
}
if(prefix != null) {
return prefix.trim();
}
}
return null;
}
}