Annotation of java/classes/org/w3c/rdf/examples/SiRPACServlet.java, revision 1.14

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.4       barstow    16:  * 1. "RDF" - the RDF/XML document 
                     17:  * 2. "BAGS" - if "on", each Description should have its own Bag;
                     18:  *   the default is not to do this.
1.11      barstow    19:  * 3. "STREAM" if "on", the stream mode is turned off so that aboutEach
1.10      barstow    20:  *   and aboutEachPrefix are supported.
1.14    ! barstow    21:  * 4. "SAVE_RDF" if "on", the RDF will be copied to a file.
1.1       barstow    22:  *
1.7       barstow    23:  * @author Art Barstow <barstow@w3.org>
1.1       barstow    24:  *
1.7       barstow    25:  * The graphics package is AT&T's GraphVis tool.
1.1       barstow    26:  */
                     27: 
                     28: package org.w3c.rdf.examples;
                     29: 
                     30: import java.io.*;
                     31: import java.util.StringTokenizer;
                     32: import java.util.Enumeration;
                     33: import javax.servlet.*;
                     34: import javax.servlet.http.*;
                     35: 
                     36: import org.xml.sax.InputSource;
                     37: import org.xml.sax.Parser;
                     38: import org.xml.sax.SAXException;
                     39: import org.xml.sax.helpers.*;
                     40: 
                     41: import org.w3c.rdf.model.*;
                     42: import org.w3c.rdf.syntax.*;
                     43: import org.w3c.rdf.syntax.RDFConsumer;
                     44: import org.w3c.rdf.util.xml.DumpConsumer;
                     45: import org.w3c.rdf.util.xml.ErrorStore;
                     46: import org.w3c.rdf.implementation.model.StatementImpl;
                     47: import org.w3c.rdf.implementation.model.NodeFactoryImpl;
                     48: import org.w3c.rdf.implementation.syntax.sirpac.*;
                     49: 
                     50: public class SiRPACServlet extends HttpServlet
                     51: {
1.14    ! barstow    52:     final static public String REVISION = "$Id: SiRPACServlet.java,v 1.13 2000/10/27 19:52:27 barstow Exp $";
1.1       barstow    53: 
1.7       barstow    54:     // The email address for bug reports
1.4       barstow    55:     private static final String MAIL_TO = "barstow@w3.org";
1.10      barstow    56: 
                     57:     // Names of the POST parameters
                     58:     //   The XML'ized RDF
                     59:     private static final String POST_TEXT            = "RDF";
                     60:     //   Flag for turning on treating each Description as a bag
                     61:     private static final String POST_BAG             = "BAGS";
                     62:     //   Flag for turning off SiRPAC's stream parsing mode
                     63:     private static final String POST_STREAM_MODE     = "STREAM";
1.14    ! barstow    64:     //   Flag to indicate that the RDF should be copied to a file
        !            65:     private static final String POST_SAVE_RDF        = "SAVE_RDF";
1.3       barstow    66:  
                     67:     // Names of the servlet's parameters
1.5       barstow    68:     private static final String SIRPAC_TMP_DIR       = "SIRPAC_TMP_DIR";
                     69:     private static final String GRAPH_VIZ_ROOT       = "GRAPH_VIZ_ROOT";
                     70:     private static final String GRAPH_VIZ_PATH       = "GRAPH_VIZ_PATH";
                     71:     private static final String GRAPH_VIZ_LIB_DIR    = "GRAPH_VIZ_LIB_DIR";
                     72:     private static final String GRAPH_VIZ_FONT_DIR   = "GRAPH_VIZ_FONT_DIR";
1.3       barstow    73: 
                     74:     // Variables for the servlet's parameters
1.5       barstow    75:     private static String m_SiRPACTmpDir      = null;
                     76:     private static String m_GraphVizPath      = null;
                     77:     private static String m_GraphVizFontDir   = null;
                     78:     private static String m_GraphVizLibDir    = null;
1.4       barstow    79: 
                     80:     // Names of environment variable need by GraphVis
                     81:     private static String DOTFONTPATH     = "DOTFONTPATH";
                     82:     private static String LD_LIBRARY_PATH = "LD_LIBRARY_PATH";
                     83: 
                     84:     // Names used for temporary files
                     85:     private static final String TMP_FILE_PREFIX = "sirpac_";
                     86:     private static final String TMP_DIR_SUFFIX  = ".tmp";
                     87:     private static final String DOT_SUFFIX      = ".dot";
                     88:     private static final String GIF_SUFFIX      = ".gif";
1.14    ! barstow    89:     private static final String RDF_SUFFIX      = ".rdf";
1.3       barstow    90: 
1.6       barstow    91:     // Default GraphViz parameter names and their default values
                     92:     private static final String NODE_COLOR         = "NODE_COLOR";
                     93:     private static final String DEFAULT_NODE_COLOR = "black";
                     94: 
                     95:     private static final String NODE_TEXT_COLOR         = "NODE_TEXT_COLOR";
                     96:     private static final String DEFAULT_NODE_TEXT_COLOR = "black";
                     97: 
                     98:     private static final String EDGE_COLOR         = "EDGE_COLOR";
                     99:     private static final String DEFAULT_EDGE_COLOR = "black";
                    100: 
                    101:     private static final String EDGE_TEXT_COLOR         = "EDGE_TEXT_COLOR";
                    102:     private static final String DEFAULT_EDGE_TEXT_COLOR = "black";
                    103: 
                    104:     private static final String ORIENTATION         = "ORIENTATION";
                    105:     private static final String DEFAULT_ORIENTATION = "TB";  // Top to Bottom
                    106: 
                    107:     private static final String FONT_SIZE         = "FONT_SIZE";
                    108:     private static final String DEFAULT_FONT_SIZE = "10";
                    109: 
                    110:     // Fonts are not currently configurable
                    111:     private static final String DEFAULT_FONT = "arial";
                    112: 
1.8       barstow   113:     // Servlet name
                    114:     private static final String SERVLET_NAME = "SiRPACServlet";
                    115: 
1.3       barstow   116:     // The parser
                    117:     private SiRPAC       m_sirpac = null;
                    118:     // The error handler
                    119:     private ErrorStore    m_errorHandler;
1.1       barstow   120: 
1.3       barstow   121:     /*
1.4       barstow   122:      * Create a File object in the m_SiRPACTmpDir directory
1.3       barstow   123:      *
1.4       barstow   124:      *@param directory the file's directory
                    125:      *@param prefix the file's prefix name (not its directory)
                    126:      *@param suffix the file's suffix or extension name
                    127:      *@return a File object if a temporary file is created; null otherwise
1.3       barstow   128:      */
1.4       barstow   129:     private File createTempFile (String directory, String prefix, String suffix) {
                    130:         File f;
                    131:         try {
                    132:             File d = new File(directory);
                    133:             f = File.createTempFile(prefix, suffix, d);
                    134:         } catch (Exception e) {
                    135:             return null;
                    136:         }
                    137:         return f;
                    138:     }
                    139: 
1.14    ! barstow   140: 
        !           141:     /*
        !           142:      * Copy the given string of RDF to a file in the given directory
        !           143:      *
        !           144:      *@param dir the file's directory
        !           145:      *@param rdf the string of RDF
        !           146:      *@return void
        !           147:      */
        !           148: 
        !           149:     private void copyRDFStringToFile(String tmpDir, String rdf) 
        !           150:     {
        !           151:         try {
        !           152:             // Generate a unique file name 
        !           153:             File tmpFile = createTempFile(tmpDir, TMP_FILE_PREFIX, RDF_SUFFIX);
        !           154:             if (tmpFile == null) {
        !           155:                 // Not really a critical error, just return
        !           156:                 return;
        !           157:             }
        !           158: 
        !           159:             // Create a PrintWriter for the GraphViz consumer
        !           160:             FileWriter fw = new FileWriter(tmpFile);
        !           161:             PrintWriter pw = new PrintWriter(fw);
        !           162: 
        !           163:             pw.println(rdf);
        !           164:             pw.close();
        !           165:         } catch (Exception e) {
        !           166:             // Just return - not critical
        !           167:             return;
        !           168:         }
        !           169:     }
        !           170: 
1.4       barstow   171:     /*
                    172:      * Invokes the GraphVis program to create a GIF image from the
                    173:      * the given DOT data file
                    174:      *
                    175:      *@param dotFileName the name of the DOT data file
                    176:      *@param gifFileName the name of the GIF data file 
                    177:      *@return true if success; false if any failure occurs
                    178:      */
                    179:     private boolean generateGifFile(String dotFileName, String gifFileName) {
1.5       barstow   180:         String environment[] = {DOTFONTPATH     + "=" + m_GraphVizFontDir,
                    181:                                 LD_LIBRARY_PATH + "=" + m_GraphVizLibDir};
1.4       barstow   182: 
1.5       barstow   183:         String cmdArray[] = {m_GraphVizPath, "-Tgif", "-o", gifFileName, dotFileName};
1.4       barstow   184: 
                    185:         Runtime rt = Runtime.getRuntime();
                    186:         try {
                    187:             Process p = rt.exec(cmdArray, environment);
                    188:             p.waitFor();
                    189:         } catch (Exception e) {
                    190:             return false;
                    191:         }
                    192: 
                    193:         return true;
1.3       barstow   194:     }
1.1       barstow   195: 
1.3       barstow   196:     /*
1.6       barstow   197:      * Returns a parameter from a request or the parameter's default
                    198:      * value.
                    199:      *
                    200:      *@param req a Servlet request
                    201:      *@return if the request contains the specfied parameter its value
                    202:      *  in the request is returned; otherwise its default value is
                    203:      *  returned
                    204:      */
                    205:     private String getParameter(HttpServletRequest req, String param, String defString) {
                    206:         String s = req.getParameter(param);
                    207:         return (s == null) ? defString : s;
                    208:     }
                    209: 
                    210:     /*
                    211:      * If the request contains any graph-related parameters, pass them
                    212:      * to the graph consumer for handling
                    213:      *
                    214:      *@param req the response
                    215:      *@param consumer the GraphViz consumer
                    216:      */
                    217:     private void processGraphParameters (HttpServletRequest req, GraphVizDumpConsumer consumer) {
                    218:         // Look for colors
                    219: 
                    220:        String s;
                    221:        
                    222:         String nodeColor     = getParameter (req, NODE_COLOR, DEFAULT_NODE_COLOR);
                    223:         String nodeTextColor = getParameter (req, NODE_TEXT_COLOR, DEFAULT_NODE_TEXT_COLOR);
                    224:         String edgeColor     = getParameter (req, EDGE_COLOR, DEFAULT_EDGE_COLOR);
                    225:         String edgeTextColor = getParameter (req, EDGE_TEXT_COLOR, DEFAULT_EDGE_TEXT_COLOR);
                    226:         String fontSize = getParameter (req, FONT_SIZE, DEFAULT_FONT_SIZE);
                    227: 
                    228:         // Orientation must be either 
                    229:         String orientation = req.getParameter (ORIENTATION);
                    230:         if (orientation.equals("Left to Right"))
                    231:             orientation = "LR";
                    232:         else
                    233:             orientation = DEFAULT_ORIENTATION;
                    234: 
                    235:         // Add an attribute for all of the graph's nodes
                    236:         consumer.addGraphAttribute("node [fontname=" + DEFAULT_FONT + 
                    237:                                    ",fontsize="  + fontSize +
                    238:                                    ",color="     + nodeColor +
                    239:                                    ",fontcolor=" + nodeTextColor + "]");
                    240: 
                    241:         // Add an attribute for all of the graph's edges
                    242:         consumer.addGraphAttribute("edge [fontname=" + DEFAULT_FONT + 
                    243:                                    ",fontsize="  + fontSize +
                    244:                                    ",color="     + edgeColor +
                    245:                                    ",fontcolor=" + edgeTextColor + "]");
                    246: 
                    247:         // Add an attribute for the orientation
                    248:         consumer.addGraphAttribute("rankdir=" + orientation + ";");
                    249:     }
                    250: 
                    251:     /*
1.3       barstow   252:      * Generate a graph of the RDF data model
                    253:      *
1.4       barstow   254:      *@param out the servlet's output stream
1.6       barstow   255:      *@param rdf the RDF text
                    256:      *@param req a Servlet request
1.3       barstow   257:      */
1.14    ! barstow   258:     private void generateGraph (ServletOutputStream out, String rdf, HttpServletRequest req, boolean saveRDF) {
1.2       barstow   259:         try {
                    260:             out.println("<hr title=\"visualisation\">");
1.3       barstow   261:             out.println("<h3>Graph of the data model</h3>");
                    262: 
1.4       barstow   263:             // Stop if any of the parameters are missing
1.5       barstow   264:             if (m_SiRPACTmpDir == null || m_GraphVizPath == null || 
                    265:                 m_GraphVizFontDir == null || m_GraphVizLibDir == null) { 
1.8       barstow   266:  
                    267:                 // Put the paths in a comment in the returned content
                    268:                 out.println("<!-- SIRPAC TMP = " + m_SiRPACTmpDir);
                    269:                 out.println("GRAPH VIZ  = " + m_GraphVizPath);
                    270:                 out.println("GRAPH LIB  = " + m_GraphVizLibDir);
                    271:                 out.println("GRAPH FON  = " + m_GraphVizFontDir + " -->");
                    272: 
1.3       barstow   273:                 out.println("Servlet initialization failed.  A graph cannot be generated.");
                    274:                 return;
                    275:             } 
                    276: 
1.4       barstow   277:             // The temporary directory
                    278:             String tmpDir = m_SiRPACTmpDir;
1.3       barstow   279: 
1.4       barstow   280:             // Must generate a unique file name that the DOT consumer
                    281:             // will use 
                    282:             File dotFile = createTempFile(tmpDir, TMP_FILE_PREFIX, DOT_SUFFIX);
                    283:             if (dotFile == null) {
                    284:                 out.println("Failed to create a temporary DOT file. A graph cannot be generated.");
1.3       barstow   285:                 return;
                    286:             }
                    287: 
                    288:             // Create a PrintWriter for the GraphViz consumer
1.4       barstow   289:             FileWriter fw = new FileWriter(dotFile);
1.3       barstow   290:             PrintWriter pw = new PrintWriter(fw);
                    291: 
                    292:             // Run the parser using the DOT consumer to capture
                    293:             // the output in a file
                    294:            StringReader         sr = new StringReader (rdf);
                    295:            InputSource          is = new InputSource (sr);
                    296:             GraphVizDumpConsumer consumer = new GraphVizDumpConsumer(pw);
                    297: 
1.6       barstow   298:             // Process any graph-related parameters in the request
                    299:             processGraphParameters(req, consumer);
                    300: 
1.3       barstow   301:            try {
                    302:                 m_sirpac.parse(is, consumer);
                    303:            } catch (Exception e) {
                    304:                 out.println("An attempt to generate the graph data failed ("
                    305:                             + e.getMessage() + ").");
1.4       barstow   306:                 pw.close();
                    307:                 dotFile.delete();
1.3       barstow   308:                 return;
                    309:            }
                    310: 
1.4       barstow   311:             // Must close the DOT input file so the GraphViz can
                    312:             // open and read it
                    313:             pw.close();
                    314: 
                    315:             // Must generate a unique file name for the GIF file
                    316:             // that will be created
                    317:             File gifFile = createTempFile(tmpDir, TMP_FILE_PREFIX, GIF_SUFFIX);
                    318:             if (gifFile == null) {
                    319:                 out.println("Failed to create a temporary GIF file. A graph cannot be generated.");
                    320:                 dotFile.delete();
                    321:                 return;
                    322:             }
                    323: 
1.3       barstow   324:             // Pass the DOT data file to the GraphViz dot program
1.4       barstow   325:             // so it can create a GIF image of the data model
                    326:             String dotFileName = dotFile.getAbsolutePath();
                    327:             String gifFileName = gifFile.getAbsolutePath();
                    328: 
                    329:             if (!generateGifFile(dotFileName, gifFileName)) {
                    330:                 out.println("An attempt to create a graph failed.");
                    331:                 dotFile.delete();
                    332:                 gifFile.delete();
                    333:                 return;
                    334:             }
1.3       barstow   335: 
1.7       barstow   336:             // Cleanup
                    337:             dotFile.delete();
1.3       barstow   338: 
1.9       barstow   339:             // NOTE: Cannot delete the GIF file here because its
                    340:             // pathname is returned to the client
1.8       barstow   341:             String imagePath = SERVLET_NAME +
1.7       barstow   342:                                TMP_DIR_SUFFIX + File.separator + 
                    343:                                gifFile.getName();
1.2       barstow   344: 
1.4       barstow   345:             if (gifFile.length() > 0)
                    346:                 out.println("<img src=\"" + imagePath + "\"/>");
                    347:             else
                    348:                 out.println("The graph image file is empty.");
1.2       barstow   349: 
1.14    ! barstow   350:             // One last thing to do before exiting - copy the RDF to a file
        !           351:             if (saveRDF)
        !           352:                 copyRDFStringToFile(tmpDir, rdf);
        !           353: 
1.2       barstow   354:         } catch (Exception e) {
                    355:             System.err.println("Exception: " + e.getMessage());
                    356:         }
                    357:     }
                    358: 
                    359:     /*
1.3       barstow   360:      * Search the given string for substring "key"
1.2       barstow   361:      * and if it is found, replace it with string "replacement"
                    362:      *
1.4       barstow   363:      *@param input the input string
                    364:      *@param key the string to search for
                    365:      *@param replacement the string to replace all occurences of "key"
1.2       barstow   366:      *@return if no substitutions are done, input is returned; otherwise 
                    367:      * a new string is returned.
                    368:      */
1.12      barstow   369:     public static String replaceString(String input, String key, String replacement) {
1.2       barstow   370:         StringBuffer sb = new StringBuffer("");
                    371:         StringTokenizer st = new StringTokenizer(input, key);
                    372: 
                    373:         // Must handle the case where the input string begins with the key
                    374:         if (input.startsWith(key))
                    375:             sb = sb.append(replacement);
                    376:         while (st.hasMoreTokens()) {
                    377:             sb = sb.append(st.nextToken());
                    378:             if (st.hasMoreTokens())
                    379:                 sb = sb.append(replacement);
                    380:         }
                    381:         if (sb.length() >= 1)
                    382:             return sb.toString();
                    383:         else
                    384:             return input;
                    385:     }
                    386: 
1.3       barstow   387:     /*
                    388:      * Print the document's header info
                    389:      *
                    390:      *@param out the servlet's output stream
                    391:      */
1.1       barstow   392:     private void printDocumentHeader (ServletOutputStream out) {
                    393: 
                    394:         try {
                    395: 
                    396:             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"");
                    397:             out.println("      \"http://www.w3.org/TR/REC-html40/loose.dtd\">");
                    398:             out.println("<HTML><HEAD>");
                    399:             out.println("<TITLE>RDF creation</TITLE>");
                    400:             out.println("<LINK HREF=\"rdf.css\" REL=\"stylesheet\">");
                    401:             out.println("</HEAD>");
                    402:             out.println("<BODY>");
                    403: 
                    404:         } catch (Exception e) {
1.2       barstow   405:             System.err.println("Exception: " + e.getMessage());
1.1       barstow   406:         }
                    407:     }
                    408: 
1.3       barstow   409:     /*
                    410:      * Print the rdf listing
                    411:      *
                    412:      *@param out the servlet's output stream
                    413:      *@param rdf the RDF code
                    414:      */
1.1       barstow   415:     private void printListing (ServletOutputStream out, String rdf) {
                    416:         try {
                    417:             out.println("<hr title=\"original source\">");
                    418:             out.println("<h3>The original RDF/XML document</h3>");
                    419:             out.println("<pre>");
                    420: 
1.2       barstow   421:             String s = replaceString(rdf, "<", "&lt;");
                    422:             StringTokenizer st = new StringTokenizer(s, "\n");
1.1       barstow   423: 
                    424:             // Now output the RDF one line at a time with line numbers
                    425:             int lineNum = 1;
                    426:             while (st.hasMoreTokens()) {
                    427:                 out.print ("<a name=\"" + lineNum + "\">" + lineNum +
                    428:                           "</a>: " + st.nextToken());
                    429:                 lineNum++;
                    430:             }
                    431: 
                    432:             out.println("</pre>");
                    433:         } catch (Exception e) {
                    434:             System.err.println("Exception: " + e.getMessage());
                    435:         }
                    436:     }
                    437: 
1.3       barstow   438:     /*
                    439:      * Print the header for the triple listing
                    440:      *
                    441:      *@param out the servlet's output stream
                    442:      */
1.1       barstow   443:     private void printTripleHeader (ServletOutputStream out) {
                    444:         try {
                    445:             out.println("<hr title=\"triples\">");
                    446:             out.println("<h3>Triples of the data model</h3>");
                    447:             
                    448:             // The output for each triple will be pre-formatted
                    449:             out.println("<pre>");
                    450:         } catch (Exception e) {
                    451:             System.err.println("Exception: " + e.getMessage());
                    452:         }
                    453:     }
                    454: 
1.3       barstow   455:     /*
                    456:      * Print the footer info for the triple listing
                    457:      *
                    458:      *@param out the servlet's output stream
                    459:      */
1.14    ! barstow   460:     private void printTripleFooter (ServletOutputStream out, SiRPACServletDumpConsumer consumer) {
1.1       barstow   461:         try {
                    462:             out.println("</pre>");
1.14    ! barstow   463:             if (consumer != null)
        !           464:                 out.println("<p>The number of triples = " + consumer.getNumStatements() + "</p>");
1.1       barstow   465:         } catch (Exception e) {
                    466:             System.err.println("Exception: " + e.getMessage());
                    467:         }
                    468:     }
1.3       barstow   469: 
                    470:     /*
                    471:      * Print the document's footer info
                    472:      *
                    473:      *@param out the servlet's output stream
                    474:      *@param rdf the RDF code
                    475:      */
1.1       barstow   476:     private void printDocumentFooter (ServletOutputStream out, String rdf) {
                    477:         try {
                    478: 
                    479:             out.println("<hr title=\"Problem reporting\">");
                    480:             out.println("<h3>Feedback</h3>");
1.2       barstow   481:             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   482:             out.println("<form enctype=\"text/plain\" method=\"post\" action=\"mailto:" + MAIL_TO + "\">");
                    483:             out.println("<textarea cols=\"60\" rows=\"4\" name=\"report\"></textarea>");
1.2       barstow   484:             out.println("<p><input type=\"hidden\" name=\"RDF\" value=\"&lt;?xml version=&quot;1.0&quot;?>");
1.1       barstow   485: 
1.2       barstow   486:             // The listing is being passed as a parameter so the '<' 
                    487:             // and '"' characters must be replaced with &lt; and &quot, 
                    488:             // respectively
                    489:             String s1 = replaceString(rdf, "<", "&lt;");
                    490:             String s2 = replaceString(s1,  "\"", "&quot;");
                    491:             out.println(s2 + "\">");
1.1       barstow   492: 
                    493:             out.println("<input type=\"submit\" value=\"Submit problem report\">");
                    494:             out.println("</form>");
1.13      barstow   495:             out.println("<hr/>");
1.1       barstow   496:             out.println("</BODY>");
                    497:             out.println("</HTML>");
                    498: 
                    499:         } catch (Exception e) {
1.2       barstow   500:             System.err.println("Exception: " + e.getMessage());
1.1       barstow   501:         }
                    502: 
                    503:     }
                    504: 
1.3       barstow   505:     /*
                    506:      * Servlet's get info method
                    507:      */
1.1       barstow   508:     public String getServletInfo () {
                    509:        return "Servlet Wrapper for SiRPAC. This is revision " + REVISION;
                    510:     }
                    511: 
1.3       barstow   512:     /*
                    513:      * Servlet's init method
                    514:      *
                    515:      *@param config the servlet's configuration object
                    516:      */
1.1       barstow   517:     public void init(ServletConfig config) throws ServletException {
                    518:        super.init (config);
                    519: 
                    520:        m_sirpac = new SiRPAC();
                    521:         m_errorHandler = new ErrorStore();
1.3       barstow   522:         m_sirpac.setErrorHandler(m_errorHandler);
1.1       barstow   523: 
1.3       barstow   524:         // Cache the parameters
                    525:         m_SiRPACTmpDir = config.getInitParameter(SIRPAC_TMP_DIR);
1.5       barstow   526: 
                    527:         // All of the Graph Viz paths extend from GRAPH_VIZ_ROOT
                    528:         String GraphVizRoot = config.getInitParameter(GRAPH_VIZ_ROOT);
                    529: 
                    530:         m_GraphVizPath = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_PATH);
                    531:         m_GraphVizFontDir = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_FONT_DIR);
                    532:         m_GraphVizLibDir = GraphVizRoot + "/" + config.getInitParameter(GRAPH_VIZ_LIB_DIR);
1.1       barstow   533:     }
                    534: 
1.3       barstow   535:     /*
                    536:      * Servlet's destroy info method
                    537:      */
1.1       barstow   538:     public void destroy () {
                    539:        super.destroy ();
                    540:     }
                    541: 
1.3       barstow   542:     /*
1.4       barstow   543:      * Servlet's doGet info method - NOT supported
1.3       barstow   544:      *
                    545:      *@param req the request
                    546:      *@param res the response
                    547:      */
1.1       barstow   548:     public void doGet (HttpServletRequest req, HttpServletResponse res)
                    549:         throws ServletException, IOException {
1.3       barstow   550: 
1.1       barstow   551:        ServletOutputStream out = res.getOutputStream ();
                    552: 
                    553:        res.setContentType ("text/html");
                    554: 
1.3       barstow   555:        out.println ("<h1>GET is NOT supported!</h1>\n\n<p>Please send RDF through POST!</p>\n");
1.1       barstow   556:     }
                    557: 
1.3       barstow   558:     /*
                    559:      * Servlet's doPost method
                    560:      *
                    561:      *@param req the request
                    562:      *@param res the response
                    563:      */
1.1       barstow   564:     public void doPost (HttpServletRequest req, HttpServletResponse res)
                    565:         throws ServletException, IOException {
                    566: 
                    567:        ServletOutputStream out = res.getOutputStream ();
                    568: 
1.10      barstow   569:        String                       sRDF = req.getParameter (POST_TEXT);
                    570:        String                       sBags = req.getParameter (POST_BAG);
                    571:        String                       sStreamMode = req.getParameter (POST_STREAM_MODE);
1.14    ! barstow   572:        String                       sSaveRDF = req.getParameter (POST_SAVE_RDF);
1.1       barstow   573:        StringReader                 sr = new StringReader (sRDF);
                    574:        InputSource                  is = new InputSource (sr);
1.3       barstow   575:         boolean                      error = false;
1.1       barstow   576:         SiRPACServletDumpConsumer    consumer = new SiRPACServletDumpConsumer();
1.9       barstow   577: 
                    578:         // Re-initialize the parser
                    579:        m_sirpac = new SiRPAC();
                    580:         m_errorHandler = new ErrorStore();
                    581:         m_sirpac.setErrorHandler(m_errorHandler);
1.1       barstow   582: 
                    583:         printDocumentHeader (out);
                    584:         printListing (out, sRDF);
                    585:         printTripleHeader (out);
                    586: 
                    587:        try {
1.3       barstow   588:             // Override the default triple output handler
1.1       barstow   589:             consumer.setOutputStream(out);
                    590: 
1.6       barstow   591:             // Toggle Bag handling - always false unless explicitly
                    592:             // included in the request
                    593:             m_sirpac.createBags (false);
1.1       barstow   594:            if (sBags != null && sBags.equals ("on"))
                    595:                m_sirpac.createBags (true);
1.10      barstow   596: 
                    597:             // Set parser's streaming mode
1.11      barstow   598:            if (sStreamMode != null && sStreamMode.equals ("on"))
1.10      barstow   599:                m_sirpac.setStreamMode (false);
1.1       barstow   600: 
                    601:             m_sirpac.parse(is, consumer);
                    602: 
1.14    ! barstow   603:             printTripleFooter(out, consumer);
1.12      barstow   604: 
1.14    ! barstow   605:             generateGraph(out, sRDF, req, (sSaveRDF != null) ? true : false);
1.2       barstow   606: 
1.1       barstow   607:        } catch (SAXException e) {
1.3       barstow   608:             error = true;
1.1       barstow   609:        } catch (Exception e) {
1.3       barstow   610:             error = true;
1.1       barstow   611:            e.printStackTrace ();
                    612:        }
                    613: 
                    614: 
                    615:        res.setContentType ("text/html");
1.3       barstow   616: 
                    617:        if (error) {
1.14    ! barstow   618:             printTripleFooter(out, null);
1.1       barstow   619:            out.println ("<h1>Errors during parsing</h1>\n");
                    620:             out.println ("<pre>\n");
1.2       barstow   621: 
                    622:             // Make the line number a link to the listing
1.1       barstow   623:             out.println ("Fatal error: " + m_errorHandler.getErrorMessage());
                    624:             out.println ("   (Line number = " + "<a href=\"#" + 
                    625:                          m_errorHandler.getLineNumber() + "\">" + 
                    626:                          m_errorHandler.getLineNumber() + "</a>" +
                    627:                          ", Column number = " + 
                    628:                          m_errorHandler.getColumnNumber() + ")");
                    629: 
                    630:            out.println ("</pre>\n\n");
                    631:        }
                    632: 
                    633:         printDocumentFooter(out, sRDF);
                    634:     }
                    635: }

Webmaster