Annotation of java/classes/org/w3c/rdf/examples/SiRPACServlet.java, revision 1.19
1.1 barstow 1: /**
2: * SiRPACServlet - Simple RDF Parser & Compiler Servlet wrapper
3: *
4: * Copyright © World Wide Web Consortium, (Massachusetts Institute of
5: * Technology, Institut National de Recherche en Informatique et en
6: * Automatique, Keio University).
7: *
8: * All Rights Reserved.
9: *
10: * Please see the full Copyright clause at
11: * <http://www.w3.org/Consortium/Legal/copyright-software.html>
12: *
1.3 barstow 13: * This servlet is a wrapper for the SiRPAC RDF parser. The servlet
1.7 barstow 14: * expects the following variables through the POST method:
1.3 barstow 15: *
1.19 ! barstow 16: * RDF - the RDF in an XML syntax
! 17: *
! 18: * BAGS - if "on", each Description should have its own Bag;
! 19: * the default is not to do this.
! 20: *
! 21: * STREAM - if "on", the stream mode is turned off so that aboutEach
! 22: * and aboutEachPrefix are supported. It is off by default.
! 23: *
! 24: * SAVE_DOT_FILE - if "on", the DOT file is saved and a link to the
! 25: * file is provided to the user
! 26: *
! 27: * SAVE_RDF - if "on", the RDF will be copied to a file.
! 28: *
! 29: * URI - the URI to parse [instead of the RDF]; may not be specified
! 30: *
! 31: * ORIENTATION - the graph's orientation (left to right or top to
! 32: * bottom
! 33: *
! 34: * FONT_SIZE - the font size to use [10, 12, 14, 16 and 20 are supported]
! 35: *
! 36: * GRAPH_FORMAT - the graph's output format. Supported values are:
! 37: *
! 38: * GIF_EMBED - embed the graph as a GIF [the default]
! 39: * GIF_LINK - don't embed the GIF but create a link for it
! 40: * SVG_LINK - create the graph in SVG format and create a link to the file
! 41: * PS_LINK - create a PostScript image of the file and a link to the file
! 42: * HP_PCL_LINK - create a HPGL/2 - PCL (Laserwriter) image of the file
! 43: * and a link to the file
! 44: * HP_GL_LINK - create a HPGL - PCL (pen plotter) image of the file and
! 45: * a link to the file
! 46: * NO_GRAPH - do not generate a graph
1.1 barstow 47: *
1.7 barstow 48: * @author Art Barstow <barstow@w3.org>
1.1 barstow 49: *
1.7 barstow 50: * The graphics package is AT&T's GraphVis tool.
1.1 barstow 51: */
52:
53: package org.w3c.rdf.examples;
54:
55: import java.io.*;
1.16 barstow 56: import java.net.MalformedURLException;
57: import java.net.URL;
1.1 barstow 58: import java.util.StringTokenizer;
59: import java.util.Enumeration;
60: import javax.servlet.*;
61: import javax.servlet.http.*;
62:
63: import org.xml.sax.InputSource;
64: import org.xml.sax.Parser;
65: import org.xml.sax.SAXException;
66: import org.xml.sax.helpers.*;
67:
68: import org.w3c.rdf.model.*;
69: import org.w3c.rdf.syntax.*;
70: import org.w3c.rdf.syntax.RDFConsumer;
71: import org.w3c.rdf.util.xml.DumpConsumer;
72: import org.w3c.rdf.util.xml.ErrorStore;
1.16 barstow 73: import org.w3c.rdf.util.xml.GenericParser;
1.1 barstow 74: import org.w3c.rdf.implementation.model.StatementImpl;
75: import org.w3c.rdf.implementation.model.NodeFactoryImpl;
76: import org.w3c.rdf.implementation.syntax.sirpac.*;
77:
78: public class SiRPACServlet extends HttpServlet
79: {
1.19 ! barstow 80: final static public String REVISION = "$Id: SiRPACServlet.java,v 1.18 2001/02/05 22:56:51 barstow Exp $";
1.1 barstow 81:
1.7 barstow 82: // The email address for bug reports
1.4 barstow 83: private static final String MAIL_TO = "barstow@w3.org";
1.10 barstow 84:
1.19 ! barstow 85: // Names of the POST parameters (described above) and their
! 86: // defaults
! 87: private static final String TEXT = "RDF";
! 88: private static final String BAG = "BAGS";
! 89: private static final String STREAM_MODE = "STREAM";
! 90: private static final String SAVE_DOT_FILE = "SAVE_DOT_FILE";
! 91: private static final String SAVE_RDF = "SAVE_RDF";
! 92: private static final String URI = "URI";
1.3 barstow 93:
1.6 barstow 94: private static final String NODE_COLOR = "NODE_COLOR";
95: private static final String DEFAULT_NODE_COLOR = "black";
96:
97: private static final String NODE_TEXT_COLOR = "NODE_TEXT_COLOR";
98: private static final String DEFAULT_NODE_TEXT_COLOR = "black";
99:
100: private static final String EDGE_COLOR = "EDGE_COLOR";
101: private static final String DEFAULT_EDGE_COLOR = "black";
102:
103: private static final String EDGE_TEXT_COLOR = "EDGE_TEXT_COLOR";
104: private static final String DEFAULT_EDGE_TEXT_COLOR = "black";
105:
106: private static final String ORIENTATION = "ORIENTATION";
107: private static final String DEFAULT_ORIENTATION = "TB"; // Top to Bottom
108:
109: private static final String FONT_SIZE = "FONT_SIZE";
110: private static final String DEFAULT_FONT_SIZE = "10";
111:
1.19 ! barstow 112: private static final String FORMAT = "FORMAT";
! 113: private static final String FORMAT_GIF_EMBED = "GIF_EMBED";
! 114: private static final String FORMAT_GIF_LINK = "GIF_LINK";
! 115: private static final String FORMAT_SVG_LINK = "SVG_LINK";
! 116: private static final String FORMAT_PS_LINK = "PS_LINK";
! 117: private static final String FORMAT_HP_PCL_LINK = "HP_PCL_LINK";
! 118: private static final String FORMAT_HP_GL_LINK = "HP_GL_LINK";
! 119: private static final String FORMAT_NO_GRAPH = "NO_GRAPH";
! 120:
1.6 barstow 121: // Fonts are not currently configurable
122: private static final String DEFAULT_FONT = "arial";
123:
1.19 ! barstow 124: // Names of the servlet's parameters - for Jigsaw web server
! 125: private static final String SIRPAC_TMP_DIR = "SIRPAC_TMP_DIR";
! 126: private static final String GRAPH_VIZ_ROOT = "GRAPH_VIZ_ROOT";
! 127: private static final String GRAPH_VIZ_PATH = "GRAPH_VIZ_PATH";
! 128: private static final String GRAPH_VIZ_LIB_DIR = "GRAPH_VIZ_LIB_DIR";
! 129: private static final String GRAPH_VIZ_FONT_DIR = "GRAPH_VIZ_FONT_DIR";
! 130:
! 131: // Variables for the servlet's parameters
! 132: private static String m_SiRPACTmpDir = null;
! 133: private static String m_GraphVizPath = null;
! 134: private static String m_GraphVizFontDir = null;
! 135: private static String m_GraphVizLibDir = null;
! 136:
! 137: // Names of environment variable need by GraphVis
! 138: private static String DOTFONTPATH = "DOTFONTPATH";
! 139: private static String LD_LIBRARY_PATH = "LD_LIBRARY_PATH";
! 140:
! 141: // Names used for temporary files
! 142: private static final String TMP_FILE_PREFIX = "sirpac_";
! 143: private static final String SUFFIX_TMP_DIR = ".tmp";
! 144: private static final String SUFFIX_DOT = ".dot";
! 145: private static final String SUFFIX_RDF = ".rdf";
! 146:
! 147: // Names used for file suffixes and for GraphViz's command line
! 148: // option
! 149: private static final String NAME_GIF = "gif";
! 150: private static final String NAME_HPGL = "hpgl";
! 151: private static final String NAME_PCL = "pcl";
! 152: private static final String NAME_PS = "ps";
! 153: private static final String NAME_SVG = "svg";
! 154:
! 155: // Default GraphViz parameter names and their default values
1.8 barstow 156: // Servlet name
157: private static final String SERVLET_NAME = "SiRPACServlet";
158:
1.3 barstow 159: /*
1.4 barstow 160: * Create a File object in the m_SiRPACTmpDir directory
1.3 barstow 161: *
1.4 barstow 162: *@param directory the file's directory
163: *@param prefix the file's prefix name (not its directory)
164: *@param suffix the file's suffix or extension name
165: *@return a File object if a temporary file is created; null otherwise
1.3 barstow 166: */
1.4 barstow 167: private File createTempFile (String directory, String prefix, String suffix) {
168: File f;
169: try {
170: File d = new File(directory);
171: f = File.createTempFile(prefix, suffix, d);
172: } catch (Exception e) {
173: return null;
174: }
175: return f;
176: }
177:
1.14 barstow 178:
179: /*
180: * Copy the given string of RDF to a file in the given directory
181: *
182: *@param dir the file's directory
183: *@param rdf the string of RDF
184: *@return void
185: */
186:
187: private void copyRDFStringToFile(String tmpDir, String rdf)
188: {
189: try {
190: // Generate a unique file name
1.19 ! barstow 191: File tmpFile = createTempFile(tmpDir, TMP_FILE_PREFIX, SUFFIX_RDF);
1.14 barstow 192: if (tmpFile == null) {
193: // Not really a critical error, just return
194: return;
195: }
196:
197: // Create a PrintWriter for the GraphViz consumer
198: FileWriter fw = new FileWriter(tmpFile);
199: PrintWriter pw = new PrintWriter(fw);
200:
201: pw.println(rdf);
202: pw.close();
203: } catch (Exception e) {
204: // Just return - not critical
205: return;
206: }
207: }
208:
1.4 barstow 209: /*
1.19 ! barstow 210: * Given the graph's format option, return either the corresponding
! 211: * command line option for that option or the file name suffix for
! 212: * the graph option. For example GIF files have ".gif" for its
! 213: * suffix and GraphViz uses "-Tgif" for the command line.
! 214: *
! 215: * NOTE: default is GIF.
! 216: *
! 217: *@param graphFormat the graph's output format
! 218: *@param suffix. If true, the name returned is for the graph's
! 219: * file name suffix; otherwise, the name returned is for the
! 220: * graph's command line option.
! 221: *@return the suffix to use for the graph's output file
! 222: */
! 223: private String getFormatName(String graphFormat, boolean suffix) {
! 224:
! 225: String name = (suffix) ? "." : "-T";
! 226:
! 227: if (graphFormat.equals(FORMAT_SVG_LINK)) return name + NAME_SVG;
! 228: if (graphFormat.equals(FORMAT_PS_LINK)) return name + NAME_PS;
! 229: if (graphFormat.equals(FORMAT_HP_GL_LINK)) return name + NAME_HPGL;
! 230: if (graphFormat.equals(FORMAT_HP_PCL_LINK)) return name + NAME_PCL;
! 231:
! 232: return name + NAME_GIF;
! 233: }
! 234:
! 235: /*
1.4 barstow 236: * Invokes the GraphVis program to create a GIF image from the
237: * the given DOT data file
238: *
239: *@param dotFileName the name of the DOT data file
1.19 ! barstow 240: *@param outputFileName the name of the GIF data file
1.4 barstow 241: *@return true if success; false if any failure occurs
242: */
1.19 ! barstow 243: private boolean generateOutputFile(String dotFileName, String outputFileName, String graphFormat) {
1.5 barstow 244: String environment[] = {DOTFONTPATH + "=" + m_GraphVizFontDir,
245: LD_LIBRARY_PATH + "=" + m_GraphVizLibDir};
1.4 barstow 246:
1.19 ! barstow 247: String formatOption = getFormatName(graphFormat, false);
! 248:
! 249: String cmdArray[] = {m_GraphVizPath, formatOption, "-o", outputFileName, dotFileName};
1.4 barstow 250:
251: Runtime rt = Runtime.getRuntime();
252: try {
253: Process p = rt.exec(cmdArray, environment);
254: p.waitFor();
255: } catch (Exception e) {
256: return false;
257: }
258:
259: return true;
1.3 barstow 260: }
1.1 barstow 261:
1.3 barstow 262: /*
1.6 barstow 263: * Returns a parameter from a request or the parameter's default
264: * value.
265: *
266: *@param req a Servlet request
267: *@return if the request contains the specfied parameter its value
268: * in the request is returned; otherwise its default value is
269: * returned
270: */
271: private String getParameter(HttpServletRequest req, String param, String defString) {
272: String s = req.getParameter(param);
273: return (s == null) ? defString : s;
274: }
275:
276: /*
277: * If the request contains any graph-related parameters, pass them
278: * to the graph consumer for handling
279: *
280: *@param req the response
281: *@param consumer the GraphViz consumer
282: */
283: private void processGraphParameters (HttpServletRequest req, GraphVizDumpConsumer consumer) {
284: // Look for colors
285:
286: String s;
287:
288: String nodeColor = getParameter (req, NODE_COLOR, DEFAULT_NODE_COLOR);
289: String nodeTextColor = getParameter (req, NODE_TEXT_COLOR, DEFAULT_NODE_TEXT_COLOR);
290: String edgeColor = getParameter (req, EDGE_COLOR, DEFAULT_EDGE_COLOR);
291: String edgeTextColor = getParameter (req, EDGE_TEXT_COLOR, DEFAULT_EDGE_TEXT_COLOR);
292: String fontSize = getParameter (req, FONT_SIZE, DEFAULT_FONT_SIZE);
293:
294: // Orientation must be either
295: String orientation = req.getParameter (ORIENTATION);
1.19 ! barstow 296: if (orientation.equals("LR"))
1.6 barstow 297: orientation = "LR";
298: else
299: orientation = DEFAULT_ORIENTATION;
300:
301: // Add an attribute for all of the graph's nodes
302: consumer.addGraphAttribute("node [fontname=" + DEFAULT_FONT +
303: ",fontsize=" + fontSize +
304: ",color=" + nodeColor +
305: ",fontcolor=" + nodeTextColor + "]");
306:
307: // Add an attribute for all of the graph's edges
308: consumer.addGraphAttribute("edge [fontname=" + DEFAULT_FONT +
309: ",fontsize=" + fontSize +
310: ",color=" + edgeColor +
311: ",fontcolor=" + edgeTextColor + "]");
312:
313: // Add an attribute for the orientation
314: consumer.addGraphAttribute("rankdir=" + orientation + ";");
315: }
316:
317: /*
1.3 barstow 318: * Generate a graph of the RDF data model
319: *
1.4 barstow 320: *@param out the servlet's output stream
1.6 barstow 321: *@param rdf the RDF text
322: *@param req a Servlet request
1.19 ! barstow 323: *@param source the name of input source
! 324: *@param graphFormat the graph's format
! 325: *@param saveRDF the RDF can be cached [saved to the file system]
! 326: *@param saveDOTFile the DOT file should be cached
! 327: *@param createBags the parser should create a bag for each Description
! 328: *@param streamMode the parser's stream mode setting
1.3 barstow 329: */
1.19 ! barstow 330: private void generateGraph (ServletOutputStream out, String rdf, HttpServletRequest req, String source, String graphFormat, boolean saveRDF, boolean saveDOTFile, boolean createBags, boolean streamMode) {
1.2 barstow 331: try {
332: out.println("<hr title=\"visualisation\">");
1.3 barstow 333: out.println("<h3>Graph of the data model</h3>");
334:
1.4 barstow 335: // Stop if any of the parameters are missing
1.19 ! barstow 336: if (m_SiRPACTmpDir == null ||
! 337: m_GraphVizPath == null ||
! 338: m_GraphVizFontDir == null ||
! 339: m_GraphVizLibDir == null) {
1.8 barstow 340:
341: // Put the paths in a comment in the returned content
342: out.println("<!-- SIRPAC TMP = " + m_SiRPACTmpDir);
343: out.println("GRAPH VIZ = " + m_GraphVizPath);
344: out.println("GRAPH LIB = " + m_GraphVizLibDir);
345: out.println("GRAPH FON = " + m_GraphVizFontDir + " -->");
346:
1.3 barstow 347: out.println("Servlet initialization failed. A graph cannot be generated.");
348: return;
349: }
350:
1.4 barstow 351: // The temporary directory
352: String tmpDir = m_SiRPACTmpDir;
1.3 barstow 353:
1.4 barstow 354: // Must generate a unique file name that the DOT consumer
355: // will use
1.19 ! barstow 356: File dotFile = createTempFile(tmpDir, TMP_FILE_PREFIX, SUFFIX_DOT);
1.4 barstow 357: if (dotFile == null) {
358: out.println("Failed to create a temporary DOT file. A graph cannot be generated.");
1.3 barstow 359: return;
360: }
361:
362: // Create a PrintWriter for the GraphViz consumer
1.4 barstow 363: FileWriter fw = new FileWriter(dotFile);
1.3 barstow 364: PrintWriter pw = new PrintWriter(fw);
365:
366: // Run the parser using the DOT consumer to capture
367: // the output in a file
368: StringReader sr = new StringReader (rdf);
369: InputSource is = new InputSource (sr);
370: GraphVizDumpConsumer consumer = new GraphVizDumpConsumer(pw);
371:
1.6 barstow 372: // Process any graph-related parameters in the request
373: processGraphParameters(req, consumer);
1.15 barstow 374:
375: // Reinitialize the parser's genid counter so the genids
376: // of the triple will match the genids of the graph
1.18 barstow 377: SiRPAC sirpac = new SiRPAC();
378: ErrorStore errorHandler = new ErrorStore();
379:
380: sirpac.setErrorHandler(errorHandler);
381: sirpac.createBags (createBags);
382: sirpac.setStreamMode (streamMode);
383: sirpac.setSource(source);
1.6 barstow 384:
1.3 barstow 385: try {
1.18 barstow 386: sirpac.parse(is, consumer);
1.3 barstow 387: } catch (Exception e) {
388: out.println("An attempt to generate the graph data failed ("
389: + e.getMessage() + ").");
1.4 barstow 390: pw.close();
391: dotFile.delete();
1.3 barstow 392: return;
393: }
394:
1.4 barstow 395: // Must close the DOT input file so the GraphViz can
396: // open and read it
397: pw.close();
398:
399: // Must generate a unique file name for the GIF file
400: // that will be created
1.19 ! barstow 401: String suffix = getFormatName(graphFormat, true);
! 402: File outputFile = createTempFile(tmpDir, TMP_FILE_PREFIX, suffix);
! 403: if (outputFile == null) {
! 404: out.println("Failed to create a temporary file for the graph. A graph cannot be generated.");
1.4 barstow 405: dotFile.delete();
406: return;
407: }
408:
1.3 barstow 409: // Pass the DOT data file to the GraphViz dot program
1.4 barstow 410: // so it can create a GIF image of the data model
411: String dotFileName = dotFile.getAbsolutePath();
1.19 ! barstow 412: String outputFileName = outputFile.getAbsolutePath();
1.4 barstow 413:
1.19 ! barstow 414: if (!generateOutputFile(dotFileName, outputFileName, graphFormat)) {
1.4 barstow 415: out.println("An attempt to create a graph failed.");
416: dotFile.delete();
1.19 ! barstow 417: outputFile.delete();
1.4 barstow 418: return;
419: }
1.3 barstow 420:
1.19 ! barstow 421: // Handle the DOT file
! 422: if (saveDOTFile) {
! 423: // Make the DOT file link'able if so requested
! 424: String dotPath = SERVLET_NAME + SUFFIX_TMP_DIR +
! 425: File.separator + dotFile.getName();
! 426: out.println("<a href=\"" + dotPath + "\">Get the DOT file.</a><br /><br />");
! 427: }
! 428: else {
! 429: // Delete it ...
! 430: dotFile.delete();
! 431: }
1.3 barstow 432:
1.9 barstow 433: // NOTE: Cannot delete the GIF file here because its
434: // pathname is returned to the client
1.19 ! barstow 435: String imagePath = SERVLET_NAME + SUFFIX_TMP_DIR + File.separator +
! 436: outputFile.getName();
! 437:
! 438: // Handle the embedded image formats first
! 439: if (graphFormat.equals(FORMAT_GIF_EMBED)) {
! 440: if (outputFile.length() > 0)
! 441: out.println("<img src=\"" + imagePath + "\"/>");
! 442: else
! 443: out.println("The graph image file is empty.");
! 444: } else {
! 445: if (outputFile.length() > 0)
! 446: out.println("<a href=\"" + imagePath + "\">Get/view the graph's image file (" + suffix + ").</a><br /><br />");
! 447: else
! 448: out.println("The graph image file is empty.");
! 449: }
1.2 barstow 450:
1.14 barstow 451: // One last thing to do before exiting - copy the RDF to a file
452: if (saveRDF)
453: copyRDFStringToFile(tmpDir, rdf);
454:
1.2 barstow 455: } catch (Exception e) {
456: System.err.println("Exception: " + e.getMessage());
457: }
458: }
459:
460: /*
1.3 barstow 461: * Search the given string for substring "key"
1.2 barstow 462: * and if it is found, replace it with string "replacement"
463: *
1.4 barstow 464: *@param input the input string
465: *@param key the string to search for
466: *@param replacement the string to replace all occurences of "key"
1.2 barstow 467: *@return if no substitutions are done, input is returned; otherwise
468: * a new string is returned.
469: */
1.12 barstow 470: public static String replaceString(String input, String key, String replacement) {
1.2 barstow 471: StringBuffer sb = new StringBuffer("");
472: StringTokenizer st = new StringTokenizer(input, key);
473:
474: // Must handle the case where the input string begins with the key
475: if (input.startsWith(key))
476: sb = sb.append(replacement);
477: while (st.hasMoreTokens()) {
478: sb = sb.append(st.nextToken());
479: if (st.hasMoreTokens())
480: sb = sb.append(replacement);
481: }
482: if (sb.length() >= 1)
483: return sb.toString();
484: else
485: return input;
486: }
487:
1.3 barstow 488: /*
489: * Print the document's header info
490: *
491: *@param out the servlet's output stream
492: */
1.1 barstow 493: private void printDocumentHeader (ServletOutputStream out) {
494:
495: try {
496:
497: out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"");
498: out.println(" \"http://www.w3.org/TR/REC-html40/loose.dtd\">");
499: out.println("<HTML><HEAD>");
500: out.println("<TITLE>RDF creation</TITLE>");
501: out.println("<LINK HREF=\"rdf.css\" REL=\"stylesheet\">");
502: out.println("</HEAD>");
503: out.println("<BODY>");
504:
505: } catch (Exception e) {
1.2 barstow 506: System.err.println("Exception: " + e.getMessage());
1.1 barstow 507: }
508: }
509:
1.3 barstow 510: /*
511: * Print the rdf listing
512: *
513: *@param out the servlet's output stream
514: *@param rdf the RDF code
515: */
1.16 barstow 516: private void printListing (ServletOutputStream out, String rdf, boolean needCR) {
1.1 barstow 517: try {
518: out.println("<hr title=\"original source\">");
519: out.println("<h3>The original RDF/XML document</h3>");
520: out.println("<pre>");
521:
1.2 barstow 522: String s = replaceString(rdf, "<", "<");
523: StringTokenizer st = new StringTokenizer(s, "\n");
1.1 barstow 524:
525: // Now output the RDF one line at a time with line numbers
526: int lineNum = 1;
527: while (st.hasMoreTokens()) {
1.16 barstow 528: if (needCR)
529: out.print ("<a name=\"" + lineNum + "\">" + lineNum +
530: "</a>: " + st.nextToken() + "\n");
531: else
532: out.print ("<a name=\"" + lineNum + "\">" + lineNum +
533: "</a>: " + st.nextToken());
1.1 barstow 534: lineNum++;
535: }
536:
537: out.println("</pre>");
538: } catch (Exception e) {
539: System.err.println("Exception: " + e.getMessage());
540: }
541: }
542:
1.3 barstow 543: /*
544: * Print the header for the triple listing
545: *
546: *@param out the servlet's output stream
547: */
1.17 barstow 548: private void printTripleTableHeader (ServletOutputStream out) {
1.1 barstow 549: try {
550: out.println("<hr title=\"triples\">");
551: out.println("<h3>Triples of the data model</h3>");
1.17 barstow 552: out.println("<table border><tr>" +
553: "<td><b>Number</b></td>" +
554: "<td><b>Subject</b></td>" +
555: "<td><b>Predicate</b></td>" +
556: "<td><b>Object</b></td>" +
557: "</tr>");
1.1 barstow 558: } catch (Exception e) {
559: System.err.println("Exception: " + e.getMessage());
560: }
561: }
562:
1.3 barstow 563: /*
564: * Print the footer info for the triple listing
565: *
566: *@param out the servlet's output stream
567: */
1.17 barstow 568: private void printTripleTableFooter (ServletOutputStream out, SiRPACServletDumpConsumer consumer) {
1.1 barstow 569: try {
1.17 barstow 570: out.println("</table>");
1.14 barstow 571: if (consumer != null)
572: out.println("<p>The number of triples = " + consumer.getNumStatements() + "</p>");
1.1 barstow 573: } catch (Exception e) {
574: System.err.println("Exception: " + e.getMessage());
575: }
576: }
1.3 barstow 577:
578: /*
579: * Print the document's footer info
580: *
581: *@param out the servlet's output stream
582: *@param rdf the RDF code
583: */
1.1 barstow 584: private void printDocumentFooter (ServletOutputStream out, String rdf) {
585: try {
586:
587: out.println("<hr title=\"Problem reporting\">");
588: out.println("<h3>Feedback</h3>");
1.2 barstow 589: out.println("<p>If you suspect that SiRPAC produced an 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>");
1.1 barstow 590: out.println("<form enctype=\"text/plain\" method=\"post\" action=\"mailto:" + MAIL_TO + "\">");
591: out.println("<textarea cols=\"60\" rows=\"4\" name=\"report\"></textarea>");
1.2 barstow 592: out.println("<p><input type=\"hidden\" name=\"RDF\" value=\"<?xml version="1.0"?>");
1.1 barstow 593:
1.2 barstow 594: // The listing is being passed as a parameter so the '<'
595: // and '"' characters must be replaced with < and ",
596: // respectively
1.16 barstow 597: if (rdf != null) {
598: String s1 = replaceString(rdf, "<", "<");
599: String s2 = replaceString(s1, "\"", """);
600: out.println(s2 + "\">");
601: }
1.1 barstow 602:
603: out.println("<input type=\"submit\" value=\"Submit problem report\">");
604: out.println("</form>");
1.13 barstow 605: out.println("<hr/>");
1.1 barstow 606: out.println("</BODY>");
607: out.println("</HTML>");
608:
609: } catch (Exception e) {
1.2 barstow 610: System.err.println("Exception: " + e.getMessage());
1.1 barstow 611: }
612:
613: }
614:
1.3 barstow 615: /*
1.16 barstow 616: * Given a URI string, open it, read its contents into a String
617: * and return the String
618: *
619: *@param uri the URI to open
620: *@return the content at the URI or null if any error occurs
621: */
622: private String getByteStream (String uri) {
623: try {
624: URL url = new URL(uri);
625: InputStream is = url.openStream();
626: String s = new String("");
627:
628: int c;
629: int numRead = 0;
630:
631: while ((c = is.read()) != -1) {
632: s += (char)c;
633: if (numRead == 15) {
634: // A server could return content but not the RDF/XML that
635: // we need. Check the beginning of s and if it looks like
636: // a generic HTML message, return an error.
637: if (s.startsWith("<!DOCTYPE HTML"))
638: return null;
639: }
640: numRead++;
641: }
642:
643: if (s.equals(""))
644: // Nothing was returned
645: return null;
646:
647: return s;
648:
649: } catch (Exception e) {
650: return null;
651: }
652: }
653:
654: /*
1.3 barstow 655: * Servlet's get info method
656: */
1.1 barstow 657: public String getServletInfo () {
658: return "Servlet Wrapper for SiRPAC. This is revision " + REVISION;
659: }
660:
1.3 barstow 661: /*
662: * Servlet's init method
663: *
664: *@param config the servlet's configuration object
665: */
1.1 barstow 666: public void init(ServletConfig config) throws ServletException {
667: super.init (config);
668:
1.3 barstow 669: // Cache the parameters
670: m_SiRPACTmpDir = config.getInitParameter(SIRPAC_TMP_DIR);
1.5 barstow 671:
672: // All of the Graph Viz paths extend from GRAPH_VIZ_ROOT
673: String GraphVizRoot = config.getInitParameter(GRAPH_VIZ_ROOT);
674:
675: m_GraphVizPath = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_PATH);
676: m_GraphVizFontDir = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_FONT_DIR);
677: m_GraphVizLibDir = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_LIB_DIR);
1.1 barstow 678: }
679:
1.3 barstow 680: /*
681: * Servlet's destroy info method
682: */
1.1 barstow 683: public void destroy () {
684: super.destroy ();
685: }
686:
1.3 barstow 687: /*
1.4 barstow 688: * Servlet's doGet info method - NOT supported
1.3 barstow 689: *
690: *@param req the request
691: *@param res the response
692: */
1.1 barstow 693: public void doGet (HttpServletRequest req, HttpServletResponse res)
694: throws ServletException, IOException {
1.3 barstow 695:
1.1 barstow 696: ServletOutputStream out = res.getOutputStream ();
697:
698: res.setContentType ("text/html");
699:
1.3 barstow 700: out.println ("<h1>GET is NOT supported!</h1>\n\n<p>Please send RDF through POST!</p>\n");
1.1 barstow 701: }
702:
1.3 barstow 703: /*
704: * Servlet's doPost method
705: *
706: *@param req the request
707: *@param res the response
708: */
1.1 barstow 709: public void doPost (HttpServletRequest req, HttpServletResponse res)
710: throws ServletException, IOException {
711:
712: ServletOutputStream out = res.getOutputStream ();
713:
1.19 ! barstow 714: String sRDF = req.getParameter (TEXT);
! 715: String sBags = req.getParameter (BAG);
! 716: String sStreamMode = req.getParameter (STREAM_MODE);
! 717: String sSaveRDF = req.getParameter (SAVE_RDF);
! 718: String sSaveDOTFile = req.getParameter (SAVE_DOT_FILE);
! 719: String sURI = req.getParameter (URI);
! 720: String sFormat = req.getParameter (FORMAT);
1.16 barstow 721:
1.18 barstow 722: SiRPAC sirpac = new SiRPAC();
723: ErrorStore errorHandler = new ErrorStore();
1.16 barstow 724: InputSource is = null;
725: boolean error = false;
726: String sError = null;
727:
1.1 barstow 728: SiRPACServletDumpConsumer consumer = new SiRPACServletDumpConsumer();
1.9 barstow 729:
1.18 barstow 730: // Initialize the parser
731: sirpac.setErrorHandler(errorHandler);
1.1 barstow 732:
1.16 barstow 733: // If a URI was sent in the request, it takes precedence
734: if (sURI != null && sURI.length() >= 1) {
735: try {
736: is = GenericParser.getInputSource(sURI);
737: sRDF = getByteStream(sURI);
738: if (sRDF == null)
739: sError = "An attempt to load the RDF from URI '" + sURI + "' failed. (The file may not exist or the server is down.)";
1.18 barstow 740: sirpac.setSource(sURI);
1.16 barstow 741: } catch (MalformedURLException e) {
742: sError = "An attempt to load URI '" + sURI
743: + "' produced a MalforedURL Exception.\n["
744: + e.getMessage() + "]";
745: } catch (IOException e) {
746: sError = "An attempt to load URI '" + sURI
747: + "' produced an IO Exception.\n["
748: + e.getMessage() + "]";
749: }
750: } else {
751: StringReader sr = new StringReader (sRDF);
752: is = new InputSource (sr);
1.18 barstow 753: sirpac.setSource("online:#");
1.16 barstow 754: }
755:
756: if (sError != null) {
757: printDocumentHeader (out);
758: out.println ("<h1>" + sError + "</h1>\n");
759: printDocumentFooter(out, null);
760: return;
761: }
762:
1.1 barstow 763: printDocumentHeader (out);
1.16 barstow 764: printListing (out, sRDF, sURI != null && sURI.length() >= 1);
1.17 barstow 765: printTripleTableHeader (out);
1.1 barstow 766:
767: try {
1.3 barstow 768: // Override the default triple output handler
1.1 barstow 769: consumer.setOutputStream(out);
770:
1.18 barstow 771: // Create a bag per Description element? [off by default]
1.1 barstow 772: if (sBags != null && sBags.equals ("on"))
1.18 barstow 773: sirpac.createBags (true);
1.10 barstow 774:
1.18 barstow 775: // Set parser's streaming mode [on by default]
1.11 barstow 776: if (sStreamMode != null && sStreamMode.equals ("on"))
1.18 barstow 777: sirpac.setStreamMode (false);
1.1 barstow 778:
1.18 barstow 779: sirpac.parse(is, consumer);
1.1 barstow 780:
1.17 barstow 781: printTripleTableFooter(out, consumer);
1.12 barstow 782:
1.19 ! barstow 783: if (sFormat != null && !sFormat.equals(FORMAT_NO_GRAPH)) {
! 784: generateGraph(out,
! 785: sRDF,
! 786: req,
! 787: sirpac.source(),
! 788: sFormat,
! 789: (sSaveRDF != null) ? true : false,
! 790: (sSaveDOTFile != null && sSaveDOTFile.equals ("on") ? true : false),
! 791: (sBags != null && sBags.equals ("on") ? true : false),
! 792: (sStreamMode != null && sStreamMode.equals ("on") ? false : true));
! 793: }
1.2 barstow 794:
1.1 barstow 795: } catch (SAXException e) {
1.3 barstow 796: error = true;
1.1 barstow 797: } catch (Exception e) {
1.3 barstow 798: error = true;
1.1 barstow 799: e.printStackTrace ();
800: }
801:
802: res.setContentType ("text/html");
1.3 barstow 803:
804: if (error) {
1.17 barstow 805: printTripleTableFooter(out, null);
1.1 barstow 806: out.println ("<h1>Errors during parsing</h1>\n");
807: out.println ("<pre>\n");
1.2 barstow 808:
809: // Make the line number a link to the listing
1.18 barstow 810: out.println ("Fatal error: " + errorHandler.getErrorMessage());
1.1 barstow 811: out.println (" (Line number = " + "<a href=\"#" +
1.18 barstow 812: errorHandler.getLineNumber() + "\">" +
813: errorHandler.getLineNumber() + "</a>" +
1.1 barstow 814: ", Column number = " +
1.18 barstow 815: errorHandler.getColumnNumber() + ")");
1.1 barstow 816:
817: out.println ("</pre>\n\n");
818: }
819:
820: printDocumentFooter(out, sRDF);
821: }
822: }
Webmaster