/** * 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 * * * 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) * * @author Janne Saarela * * major re-write by Arthur Barstow */ package org.w3c.rdf.examples; import java.io.*; import java.util.StringTokenizer; 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.*; import org.w3c.rdf.model.*; import org.w3c.rdf.syntax.*; import org.w3c.rdf.syntax.RDFConsumer; import org.w3c.rdf.util.xml.DumpConsumer; import org.w3c.rdf.util.xml.ErrorStore; import org.w3c.rdf.implementation.model.StatementImpl; import org.w3c.rdf.implementation.model.NodeFactoryImpl; import org.w3c.rdf.implementation.syntax.sirpac.*; public class SiRPACServlet extends HttpServlet { final static public String REVISION = "$Id: SiRPACServlet.java,v 1.1 2000/10/04 15:47:00 barstow Exp $"; private SiRPAC m_sirpac = null; private ErrorStore m_errorHandler; private static final String MAIL_TO = "barstow@mediaone.net"; private void printDocumentHeader (ServletOutputStream out) { try { out.println(""); out.println(""); out.println("RDF creation"); out.println(""); out.println(""); out.println(""); } catch (Exception e) { // @@ } } private void printListing (ServletOutputStream out, String rdf) { try { out.println("
"); out.println("

The original RDF/XML document

"); out.println("
");

            // Replace all "<" with "<"
            StringBuffer sb = new StringBuffer("");
            StringTokenizer st = new StringTokenizer(rdf, "<");

            // Must handle a leading "<" 
            if (rdf.startsWith("<"))
                sb = sb.append("<");
            while (st.hasMoreTokens()) {
                sb = sb.append(st.nextToken());
                if (st.hasMoreTokens())
                    sb = sb.append("<");
            }

            // Now output the RDF one line at a time with line numbers
            int lineNum = 1;
            st = new StringTokenizer(sb.toString(), "\n");
            while (st.hasMoreTokens()) {
                out.print ("" + lineNum +
                          ": " + st.nextToken());
                lineNum++;
            }

            out.println("
"); } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); } } private void printTripleHeader (ServletOutputStream out) { try { out.println("
"); out.println("

Triples of the data model

"); // The output for each triple will be pre-formatted out.println("
");
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        }
    }

    private void printTripleFooter (ServletOutputStream out) {
        try {
            out.println("
"); } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); } } private void printDocumentFooter (ServletOutputStream out, String rdf) { try { out.println("
"); out.println("

Feedback

"); out.println("

If you suspect SiRPAC produced an error, please type an explanation below why you think so. Once you press Submit problem report button, the message along with the example is mailed automatically to barstow@w3.org

"); out.println("
"); out.println(""); out.println("

" + rdf + "\">"); /* out.println(rdf + "\">"); <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:DC="http://purl.org/dc/elements/1.0/"> <rdf:Description about="http://www.w3.org/RDF/Implementations/SiRPAC/"> <DC:Creator rdf:resource="http://www.w3.org/People/Janne/" /> <DC:Subject rdf:resource="XXXXXXXXXXX"/> </rdf:Description> </rdf:RDF> "> */ out.println(""); out.println("

"); out.println("
Back to main page."); out.println(""); out.println(""); } catch (Exception e) { // @@ } } public String getServletInfo () { return "Servlet Wrapper for SiRPAC. This is revision " + REVISION; } public void init(ServletConfig config) throws ServletException { super.init (config); m_sirpac = new SiRPAC(); m_errorHandler = new ErrorStore(); m_sirpac.setErrorHandler(m_errorHandler); } 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 ("

No GET

\n\n

Please send RDF through POST!

\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); String error = null; SiRPACServletDumpConsumer consumer = new SiRPACServletDumpConsumer(); printDocumentHeader (out); printListing (out, sRDF); printTripleHeader (out); try { // Override the default tripe output handler consumer.setOutputStream(out); if (sBags != null && sBags.equals ("on")) m_sirpac.createBags (true); m_sirpac.parse(is, consumer); } catch (SAXException e) { error = e.toString(); } catch (Exception e) { error = e.toString(); e.printStackTrace (); } printTripleFooter(out); res.setContentType ("text/html"); if (error != null) { out.println ("

Errors during parsing

\n"); out.println ("
\n");
            out.println ("Fatal error: " + m_errorHandler.getErrorMessage());
            out.println ("   (Line number = " + "" + 
                         m_errorHandler.getLineNumber() + "" +
                         ", Column number = " + 
                         m_errorHandler.getColumnNumber() + ")");

	    out.println ("
\n\n"); } printDocumentFooter(out, sRDF); } }