version 1.3, 2000/10/05 03:56:30
|
version 1.4, 2000/10/09 00:26:16
|
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 two incoming variables through the POST method: |
* expects two incoming variables through the POST method: |
* |
* |
* 1. The RDF/XML document via the 'RDF' variable |
* 1. "RDF" - the RDF/XML document |
* 2. The checkbox status via the 'BAGS' variable (='on' when activated) |
* 2. "BAGS" - if "on", each Description should have its own Bag; |
|
* the default is not to do this. |
* |
* |
* @author Janne Saarela <jsaarela@w3.org> |
* @author Janne Saarela <jsaarela@w3.org> |
* |
* |
Line 48 public class SiRPACServlet extends HttpS
|
Line 49 public class SiRPACServlet extends HttpS
|
{ |
{ |
final static public String REVISION = "$Id$"; |
final static public String REVISION = "$Id$"; |
|
|
// @@ |
// @@ The email address for bug reports |
private static final String MAIL_TO = "barstow@mediaone.net"; |
private static final String MAIL_TO = "barstow@w3.org"; |
|
|
// Names of the servlet's parameters |
// Names of the servlet's parameters |
private static final String SIRPAC_TMP_DIR = "SIRPAC_TMP_DIR"; |
private static final String SIRPAC_TMP_DIR = "SIRPAC_TMP_DIR"; |
private static final String DOT_PATH = "DOT_PATH"; |
private static final String DOT_PATH = "DOT_PATH"; |
private static final String DOT_LIB_DIR = "DOT_LIB_DIR"; |
private static final String DOT_LIB_DIR = "DOT_LIB_DIR"; |
private static final String DOT_FONT_DIR = "DOT_FONT_DIR"; |
private static final String DOT_FONT_DIR = "DOT_FONT_DIR"; |
|
|
// Variables for the servlet's parameters |
// Variables for the servlet's parameters |
private static String m_SiRPACTmpDir = null; |
private static String m_SiRPACTmpDir = null; |
private static String m_DotPath = null; |
private static String m_DotPath = null; |
private static String m_DotFontDir = null; |
private static String m_DotFontDir = null; |
private static String m_DotLibDir = null; |
private static String m_DotLibDir = 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"; |
|
|
// The parser |
// The parser |
private SiRPAC m_sirpac = null; |
private SiRPAC m_sirpac = null; |
Line 69 public class SiRPACServlet extends HttpS
|
Line 80 public class SiRPACServlet extends HttpS
|
private ErrorStore m_errorHandler; |
private ErrorStore m_errorHandler; |
|
|
/* |
/* |
* Create a temporary PrintWriter object |
* Create a File object in the m_SiRPACTmpDir directory |
* |
* |
*@return a PrintWriter object if a temporary file is created; |
*@param directory the file's directory |
* null otherwise |
*@param prefix the file's prefix name (not its directory) |
|
*@param suffix the file's suffix or extension name |
|
*@return a File object if a temporary file is created; null otherwise |
*/ |
*/ |
/* |
private File createTempFile (String directory, String prefix, String suffix) { |
private String getTempFileName (PrintWriter pw) { |
File f; |
|
try { |
|
File d = new File(directory); |
|
f = File.createTempFile(prefix, suffix, d); |
|
} catch (Exception e) { |
|
return null; |
|
} |
|
return f; |
|
} |
|
|
|
/* |
|
* Invokes the GraphVis program to create a GIF image from the |
|
* the given DOT data file |
|
* |
|
*@param dotFileName the name of the DOT data file |
|
*@param gifFileName the name of the GIF data file |
|
*@return true if success; false if any failure occurs |
|
*/ |
|
private boolean generateGifFile(String dotFileName, String gifFileName) { |
|
String environment[] = {DOTFONTPATH + "=" + m_DotFontDir, |
|
LD_LIBRARY_PATH + "=" + m_DotLibDir}; |
|
|
|
String cmdArray[] = {m_DotPath, "-Tgif", "-o", gifFileName, dotFileName}; |
|
|
|
Runtime rt = Runtime.getRuntime(); |
|
try { |
|
Process p = rt.exec(cmdArray, environment); |
|
p.waitFor(); |
|
} catch (Exception e) { |
|
return false; |
|
} |
|
|
|
return true; |
} |
} |
*/ |
|
|
|
/* |
/* |
* Generate a graph of the RDF data model |
* Generate a graph of the RDF data model |
* |
* |
*@param out |
*@param out the servlet's output stream |
*@param rdf |
*@param rdf the request |
*@param req |
*@param req the response |
*/ |
*/ |
private void generateGraph (ServletOutputStream out, String rdf, HttpServletRequest req) { |
private void generateGraph (ServletOutputStream out, String rdf, HttpServletRequest req) { |
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>"); |
|
|
// No need to continue if any of the parameters are missing |
// Stop if any of the parameters are missing |
if (m_SiRPACTmpDir == null || m_DotPath == null || |
if (m_SiRPACTmpDir == null || m_DotPath == null || |
m_DotFontDir == null || m_DotLibDir == null) { |
m_DotFontDir == null || m_DotLibDir == null) { |
out.println("Servlet initialization failed. A graph cannot be generated."); |
out.println("Servlet initialization failed. A graph cannot be generated."); |
return; |
return; |
} |
} |
|
|
// Must generate a unique file name that the Dot consumer |
// The temporary directory |
// will use |
String tmpDir = m_SiRPACTmpDir; |
|
|
File f; |
// Must generate a unique file name that the DOT consumer |
try { |
// will use |
File d = new File(m_SiRPACTmpDir); |
File dotFile = createTempFile(tmpDir, TMP_FILE_PREFIX, DOT_SUFFIX); |
f = File.createTempFile("sirpac_", ".dot", d); |
if (dotFile == null) { |
} catch (Exception e) { |
out.println("Failed to create a temporary DOT file. A graph cannot be generated."); |
out.println("Failed to create a temporary file. A graph cannot be generated (" + e.getMessage() + ")."); |
|
return; |
return; |
} |
} |
|
|
// Create a PrintWriter for the GraphViz consumer |
// Create a PrintWriter for the GraphViz consumer |
FileWriter fw = new FileWriter(f); |
FileWriter fw = new FileWriter(dotFile); |
PrintWriter pw = new PrintWriter(fw); |
PrintWriter pw = new PrintWriter(fw); |
|
|
// Run the parser using the DOT consumer to capture |
// Run the parser using the DOT consumer to capture |
Line 125 public class SiRPACServlet extends HttpS
|
Line 168 public class SiRPACServlet extends HttpS
|
} catch (Exception e) { |
} catch (Exception e) { |
out.println("An attempt to generate the graph data failed (" |
out.println("An attempt to generate the graph data failed (" |
+ e.getMessage() + ")."); |
+ e.getMessage() + ")."); |
|
pw.close(); |
|
dotFile.delete(); |
return; |
return; |
} |
} |
|
|
// Pass the DOT data file to the GraphViz dot program |
// Must close the DOT input file so the GraphViz can |
// to create the GIF file |
// open and read it |
String dotFileName = f.getAbsolutePath(); |
|
|
|
// Close the file |
|
pw.close(); |
pw.close(); |
|
|
// @@ Delete the DOT file |
// Must generate a unique file name for the GIF file |
|
// that will be created |
|
File gifFile = createTempFile(tmpDir, TMP_FILE_PREFIX, GIF_SUFFIX); |
|
if (gifFile == null) { |
|
out.println("Failed to create a temporary GIF file. A graph cannot be generated."); |
|
dotFile.delete(); |
|
return; |
|
} |
|
|
|
// Pass the DOT data file to the GraphViz dot program |
|
// so it can create a GIF image of the data model |
|
String dotFileName = dotFile.getAbsolutePath(); |
|
String gifFileName = gifFile.getAbsolutePath(); |
|
|
|
if (!generateGifFile(dotFileName, gifFileName)) { |
|
out.println("An attempt to create a graph failed."); |
|
dotFile.delete(); |
|
gifFile.delete(); |
|
return; |
|
} |
|
|
|
// @@ Cleanup |
|
// dotFile.delete(); |
|
|
|
// @@ Delete the GIF file |
|
|
// @@ Delete the GIF file |
String imagePath = HttpUtils.getRequestURL(req).toString() + |
|
TMP_DIR_SUFFIX + File.separator + gifFile.getName(); |
|
|
out.println("<img src=\"http://localhost:8001/servlet/SiRPAC.tmp/1.gif\"/>"); |
if (gifFile.length() > 0) |
|
out.println("<img src=\"" + imagePath + "\"/>"); |
|
else |
|
out.println("The graph image file is empty."); |
|
|
} catch (Exception e) { |
} catch (Exception e) { |
System.err.println("Exception: " + e.getMessage()); |
System.err.println("Exception: " + e.getMessage()); |
Line 151 out.println("<img src=\"http://localhost
|
Line 220 out.println("<img src=\"http://localhost
|
* Search the given string for substring "key" |
* Search the given string for substring "key" |
* and if it is found, replace it with string "replacement" |
* and if it is found, replace it with string "replacement" |
* |
* |
|
*@param input the input string |
|
*@param key the string to search for |
|
*@param replacement the string to replace all occurences of "key" |
*@return if no substitutions are done, input is returned; otherwise |
*@return if no substitutions are done, input is returned; otherwise |
* a new string is returned. |
* a new string is returned. |
*/ |
*/ |
Line 322 out.println("<img src=\"http://localhost
|
Line 394 out.println("<img src=\"http://localhost
|
} |
} |
|
|
/* |
/* |
* Servlet's doGet info method |
* Servlet's doGet info method - NOT supported |
* |
* |
*@param req the request |
*@param req the request |
*@param res the response |
*@param res the response |