Package org.apache.cocoon.samples.xmlform

Source Code of org.apache.cocoon.samples.xmlform.ValidatingFormAction

/*
* $Header: /home/cvspublic/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/xmlform/Attic/ValidatingFormAction.java,v 1.3.2.2 2002/06/11 23:54:53 vgritsenko Exp $
* $Revision: 1.3.2.2 $
* $Date: 2002/06/11 23:54:53 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. The end-user documentation included with the redistribution, if
*    any, must include the following acknowlegement:
*       "This product includes software developed by the
*        Apache Software Foundation (http://www.apache.org/)."
*    Alternately, this acknowlegement may appear in the software itself,
*    if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
*    Foundation" must not be used to endorse or promote products derived
*    from this software without prior written permission. For written
*    permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
*    nor may "Apache" appear in their names without prior written
*    permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.cocoon.samples.xmlform;

import org.apache.avalon.framework.parameters.Parameters;

import org.apache.cocoon.acting.AbstractAction;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.validation.Schema;
import org.apache.cocoon.validation.SchemaFactory;
import org.apache.cocoon.validation.Validator;
import org.apache.cocoon.xmlform.Form;
import org.apache.cocoon.xmlform.FormBeanBinder;

import org.xml.sax.InputSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;

/**
* This simple action demonstrates binding between
* HTML Forms and JavaBeans through XPath,
* as well as dynamic Schematron validation.
*/
public class ValidatingFormAction extends AbstractAction {

  public static final String OBJECT_MAP_NEXT_PAGE = "nextPage"

  private final String formName_ = "xmlForm";
  private Validator validator_;
 
  public synchronized void setup(SourceResolver resolver)
    throws InstantiationException, java.io.IOException
  {
    // initialize the schematron Validor with a schema files
    String fileLocation = getFile( resolver, "schematron/xmlform-sch-report-Demo2.xml" );
    File file = new File( fileLocation );
    if ( !file.exists () ) throw new RuntimeException("Error: schema file not found !");
   InputStream istrm = new FileInputStream ( file );
   InputSource is = new InputSource ( istrm );
   SchemaFactory schf = SchemaFactory.lookup ( SchemaFactory.NAMESPACE_SCHEMATRON );
   Schema sch = schf.compileSchema ( is );
   validator_ = sch.newValidator()
  }
 
 
  public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String src, Parameters param) throws java.lang.Exception {
      Request request = ObjectModelHelper.getRequest(objectModel);
      Session session = request.getSession();

      // initialize validator if necessary
      if (validator_ == null ) setup( resolver );
     
      // reset the session state if requested
      if ( request.getParameter("reset"!= null) session.removeAttribute( formName_ );

      Form form = (Form) session.getAttribute( formName_ );
      if (form == null)
        {
          // first time here, populate the bean with initial values
          form = new Form();
          form.setInstance( new  TestBean() );
          session.setAttribute( formName_, form );
        }
      TestBean  jBean = (TestBean) form.getInstance();

      // call prepare in case the bean needs to massaged before population
      prepare( jBean );
     
      // update the bean with request parameter values
      FormBeanBinder.bind(request, jBean);

      // apply additional buziness logic to the bean
      jBean.incrementCount();

      // validate state of the bean against a Schematron schema.
      // Schema Phase is "Full"
      SortedSet violations = validate( objectModel, form, "Full" );

      // set the nextPage control flow parameter
      // according to the validation result
      if (violations != null)
        return nextPage("input");
      else
        return nextPage( "success" );

  }
 
  public  String getFile(SourceResolver sr, String FileName) {
    try{
    final String  FILE_PREFIX = "file:";
    String path = sr.resolve(FileName).getSystemId();
    if(path.startsWith(FILE_PREFIX)){
       path = path.substring(FILE_PREFIX.length());
    }
    return path;

    }
    catch(Exception e){
       getLogger().error("could not read mapping file",e);
      return null;
    }
  }
 
 
  /**
   * Called to determine the exit point of an action.
   * The pageName is made available in the objectMap,
   * which can be then referenced in the pipeline
   * @param pageName logical name for a next page
   * @return Map a pipeline objectMap containing the pageName
   *
   */
  protected Map nextPage( String pageName )
  {
    Map objectModel = new HashMap();
    objectModel.put( OBJECT_MAP_NEXT_PAGE,  pageName );   
    return objectModel;
  }
 
  /**
   * This method is called before
   * the form bean population.
   * Has semantics similar to that of the
   * ActionForm.reset() in Struts
   *
   * Can be used for clearing checkbox fields,
   * because the browser will not send them when
   * not checked.
   *
   * @param formBean the actual JavaBean which
   * will be populated with the request parameters
   */
  protected void prepare(Object formBean)
  {
    // do nothing by default;
    return;
  }

  /**
   * validates a bean
   */
  protected synchronized SortedSet validate( Map objectModel, Form form )
  {
    return validate( objectModel, form, null );
  }
 

  /**
   * validates a bean
   *
   * @param form the bean to be validated
   * @param phase the validation phase
   */
  protected synchronized SortedSet validate( Map objectModel, Form form, String phase )
  {
    if ( phase != null )
    {
      validator_.setProperty ( Validator.PROPERTY_PHASE, phase );
    }

    SortedSet violations = validator_.validate( form.getInstance() );

      // if the validation result is not empty, then
      // make the validation result available to the pipeline
      // it can be later checked by a <map:selector/>
      // or inserted in the SAX stream by a CastorTransformer
      // or maybe even both
      if (violations != null)
      {
        form.setViolation( violations );
        //Request request = ObjectModelHelper.getRequest(objectModel);
      }
    return violations;
  }
  

}
TOP

Related Classes of org.apache.cocoon.samples.xmlform.ValidatingFormAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.