ivelin      02/04/04 04:28:50

  Added:       src/scratchpad/src/org/apache/cocoon/samples/xmlform
                        FormBinderAction.java NestedBean.java TestBean.java
                        ValidatingFormAction.java
  Log:
  Added CocoonForm demos. HTML Form <-> JavaBean binding through XPath and
  Schematron validation.
  
  Revision  Changes    Path
  1.1                  
xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/xmlform/FormBinderAction.java
  
  Index: FormBinderAction.java
  ===================================================================
  package org.apache.cocoon.samples.xmlform;
  
  import java.util.Map;
  import org.apache.cocoon.environment.Redirector;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.acting.*;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.Constants;
  import org.apache.cocoon.environment.Session;
  import org.apache.cocoon.environment.Context;
  
  import org.apache.cocoon.xmlform.FormBeanBinder;
  
  public class FormBinderAction extends AbstractAction {
  
    public FormBinderAction() {
    }
    public Map act(Redirector redirector, SourceResolver resolver, Map objectmodel, 
String src, Parameters param) throws java.lang.Exception {
        Request request =(Request)objectmodel.get(Constants.REQUEST_OBJECT);
        Session session = request.getSession();
  
        String formName = "xmlform";
        
        TestBean  jBean = (TestBean) session.getAttribute( formName );
        if (jBean == null)
          {
          // first time here, populate the bean with initial values
          session.setAttribute("xmlform",new  TestBean("Living in the 
session","session"));
          } 
        else
          {
          // been here before, update the bean with client's values
          FormBeanBinder.bind(request, jBean);
          // apply additional buziness logic to the bean
          jBean.incrementCount();
          }
  
        return objectmodel;
    }
  }
  
  
  
  
  1.1                  
xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/xmlform/NestedBean.java
  
  Index: NestedBean.java
  ===================================================================
  package org.apache.cocoon.samples.xmlform;
  
    
    public class NestedBean 
      {
      private String kind = "mammal";
      
      public void setKind(String newKind) 
        {
        kind = newKind;
        }
      
      public String getKind() 
        {
        return kind;
        }
      }
  
  
  
  1.1                  
xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/xmlform/TestBean.java
  
  Index: TestBean.java
  ===================================================================
  package org.apache.cocoon.samples.xmlform;
  
  import java.util.List;
  import java.util.ArrayList;
  
  public class TestBean {
    private String name = "dog";
    private String scope = "galaxy";
    private int count = 0;
    private ArrayList preferences = new ArrayList();
    private NestedBean personal = new NestedBean();
  
    public TestBean(){
      preferences.add("likeVodka");
      preferences.add("likeSkiing");
    }
  
    public TestBean(String newName,String newScope){
      this();
      name= newName;
      scope=newScope;
    }
    
    public String getName() {
      return name;
    }
    public void setName(String newName) {
      name = newName;
    }
  
    public void setScope(String newScope) {
      scope = newScope;
    }
  
    public String getScope() {
      return scope;
    }
  
    public ArrayList getPreferences() 
      {
      return preferences;
      }
    
      public NestedBean getPersonalInfo() 
        {
        return personal;
       }
  
      public void setPersonalInfo( NestedBean newPersonal ) 
        {
        personal = newPersonal;
       }
  
    public int getCount() {
      return count;
    }
  
    public void incrementCount() {
      count++;
    }
    
    }
  
  
  
  1.1                  
xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/xmlform/ValidatingFormAction.java
  
  Index: ValidatingFormAction.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/samples/xmlform/ValidatingFormAction.java,v
 1.1 2002/04/04 12:28:49 ivelin Exp $
   * $Revision: 1.1 $
   * $Date: 2002/04/04 12:28:49 $
   *
   * ====================================================================
   * 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 [EMAIL PROTECTED]
   *
   * 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;
  
  
  // Java classes
  import java.util.Map;
  import java.util.Iterator;
  import java.util.Properties;
  import java.io.InputStream;
  import java.io.FileInputStream;
  import java.io.File;
  
  // XML classes
  import javax.xml.transform.stream.StreamSource;
  import javax.xml.transform.TransformerException;
  import org.xml.sax.InputSource;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  // Cocoon classes
  import org.apache.cocoon.environment.Redirector;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.acting.*;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.Constants;
  import org.apache.cocoon.environment.Session;
  import org.apache.cocoon.environment.Context;
  
  // Schematron classes
  import org.apache.cocoon.validation.schematron.Validator;
  import org.apache.cocoon.validation.schematron.ValidationResult;
  
  
  import org.apache.cocoon.xmlform.FormBeanBinder;
  
  /**
   * This simple action demonstrates binding between
   * HTML Forms and JavaBeans through XPath,
   * as well as dynamic Schematron validation.
   */
  public class ValidatingFormAction extends AbstractAction {
  
    private final String formName_ = "xmlForm";
   private Validator validator_;
    
    public ValidatingFormAction() 
    {
    }
  
    
    public synchronized void setup(SourceResolver resolver) throws 
org.xml.sax.SAXException, 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 );
     validator_ = new Validator( is );  
    }
    
    
    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, 
String src, Parameters param) throws java.lang.Exception {
        Request request =(Request)objectModel.get(Constants.REQUEST_OBJECT);
        Session session = request.getSession();
  
        // initialize validator if necessary
        if (validator_ == null ) setup( resolver );
        
        String formName = "xmlForm";
        
        // reset the session state if requested
        if ( request.getParameter("reset")  != null) session.removeAttribute( formName 
);
        
        TestBean  jBean = (TestBean) session.getAttribute( formName );
        if (jBean == null)
          {
            // first time here, populate the bean with initial values
            jBean = new  TestBean();
            session.setAttribute( formName, jBean);
          } 
        else
          {
          // been here before, update the bean with client's values
          FormBeanBinder.bind(request, jBean);
        }
        // validate state of the bean against a Schematron schema.
        // Schema Phase is "Full"
        Properties props = new Properties();
        props.put("phase", "Full");
        ValidationResult vres = validator_.validate(jBean, props);
        // 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 (!vres.isEmpty())
        {
          request.setAttribute ( formName + "ValidationResult", vres );
        }
        request.setAttribute ( "validationPassed",  new Boolean( vres.isEmpty() ) );
        // apply additional buziness logic to the bean
        jBean.incrementCount();
  
        return objectModel;
    }
    
    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;
      }
    }
    
  }
  
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to