Annotation of java/classes/org/w3c/rdf/examples/ARPServlet.java, revision 1.55
1.16 barstow 1: /***********************************************************************
2: *
3: * ARPServlet - this servlet implements an RDF Validation service. As
4: * of this writing, the following RDF validation service used this
5: * servlet:
6: *
7: * http://www.w3.org/RDF/Validator/
8: *
9: ***********************************************************************
1.1 barstow 10: *
11: * Copyright © World Wide Web Consortium, (Massachusetts Institute of
12: * Technology, Institut National de Recherche en Informatique et en
13: * Automatique, Keio University).
14: *
15: * All Rights Reserved.
16: *
17: * Please see the full Copyright clause at
18: * <http://www.w3.org/Consortium/Legal/copyright-software.html>
19: *
1.16 barstow 20: ***********************************************************************
21: *
22: * This servlet is a wrapper for the ARP RDF parser. See the following
23: * for information about the ARP RDF parser:
24: *
25: * http://www.hpl.hp.co.uk/people/jjc/arp/
26: *
27: ***********************************************************************
28: *
29: * Implementation notes:
30: *
31: * o This servlet supports the HTTP POST operation; it does not
32: * support the HTTP GET operation
33: *
34: * o Depending upon the parameters given to the servlet it may
35: * invoke a GraphViz suprocess to generate a graph of the RDF.
36: * See the following for more information about GraphViz:
1.1 barstow 37: *
1.16 barstow 38: * http://www.research.att.com/sw/tools/graphviz/
1.1 barstow 39: *
1.16 barstow 40: * The servlet assumes version 1.7.4 of GraphViz.
1.1 barstow 41: *
1.16 barstow 42: * o Depending upon the parameters given to the servlet, the RDF
43: * to be validated may be copied to a file. The name of the file
44: * is automatically generated via Java's temporary file APIs. The
45: * location of the directory where the file is stored is configured
46: * via the serverlet's init() method. See below for more information.
47: *
48: * o See the section on Server Initialization for more information.
49: *
50: ***********************************************************************
51: *
52: * HTTP POST parameters - the servlet expects/assumes the following
53: * variables are defined via the HTTP POST request:
54: *
55: * RDF - the RDF (assumed to be in RDF/XML syntax) to be validated
56: *
57: * SAVE_DOT_FILE - if "on", the GraphViz DOT file is saved and a
58: * link to the file is returned; otherwise the DOT file is not saved
59: *
60: * SAVE_RDF - if "on", the RDF will be copied to a file; otherwise
61: * the RDF is not copied to a file
1.1 barstow 62: *
1.10 barstow 63: * EMBEDDED_RDF - if "on", then the RDF is not enclosed in <RDF>...</RDF>
1.30 duerst 64: * tags; otherwise it assumed that the RDF is enclosed in these tags.
1.10 barstow 65: *
1.55 ! duerst 66: * URI - the URI of the RDF to validate
! 67: *
! 68: * PARSE - if "Parse RDF", then parse RDF from the textarea;
! 69: * if "Parse URI: " then parse the RDF at the URI.
1.1 barstow 70: *
71: * ORIENTATION - the graph's orientation (left to right or top to
1.16 barstow 72: * bottom); default is left to right
1.1 barstow 73: *
1.16 barstow 74: * FONT_SIZE - the font size to use (10, 12, 14, 16 and 20 are
75: * supported); the default is 10
1.1 barstow 76: *
1.16 barstow 77: * ANON_NODES_EMPTY - if "on", anonymous nodes are not labeled; otherwise
78: * anonymous nodes are labeled;
1.12 barstow 79: *
80: * TRIPLES_AND_GRAPH - support values are:
81: *
1.16 barstow 82: * PRINT_BOTH - display triples and a graph (the default)
83: * PRINT_TRIPLES - only display the triples
84: * PRINT_GRAPH - only display the graph
1.12 barstow 85: *
1.1 barstow 86: * GRAPH_FORMAT - the graph's output format. Supported values are:
87: *
1.16 barstow 88: * GIF_EMBED - embed the graph as a GIF (the default)
1.1 barstow 89: * GIF_LINK - don't embed the GIF but create a link for it
90: * SVG_LINK - create the graph in SVG format and create a link to the file
91: * PNG_EMBED - create the graph in PNG format and embed the graph in the
92: * document that is returned
93: * PNG_LINK - create the graph in PNG format and create a link to the file
94: * PS_LINK - create a PostScript image of the file and a link to the file
95: * HP_PCL_LINK - create a HPGL/2 - PCL (Laserwriter) image of the file
96: * and a link to the file
97: * HP_GL_LINK - create a HPGL - PCL (pen plotter) image of the file and
98: * a link to the file
99: *
100: * NTRIPLES if "on" the tabular output will be in the NTriples format;
101: * otherwise a table of Subject, Predicate, Objects will be generated
102: *
1.16 barstow 103: ***********************************************************************
104: *
105: * Server Initialization - this servlet requires the following
106: * parameters be set in the servlet's init() method - via the
107: * ServletConfig object:
1.1 barstow 108: *
1.16 barstow 109: * GRAPH_VIZ_ROOT - the absolute path of the top-level directory containing
110: * GraphViz's binary distribution
111: *
112: * GRAPH_VIZ_PATH - the relative path (based on GRAPH_VIZ_ROOT) of
113: * the DOT executable (e.g. dotneato/dot) - the program used to generate
114: * a graph from a DOT file.
115: *
116: * GRAPH_VIZ_FONT_DIR - the relative path (based on GRAPH_VIZ_ROOT) of
117: * the fonts directory used by GraphViz (e.g. Fonts)
118: *
119: * SERVLET_TMP_DIR - the absolute path of the directory to be used to
120: * store temporary files used by the servlet and GraphViz. This
121: * directory must be writable by the servlet.
122: *
123: * NOTE - Some files created by the servlet are not removed by
124: * servlet (e.g. graph image files).
125: *
126: * If any of these parameters are not defined, the servlet will NOT
127: * validate the RDF.
128: *
129: ***********************************************************************
130: *
131: * Dependencies - this servlet requires the following Java packages
132: * as well as GraphViz (described above):
133: *
134: * ARP RDF parser: http://www.hpl.hp.co.uk/people/jjc/arp/download.html
135: *
136: * SAX-based XML parser: e.g. Xerces at http://xml.apache.org/
137: *
138: * Java servlet package: http://java.sun.com/products/servlet/archive.html
139: *
140: * Apache Regular Expression: http://jakarta.apache.org/builds/jakarta-regexp/release/v1.2/
141: *
142: ***********************************************************************
143: *
144: * Author: Art Barstow <barstow@w3.org>
1.30 duerst 145: * Author (internationalization): Martin J. Duerst <duerst@w3.org>
1.16 barstow 146: *
1.55 ! duerst 147: * $Id: ARPServlet.java,v 1.54 2002/08/08 06:23:22 duerst Exp $
1.16 barstow 148: *
149: ***********************************************************************/
1.1 barstow 150:
1.16 barstow 151: // http://dev.w3.org/cvsweb/java/classes/org/w3c/rdf/examples/
152: package org.w3c.rdf.examples;
1.1 barstow 153:
154: import java.io.*;
155: import java.net.MalformedURLException;
156: import java.net.URL;
1.33 duerst 157: import java.net.URLConnection;
1.1 barstow 158: import java.util.StringTokenizer;
159: import java.util.Enumeration;
1.14 barstow 160: import java.util.Hashtable;
1.16 barstow 161:
162: // http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/package-summary.html
163: import javax.servlet.*;
1.1 barstow 164: import javax.servlet.http.*;
1.32 duerst 165: import javax.mail.internet.ContentType;
1.1 barstow 166:
1.16 barstow 167: // http://xml.apache.org/apiDocs/org/xml/sax/package-summary.html
168: import org.xml.sax.InputSource;
1.1 barstow 169: import org.xml.sax.Parser;
170: import org.xml.sax.SAXException;
171: import org.xml.sax.SAXParseException;
172: import org.xml.sax.ErrorHandler;
173: import org.xml.sax.helpers.*;
174:
1.16 barstow 175: // http://jakarta.apache.org/regexp/apidocs/org/apache/regexp/RE.html
1.3 barstow 176: import org.apache.regexp.RE;
1.53 duerst 177: import org.apache.regexp.RESyntaxException;
1.3 barstow 178:
1.16 barstow 179: // http://www.hpl.hp.co.uk/people/jjc/arp/apidocs/index.html
180: import com.hp.hpl.jena.rdf.arp.*;
1.1 barstow 181:
182: public class ARPServlet extends HttpServlet
183: {
1.55 ! duerst 184: final static public String REVISION = "$Id: ARPServlet.java,v 1.54 2002/08/08 06:23:22 duerst Exp $";
1.1 barstow 185:
186: // The email address for bug reports
1.6 barstow 187: private static final String MAIL_TO = "www-rdf-validator@w3.org";
1.1 barstow 188:
189: // Names of the POST parameters (described above) and their
1.12 barstow 190: // defaults (if applicable)
191: private static final String TEXT = "RDF";
192: private static final String SAVE_DOT_FILE = "SAVE_DOT_FILE";
193: private static final String SAVE_RDF = "SAVE_RDF";
194: private static final String EMBEDDED_RDF = "EMBEDDED_RDF";
195: private static final String URI = "URI";
1.55 ! duerst 196: private static final String PARSE = "PARSE";
1.12 barstow 197: private static final String NTRIPLES = "NTRIPLES";
198: private static final String ANON_NODES_EMPTY = "ANON_NODES_EMPTY";
1.1 barstow 199:
200: private static final String NODE_COLOR = "NODE_COLOR";
201: private static final String DEFAULT_NODE_COLOR = "black";
202:
203: private static final String NODE_TEXT_COLOR = "NODE_TEXT_COLOR";
204: private static final String DEFAULT_NODE_TEXT_COLOR = "black";
205:
206: private static final String EDGE_COLOR = "EDGE_COLOR";
207: private static final String DEFAULT_EDGE_COLOR = "black";
208:
209: private static final String EDGE_TEXT_COLOR = "EDGE_TEXT_COLOR";
210: private static final String DEFAULT_EDGE_TEXT_COLOR = "black";
211:
212: private static final String ORIENTATION = "ORIENTATION";
213: private static final String DEFAULT_ORIENTATION = "TB"; // Top to Bottom
214:
215: private static final String FONT_SIZE = "FONT_SIZE";
216: private static final String DEFAULT_FONT_SIZE = "10";
217:
1.12 barstow 218: // Print graph and/or triples
219: private static final String TRIPLES_AND_GRAPH = "TRIPLES_AND_GRAPH";
220: private static final String PRINT_BOTH = "PRINT_BOTH";
221: private static final String PRINT_TRIPLES = "PRINT_TRIPLES";
222: private static final String PRINT_GRAPH = "PRINT_GRAPH";
223:
224: // Graph formats
1.1 barstow 225: private static final String FORMAT = "FORMAT";
226: private static final String FORMAT_GIF_EMBED = "GIF_EMBED";
227: private static final String FORMAT_GIF_LINK = "GIF_LINK";
228: private static final String FORMAT_SVG_LINK = "SVG_LINK";
229: private static final String FORMAT_PNG_EMBED = "PNG_EMBED";
230: private static final String FORMAT_PNG_LINK = "PNG_LINK";
231: private static final String FORMAT_PS_LINK = "PS_LINK";
232: private static final String FORMAT_HP_PCL_LINK = "HP_PCL_LINK";
233: private static final String FORMAT_HP_GL_LINK = "HP_GL_LINK";
234:
235: // Fonts are not currently configurable
236: private static final String DEFAULT_FONT = "arial";
237:
238: // Names of the servlet's parameters - for Jigsaw web server
239: private static final String SERVLET_TMP_DIR = "SERVLET_TMP_DIR";
240: private static final String GRAPH_VIZ_ROOT = "GRAPH_VIZ_ROOT";
241: private static final String GRAPH_VIZ_PATH = "GRAPH_VIZ_PATH";
242: private static final String GRAPH_VIZ_FONT_DIR = "GRAPH_VIZ_FONT_DIR";
243:
244: // Variables for the servlet's parameters
245: private static String m_ServletTmpDir = null;
246: private static String m_GraphVizPath = null;
247: private static String m_GraphVizFontDir = null;
248:
1.30 duerst 249: // Names of environment variable needed by GraphVis
1.1 barstow 250: private static String DOTFONTPATH = "DOTFONTPATH";
251: private static String LD_LIBRARY_PATH = "LD_LIBRARY_PATH";
252:
253: // Names used for temporary files
254: private static final String TMP_FILE_PREFIX = "servlet_";
255: private static final String SUFFIX_TMP_DIR = ".tmp";
256: private static final String SUFFIX_DOT = ".dot";
257: private static final String SUFFIX_RDF = ".rdf";
258:
259: // Names used for file suffixes and for GraphViz's command line
260: // option
261: private static final String NAME_GIF = "gif";
262: private static final String NAME_HPGL = "hpgl";
263: private static final String NAME_PCL = "pcl";
264: private static final String NAME_PNG = "png";
265: private static final String NAME_PS = "ps";
266: private static final String NAME_SVG = "svg";
267:
268: // Default GraphViz parameter names and their default values
269: // Servlet name
270: private static final String SERVLET_NAME = "ARPServlet";
271:
272: // Name for the DOT file title
273: private static final String DOT_TITLE = "dotfile";
274:
1.30 duerst 275: // The string to use to prefix anonymous nodes.
1.14 barstow 276: private static final String ANON_NODE = "genid:";
277:
1.1 barstow 278: // The string to use for a namespace name when no
279: // namespace is available - e.g. for the RDF that is
280: // directly entered into the input form.
1.14 barstow 281: private static final String DEFAULT_NAMESPACE = "online:";
1.1 barstow 282:
1.53 duerst 283: // exception used by getRDFfromURI
284: private class getRDFException extends Exception {
285: public getRDFException (String s) {
286: super (s);
287: }
288: }
289:
1.1 barstow 290: /*
1.14 barstow 291: * Create a File object from the given directory and file names
1.1 barstow 292: *
293: *@param directory the file's directory
294: *@param prefix the file's prefix name (not its directory)
295: *@param suffix the file's suffix or extension name
296: *@return a File object if a temporary file is created; null otherwise
297: */
1.4 barstow 298: private File createTempFile (String directory, String prefix, String suffix)
299: {
1.1 barstow 300: File f;
301: try {
302: File d = new File(directory);
303: f = File.createTempFile(prefix, suffix, d);
304: } catch (Exception e) {
305: return null;
306: }
307: return f;
308: }
309:
310: /*
311: * Given a URI string, open it, read its contents into a String
312: * and return the String
313: *
314: *@param uri the URI to open
315: *@return the content at the URI or null if any error occurs
316: */
1.53 duerst 317: private String getRDFfromURI (String uri) throws getRDFException
1.4 barstow 318: {
1.53 duerst 319: /* add something like this code here, to allow reading from a file:
320: (if we really want to allow this!)
321: File ff = new File(uri);
322: in = new FileInputStream(ff);
323: */
324: URL url = null;
325: try {
326: url = new URL(uri);
327: } catch (MalformedURLException e) {
328: throw new getRDFException("Malformed URI.");
329: }
330:
331: URLConnection con = null;
332: try {
333: con = url.openConnection();
334: con.setRequestProperty("Accept", "application/rdf+xml");
335: con.connect();
336: } catch (Exception e) {
337: throw new getRDFException("Unable to open connection.");
338: }
339: String contentT = con.getContentType();
340: String HTTPcharset = null;
341: if (contentT != null) {
342: ContentType contentType = null;
343: try {
344: contentType = new ContentType(con.getContentType());
345: } catch (javax.mail.internet.ParseException e) {
346: throw new getRDFException("Unparsable content type.");
1.32 duerst 347: }
1.53 duerst 348: HTTPcharset = contentType.getParameter("charset");
349: }
350:
351: // need buffer for lookahead for encoding detection
352: BufferedInputStream bis = null;
353: try {
354: bis = new BufferedInputStream(con.getInputStream());
355: } catch (IOException e) {
356: throw new getRDFException("Cannot open stream.");
357: }
358: bis.mark(200); // mark start so that we can get back to it
359: String s = "";
360:
1.54 duerst 361: try { // read start of file as bytes
362: int c;
363: int numRead = 0;
1.53 duerst 364: while ((c = bis.read()) != -1) {
365: s += (char)c;
1.54 duerst 366: if (numRead++ >= 195) break;
1.53 duerst 367: }
368: } catch (IOException e) {
369: throw new getRDFException("IOException while starting reading.");
370: }
371:
372: if (s.equals(""))
373: // Nothing was returned
374: throw new getRDFException("Empty document, ignored.");
375:
376: // A server could return content but not the RDF/XML that
377: // we need. Check the beginning of s and if it looks like
378: // a generic HTML message, return an error.
379: if (s.startsWith("<!DOCTYPE"))
380: throw new getRDFException("Document looks like HTML, ignored.");
1.54 duerst 381:
382: String APPFcharset = null; // 'charset' according to XML APP. F
383: int ignoreBytes = 0;
384: if (s.startsWith("\u00FE\u00FF")) {
385: APPFcharset = "UTF-16BE";
386: ignoreBytes = 2;
387: }
388: else if (s.startsWith("\u00FF\u00FE")) {
389: APPFcharset = "UTF-16LE";
390: ignoreBytes = 2;
391: }
392: else if (s.startsWith("\u00EF\u00BB\u00BF")) {
393: APPFcharset = "UTF-8";
394: ignoreBytes = 3;
395: }
396: else if (s.startsWith("\u0000<\u0000?")) {
397: APPFcharset = "UTF-16BE";
398: }
399: else if (s.startsWith("<\u0000?\u0000")) {
400: APPFcharset = "UTF-16LE";
401: }
402: else if (s.startsWith("<?xml")) {
403: APPFcharset = "US-ASCII";
404: }
405: else if (s.startsWith("\u004C\u006F\u00A7\u0094")) {
406: APPFcharset = "CP037"; // EBCDIC
407: }
408:
409: // convert start of xml input according to APPFcharset
410: String xmlstart = null;
411: try {
412: xmlstart = new String(s.substring(ignoreBytes).getBytes("iso-8859-1"), APPFcharset);
413: } catch (UnsupportedEncodingException e) {
414: throw new getRDFException("Unsupported encoding '"+APPFcharset+"'.");
415: }
416: RE r;
417: try {
418: r = new RE("<\\?xml[ \\t\\n\\r]+version[ \\t\\n\\r]?=[ \\t\\n\\r]?(['\"])([a-zA-Z0-9_:]|\\.|-)+\\1[ \\t\\n\\r]+encoding[ \\t\\n\\r]?=[ \\t\\n\\r]?(['\"])([A-Za-z]([A-Za-z0-9._]|-)*)\\3");
419: } catch (RESyntaxException res) {
420: throw new getRDFException("Wrong regular expression syntax.");
421: }
422: // r.setMatchFlags(MATCH_NORMAL | MATCH_SINGLELINE);
423: String XMLcharset = null;
424: if (r.match(xmlstart) && r.getParenStart(0)==0)
425: XMLcharset = r.getParen(4);
426: if (HTTPcharset != null)
427: HTTPcharset = HTTPcharset.toUpperCase();
428: if (XMLcharset != null)
429: XMLcharset = XMLcharset.toUpperCase();
430:
431: String finalCharset = null;
432: if (HTTPcharset != null) {
433: if (XMLcharset != null && !HTTPcharset.equals(XMLcharset))
434: throw new getRDFException("Charset conflict: Content-Type: "
435: + contentT+ ". XML encoding: " + XMLcharset + ".");
436: finalCharset = HTTPcharset;
437: }
438: else if (XMLcharset != null)
439: finalCharset = XMLcharset;
440: if ((finalCharset != null && finalCharset.equals("UTF-16")) ||
441: (finalCharset == null && APPFcharset.startsWith("UTF-16")))
442: if (ignoreBytes == 2)
443: finalCharset = APPFcharset; // use correct endianness
444: else
445: throw new getRDFException("Illegal XML: UTF-16 without BOM.");
446: if (finalCharset == null)
447: finalCharset = "UTF-8";
448:
449: try {
450: bis.reset(); // move back to start of stream
451: bis.skip(ignoreBytes); // skip BOM
452: } catch (IOException e) {
453: throw new getRDFException("IOException while resetting stream.");
454: }
455:
456: InputStreamReader isr = null;
457: try {
458: isr = new InputStreamReader(bis, finalCharset);
459: } catch (UnsupportedEncodingException e) {
460: throw new getRDFException("Unsupported encoding '"+finalCharset+"'.");
461: }
462:
463: StringBuffer sb = new StringBuffer("");
464: int charnum = 0;
465: try { // read whole file as characters
466: int c;
467: while ((c = isr.read()) != -1) {
468: sb.append((char)c);
469: charnum++;
470: }
471: } catch (IOException e) {
472: throw new getRDFException("IOException while reading URI at character "
473: + charnum + " using encoding " + XMLcharset + ".");
474: }
475:
476: // todo: fix encoding parameter in xml pseudo-PI
477:
478: return sb.toString();
1.1 barstow 479: }
480:
481: /*
1.4 barstow 482: * Copy the given string of RDF to a file in the given directory.
483: * This is only done if the servlet is explictly asked to save
484: * the RDF to a file.
1.1 barstow 485: *
1.14 barstow 486: *@param tmpDir the file's directory
1.1 barstow 487: *@param rdf the string of RDF
488: */
489: private void copyRDFStringToFile(String tmpDir, String rdf)
490: {
491: try {
492: // Generate a unique file name
493: File tmpFile = createTempFile(tmpDir, TMP_FILE_PREFIX, SUFFIX_RDF);
494: if (tmpFile == null) {
495: // Not really a critical error, just return
496: return;
497: }
498:
499: // Create a PrintWriter for the GraphViz consumer
500: FileWriter fw = new FileWriter(tmpFile);
501: PrintWriter pw = new PrintWriter(fw);
502:
503: pw.println(rdf);
504: pw.close();
505: } catch (Exception e) {
1.4 barstow 506: System.err.println(SERVLET_NAME + ": error occured trying to save RDF to file '" + tmpDir + TMP_FILE_PREFIX + SUFFIX_RDF + "'.");
1.1 barstow 507: return;
508: }
509: }
510:
511: /*
512: * Given the graph's format option, return either the corresponding
513: * command line option for that option or the file name suffix for
514: * the graph option. For example GIF files have ".gif" for its
515: * suffix and GraphViz uses "-Tgif" for the command line.
516: *
517: * NOTE: default is GIF.
518: *
519: *@param graphFormat the graph's output format
520: *@param suffix. If true, the name returned is for the graph's
521: * file name suffix; otherwise, the name returned is for the
522: * graph's command line option.
523: *@return the suffix to use for the graph's output file
524: */
525: private String getFormatName(String graphFormat, boolean suffix) {
526:
527: String name = (suffix) ? "." : "-T";
528:
1.4 barstow 529: if (graphFormat.equals(FORMAT_PNG_EMBED)) return name + NAME_PNG;
530: if (graphFormat.equals(FORMAT_PNG_LINK)) return name + NAME_PNG;
531: if (graphFormat.equals(FORMAT_SVG_LINK)) return name + NAME_SVG;
532: if (graphFormat.equals(FORMAT_PS_LINK)) return name + NAME_PS;
533: if (graphFormat.equals(FORMAT_HP_GL_LINK)) return name + NAME_HPGL;
1.1 barstow 534: if (graphFormat.equals(FORMAT_HP_PCL_LINK)) return name + NAME_PCL;
535:
536: return name + NAME_GIF;
537: }
538:
539: /*
540: * Invokes the GraphVis program to create a graph image from the
541: * the given DOT data file
542: *
543: *@param dotFileName the name of the DOT data file
544: *@param outputFileName the name of the output data file
1.14 barstow 545: *@param graphFormat the graph's format
1.1 barstow 546: *@return true if success; false if any failure occurs
547: */
1.8 barstow 548: private boolean generateGraphFile(String dotFileName,
1.4 barstow 549: String outputFileName, String graphFormat)
550: {
1.16 barstow 551: String environment[] = {DOTFONTPATH + "=" + m_GraphVizFontDir};
1.1 barstow 552:
553: String formatOption = getFormatName(graphFormat, false);
554:
555: String cmdArray[] = {m_GraphVizPath, formatOption, "-o", outputFileName, dotFileName};
556: Runtime rt = Runtime.getRuntime();
557: try {
558: Process p = rt.exec(cmdArray, environment);
559: p.waitFor();
1.10 barstow 560:
1.1 barstow 561: } catch (Exception e) {
562: System.err.println("Error: generating OutputFile.");
563: return false;
564: }
565: return true;
566: }
567:
568: /*
569: * Returns a parameter from a request or the parameter's default
570: * value.
571: *
572: *@param req a Servlet request
1.14 barstow 573: *@param param the name of the parameter
574: *@param defString the string returned if the param is not found
1.1 barstow 575: *@return if the request contains the specfied parameter its value
576: * in the request is returned; otherwise its default value is
577: * returned
578: */
579: private String getParameter(HttpServletRequest req, String param,
580: String defString)
581: {
582: String s = req.getParameter(param);
583: return (s == null) ? defString : s;
584: }
585:
586: /*
587: * If the request contains any graph-related parameters, pass them
588: * to the graph consumer for handling
589: *
590: *@param req the response
1.14 barstow 591: *@param pw the PrintWriter
1.1 barstow 592: *@param consumer the GraphViz consumer
593: */
1.14 barstow 594: private void processGraphParameters (HttpServletRequest req, PrintWriter pw)
1.1 barstow 595: {
1.4 barstow 596: // Print the graph header
597: pw.println("digraph " + DOT_TITLE + "{ " );
1.1 barstow 598:
599: // Look for colors
1.4 barstow 600: String nodeColor = getParameter(req, NODE_COLOR,
601: DEFAULT_NODE_COLOR);
602: String nodeTextColor = getParameter(req, NODE_TEXT_COLOR,
603: DEFAULT_NODE_TEXT_COLOR);
604: String edgeColor = getParameter(req, EDGE_COLOR,
605: DEFAULT_EDGE_COLOR);
606: String edgeTextColor = getParameter(req, EDGE_TEXT_COLOR,
607: DEFAULT_EDGE_TEXT_COLOR);
608: String fontSize = getParameter(req, FONT_SIZE,
609: DEFAULT_FONT_SIZE);
1.1 barstow 610:
611: // Orientation must be either
612: String orientation = req.getParameter (ORIENTATION);
613: if (orientation.equals("LR"))
614: orientation = "LR";
615: else
616: orientation = DEFAULT_ORIENTATION;
617:
618: // Add an attribute for all of the graph's nodes
619: pw.println("node [fontname=" + DEFAULT_FONT +
1.7 barstow 620: ",fontsize=" + fontSize +
621: ",color=" + nodeColor +
1.12 barstow 622: ",fontcolor=" + nodeTextColor + "];");
1.1 barstow 623:
624: // Add an attribute for all of the graph's edges
625: pw.println("edge [fontname=" + DEFAULT_FONT +
1.8 barstow 626: ",fontsize=" + fontSize +
627: ",color=" + edgeColor +
628: ",fontcolor=" + edgeTextColor + "];");
1.1 barstow 629:
630: // Add an attribute for the orientation
631: pw.println("rankdir=" + orientation + ";");
632: }
633:
634: private static class SaxErrorHandler implements org.xml.sax.ErrorHandler
635: {
1.22 duerst 636: PrintWriter out;
1.1 barstow 637: boolean silent = false;
1.5 barstow 638: String fatalErrors = "";
639: String errors = "";
640: String warnings = "";
1.1 barstow 641:
642: /*
643: * Constructuor for a SaxErrorHandler
644: *
1.22 duerst 645: *@param out the servlet's PrintWriter
1.1 barstow 646: *@param silent if false, output is suprressed
647: */
1.22 duerst 648: public SaxErrorHandler(PrintWriter out, boolean silent)
1.1 barstow 649: {
650: this.out = out;
651: this.silent = silent;
652: }
653:
654: /*
655: * Create a formatted string from the exception's message
656: *
1.5 barstow 657: *@param e the SAX Parse Exception
1.1 barstow 658: *@return a formatted string
659: */
660: private static String format(org.xml.sax.SAXParseException e)
661: {
662: String msg = e.getMessage();
663: if (msg == null)
664: msg = e.toString();
665: return msg + "[Line = " + e.getLineNumber() + ", Column = " + e.getColumnNumber() + "]";
666: }
667:
668: /*
669: * Handle a parse error
670: *
1.5 barstow 671: *@param e the SAX Parse Exception
1.1 barstow 672: */
673: public void error(org.xml.sax.SAXParseException e)
674: throws org.xml.sax.SAXException
675: {
676: if (this.silent) return;
677:
1.5 barstow 678: this.errors += "Error: " + format(e) + "<br />";
1.1 barstow 679: }
680:
681: /*
682: * Handle a fatal parse error
683: *
1.5 barstow 684: *@param e the SAX Parse Exception
1.1 barstow 685: */
686: public void fatalError(org.xml.sax.SAXParseException e)
687: throws org.xml.sax.SAXException
688: {
689: if (this.silent) return;
690:
1.5 barstow 691: this.fatalErrors += "FatalError: " + format(e) + "<br />";
1.1 barstow 692: }
693:
694: /*
695: * Handle a parse warning
696: *
1.5 barstow 697: *@param e the SAX Parse Exception
1.1 barstow 698: */
699: public void warning(org.xml.sax.SAXParseException e)
700: throws org.xml.sax.SAXException
701: {
702: if (this.silent) return;
703:
1.5 barstow 704: this.warnings += "Warning: " + format(e) + "<br />";
1.1 barstow 705: }
1.5 barstow 706:
707: /*
708: * Return the error messages
709: *
710: *@return the error messages or an empty string if there are
711: * no messages
712: */
713: public String getErrors()
714: {
715: return this.errors;
716: }
717:
718: /*
719: * Return the fatal error messages
720: *
721: *@return the fatal error messages or an empty string if there are
722: * no messages
723: */
724: public String getFatalErrors()
725: {
726: return this.fatalErrors;
727: }
728:
729: /*
730: * Return the warning messages
731: *
732: *@return the warning messages or an empty string if there are
733: * no messages
734: */
735: public String getWarnings()
736: {
737: return this.warnings;
738: }
1.1 barstow 739: }
740:
741: /*
742: * Generate a graph of the RDF data model
743: *
744: *@param out the servlet's output stream
1.4 barstow 745: *@param pw the graph file's PrintWriter
746: *@param dotFile the File handle for the graph file
1.1 barstow 747: *@param rdf the RDF text
748: *@param req a Servlet request
749: *@param graphFormat the graph's format
750: *@param saveRDF the RDF can be cached [saved to the file system]
751: *@param saveDOTFile the DOT file should be cached
752: */
1.22 duerst 753: private void generateGraph(PrintWriter out, PrintWriter pw,
1.4 barstow 754: File dotFile, String rdf, HttpServletRequest req, String graphFormat,
1.1 barstow 755: boolean saveRDF, boolean saveDOTFile)
756: {
757: try {
758: out.println("<hr title=\"visualisation\">");
759: out.println("<h3>Graph of the data model</h3>");
760:
761: // The temporary directory
762: String tmpDir = m_ServletTmpDir;
763:
764: // Add the graph footer
765: pw.println( " }");
766:
1.4 barstow 767: // Close the DOT input file so the GraphViz can
1.1 barstow 768: // open and read it
769: pw.close();
770:
771: // Generate a unique file name for the output file
772: // that will be created
773: String suffix = getFormatName(graphFormat, true);
774: File outputFile = createTempFile(tmpDir, TMP_FILE_PREFIX, suffix);
775: if (outputFile == null) {
776: out.println("Failed to create a temporary file for the graph. A graph cannot be generated.");
777: dotFile.delete();
778: return;
779: }
780:
781: // Pass the DOT data file to the GraphViz dot program
782: // so it can create a graph image of the data model
783: String dotFileName = dotFile.getAbsolutePath();
784: String outputFileName = outputFile.getAbsolutePath();
785:
1.8 barstow 786: if (!generateGraphFile(dotFileName, outputFileName, graphFormat)) {
1.1 barstow 787: out.println("An attempt to create a graph failed.");
788: dotFile.delete();
789: outputFile.delete();
790: return;
791: }
792: // Handle the DOT file
793: if (saveDOTFile) {
794: // Make the DOT file link'able if so requested
795: String dotPath = SERVLET_NAME + SUFFIX_TMP_DIR +
796: File.separator + dotFile.getName();
797: out.println("<a href=\"" + dotPath + "\">Download the DOT file.</a><br /><br />");
798: }
799: else {
800: // Delete it ...
801: dotFile.delete();
802: }
803:
804: // NOTE: Cannot delete the output file here because its
805: // pathname is returned to the client
806: String imagePath = SERVLET_NAME + SUFFIX_TMP_DIR + File.separator +
807: outputFile.getName();
808:
809: // Handle the embedded image formats first
810: if (graphFormat.equals(FORMAT_GIF_EMBED) ||
811: graphFormat.equals(FORMAT_PNG_EMBED)) {
812: if (outputFile.length() > 0)
813: out.println("<img src=\"" + imagePath + "\"/>");
814: else
815: out.println("The graph image file is empty.");
816: } else {
817: if (outputFile.length() > 0)
818: out.println("<a href=\"" + imagePath + "\">Get/view the graph's image file (" + suffix + ").</a><br /><br />");
819: else
820: out.println("The graph image file is empty.");
821: }
822:
823: // One last thing to do before exiting - copy the RDF to a file
824: if (saveRDF)
825: copyRDFStringToFile(tmpDir, rdf);
826:
827: } catch (Exception e) {
828: System.err.println("Exception generating graph: " + e.getMessage());
829: }
830: }
831:
832: /*
833: * Search the given string for substring "key"
834: * and if it is found, replace it with string "replacement"
835: *
836: *@param input the input string
837: *@param key the string to search for
838: *@param replacement the string to replace all occurences of "key"
1.3 barstow 839: *@return if no substitutions are done, input is returned; otherwise
1.1 barstow 840: * a new string is returned.
841: */
842: public static String replaceString(String input, String key,
843: String replacement)
844: {
1.3 barstow 845: try {
846: RE re = new RE(key);
847: return re.subst(input, replacement);
1.53 duerst 848: } catch (RESyntaxException e) {
1.3 barstow 849: return input;
1.1 barstow 850: }
851: }
852:
853: /*
854: * Print the document's header info
855: *
856: *@param out the servlet's output stream
857: */
1.22 duerst 858: private void printDocumentHeader (PrintWriter out)
1.1 barstow 859: {
860: try {
861:
1.6 barstow 862: out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"" +
863: " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
864: "<html><head>" +
865: "<title>RDF Validator</title>" +
866: "<link href='http://www.w3.org/StyleSheets/base.css' rel='stylesheet' type='text/css'/>" +
867: "<style type='text/css'>" +
868: " TD {" +
869: " background:#EEEEEE;" +
870: " font-family:'courier new',courier,serif;" +
871: " }" +
872: "</style>" +
873: "</head>" +
874: "<body>");
1.1 barstow 875:
876: } catch (Exception e) {
1.8 barstow 877: System.err.println("Exception (printDocumentHeader): " + e.getMessage());
1.1 barstow 878: }
879: }
880:
881: /*
882: * Print the rdf listing
883: *
884: *@param out the servlet's output stream
885: *@param rdf the RDF code
1.14 barstow 886: *@param needCR if true, add a CarriageReturn to the output; if false,
887: * do not add it
1.1 barstow 888: */
1.22 duerst 889: private void printListing (PrintWriter out, String rdf,
1.1 barstow 890: boolean needCR)
891: {
892: try {
1.8 barstow 893: out.println("<hr title=\"original source\">" +
1.29 duerst 894: "<h3>The original RDF/XML document</h3>" +
1.8 barstow 895: "<pre>");
1.1 barstow 896:
897: String s = replaceString(rdf, "<", "<");
898:
899: // Now output the RDF one line at a time with line numbers
900: int lineNum = 1;
901: int nl = 0;
902: String terminator = needCR?"\n":"";
903: do {
904: String tok;
905: nl = s.indexOf('\n');
906: if ( nl == -1 ) {
907: tok = s;
908: } else {
909: tok = s.substring(0,nl);
910: s = s.substring(nl+1);
911: }
1.8 barstow 912: out.print("<a name=\"" + lineNum + "\">" + lineNum +
913: "</a>: " + tok + terminator);
1.1 barstow 914: lineNum++;
915: } while ( nl != -1 );
916:
917: out.println("</pre>");
918: } catch (Exception e) {
1.8 barstow 919: System.err.println("Exception (printListing): " + e.getMessage());
1.1 barstow 920: }
921: }
922:
923: /*
924: * Print the header for the triple listing
925: *
926: *@param out the servlet's output stream
1.14 barstow 927: *@param nTriples if true, output is N-Triples syntax
1.1 barstow 928: */
1.22 duerst 929: private void printTripleTableHeader (PrintWriter out, boolean nTriples)
1.1 barstow 930: {
931: try {
932: if (nTriples) {
1.6 barstow 933: out.println("<h3>Triples of the Data Model in " +
934: "<a href=\"http://www.w3.org/2001/sw/RDFCore/ntriples/\">" +
935: "N-Triples</a> Format (Sub, Pred, Obj)</h3>" +
936: "<pre>");
1.1 barstow 937: } else {
938: out.println("<hr title=\"triples\">");
939: out.println("<h3>Triples of the Data Model</h3>");
940: out.println("<table border><tr>" +
941: "<td><b>Number</b></td>" +
942: "<td><b>Subject</b></td>" +
943: "<td><b>Predicate</b></td>" +
944: "<td><b>Object</b></td>" +
945: "</tr>");
946: }
947: } catch (Exception e) {
1.8 barstow 948: System.err.println("Exception (printTripleTableHeader): " + e.getMessage());
1.1 barstow 949: }
950: }
951:
952: /*
953: * Print the footer info for the triple listing
954: *
955: *@param out the servlet's output stream
1.14 barstow 956: *@param nTriples if true, output is N-Triples syntax
1.1 barstow 957: */
1.22 duerst 958: private void printTripleTableFooter (PrintWriter out,
1.1 barstow 959: boolean nTriples)
960: {
961: try {
962: if (nTriples)
963: out.println("</pre>");
964: else
965: out.println("</table>");
966: } catch (Exception e) {
1.8 barstow 967: System.err.println("Exception (printTripleTableFooter): " + e.getMessage());
1.1 barstow 968: }
969: }
970:
971: /*
972: * Print the document's footer info
973: *
974: *@param out the servlet's output stream
975: *@param rdf the RDF code
976: */
1.22 duerst 977: private void printDocumentFooter (PrintWriter out, String rdf)
1.1 barstow 978: {
979: try {
980:
1.8 barstow 981: String s;
982:
983: s = "<hr title=\"Problem reporting\">" +
984: "<h3>Feedback</h3>" +
985: "<p>If you suspect the parser is in error, please enter an explanation below and then press the <b>Submit problem report</b> button, to mail the report (and listing) to <i>" + MAIL_TO + "</i></p>" +
986: "<form enctype='text/plain' method='post' action='mailto:" + MAIL_TO + "'>" +
1.9 barstow 987: "<textarea cols='60' rows='4' name='report'></textarea>";
1.8 barstow 988: out.println(s);
1.1 barstow 989:
1.9 barstow 990: out.println("<input type='hidden' name='RDF' value=\"<?xml version="1.0">");
991:
1.1 barstow 992: // The listing is being passed as a parameter so the '<'
993: // and '"' characters must be replaced with < and ",
994: // respectively
995: if (rdf != null) {
1.9 barstow 996: String s1;
997: s1 = replaceString(rdf, "<", "<");
1.11 barstow 998: s1 = replaceString(s1, ">", ">");
1.9 barstow 999: s1 = replaceString(s1, "\"", """);
1000: out.println(s1);
1.1 barstow 1001: }
1.9 barstow 1002: out.println("\"\\>");
1.1 barstow 1003:
1.8 barstow 1004: out.println("<input type='submit' value='Submit problem report'\\>" +
1.9 barstow 1005: "</form></body></html>");
1.1 barstow 1006:
1007: } catch (Exception e) {
1.8 barstow 1008: System.err.println("Exception (printDocumentFooter): " + e.getMessage());
1.1 barstow 1009: }
1010: }
1011:
1012: /*
1013: * Servlet's get info method
1014: */
1015: public String getServletInfo () {
1016: return "Servlet wrapper for the ARP RDF parser. This is revision " + REVISION;
1017: }
1018:
1019: /*
1020: * Servlet's init method
1021: *
1022: *@param config the servlet's configuration object
1023: *@throws ServletException
1024: */
1025: public void init(ServletConfig config) throws ServletException
1026: {
1027: super.init (config);
1028:
1029: // Cache the parameters
1030: m_ServletTmpDir = config.getInitParameter(SERVLET_TMP_DIR);
1031:
1032: // All of the Graph Viz paths extend from GRAPH_VIZ_ROOT
1033: String GraphVizRoot = config.getInitParameter(GRAPH_VIZ_ROOT);
1034:
1035: m_GraphVizPath = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_PATH);
1036: m_GraphVizFontDir = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_FONT_DIR);
1.20 duerst 1037: System.out.println("GRAPH_VIZ_ROOT = " + GraphVizRoot);
1038: System.out.println("GRAPH_VIZ_PATH = " + m_GraphVizPath);
1039: System.out.println("GRAPH_VIZ_FNTDIR = " + m_GraphVizFontDir);
1040: System.out.println("SERVLET_TMP_DIR = " + m_ServletTmpDir);
1.1 barstow 1041:
1042: if (m_ServletTmpDir == null || GraphVizRoot == null) {
1043: System.err.println (
1044: "<html>" +
1045: "<h1>Servlet Initialization Error</h1>" +
1046: "<h2>One or more of the following parameters has not been initialized: " +
1047: SERVLET_TMP_DIR + "," + GRAPH_VIZ_ROOT + "," +
1.16 barstow 1048: GRAPH_VIZ_FONT_DIR + "," + GRAPH_VIZ_PATH + "." +
1.1 barstow 1049: "</h2>" +
1050: "</html>");
1051: }
1052: }
1053:
1054: /*
1055: * Servlet's destroy info method
1056: */
1057: public void destroy () {
1058: super.destroy ();
1059: }
1060:
1061: /*
1062: * Servlet's doGet info method - NOT supported
1063: *
1064: *@param req the request
1065: *@param res the response
1066: *@throws ServletException, IOException
1067: */
1068: public void doGet (HttpServletRequest req, HttpServletResponse res)
1069: throws ServletException, IOException
1070: {
1071: String sRDF = req.getParameter(TEXT);
1072: String sURI = req.getParameter(URI);
1073:
1074: if (sURI == null && sRDF == null) {
1.22 duerst 1075: res.setContentType ("text/html;charset=utf-8");
1076: PrintWriter out = res.getWriter ();
1.1 barstow 1077:
1.6 barstow 1078: out.println("<h1>Data Error</h1>" +
1079: "Must specify the RDF (RDF is a string) or the URI parameter." +
1080: "</h1>");
1.1 barstow 1081: return;
1082: }
1083:
1.20 duerst 1084: try {
1085: process(req, res,
1086: (sRDF != null) ? java.net.URLDecoder.decode(sRDF) : null,
1087: (sURI != null) ? java.net.URLDecoder.decode(sURI) : null);
1088: } catch (Exception e) {
1089: System.err.println("Exception: URLDecoder.decode()");
1090: }
1.1 barstow 1091: }
1092:
1093: /*
1094: * Servlet's doPost method
1095: *
1096: *@param req the request
1097: *@param res the response
1.17 duerst 1098: *@throws ServletException, IOException, java.io.UnsupportedEncodingException
1.1 barstow 1099: */
1100: public void doPost (HttpServletRequest req, HttpServletResponse res)
1101: throws ServletException, IOException
1102: {
1.19 duerst 1103: // String encoding = req.getCharacterEncoding();
1104: // if (encoding == null) {
1105: // req.setCharacterEncoding("UTF-8");
1106: // }
1.52 duerst 1107: String sRDF = new String(req.getParameter(TEXT).getBytes("iso-8859-1"), "utf-8");
1.35 duerst 1108: String sURI = new String(req.getParameter(URI).getBytes("iso-8859-1"), "utf-8");
1.55 ! duerst 1109: String parse = req.getParameter(PARSE);
! 1110: boolean parseRDF = true;
! 1111: if (parse != null)
! 1112: parseRDF = parse.equals("Parse RDF");
! 1113: else if (sURI != null) // keep working even if PARSE is not present
! 1114: parseRDF = false;
! 1115:
! 1116: if ((!parseRDF && sURI == null) || (parseRDF && sRDF == null)) {
1.22 duerst 1117: res.setContentType ("text/html;charset=utf-8");
1118: PrintWriter out = res.getWriter ();
1.1 barstow 1119:
1.55 ! duerst 1120: out.println("<h1>" + (parseRDF ? "RDF" : "URI") + " was not specified.</h1>");
1.1 barstow 1121: return;
1122: }
1.55 ! duerst 1123: if (parseRDF)
! 1124: sURI = null;
! 1125: else
! 1126: sRDF = null;
1.1 barstow 1127:
1128: process(req,res,sRDF, sURI);
1129: }
1130:
1131: /*
1132: * Output a Resource in NTriples syntax
1133: *
1134: *@param out the servlet's output stream
1135: *@param r the Resource to output
1136: */
1.22 duerst 1137: static private void printResource(PrintWriter out, AResource r)
1.1 barstow 1138: {
1.26 duerst 1139: if (r.isAnonymous() )
1140: out.print("_:j" + r.getAnonymousID() + " ");
1141: else
1142: out.print("<" + r.getURI() + "> ");
1.1 barstow 1143: }
1144:
1145: /*
1.18 duerst 1146: * Convert to Hex and padd left with zeroes
1147: *
1148: *@param in the integer to convert and padd
1149: *@param in the length of the result
1150: *@return the padded string
1151: */
1152: // MJD: is there an easier way to do this?
1.20 duerst 1153: static private String hexPadd (int number, int length)
1.18 duerst 1154: {
1155: String t = Integer.toHexString(number).toUpperCase();
1.20 duerst 1156: int hexlength = t.length();
1.18 duerst 1157:
1158: if ( hexlength > length ) { // too long, truncate
1159: hexlength = length;
1160: }
1161:
1.20 duerst 1162: int zerolength = length - hexlength;
1.18 duerst 1163: String r = "";
1164:
1165: for (int i=0; i < zerolength; i++) {
1166: r += "0";
1167: }
1168: for (int i=0; i < hexlength; i++) {
1.24 duerst 1169: r += t.charAt(i);
1.18 duerst 1170: }
1171: return r;
1172: }
1173:
1174: /*
1.1 barstow 1175: * Output a Literal in NTriples syntax
1176: *
1177: *@param out the servlet's output stream
1178: *@param l the Literal to output
1179: */
1.22 duerst 1180: static private void printNTripleLiteral(PrintWriter out, ALiteral l)
1.1 barstow 1181: {
1.28 duerst 1182: out.print("\"");
1183: char ar[] = l.toString().toCharArray();
1.1 barstow 1184:
1.28 duerst 1185: for (int i=0;i<ar.length;i++) {
1186: switch (ar[i]) {
1187: case '\\':
1188: out.print("\\\\");
1189: break;
1190: case '"':
1191: out.print("\\\"");
1192: break;
1193: case '\n':
1194: out.print("\\n");
1195: break;
1196: case '\r':
1197: out.print("\\r");
1198: break;
1199: case '\t':
1200: out.print("\\t");
1201: break;
1202: default:
1203: if ( ar[i] >= 32 && ar[i] <= 127 )
1204: out.print(ar[i]);
1205: else if ( ar[i] < 0xD800 || ar[i] >= 0xE000)
1206: out.print("\\u" + hexPadd(ar[i], 4) );
1207: else { // deal with surrogates
1208: // check for correct surrogate pair
1209: // this code should probably move somewhere else:
1210: // check when we get the input
1211: if ( ar[i] >= 0xDC00 ) {
1212: out.print("{{{error: lone low surrogate}}}");
1.18 duerst 1213: }
1.28 duerst 1214: else if ( ++i >= ar.length ) {
1215: out.print("{{{error: lone surrogate at end of string}}}");
1216: }
1217: else if ( ar[i] < 0xDC00 || ar[i] >= 0xE000 ) {
1218: out.print("{{{error: high surrogate not followed by low surrogate}}}");
1219: }
1220: // no errors, actually print
1221: else {
1222: int scalarvalue = 0x10000 + (ar[i-1] * 1024) + ar[i];
1223: out.print("\\U" + hexPadd(scalarvalue, 8) );
1224: }
1225: }
1.1 barstow 1226: }
1.28 duerst 1227: }
1228: out.print("\" ");
1.1 barstow 1229: }
1230:
1231: /*
1232: * Control point for outputing an triple in NTriple syntax
1233: *
1234: *@param out the servlet's output stream
1235: *@param subj the subject
1236: *@param pred the predicate
1237: *@param objRes the object as a Resource (may be null)
1238: *@param objLit the object as a Literal (may be null)
1239: */
1.22 duerst 1240: static private void printNTriple(PrintWriter out, AResource subj,
1.1 barstow 1241: AResource pred, AResource objRes, ALiteral objLit)
1242: {
1.27 duerst 1243: printResource(out, subj);
1244: printResource(out, pred);
1245: if (objRes != null)
1246: printResource(out, objRes);
1247: else
1248: printNTripleLiteral(out, objLit);
1249: out.println(".");
1.1 barstow 1250: }
1251:
1252: /*
1253: * Create a HTML anchor from the URI or anonNode of the
1254: * given Resource
1255: *
1.14 barstow 1256: *@param r the Resource
1.1 barstow 1257: *@return the string as an HTML anchor
1258: */
1259: static private String addAnchor(AResource r)
1260: {
1261: if (r.isAnonymous())
1.14 barstow 1262: return ANON_NODE + r.getAnonymousID();
1.1 barstow 1263: else
1264: return "<a href='" + r.getURI() + "'>" + r.getURI() + "</a>";
1265: }
1266:
1267: /*
1268: * Output a triple as a row in HTML
1269: *
1270: *@param out the servlet's output stream
1271: *@param subj the subject
1272: *@param pred the predicate
1273: *@param objRes the object as a Resource (may be null)
1274: *@param objLit the object as a Literal (may be null)
1275: *@param num the statement number
1276: */
1.22 duerst 1277: static private void printTableRow(PrintWriter out, AResource subj,
1.1 barstow 1278: AResource pred, AResource objRes, ALiteral objLit, int num)
1279: {
1.27 duerst 1280: out.println("<tr><td>" + num + "</td>");
1281: out.println("<td>" + addAnchor(subj) + "</td>");
1282: out.println("<td>" + addAnchor(pred) + "</td>");
1283: if (objRes != null)
1284: out.println("<td>" + addAnchor(objRes) + "</td>");
1285: else {
1286: out.println("<td>");
1287: String s1 = objLit.toString().trim();
1288: s1 = replaceString(s1, "<", "<");
1289: s1 = replaceString(s1, ">", ">");
1290: out.println(s1);
1291: out.println("</td>");
1292: }
1293: out.println("</tr>");
1.1 barstow 1294: }
1295:
1.4 barstow 1296: private static class SH implements StatementHandler
1.1 barstow 1297: {
1.22 duerst 1298: PrintWriter out;
1.12 barstow 1299: PrintWriter pw;
1.1 barstow 1300: boolean isNTriples;
1.12 barstow 1301: boolean printTriples;
1302: boolean printGraph;
1303: boolean anonNodesEmpty;
1.14 barstow 1304: int numStatements;
1305: int numLiterals;
1306: Hashtable subjects;
1307: int numSubjects;
1.1 barstow 1308:
1309: /*
1.4 barstow 1310: * Constructuor for the StatementHandler. The primary
1311: * responsiblitly is to cache init variables
1.1 barstow 1312: *
1.22 duerst 1313: *@param out the servlet's PrintWriter
1.4 barstow 1314: *@param pw the Dot file's PrintWriter
1.1 barstow 1315: * syntax; otherwise use HTML syntax
1.12 barstow 1316: *@param isNTriples if true, output using the NTriples
1317: *@param printTriples if true, print the triples
1318: *@param printGraph if true, create the graph file
1319: *@param printGraph if true, anonomyous nodes should be empty
1.1 barstow 1320: */
1.22 duerst 1321: public SH(PrintWriter out, PrintWriter pw, boolean isNTriples,
1.12 barstow 1322: boolean printTriples, boolean printGraph, boolean anonNodesEmpty)
1.1 barstow 1323: {
1324: this.out = out;
1.12 barstow 1325: this.pw = pw;
1.1 barstow 1326: this.isNTriples = isNTriples;
1.12 barstow 1327: this.printTriples = printTriples;
1328: this.printGraph = printGraph;
1329: this.anonNodesEmpty = anonNodesEmpty;
1.14 barstow 1330:
1331: this.numStatements = 0;
1332: this.numLiterals = 0;
1333:
1334: this.subjects = new Hashtable();
1335: this.numSubjects = 0;
1.4 barstow 1336: }
1337:
1338: /*
1339: * Generic handler for a Resource/Resource/Resource triple (S/P/O).
1340: * Dispatches to the methods that do the real work.
1341: *
1342: *@param subj the subject
1343: *@param pred the predicate
1344: *@param obj the object (as a Resource)
1345: */
1346: public void statement(AResource subj, AResource pred, AResource obj)
1347: {
1.12 barstow 1348: if (printTriples)
1349: statementResource(subj, pred, obj);
1350: if (printGraph)
1351: statementDotResource(subj, pred, obj);
1.4 barstow 1352: }
1353:
1354: /*
1355: * Generic handler for a Resource/Resource/Resource triple (S/P/O).
1356: * Dispatches to the methods that do the real work.
1357: *
1358: *@param subj the subject
1359: *@param pred the predicate
1360: *@param obj the object (as a Literal)
1361: */
1362: public void statement(AResource subj, AResource pred, ALiteral lit)
1363: {
1.13 barstow 1364: numLiterals++;
1365:
1.12 barstow 1366: if (printTriples)
1367: statementLiteral(subj, pred, lit);
1368: if (printGraph)
1369: statementDotLiteral(subj, pred, lit);
1.1 barstow 1370: }
1371:
1372: /*
1373: * Handler for a Resource/Resource/Resource triple (S/P/O)
1374: * Outputs the given triple using NTriples or HTML syntax.
1375: *
1376: *@param subj the subject
1377: *@param pred the predicate
1378: *@param obj the object (as a Resource)
1379: */
1.4 barstow 1380: public void statementResource(AResource subj, AResource pred, AResource obj)
1.1 barstow 1381: {
1382: numStatements++;
1383:
1384: if (this.isNTriples)
1385: printNTriple(out, subj, pred, obj, null);
1386: else
1387: printTableRow(out, subj, pred, obj, null, this.numStatements);
1388: }
1.4 barstow 1389:
1.1 barstow 1390: /*
1391: * Handler for a Resource/Resource/Literal triple (S/P/O)
1392: * Outputs the given triple using NTriples or HTML syntax.
1393: *
1394: *@param subj the subject
1395: *@param pred the predicate
1396: *@param obj the object (as a Literal)
1397: */
1.4 barstow 1398: public void statementLiteral(AResource subj, AResource pred, ALiteral lit)
1.1 barstow 1399: {
1400: numStatements++;
1401:
1402: if (this.isNTriples)
1403: printNTriple(out, subj, pred, null, lit);
1404: else
1405: printTableRow(out, subj, pred, null, lit, this.numStatements);
1406: }
1.4 barstow 1407:
1.12 barstow 1408: /*
1409: * Print the first part of a triple's Dot file. See below for
1410: * more info. This is the same regardless if the triple's
1411: * object is a Resource or a Literal
1412: *
1413: *@param subj the subject
1414: */
1415: public void printFirstPart(AResource subj)
1416: {
1.14 barstow 1417: if (subj.isAnonymous()) {
1.12 barstow 1418: if (this.anonNodesEmpty) {
1.14 barstow 1419: Integer n = (Integer) subjects.get(subj.getAnonymousID());
1420: if (n == null) {
1421: this.numSubjects++;
1422: subjects.put(subj.getAnonymousID(), new Integer(this.numSubjects));
1423: this.pw.println("\"" + ANON_NODE +
1424: subj.getAnonymousID() + "\" [label=\" \"];");
1425: }
1.12 barstow 1426: }
1.14 barstow 1427: this.pw.print("\"" + ANON_NODE + subj.getAnonymousID());
1.12 barstow 1428: } else {
1429: this.pw.println("\"" + subj.getURI() + "\" [URL=\"" +
1430: subj.getURI() + "\"];");
1431: this.pw.print("\"" + subj.getURI());
1432: }
1433: }
1434:
1.4 barstow 1435: /*
1436: * Handler for a Resource/Resource/Resource triple (S/P/O).
1437: * Outputs the given triple using Dot syntax.
1.12 barstow 1438: *
1439: * Each triple will be output in three lines of DOT code as
1440: * follows (not including the complication of anon nodes
1441: * and the possiblity that the anon nodes may be named
1442: * with an empty string):
1443: *
1444: * 1. "<subject>" [URL="<subject">];
1445: * 2. "<subject>" -> "<object>" [label="<predicate>",URL="<predicate>"];
1446: * 3. "<object>" [URL="<object>"];
1.4 barstow 1447: *
1448: *@param subj the subject
1449: *@param pred the predicate
1450: *@param obj the object (as a Resource)
1451: */
1452: public void statementDotResource(AResource subj, AResource pred, AResource obj)
1453: {
1454: if (this.pw == null) return;
1455:
1.12 barstow 1456: printFirstPart(subj);
1457:
1458: this.pw.print("\" -> ");
1.4 barstow 1459:
1.7 barstow 1460: if (obj.isAnonymous()) {
1.12 barstow 1461: if (this.anonNodesEmpty) {
1.14 barstow 1462: this.pw.println("\"" + ANON_NODE +
1.15 barstow 1463: obj.getAnonymousID() +
1464: "\" [label=\"" + pred.getURI() + "\",URL=\"" +
1465: pred.getURI() + "\"];");
1.12 barstow 1466: } else {
1.15 barstow 1467: this.pw.println("\"" + ANON_NODE + obj.getAnonymousID() +
1468: "\" [label=\"" + pred.getURI() + "\",URL=\"" +
1469: pred.getURI() + "\"];");
1.12 barstow 1470: }
1.7 barstow 1471: } else {
1.14 barstow 1472: this.pw.println("\"" + obj.getURI() + "\" [label=\"" +
1473: pred.getURI() + "\",URL=\"" + pred.getURI() + "\"];");
1.12 barstow 1474: this.pw.println("\"" + obj.getURI() + "\" [URL=\"" +
1475: obj.getURI() + "\"];");
1.14 barstow 1476: }
1.4 barstow 1477: }
1478:
1479: /*
1480: * Handler for a Resource/Resource/Literal triple (S/P/O).
1481: * Outputs the given triple using Dot syntax.
1.12 barstow 1482: *
1483: * Each triple will be output in three lines of DOT code as
1484: * follows (not including the complication of anon nodes
1485: * and the possiblity that the anon nodes may be named
1486: * with an empty string):
1487: *
1488: * 1. "<subject>" [URL="<subject">];
1489: * 2. "<subject>" -> "<literal>" [label="<predicate>",URL="<predicate>"];
1490: * 3. "<literal>" [shape="box"];
1.4 barstow 1491: *
1492: *@param subj the subject
1493: *@param pred the predicate
1494: *@param obj the object (as a Literal)
1495: */
1496: public void statementDotLiteral(AResource subj, AResource pred, ALiteral lit)
1497: {
1498: if (this.pw == null) return;
1499:
1.12 barstow 1500: printFirstPart(subj); // Same as Res/Res/Res
1.4 barstow 1501:
1502: /*
1503: * Before outputing the object (Literal) do the following:
1504: *
1505: * o GraphViz/DOT cannot handle embedded line terminators characters
1506: * so they must be replaced with spaces
1507: * o Limit the number of chars to make the graph legible
1508: * o Escape double quotes
1509: */
1510: String s1 = new String(lit.toString());
1511: s1 = s1.replace('\n', ' ');
1512: s1 = s1.replace('\f', ' ');
1513: s1 = s1.replace('\r', ' ');
1514: if (s1.indexOf('"') != -1)
1515: s1 = replaceString(s1, "\"", "\\\"");
1516:
1517: // Anything beyond 80 chars makes the graph too large
1518: String tmpObject;
1519: if (s1.length() >= 80)
1520: tmpObject = s1.substring(0, 80) + " ...";
1521: else
1522: tmpObject = s1.substring(0, s1.length());
1523:
1.13 barstow 1524: // Create a temporary label for the literal so that if
1525: // it is duplicated it will be unique in the graph and
1526: // thus have its own node.
1527: String tmpName = "Literal_" + Integer.toString(this.numLiterals);
1528: this.pw.print("\" -> \"" + tmpName);
1.4 barstow 1529:
1.14 barstow 1530: this.pw.println("\" [label=\"" + pred.getURI() +
1531: "\",URL=\"" + pred.getURI() + "\"];");
1.4 barstow 1532:
1.13 barstow 1533: this.pw.println("\"" + tmpName + "\" [shape=box,label=\"" + tmpObject + "\"];");
1.4 barstow 1534: }
1535: }
1536:
1.22 duerst 1537: private void printErrorMessages(PrintWriter out, SaxErrorHandler eh)
1.5 barstow 1538: {
1539: try {
1540: String s;
1541:
1542: s = eh.getFatalErrors();
1543: if (s != null && s.length() >= 1)
1544: out.println("<h2>Fatal Error Messages</h2>" + s);
1545:
1546: s = eh.getErrors();
1547: if (s != null && s.length() >= 1)
1548: out.println("<h2>Error Messages</h2>" + s);
1549:
1550: s = eh.getWarnings();
1551: if (s != null && s.length() >= 1)
1552: out.println("<h2>Warning Messages</h2>" + s);
1553: } catch (Exception e) {
1554: System.err.println(SERVLET_NAME + ": Error printing error messages.");
1555: }
1556: }
1557:
1558: /*
1559: * Initialize the graph output file. If an error occurs, this
1560: * function will print an error message.
1561: *
1562: *@param out the servlet's output stream
1563: *@req the servlet request object
1564: *@return the File object for the graph file; null if an error occurs
1565: */
1.22 duerst 1566: private File initGraphFile(PrintWriter out,
1.4 barstow 1567: HttpServletRequest req)
1568: {
1569: try {
1570: // Stop if any of the parameters are missing
1571: if (m_ServletTmpDir == null ||
1572: m_GraphVizPath == null ||
1.16 barstow 1573: m_GraphVizFontDir == null)
1.4 barstow 1574: {
1575: // Put the paths in a comment in the returned content
1576: out.println("<!-- SERVLET_TMP_DIR = " + m_ServletTmpDir);
1577: out.println("GRAPH_VIZ_PATH = " + m_GraphVizPath);
1578: out.println("GRAPH_FONT_DIR = " + m_GraphVizFontDir + " -->");
1579:
1.12 barstow 1580: out.println("<h1>Servlet initialization failed</h1>");
1581: out.println("Please send a message to <a href='mailto:" + MAIL_TO + "'>" + MAIL_TO + "</a> and mention this problem.");
1.4 barstow 1582: return null;
1583: }
1584: } catch (Exception e) {
1585: System.err.println("Unable to create a temporary graph file. A graph cannot be generated.");
1586: return null;
1587: }
1588:
1589: File dotFile = null;
1590:
1.27 duerst 1591: // Must generate a unique file name that the DOT handler will use
1592: dotFile = createTempFile(m_ServletTmpDir, TMP_FILE_PREFIX, SUFFIX_DOT);
1593: if (dotFile == null) {
1594: out.println("<h1>Failed to create a temporary graph file. A graph cannot be generated.</h1>");
1595: return null;
1596: }
1.4 barstow 1597:
1598: return dotFile;
1.1 barstow 1599: }
1.6 barstow 1600:
1601: /*
1602: * Check if the given URI is supported or not
1603: *
1604: *@param out the servlet's output stream
1605: *@param uri the URI to check
1606: *@return true if the URI is supported; false otherwise
1607: */
1.22 duerst 1608: private boolean isURISupported(PrintWriter out, String uri)
1.6 barstow 1609: {
1610: try {
1611: if (uri.length() >= 4 && uri.substring(0,4).equalsIgnoreCase("file")) {
1612: out.println("<h1>file URI Schemes are NOT Supported</h1>");
1613: out.println("URIs from the 'file' URI scheme are not supported by this servlet.");
1614: return false;
1615: }
1616: } catch (Exception e) {
1617: System.err.println("Exception in isURISupported.");
1618: return false;
1619: }
1620:
1621: return true;
1622: }
1.1 barstow 1623:
1624: /*
1625: * Handle the servlets doGet or doPut request
1626: *
1627: *@param req the servlet's request
1628: *@param res the servlet's response
1629: *@throws SevletException, IOException
1630: */
1631: private void process(HttpServletRequest req, HttpServletResponse res,
1.52 duerst 1632: String sRDF, String sURI) throws ServletException, IOException
1.1 barstow 1633: {
1.21 duerst 1634: res.setContentType ("text/html;charset=utf-8");
1.52 duerst 1635: PrintWriter out = res.getWriter ();
1.10 barstow 1636:
1.12 barstow 1637: String sSaveRDF = req.getParameter (SAVE_RDF);
1638: String sSaveDOTFile = req.getParameter (SAVE_DOT_FILE);
1639: String sFormat = req.getParameter (FORMAT);
1640: String sNTriples = req.getParameter (NTRIPLES);
1641: String sEmbedded = req.getParameter (EMBEDDED_RDF);
1642: String sTriplesAndGraph = req.getParameter (TRIPLES_AND_GRAPH);
1643: String sAnonNodesEmpty = req.getParameter (ANON_NODES_EMPTY);
1644:
1645: // Set the print flags
1646: boolean printTriples = true;
1647: boolean printGraph = true;
1648: if (sTriplesAndGraph != null) {
1649: if (sTriplesAndGraph.equals(PRINT_TRIPLES))
1650: printGraph = false;
1651: if (sTriplesAndGraph.equals(PRINT_GRAPH))
1652: printTriples = false;
1653: }
1.1 barstow 1654:
1.12 barstow 1655: // Determine if printing the triples and/or graph
1656: boolean anonNodesEmpty = (sAnonNodesEmpty != null) ? true : false;
1.1 barstow 1657: boolean nTriples = (sNTriples != null) ? true : false;
1.12 barstow 1658:
1.10 barstow 1659: // ARP parser has embedded = true by default so if user
1660: // wants embedding, must set it to false
1661: boolean embedded = (sEmbedded != null) ? false : true;
1.1 barstow 1662:
1663: String xmlBase = DEFAULT_NAMESPACE;
1664:
1665: printDocumentHeader (out);
1666:
1667: if (sURI != null && sURI.length() >= 1) {
1.6 barstow 1668:
1669: // First check for unsupported URIs
1670: if (!isURISupported(out, sURI)) {
1671: printDocumentFooter(out, null);
1672: return;
1673: }
1674:
1.1 barstow 1675: xmlBase = sURI;
1.53 duerst 1676: try {
1677: sRDF = getRDFfromURI(sURI);
1678: if (sRDF == null)
1679: throw new getRDFException("The URI may not exist or the server is down.@@");
1680: } catch (getRDFException e) {
1681: out.println("<h1>RDF Load Error</h1>");
1682: out.println("An attempt to load the RDF from URI '" + sURI +
1683: "' failed. (" + e.getMessage() + ")");
1684: printDocumentFooter(out, null);
1685: return;
1686: }
1.1 barstow 1687: }
1.4 barstow 1688:
1689: PrintWriter pw = null; // The writer for the graph file
1690: File dotFile = null; // The graph file
1.12 barstow 1691: if (sFormat != null && printGraph) {
1.4 barstow 1692: dotFile = initGraphFile(out, req);
1693: if (dotFile == null)
1694: // Assume error has been reported
1695: return;
1696:
1697: // Create a PrintWriter for the DOT handler
1.52 duerst 1698: FileWriter fw = new FileWriter(dotFile);
1.4 barstow 1699: if (fw != null)
1700: pw = new PrintWriter(fw);
1701: if (pw != null)
1702: // Add the graph header
1703: processGraphParameters (req, pw);
1704: }
1705:
1706: // Create the StatementHandler - it will handle triples for
1707: // the table/ntriples and the graph file
1.12 barstow 1708: SH sh = new SH(out, pw, nTriples, printTriples, printGraph, anonNodesEmpty);
1.4 barstow 1709:
1710: // Create the ErrorHandler
1711: SaxErrorHandler errorHandler = new SaxErrorHandler(out, false);
1712:
1713: // Create and initialize the parser
1714: ARP parser = new com.hp.hpl.jena.rdf.arp.ARP();
1715: parser.setErrorHandler(errorHandler);
1716: parser.setStatementHandler(sh);
1.10 barstow 1717: parser.setEmbedding(embedded);
1.1 barstow 1718:
1719: printListing (out, sRDF, sURI != null && sURI.length() >= 1);
1.12 barstow 1720:
1721: if (printTriples)
1722: printTripleTableHeader (out, nTriples);
1.1 barstow 1723:
1724: try {
1.52 duerst 1725: StringReader sr = new StringReader (sRDF);
1726: parser.load(sr, xmlBase);
1.1 barstow 1727: } catch (Exception ex) {
1.53 duerst 1728: out.println ("<h1>Parser Loading Error</h1>");
1729: out.println ("Exception parsing: " + sURI + ": " + ex.toString());
1730: printDocumentFooter(out, null);
1731: return;
1.1 barstow 1732: }
1733:
1.12 barstow 1734: if (printTriples)
1735: printTripleTableFooter(out, nTriples);
1.5 barstow 1736:
1737: printErrorMessages(out, errorHandler);
1.1 barstow 1738:
1.52 duerst 1739: res.flushBuffer();
1.12 barstow 1740: if (sFormat != null && printGraph) {
1.4 barstow 1741: generateGraph(out, pw, dotFile, sRDF, req, sFormat,
1.1 barstow 1742: (sSaveRDF != null) ? true : false,
1743: (sSaveDOTFile != null && sSaveDOTFile.equals ("on") ? true : false));
1744: }
1745:
1746:
1.9 barstow 1747: if (sURI != null && sURI.length() >= 1)
1748: printDocumentFooter(out, null);
1749: else
1750: printDocumentFooter(out, sRDF);
1.1 barstow 1751: }
1752: }
Webmaster