« phpPGAdmin | Main | Edinburgh Kung Fu »

March 15, 2009

My Jython Regex Servlet

The servlet:

from javax.servlet.http import HttpServlet

import re

import string

class regexTest(HttpServlet):

    def doGet(self,request,response):

        self.doPost (request,response)

    def doPost(self,request,response):
    
        #param_names = request.getParameterNames()

        writer = response.getWriter()

        response.setContentType ("text/plain")
        
        try: expr = request.getParameterValues("expr")[0]

        except:
            
            final_hash = { "errors" : ['']}
            
            final_hash['errors'][0] = 'You must enter an expression.'

            writer.println(final_hash)

            sys.exit

        try: this_text = request.getParameterValues("text")[0]

        except:
            
            final_hash = { "errors" : ['']}
            
            final_hash['errors'][0] = 'You must enter some text.'

            writer.println(final_hash)
            
            sys.exit

        reg = regexTest()

        try: final_hash = reg.test_it(expr, this_text)
        
        except:
            
            final_hash = { "errors" : ['']}
            
            final_hash['errors'][0] = 'Failed to run test.'

            writer.println(final_hash)
            
            sys.exit
        
        #except: writer.println('')
        
        #writer.println('')

        writer.println(final_hash)
        

                
    #def __init__(self, text="Stuff"):
        
    #    self.text = text
        
    def test_it(self, exp, text):
        
        final_hash = {}
        
        try: this_reg = re.compile(exp) 
        
        except:
            
            final_hash['error'] = ['Your expression did not compile'] 
        
            return final_hash
        
        #print this_reg
        
        this_match = this_reg.findall(text)
        
        final_hash["results"] = this_match 
        
        if this_match:
        
            return final_hash
        
        else:
        
            final_hash['error'] = ['Your expression found no matches']
            
            return final_hash

#if __name__ == "__main__":
    
#    reg = regexTest()
    
#    print reg.test_it('[\d]+', 'These are words and phone 07590 574469')


The JSP with the JSON:

<%@ page contentType="text/html" %>
<!-- %@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % -->
<html>
<head>
    <link rel="stylesheet" href="styles.css" type="text/css" media="all"/>    
    <script type="text/javascript" src="js/prototype.js"></script>
    <script type="text/javascript" src="js/set_date.js"></script>
    <script type="text/javascript">
    function do_regex(){
    
        if(($('text').value == '') || ($('regex').value == '')){ alert('You must enter some text and an expression'); return false; }
    
        var url  = 'regexTest.py';
        var params = '?text=' + escape($('text').value) + '&expr=' + escape($('regex').value);
        var myAjax = new Ajax.Request(
                            url,
                            {
                                    method: 'get',
                                    parameters: params,
                                    onComplete: show_response
                            });
    
        function show_response(this_request){
        
            this_result = this_request.responseText;
        
            $('regex_here').value = this_result;
            
            }

        }

    </script>
    <title>Regular Expression Test Harness</title>
</head>
<body>
<h2 style="margin-left: 1em;">Regex Test Harness</h2>
<form action="#" method="post" style="width: 80%; margin: 2em; border: 1px solid #dcdcdc;">

<p style="margin-left: 1em;">
<label id="text_block_label" class="label_italic">Text
<em class="astrix" id="text_mandatory" style="display: none;">*</em></label>
<br/>
<em id="text_outer">
<input type="text" name="text" id="text"  class="four_c" value=""/>
</em>
</p>

<p style="margin-left: 1em;">
<label id="regex_block_label" class="label_italic">Regex
<em class="astrix" id="regex_mandatory" style="display: none;">*</em></label>
<br/>
<em id="regex_outer">
<input type="regex" name="regex" id="regex"  class="four_c" value="" onblur="do_regex()"/>
</em> <input type="button" value="Go»" class="button" onclick="do_regex()"/>
</p>

<form>
<textarea style="height: 100px; width: 400px; margin: 1em; color: red;" id="regex_here">
</textarea>
</form>

</form>
</body>
</html>

Tags: Jython , Tomcat

Posted by pj at March 15, 2009 10:01 PM

Comments