File:  [Public] / java / classes / org / w3c / rdf / SiRPACServlet.java
Revision 1.1: download - view: text, annotated - select for diffs
Wed Jan 13 15:00:35 1999 UTC (25 years, 5 months ago) by jsaarela
Branches: MAIN
CVS tags: rel-2-2, HEAD
Finished conformance testing with PR-rdf-syntax-19990105 version
of the RDF M&S spec.

/**
 * SiRPACServlet - Simple RDF Parser & Compiler Servlet wrapper
 *
 * Copyright © World Wide Web Consortium, (Massachusetts Institute of
 * Technology, Institut National de Recherche en Informatique et en
 * Automatique, Keio University).
 *
 * All Rights Reserved.
 *
 * Please see the full Copyright clause at
 * <http://www.w3.org/Consortium/Legal/copyright-software.html>
 *
 * This servlet works as a wrapper for the SiRPAC RDF parser
 * Servlet expects to have two incoming variables through POST method
 * 1. RDF/XML document with 'RDF' variable
 * 2. Checkbox status with 'BAGS' variable (='on' when activated)
 *
 * $Log: SiRPACServlet.java,v $
 * Revision 1.1  1999/01/13 15:00:35  jsaarela
 * Finished conformance testing with PR-rdf-syntax-19990105 version
 * of the RDF M&S spec.
 *
 *
 * @author Janne Saarela <jsaarela@w3.org>
 */

package org.w3c.rdf;

import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;

import org.xml.sax.InputSource;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.*;

public class SiRPACServlet extends HttpServlet
{
    final static public String	REVISION = "$Id: SiRPACServlet.java,v 1.1 1999/01/13 15:00:35 jsaarela Exp $";

    Parser	m_parser = null;
    SiRPAC	m_sirpac = null;

    public String getServletInfo () {
	return "Servlet Wrapper for SiRPAC. This is revision " + REVISION;
    }

    public void init(ServletConfig config) throws ServletException {
	super.init (config);

	// Create a new parser
	try {
	    m_parser = ParserFactory.makeParser("com.ibm.xml.parser.SAXDriver");
	} catch (Exception e) {
	    e.printStackTrace ();
	    return;
	}

	// Create a new handler.
	m_sirpac = new SiRPAC();
	
	// Register the handlers
	m_parser.setEntityResolver(m_sirpac);
	m_parser.setDTDHandler (m_sirpac);
	m_parser.setDocumentHandler(m_sirpac);
	m_parser.setErrorHandler (m_sirpac);
    }

    public void destroy () {
	super.destroy ();
    }

    public void doGet (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
	ServletOutputStream out = res.getOutputStream ();

	res.setContentType ("text/html");

	out.println ("<h1>No GET</h1>\n\n<p>Please send RDF through POST!</p>\n");
    }

    public void doPost (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
	ServletOutputStream out = res.getOutputStream ();

	String		sRDF = req.getParameter ("RDF");
	String		sBags = req.getParameter ("BAGS");
	StringReader	sr = new StringReader (sRDF);
	InputSource	is = new InputSource (sr);
	try {
	    m_parser.parse(is);
	    m_sirpac.resolve ();
	    if (sBags != null && sBags.equals ("on"))
		m_sirpac.createBags (true);
	    m_sirpac.processXML (m_sirpac.root());
	} catch (SAXException e) {
	    m_sirpac.addError (e.getMessage());
	} catch (Exception e) {
	    m_sirpac.addError ("Internal error "+e);
	    e.printStackTrace ();
	}

	String sErrors = m_sirpac.errors ();
	if (sErrors != null && sErrors.length() > 0) {
	    res.setContentType ("text/html");

	    out.println ("<h1>Errors during parsing</h1>\n\n<pre>\n");
	    out.println (sErrors);
	    out.println ("</pre>\n\n");
	} else {
	    res.setContentType ("text/x-triples");

	    Enumeration e = m_sirpac.triples ();
	    while (e.hasMoreElements()) {
		Triple t = (Triple)e.nextElement ();
		out.println ("triple(\""+t.predicate()+"\",\""+t.subject()+"\",\""+t.object()+"\").");
	    }
	}
    }
}

Webmaster