Diff for /java/classes/org/w3c/rdf/examples/SiRPACServlet.java between versions 1.18 and 1.19

version 1.18, 2001/02/05 22:56:51 version 1.19, 2001/02/20 18:23:42
Line 13 Line 13
  * This servlet is a wrapper for the SiRPAC RDF parser.  The servlet    * This servlet is a wrapper for the SiRPAC RDF parser.  The servlet 
  * expects the following variables through the POST method:   * expects the following variables through the POST method:
  *   *
  * 1. RDF - the RDF/XML document    * RDF - the RDF in an XML syntax 
  * 2. BAGS - if "on", each Description should have its own Bag;   *
  *      the default is not to do this.   * BAGS - if "on", each Description should have its own Bag;
  * 3. STREAM if "on", the stream mode is turned off so that aboutEach   *   the default is not to do this.
  *      and aboutEachPrefix are supported.   *
  * 4. SAVE_RDF if "on", the RDF will be copied to a file.   * STREAM - if "on", the stream mode is turned off so that aboutEach
  * 5. URI - the URI to parse [instead of the RDF]; may not be specified   *   and aboutEachPrefix are supported.  It is off by default.
    *
    * SAVE_DOT_FILE - if "on", the DOT file is saved and a link to the
    *   file is provided to the user
    *
    * SAVE_RDF - if "on", the RDF will be copied to a file.
    *
    * URI - the URI to parse [instead of the RDF]; may not be specified
    *
    * ORIENTATION - the graph's orientation (left to right or top to
    *   bottom
    *
    * FONT_SIZE - the font size to use [10, 12, 14, 16 and 20 are supported]
    *
    * GRAPH_FORMAT - the graph's output format.  Supported values are:
    *
    *     GIF_EMBED - embed the graph as a GIF [the default]
    *     GIF_LINK - don't embed the GIF but create a link for it
    *     SVG_LINK - create the graph in SVG format and create a link to the file
    *     PS_LINK - create a PostScript image of the file and a link to the file
    *     HP_PCL_LINK - create a HPGL/2 - PCL (Laserwriter) image of the file 
    *       and a link to the file
    *     HP_GL_LINK - create a HPGL - PCL (pen plotter) image of the file and 
    *       a link to the file
    *     NO_GRAPH - do not generate a graph
  *   *
  * @author Art Barstow <barstow@w3.org>   * @author Art Barstow <barstow@w3.org>
  *   *
Line 58  public class SiRPACServlet extends HttpS Line 82  public class SiRPACServlet extends HttpS
     // The email address for bug reports      // The email address for bug reports
     private static final String MAIL_TO = "barstow@w3.org";      private static final String MAIL_TO = "barstow@w3.org";
   
     // Names of the POST parameters      // Names of the POST parameters (described above) and their
     //   The XML'ized RDF      // defaults 
     private static final String POST_TEXT            = "RDF";      private static final String TEXT            = "RDF";
     //   Flag for turning on treating each Description as a bag      private static final String BAG             = "BAGS";
     private static final String POST_BAG             = "BAGS";      private static final String STREAM_MODE     = "STREAM";
     //   Flag for turning off SiRPAC's stream parsing mode      private static final String SAVE_DOT_FILE   = "SAVE_DOT_FILE";
     private static final String POST_STREAM_MODE     = "STREAM";      private static final String SAVE_RDF        = "SAVE_RDF";
     //   Flag to indicate that the RDF should be copied to a file      private static final String URI             = "URI";
     private static final String POST_SAVE_RDF        = "SAVE_RDF";  
     //   The URI of the RDF to parse  
     private static final String POST_URI             = "URI";  
     
     // Names of the servlet's parameters  
     private static final String SIRPAC_TMP_DIR       = "SIRPAC_TMP_DIR";  
     private static final String GRAPH_VIZ_ROOT       = "GRAPH_VIZ_ROOT";  
     private static final String GRAPH_VIZ_PATH       = "GRAPH_VIZ_PATH";  
     private static final String GRAPH_VIZ_LIB_DIR    = "GRAPH_VIZ_LIB_DIR";  
     private static final String GRAPH_VIZ_FONT_DIR   = "GRAPH_VIZ_FONT_DIR";  
   
     // Variables for the servlet's parameters  
     private static String m_SiRPACTmpDir      = null;  
     private static String m_GraphVizPath      = null;  
     private static String m_GraphVizFontDir   = null;  
     private static String m_GraphVizLibDir    = null;  
   
     // Names of environment variable need by GraphVis  
     private static String DOTFONTPATH     = "DOTFONTPATH";  
     private static String LD_LIBRARY_PATH = "LD_LIBRARY_PATH";  
   
     // Names used for temporary files  
     private static final String TMP_FILE_PREFIX = "sirpac_";  
     private static final String TMP_DIR_SUFFIX  = ".tmp";  
     private static final String DOT_SUFFIX      = ".dot";  
     private static final String GIF_SUFFIX      = ".gif";  
     private static final String RDF_SUFFIX      = ".rdf";  
   
     // Default GraphViz parameter names and their default values  
     private static final String NODE_COLOR         = "NODE_COLOR";      private static final String NODE_COLOR         = "NODE_COLOR";
     private static final String DEFAULT_NODE_COLOR = "black";      private static final String DEFAULT_NODE_COLOR = "black";
   
Line 113  public class SiRPACServlet extends HttpS Line 109  public class SiRPACServlet extends HttpS
     private static final String FONT_SIZE         = "FONT_SIZE";      private static final String FONT_SIZE         = "FONT_SIZE";
     private static final String DEFAULT_FONT_SIZE = "10";      private static final String DEFAULT_FONT_SIZE = "10";
   
       private static final String FORMAT              = "FORMAT";
       private static final String FORMAT_GIF_EMBED    = "GIF_EMBED";
       private static final String FORMAT_GIF_LINK     = "GIF_LINK";
       private static final String FORMAT_SVG_LINK     = "SVG_LINK";
       private static final String FORMAT_PS_LINK      = "PS_LINK";
       private static final String FORMAT_HP_PCL_LINK  = "HP_PCL_LINK";
       private static final String FORMAT_HP_GL_LINK   = "HP_GL_LINK";
       private static final String FORMAT_NO_GRAPH     = "NO_GRAPH";
   
     // Fonts are not currently configurable      // Fonts are not currently configurable
     private static final String DEFAULT_FONT = "arial";      private static final String DEFAULT_FONT = "arial";
   
       // Names of the servlet's parameters - for Jigsaw web server
       private static final String SIRPAC_TMP_DIR       = "SIRPAC_TMP_DIR";
       private static final String GRAPH_VIZ_ROOT       = "GRAPH_VIZ_ROOT";
       private static final String GRAPH_VIZ_PATH       = "GRAPH_VIZ_PATH";
       private static final String GRAPH_VIZ_LIB_DIR    = "GRAPH_VIZ_LIB_DIR";
       private static final String GRAPH_VIZ_FONT_DIR   = "GRAPH_VIZ_FONT_DIR";
   
       // Variables for the servlet's parameters
       private static String m_SiRPACTmpDir      = null;
       private static String m_GraphVizPath      = null;
       private static String m_GraphVizFontDir   = null;
       private static String m_GraphVizLibDir    = null;
   
       // Names of environment variable need by GraphVis
       private static String DOTFONTPATH     = "DOTFONTPATH";
       private static String LD_LIBRARY_PATH = "LD_LIBRARY_PATH";
   
       // Names used for temporary files
       private static final String TMP_FILE_PREFIX = "sirpac_";
       private static final String SUFFIX_TMP_DIR  = ".tmp";
       private static final String SUFFIX_DOT      = ".dot";
       private static final String SUFFIX_RDF      = ".rdf";
   
       // Names used for file suffixes and for GraphViz's command line
       // option
       private static final String NAME_GIF      = "gif";
       private static final String NAME_HPGL     = "hpgl";
       private static final String NAME_PCL      = "pcl";
       private static final String NAME_PS       = "ps";
       private static final String NAME_SVG      = "svg";
   
       // Default GraphViz parameter names and their default values
     // Servlet name      // Servlet name
     private static final String SERVLET_NAME = "SiRPACServlet";      private static final String SERVLET_NAME = "SiRPACServlet";
   
Line 151  public class SiRPACServlet extends HttpS Line 188  public class SiRPACServlet extends HttpS
     {      {
         try {          try {
             // Generate a unique file name               // Generate a unique file name 
             File tmpFile = createTempFile(tmpDir, TMP_FILE_PREFIX, RDF_SUFFIX);              File tmpFile = createTempFile(tmpDir, TMP_FILE_PREFIX, SUFFIX_RDF);
             if (tmpFile == null) {              if (tmpFile == null) {
                 // Not really a critical error, just return                  // Not really a critical error, just return
                 return;                  return;
Line 170  public class SiRPACServlet extends HttpS Line 207  public class SiRPACServlet extends HttpS
     }      }
   
     /*      /*
        * Given the graph's format option, return either the corresponding
        * command line option for that option or the file name suffix for
        * the graph option.  For example GIF files have ".gif" for its
        * suffix and GraphViz uses "-Tgif" for the command line.
        *
        * NOTE: default is GIF.
        *
        *@param graphFormat the graph's output format
        *@param suffix.  If true, the name returned is for the graph's
        * file name suffix; otherwise, the name returned is for the
        * graph's command line option.
        *@return the suffix to use for the graph's output file
        */
       private String getFormatName(String graphFormat, boolean suffix) {
   
           String name = (suffix) ? "." : "-T";
   
           if (graphFormat.equals(FORMAT_SVG_LINK)) return name + NAME_SVG;
           if (graphFormat.equals(FORMAT_PS_LINK)) return name + NAME_PS;
           if (graphFormat.equals(FORMAT_HP_GL_LINK)) return name + NAME_HPGL;
           if (graphFormat.equals(FORMAT_HP_PCL_LINK)) return name + NAME_PCL;
           
           return name + NAME_GIF;
       }
   
       /*
      * Invokes the GraphVis program to create a GIF image from the       * Invokes the GraphVis program to create a GIF image from the
      * the given DOT data file       * the given DOT data file
      *       *
      *@param dotFileName the name of the DOT data file       *@param dotFileName the name of the DOT data file
      *@param gifFileName the name of the GIF data file        *@param outputFileName the name of the GIF data file 
      *@return true if success; false if any failure occurs       *@return true if success; false if any failure occurs
      */       */
     private boolean generateGifFile(String dotFileName, String gifFileName) {      private boolean generateOutputFile(String dotFileName, String outputFileName, String graphFormat) {
         String environment[] = {DOTFONTPATH     + "=" + m_GraphVizFontDir,          String environment[] = {DOTFONTPATH     + "=" + m_GraphVizFontDir,
                                 LD_LIBRARY_PATH + "=" + m_GraphVizLibDir};                                  LD_LIBRARY_PATH + "=" + m_GraphVizLibDir};
   
         String cmdArray[] = {m_GraphVizPath, "-Tgif", "-o", gifFileName, dotFileName};          String formatOption = getFormatName(graphFormat, false);
   
           String cmdArray[] = {m_GraphVizPath, formatOption, "-o", outputFileName, dotFileName};
   
         Runtime rt = Runtime.getRuntime();          Runtime rt = Runtime.getRuntime();
         try {          try {
Line 228  public class SiRPACServlet extends HttpS Line 293  public class SiRPACServlet extends HttpS
   
         // Orientation must be either           // Orientation must be either 
         String orientation = req.getParameter (ORIENTATION);          String orientation = req.getParameter (ORIENTATION);
         if (orientation.equals("Left to Right"))          if (orientation.equals("LR"))
             orientation = "LR";              orientation = "LR";
         else          else
             orientation = DEFAULT_ORIENTATION;              orientation = DEFAULT_ORIENTATION;
Line 255  public class SiRPACServlet extends HttpS Line 320  public class SiRPACServlet extends HttpS
      *@param out the servlet's output stream       *@param out the servlet's output stream
      *@param rdf the RDF text       *@param rdf the RDF text
      *@param req a Servlet request       *@param req a Servlet request
        *@param source the name of input source
        *@param graphFormat the graph's format
        *@param saveRDF the RDF can be cached [saved to the file system]
        *@param saveDOTFile the DOT file should be cached
        *@param createBags the parser should create a bag for each Description
        *@param streamMode the parser's stream mode setting
      */       */
     private void generateGraph (ServletOutputStream out, String rdf, HttpServletRequest req, boolean saveRDF, String source, boolean createBags, boolean streamMode) {      private void generateGraph (ServletOutputStream out, String rdf, HttpServletRequest req, String source, String graphFormat, boolean saveRDF, boolean saveDOTFile, boolean createBags, boolean streamMode) {
         try {          try {
             out.println("<hr title=\"visualisation\">");              out.println("<hr title=\"visualisation\">");
             out.println("<h3>Graph of the data model</h3>");              out.println("<h3>Graph of the data model</h3>");
   
             // Stop if any of the parameters are missing              // Stop if any of the parameters are missing
             if (m_SiRPACTmpDir == null || m_GraphVizPath == null ||               if (m_SiRPACTmpDir == null || 
                 m_GraphVizFontDir == null || m_GraphVizLibDir == null) {                   m_GraphVizPath == null || 
                   m_GraphVizFontDir == null || 
                   m_GraphVizLibDir == null) { 
     
                 // Put the paths in a comment in the returned content                  // Put the paths in a comment in the returned content
                 out.println("<!-- SIRPAC TMP = " + m_SiRPACTmpDir);                  out.println("<!-- SIRPAC TMP = " + m_SiRPACTmpDir);
Line 280  public class SiRPACServlet extends HttpS Line 353  public class SiRPACServlet extends HttpS
   
             // Must generate a unique file name that the DOT consumer              // Must generate a unique file name that the DOT consumer
             // will use               // will use 
             File dotFile = createTempFile(tmpDir, TMP_FILE_PREFIX, DOT_SUFFIX);              File dotFile = createTempFile(tmpDir, TMP_FILE_PREFIX, SUFFIX_DOT);
             if (dotFile == null) {              if (dotFile == null) {
                 out.println("Failed to create a temporary DOT file. A graph cannot be generated.");                  out.println("Failed to create a temporary DOT file. A graph cannot be generated.");
                 return;                  return;
Line 325  public class SiRPACServlet extends HttpS Line 398  public class SiRPACServlet extends HttpS
   
             // Must generate a unique file name for the GIF file              // Must generate a unique file name for the GIF file
             // that will be created              // that will be created
             File gifFile = createTempFile(tmpDir, TMP_FILE_PREFIX, GIF_SUFFIX);              String suffix = getFormatName(graphFormat, true);
             if (gifFile == null) {              File outputFile = createTempFile(tmpDir, TMP_FILE_PREFIX, suffix);
                 out.println("Failed to create a temporary GIF file. A graph cannot be generated.");              if (outputFile == null) {
                   out.println("Failed to create a temporary file for the graph. A graph cannot be generated.");
                 dotFile.delete();                  dotFile.delete();
                 return;                  return;
             }              }
Line 335  public class SiRPACServlet extends HttpS Line 409  public class SiRPACServlet extends HttpS
             // Pass the DOT data file to the GraphViz dot program              // Pass the DOT data file to the GraphViz dot program
             // so it can create a GIF image of the data model              // so it can create a GIF image of the data model
             String dotFileName = dotFile.getAbsolutePath();              String dotFileName = dotFile.getAbsolutePath();
             String gifFileName = gifFile.getAbsolutePath();              String outputFileName = outputFile.getAbsolutePath();
   
             if (!generateGifFile(dotFileName, gifFileName)) {              if (!generateOutputFile(dotFileName, outputFileName, graphFormat)) {
                 out.println("An attempt to create a graph failed.");                  out.println("An attempt to create a graph failed.");
                 dotFile.delete();                  dotFile.delete();
                 gifFile.delete();                  outputFile.delete();
                 return;                  return;
             }              }
   
             // Cleanup              // Handle the DOT file
             dotFile.delete();              if (saveDOTFile) {
                   // Make the DOT file link'able if so requested
                   String dotPath = SERVLET_NAME + SUFFIX_TMP_DIR + 
                                    File.separator + dotFile.getName();
                   out.println("<a href=\"" + dotPath + "\">Get the DOT file.</a><br /><br />");
               }
               else {
                   // Delete it ...
                   dotFile.delete();
               }
   
             // NOTE: Cannot delete the GIF file here because its              // NOTE: Cannot delete the GIF file here because its
             // pathname is returned to the client              // pathname is returned to the client
             String imagePath = SERVLET_NAME +              String imagePath = SERVLET_NAME + SUFFIX_TMP_DIR + File.separator + 
                                TMP_DIR_SUFFIX + File.separator +                                  outputFile.getName();
                                gifFile.getName();  
               // Handle the embedded image formats first
             if (gifFile.length() > 0)              if (graphFormat.equals(FORMAT_GIF_EMBED)) {
                 out.println("<img src=\"" + imagePath + "\"/>");                  if (outputFile.length() > 0)
             else                      out.println("<img src=\"" + imagePath + "\"/>");
                 out.println("The graph image file is empty.");                  else
                       out.println("The graph image file is empty.");
               } else {
                   if (outputFile.length() > 0)
                       out.println("<a href=\"" + imagePath + "\">Get/view the graph's image file (" + suffix + ").</a><br /><br />");
                   else
                       out.println("The graph image file is empty.");
               }
   
             // One last thing to do before exiting - copy the RDF to a file              // One last thing to do before exiting - copy the RDF to a file
             if (saveRDF)              if (saveRDF)
Line 621  public class SiRPACServlet extends HttpS Line 711  public class SiRPACServlet extends HttpS
   
         ServletOutputStream out = res.getOutputStream ();          ServletOutputStream out = res.getOutputStream ();
   
         String    sRDF        = req.getParameter (POST_TEXT);          String    sRDF         = req.getParameter (TEXT);
         String    sBags       = req.getParameter (POST_BAG);          String    sBags        = req.getParameter (BAG);
         String    sStreamMode = req.getParameter (POST_STREAM_MODE);          String    sStreamMode  = req.getParameter (STREAM_MODE);
         String    sSaveRDF    = req.getParameter (POST_SAVE_RDF);          String    sSaveRDF     = req.getParameter (SAVE_RDF);
         String    sURI        = req.getParameter (POST_URI);          String    sSaveDOTFile = req.getParameter (SAVE_DOT_FILE);
           String    sURI         = req.getParameter (URI);
           String    sFormat      = req.getParameter (FORMAT);
   
         SiRPAC        sirpac = new SiRPAC();          SiRPAC        sirpac = new SiRPAC();
         ErrorStore    errorHandler = new ErrorStore();          ErrorStore    errorHandler = new ErrorStore();
Line 688  public class SiRPACServlet extends HttpS Line 780  public class SiRPACServlet extends HttpS
   
             printTripleTableFooter(out, consumer);              printTripleTableFooter(out, consumer);
   
             generateGraph(out, sRDF, req,               if (sFormat != null && !sFormat.equals(FORMAT_NO_GRAPH)) {
                 (sSaveRDF != null) ? true : false,                  generateGraph(out, 
                 sirpac.source(),                      sRDF, 
                 (sBags != null && sBags.equals ("on") ? true : false),                      req, 
                 (sStreamMode != null && sStreamMode.equals ("on") ? false : true));                      sirpac.source(),
                       sFormat,
                       (sSaveRDF != null) ? true : false,
                       (sSaveDOTFile != null && sSaveDOTFile.equals ("on") ? true : false),
                       (sBags != null && sBags.equals ("on") ? true : false),
                       (sStreamMode != null && sStreamMode.equals ("on") ? false : true));
               }
   
         } catch (SAXException e) {          } catch (SAXException e) {
             error = true;              error = true;

Removed from v.1.18  
changed lines
  Added in v.1.19


Webmaster