commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject cvs commit: jakarta-commons/latka/src/java/org/apache/commons/latka/xml ValidationReflectionHandler.java
Date Thu, 07 Feb 2002 00:13:13 GMT
dion        02/02/06 16:13:13

  Modified:    latka/src/java/org/apache/commons/latka/xml
                        ValidationReflectionHandler.java
  Log:
  Added javadoc, fixed formatting
  
  Revision  Changes    Path
  1.9       +124 -70   jakarta-commons/latka/src/java/org/apache/commons/latka/xml/ValidationReflectionHandler.java
  
  Index: ValidationReflectionHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/xml/ValidationReflectionHandler.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ValidationReflectionHandler.java	28 Aug 2001 15:50:31 -0000	1.8
  +++ ValidationReflectionHandler.java	7 Feb 2002 00:13:13 -0000	1.9
  @@ -79,88 +79,142 @@
   import org.xml.sax.SAXException;
   import org.xml.sax.XMLReader;
   
  +/**
  + * A special LatkaHandler that processes validator tags, and loads the
  + * classes to process the tags on demand
  + * <br />This allows for pluggable tag handling
  + * @author Morgan Delagrange
  + * @author dIon Gillard
  + * @version $Id: ValidationReflectionHandler.java,v 1.9 2002/02/07 00:13:13 dion Exp $
  + */
   public class ValidationReflectionHandler extends LatkaHandler {
  -
  -  protected Request  _request  = null;
  -  protected Response _response = null;
  -  protected LatkaEventInfo _listener = null;
  -  protected static String _latkaPrefix = "latka.validator.";
  -  protected ClassLoader _loader = 
  -    Thread.currentThread().getContextClassLoader();
  -
  -  protected static final Category log = 
  -    Category.getInstance(ValidationReflectionHandler.class);
  -
  -  public ValidationReflectionHandler(XMLReader reader, Response response, 
  +    /** the request processed to obtain the response being validated */
  +    protected Request  _request  = null;
  +    /** the response being validated */
  +    protected Response _response = null;
  +    /** holds request success/failure details */
  +    protected LatkaEventInfo _listener = null;
  +    /** properties prefix for tags */
  +    protected static String _latkaPrefix = "latka.validator.";
  +    /** context classloader for loading classes */
  +    protected ClassLoader _loader = 
  +                Thread.currentThread().getContextClassLoader();
  +    /** log4j category to append output to */
  +    protected static final Category log = 
  +        Category.getInstance(ValidationReflectionHandler.class);
  +
  +    /**
  +     * Create a handler to deal with validator tags and sub tags
  +     * @param reader source of XML fragment
  +     * @param response the response being validated
  +     * @param listener holds request success/failure details
  +     */    
  +    public ValidationReflectionHandler(XMLReader reader, Response response, 
                                        LatkaEventInfo listener) {
  -    super(reader);
  -    _response = response;
  -    _request  = response.getRequest();
  -    _listener = listener;
  -  }
  +        super(reader);
  +        _response = response;
  +        _request  = response.getRequest();
  +        _listener = listener;
  +    }
  +
  +    /**
  +     * Handle the start of element
  +     * <br />If the request has failed, skip processing
  +     * <br />If it's the start of a validate tag, do nothing
  +     * <br />Otherwise, load the handler (based on localName) and delegate
  +     * to it
  +     *
  +     * @param uri namespace URI if namespace processing is being done
  +     * @param localName The element type name
  +     * @param qName fully qualified element name
  +     * @param atts The specified or defaulted attributes
  +     * @throws SAXException
  +     */
  +    public void startElement(String uri, String localName, String qName, 
  +                             Attributes atts) throws SAXException {
  +
  +        if (_listener.didRequestSucceed(_request) == false) {
  +            // once a validator fails, skip the rest
  +            return;
  +        }
   
  +        if (localName.equals("validate")) {
  +            return;
  +        }
   
  -  public void startElement(String uri, String localName,
  -                           String qName, Attributes atts)
  -  throws SAXException {
  -
  -    if (_listener.didRequestSucceed(_request) == false) {
  -      // once a validator fails, skip the rest
  -      return;
  -    }
  +        // the name of the custom ValidatorHandler class
  +        String validatorName = tagNameToClass(localName);
  +        if (validatorName == null) {
  +            log.debug("Unrecognized tag name for custom validator: "
  +                      + localName);
  +            return;
  +        }
   
  -    if (localName.equals("validate")) {
  -      return;
  -    }
  +        log.info("Custom validator class: " + validatorName);
  +        log.debug("response = " + _response);
  +        ValidationHandler validator = handlerForClass(validatorName);
  +        validator.init(_reader, _response, _listener, localName);
  +        log.debug("delegating tag: " + localName);
  +        validator.delegate(uri, localName, qName, atts);
   
  -    // the name of the custom ValidatorHandler class
  -    String validatorName = tagNameToClass(localName);
  -    if (validatorName == null) {
  -      System.out.println("Unrecognized tag name for custom validator: " + localName);
  -      return;
       }
   
  -    log.info("Custom validator class: " + validatorName);
  -    log.debug("response = " + _response);
  -    ValidationHandler validator = handlerForClass(validatorName);
  -    validator.init(_reader,_response,_listener, localName);
  -    log.debug("delegating tag: " + localName);
  -    validator.delegate(uri,localName,qName,atts);
  -
  -  }
  -
  -  public void endElement(String namespaceURI, String localName,
  -                         String rawName) {
  -    if (localName.equals("validate")) {
  -      release();
  +    /**
  +     * at the end of the tag, release resources
  +     * @param namespaceURI - The Namespace URI, or the empty string if the
  +     *      element has no Namespace URI or if Namespace processing is not 
  +     *      being performed.
  +     * @param localName - The local name (without prefix), or the empty string
  +     *      if Namespace processing is not being performed.
  +     * @param rawName - The qualified XML 1.0 name (with prefix), or the empty
  +     *      string if qualified names are not available.
  +     */
  +    public void endElement(String namespaceURI, String localName, 
  +                           String rawName) {
  +        if (localName.equals("validate")) {
  +            release();
  +        }
       }
  -  }
   
  -  protected String tagNameToClass(String localName) throws SAXException {
  -    Properties props = LatkaProperties.getProperties();
  -    Enumeration e = props.propertyNames();
  -    while (e.hasMoreElements()) {
  -      String name = (String) e.nextElement();
  -      if (name.startsWith(_latkaPrefix)) {
  -        int index = name.indexOf(_latkaPrefix) + _latkaPrefix.length();
  -        String tagName = name.substring(index,name.length());
  -        if (localName.equals(tagName)) {
  -          return props.getProperty(name);
  +    /**
  +     * Return a class name for the validator tag given
  +     * @return a class name for the validator tag given
  +     * @param localName the validator tag name
  +     */
  +    protected String tagNameToClass(String localName) {
  +        Properties props = LatkaProperties.getProperties();
  +        Enumeration e = props.propertyNames();
  +        while (e.hasMoreElements()) {
  +            String name = (String) e.nextElement();
  +            // FIXME: Should use endsWith localName as this should be less
  +            // common
  +            if (name.startsWith(_latkaPrefix)) {
  +                int index = name.indexOf(_latkaPrefix) + _latkaPrefix.length();
  +                String tagName = name.substring(index, name.length());
  +                if (localName.equals(tagName)) {
  +                    return props.getProperty(name);
  +                }
  +            }
           }
  -      }
  -    }
   
  -    return null;
  -  }
  +        return null;
  +    }
   
  -  protected ValidationHandler handlerForClass(String validatorClass) 
  -  throws SAXException {
  -    try {
  -      return (ValidationHandler) Beans.instantiate(_loader,validatorClass);
  -    } catch (ClassNotFoundException e) {
  -      throw new SAXException(e);
  -    } catch (IOException e) {
  -      throw new SAXException(e);
  +    /** 
  +     * Provide a validation handler by loading the provided class.
  +     * @return a validation handler by loading the provided class
  +     * @param validatorClass class name of the validator to be loaded
  +     * @throws SAXException when there are issues loading the class
  +     */
  +    protected ValidationHandler handlerForClass(String validatorClass) 
  +    throws SAXException {
  +        try {
  +            return (ValidationHandler) Beans.instantiate(_loader, 
  +                                                         validatorClass);
  +        } catch (ClassNotFoundException e) {
  +            throw new SAXException(e);
  +        } catch (IOException e) {
  +            throw new SAXException(e);
  +        }
       }
  -  }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message