You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2008/04/08 15:04:41 UTC
svn commit: r645892 - in /turbine/fulcrum/trunk/parser:
src/java/org/apache/fulcrum/parser/ src/test/
src/test/org/apache/fulcrum/parser/ xdocs/
Author: tv
Date: Tue Apr 8 06:04:40 2008
New Revision: 645892
URL: http://svn.apache.org/viewvc?rev=645892&view=rev
Log:
Totally refactored the component to provide correctly initialized parser objects. Provide some documentation. Fixes TRB-39
Added:
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParserService.java
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserService.java
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserServiceSupport.java
Modified:
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/CookieParser.java
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParameterParser.java
turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java
turbine/fulcrum/trunk/parser/src/test/TestComponentConfig.xml
turbine/fulcrum/trunk/parser/src/test/TestRoleConfig.xml
turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java
turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/ParameterParserTest.java
turbine/fulcrum/trunk/parser/xdocs/changes.xml
turbine/fulcrum/trunk/parser/xdocs/index.xml
Modified: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java (original)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java Tue Apr 8 06:04:40 2008
@@ -34,10 +34,8 @@
import java.util.Iterator;
import java.util.Set;
-import org.apache.avalon.framework.configuration.Configurable;
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
import org.apache.commons.collections.iterators.ArrayIterator;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
@@ -75,30 +73,29 @@
* @version $Id$
*/
public class BaseValueParser
- extends AbstractLogEnabled
implements ValueParser,
- Recyclable, Configurable
+ Recyclable, ParserServiceSupport, LogEnabled
{
- /** The folding from the configuration */
- private int folding = URL_CASE_FOLDING_NONE;
+ /** The ParserService instance to query for conversion and configuration */
+ protected ParserService parserService;
- /** The automaticUpload setting from the configuration */
- private boolean automaticUpload = AUTOMATIC_DEFAULT;
+ /** A convenience logger */
+ private Logger logger;
- public BaseValueParser()
- {
- recycle();
- }
+ /**
+ * The character encoding to use when converting to byte arrays
+ */
+ private String characterEncoding = DEFAULT_CHARACTER_ENCODING;
/**
* Random access storage for parameter data.
*/
protected Hashtable parameters = new Hashtable();
- /**
- * The character encoding to use when converting to byte arrays
- */
- private String characterEncoding = "US-ASCII";
+ public BaseValueParser()
+ {
+ this(DEFAULT_CHARACTER_ENCODING);
+ }
/**
* Constructor that takes a character encoding
@@ -108,13 +105,40 @@
super();
recycle(characterEncoding);
}
+
+ /**
+ * Set a ParserService instance
+ */
+ public void setParserService(ParserService parserService)
+ {
+ this.parserService = parserService;
+
+ }
/**
+ * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
+ */
+ public void enableLogging(Logger logger)
+ {
+ this.logger = logger;
+ }
+
+ /**
+ * Provide an Avalon logger to the derived classes
+ *
+ * @return An Avalon logger instance
+ */
+ protected Logger getLogger()
+ {
+ return logger;
+ }
+
+ /**
* Recycles the parser.
*/
public void recycle()
{
- recycle("US-ASCII");
+ recycle(DEFAULT_CHARACTER_ENCODING);
}
/**
@@ -1561,10 +1585,6 @@
*/
private boolean disposed;
- protected String parameterEncoding;
-
-
-
/**
* Checks whether the object is disposed.
*
@@ -1619,112 +1639,31 @@
*/
public String convertAndTrim(String value)
{
- return convertAndTrim(value, getUrlFolding());
+ return parserService.convertAndTrim(value);
}
/**
- * A static version of the convert method, which
- * trims the string data and applies the conversion specified in
- * the property given by URL_CASE_FOLDING. It returns a new
+ * A convert method, which trims the string data and applies the
+ * conversion specified in the parameter given. It returns a new
* string so that it does not destroy the value data.
*
* @param value A String to be processed.
- * @return A new String converted to lowercase and trimmed.
+ * @param fold The parameter folding to be applied
+ * (see {@link ParserService})
+ * @return A new String converted to the correct case and trimmed.
*/
public String convertAndTrim(String value, int fold)
{
- if(value == null) return "";
-
- String tmp = value.trim();
-
- switch (fold)
- {
- case URL_CASE_FOLDING_NONE:
- {
- break;
- }
-
- case URL_CASE_FOLDING_LOWER:
- {
- tmp = tmp.toLowerCase();
- break;
- }
-
- case URL_CASE_FOLDING_UPPER:
- {
- tmp = tmp.toUpperCase();
- break;
- }
-
- default:
- {
- getLogger().error("Passed " + fold + " as fold rule, which is illegal!");
- break;
- }
- }
- return tmp;
+ return parserService.convertAndTrim(value, fold);
}
/**
- * Gets the folding value from the configuration
+ * Gets the folding value from the ParserService configuration
*
* @return The current Folding Value
*/
public int getUrlFolding()
{
- return folding;
- }
-
- /**
- * Gets the automaticUpload value from the configuration
- *
- * @return The current automaticUpload Value
- */
- public boolean getAutomaticUpload()
- {
- return automaticUpload;
- }
-
- /**
- * Avalon component lifecycle method
- */
- public void configure(Configuration conf) throws ConfigurationException
- {
- if (folding == URL_CASE_FOLDING_UNSET)
- {
- String foldString = conf.getChild(URL_CASE_FOLDING_KEY).getValue(URL_CASE_FOLDING_NONE_VALUE).toLowerCase();
-
- folding = URL_CASE_FOLDING_NONE;
-
- getLogger().debug("Setting folding from " + foldString);
-
- if (StringUtils.isNotEmpty(foldString))
- {
- if (foldString.equals(URL_CASE_FOLDING_NONE_VALUE))
- {
- folding = URL_CASE_FOLDING_NONE;
- }
- else if (foldString.equals(URL_CASE_FOLDING_LOWER_VALUE))
- {
- folding = URL_CASE_FOLDING_LOWER;
- }
- else if (foldString.equals(URL_CASE_FOLDING_UPPER_VALUE))
- {
- folding = URL_CASE_FOLDING_UPPER;
- }
- else
- {
- getLogger().error("Got " + foldString + " from " + URL_CASE_FOLDING_KEY + " property, which is illegal!");
- throw new ConfigurationException("Value " + foldString + " is illegal!");
- }
- }
- }
-
- parameterEncoding = conf.getChild(PARAMETER_ENCODING_KEY)
- .getValue(PARAMETER_ENCODING_DEFAULT).toLowerCase();
-
- automaticUpload = conf.getAttributeAsBoolean(
- AUTOMATIC_KEY,
- AUTOMATIC_DEFAULT);
+ return parserService.getUrlFolding();
}
}
Modified: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/CookieParser.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/CookieParser.java?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/CookieParser.java (original)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/CookieParser.java Tue Apr 8 06:04:40 2008
@@ -55,9 +55,6 @@
public interface CookieParser
extends ValueParser
{
- /** Avalon Identifier **/
- String ROLE = CookieParser.class.getName();
-
static final int AGE_SESSION = -1;
static final int AGE_DELETE = 0;
Modified: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java (original)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParameterParser.java Tue Apr 8 06:04:40 2008
@@ -31,11 +31,8 @@
import javax.servlet.http.HttpServletRequest;
import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang.ArrayUtils;
-import org.apache.fulcrum.upload.UploadService;
/**
* DefaultParameterParser is a utility object to handle parsing and
@@ -64,8 +61,7 @@
*/
public class DefaultParameterParser
extends BaseValueParser
- implements ParameterParser,
- Serviceable
+ implements ParameterParser
{
/**
* The servlet request to parse.
@@ -78,11 +74,6 @@
private byte[] uploadData = null;
/**
- * The upload service component to use
- */
- private UploadService uploadService = null;
-
- /**
* Create a new empty instance of ParameterParser. Uses the
* default character encoding (US-ASCII).
*
@@ -152,12 +143,11 @@
String enc = request.getCharacterEncoding();
setCharacterEncoding(enc != null
? enc
- : parameterEncoding);
+ : parserService.getParameterEncoding());
String contentType = request.getHeader("Content-type");
- if (uploadService != null
- && getAutomaticUpload()
+ if (parserService.getAutomaticUpload()
&& contentType != null
&& contentType.startsWith("multipart/form-data"))
{
@@ -168,7 +158,7 @@
try
{
- List fileItems = uploadService.parseRequest(request);
+ List fileItems = parserService.parseUpload(request);
if (fileItems != null)
{
@@ -360,31 +350,6 @@
catch ( ClassCastException e )
{
return null;
- }
- }
-
- // ---------------- Avalon Lifecycle Methods ---------------------
- /**
- * Avalon component lifecycle method
- */
- public void service(ServiceManager manager) throws ServiceException
- {
- if (manager.hasService(UploadService.ROLE))
- {
- uploadService = (UploadService)manager.lookup(UploadService.ROLE);
- }
- else
- {
- /*
- * Automatic parsing of uploaded file items was requested but no
- * UploadService is available
- */
- if (getAutomaticUpload())
- {
- throw new ServiceException(ParameterParser.ROLE,
- AUTOMATIC_KEY + " = true requires " +
- UploadService.ROLE + " to be available");
- }
}
}
}
Added: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParserService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParserService.java?rev=645892&view=auto
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParserService.java (added)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultParserService.java Tue Apr 8 06:04:40 2008
@@ -0,0 +1,322 @@
+package org.apache.fulcrum.parser;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.commons.lang.StringUtils;
+import org.apache.fulcrum.pool.PoolException;
+import org.apache.fulcrum.pool.PoolService;
+import org.apache.fulcrum.upload.UploadService;
+
+
+/**
+ * The DefaultParserService provides the efault implementation
+ * of a {@link ParserService}.
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id: BaseValueParser.java 542062 2007-05-28 00:29:43Z seade $
+ */
+public class DefaultParserService
+ extends AbstractLogEnabled
+ implements ParserService,
+ Configurable, Serviceable
+{
+ /** The folding from the configuration */
+ private int folding = URL_CASE_FOLDING_NONE;
+
+ /** The automaticUpload setting from the configuration */
+ private boolean automaticUpload = AUTOMATIC_DEFAULT;
+
+ /**
+ * The parameter encoding to use when parsing parameter strings
+ */
+ private String parameterEncoding = PARAMETER_ENCODING_DEFAULT;
+
+ /**
+ * The upload service component to use
+ */
+ private UploadService uploadService = null;
+
+ /**
+ * The pool service component to use
+ */
+ private PoolService poolService = null;
+
+ /**
+ * Get the character encoding that will be used by this ValueParser.
+ */
+ public String getParameterEncoding()
+ {
+ return parameterEncoding;
+ }
+
+ /**
+ * Trims the string data and applies the conversion specified in
+ * the property given by URL_CASE_FOLDING. It returns a new
+ * string so that it does not destroy the value data.
+ *
+ * @param value A String to be processed.
+ * @return A new String converted to lowercase and trimmed.
+ */
+ public String convert(String value)
+ {
+ return convertAndTrim(value);
+ }
+
+ /**
+ * Convert a String value according to the url-case-folding property.
+ *
+ * @param value the String to convert
+ *
+ * @return a new String.
+ *
+ */
+ public String convertAndTrim(String value)
+ {
+ return convertAndTrim(value, getUrlFolding());
+ }
+
+ /**
+ * A static version of the convert method, which
+ * trims the string data and applies the conversion specified in
+ * the property given by URL_CASE_FOLDING. It returns a new
+ * string so that it does not destroy the value data.
+ *
+ * @param value A String to be processed.
+ * @return A new String converted to lowercase and trimmed.
+ */
+ public String convertAndTrim(String value, int fold)
+ {
+ if(value == null) return "";
+
+ String tmp = value.trim();
+
+ switch (fold)
+ {
+ case URL_CASE_FOLDING_NONE:
+ {
+ break;
+ }
+
+ case URL_CASE_FOLDING_LOWER:
+ {
+ tmp = tmp.toLowerCase();
+ break;
+ }
+
+ case URL_CASE_FOLDING_UPPER:
+ {
+ tmp = tmp.toUpperCase();
+ break;
+ }
+
+ default:
+ {
+ getLogger().error("Passed " + fold + " as fold rule, which is illegal!");
+ break;
+ }
+ }
+ return tmp;
+ }
+
+ /**
+ * Gets the folding value from the configuration
+ *
+ * @return The current Folding Value
+ */
+ public int getUrlFolding()
+ {
+ return folding;
+ }
+
+ /**
+ * Gets the automaticUpload value from the configuration
+ *
+ * @return The current automaticUpload Value
+ */
+ public boolean getAutomaticUpload()
+ {
+ return automaticUpload;
+ }
+
+ /**
+ * Use the UploadService if available to parse the given request
+ * for uploaded files
+ *
+ * @return A list of {@link org.apache.commons.upload.FileItem}s
+ *
+ * @throws ServiceException if parsing fails or the UploadService
+ * is not available
+ */
+ public List parseUpload(HttpServletRequest request) throws ServiceException
+ {
+ if (uploadService == null)
+ {
+ throw new ServiceException(ParserService.ROLE, "UploadService is not available.");
+ }
+ else
+ {
+ return uploadService.parseRequest(request);
+ }
+ }
+
+ /**
+ * Get a {@link ValueParser} instance from the service. Use the
+ * given Class to create the object.
+ *
+ * @return An object that implements ValueParser
+ *
+ * @throws InstantiationException if the instance could not be created
+ */
+ public ValueParser getParser(Class ppClass) throws InstantiationException
+ {
+ ValueParser vp = null;
+
+ try
+ {
+ vp = (ValueParser) poolService.getInstance(ppClass);
+
+ if (vp instanceof ParserServiceSupport)
+ {
+ ((ParserServiceSupport)vp).setParserService(this);
+ }
+
+ if (vp instanceof LogEnabled)
+ {
+ ((LogEnabled)vp).enableLogging(getLogger().getChildLogger(ppClass.getName()));
+ }
+
+ }
+ catch (PoolException pe)
+ {
+ throw new InstantiationException("Parser class '" + ppClass + "' is illegal. " + pe.getMessage());
+ }
+ catch (ClassCastException x)
+ {
+ throw new InstantiationException("Parser class '" + ppClass + "' is illegal. " + x.getMessage());
+ }
+
+ return vp;
+ }
+
+ /**
+ * Return a used Parser to the service. This allows for
+ * pooling and recycling
+ *
+ * @param parser
+ */
+ public void putParser(ValueParser parser)
+ {
+ parser.clear();
+ poolService.putInstance(parser);
+ }
+
+ /**
+ * Avalon component lifecycle method
+ */
+ public void configure(Configuration conf) throws ConfigurationException
+ {
+ if (folding == URL_CASE_FOLDING_UNSET)
+ {
+ String foldString = conf.getChild(URL_CASE_FOLDING_KEY).getValue(URL_CASE_FOLDING_NONE_VALUE).toLowerCase();
+
+ folding = URL_CASE_FOLDING_NONE;
+
+ getLogger().debug("Setting folding from " + foldString);
+
+ if (StringUtils.isNotEmpty(foldString))
+ {
+ if (foldString.equals(URL_CASE_FOLDING_NONE_VALUE))
+ {
+ folding = URL_CASE_FOLDING_NONE;
+ }
+ else if (foldString.equals(URL_CASE_FOLDING_LOWER_VALUE))
+ {
+ folding = URL_CASE_FOLDING_LOWER;
+ }
+ else if (foldString.equals(URL_CASE_FOLDING_UPPER_VALUE))
+ {
+ folding = URL_CASE_FOLDING_UPPER;
+ }
+ else
+ {
+ getLogger().error("Got " + foldString + " from " + URL_CASE_FOLDING_KEY + " property, which is illegal!");
+ throw new ConfigurationException("Value " + foldString + " is illegal!");
+ }
+ }
+ }
+
+ parameterEncoding = conf.getChild(PARAMETER_ENCODING_KEY)
+ .getValue(PARAMETER_ENCODING_DEFAULT).toLowerCase();
+
+ automaticUpload = conf.getAttributeAsBoolean(
+ AUTOMATIC_KEY,
+ AUTOMATIC_DEFAULT);
+ }
+
+ // ---------------- Avalon Lifecycle Methods ---------------------
+ /**
+ * Avalon component lifecycle method
+ */
+ public void service(ServiceManager manager) throws ServiceException
+ {
+ if (manager.hasService(UploadService.ROLE))
+ {
+ uploadService = (UploadService)manager.lookup(UploadService.ROLE);
+ }
+ else
+ {
+ /*
+ * Automatic parsing of uploaded file items was requested but no
+ * UploadService is available
+ */
+ if (getAutomaticUpload())
+ {
+ throw new ServiceException(ParserService.ROLE,
+ AUTOMATIC_KEY + " = true requires " +
+ UploadService.ROLE + " to be available");
+ }
+ }
+
+ if (manager.hasService(PoolService.ROLE))
+ {
+ poolService = (PoolService)manager.lookup(PoolService.ROLE);
+ }
+ else
+ {
+ throw new ServiceException(ParserService.ROLE,
+ "Service requires " +
+ PoolService.ROLE + " to be available");
+ }
+ }
+}
Modified: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParameterParser.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParameterParser.java?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParameterParser.java (original)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParameterParser.java Tue Apr 8 06:04:40 2008
@@ -54,9 +54,6 @@
public interface ParameterParser
extends ValueParser
{
- /** Avalon Identifier **/
- String ROLE = ParameterParser.class.getName();
-
/**
* Gets the parsed servlet request.
*
Added: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserService.java?rev=645892&view=auto
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserService.java (added)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserService.java Tue Apr 8 06:04:40 2008
@@ -0,0 +1,168 @@
+package org.apache.fulcrum.parser;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.avalon.framework.service.ServiceException;
+
+
+/**
+ * ParserService defines the methods which are needed by the parser objects
+ * to get their necessities.
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id: ValueParser.java 535465 2007-05-05 06:58:06Z tv $
+ */
+public interface ParserService
+{
+ /** Avalon Identifier **/
+ String ROLE = ParserService.class.getName();
+
+ /** Default Encoding for Parameter Parser */
+ String PARAMETER_ENCODING_DEFAULT = "ISO-8859-1";
+
+ /** Key for the Parameter Parser Encoding */
+ String PARAMETER_ENCODING_KEY = "parameterEncoding";
+
+ /** Property for setting the URL folding value */
+ String URL_CASE_FOLDING_KEY = "urlCaseFolding";
+
+ /** No folding */
+ String URL_CASE_FOLDING_NONE_VALUE = "none";
+
+ /** Fold to lower case */
+ String URL_CASE_FOLDING_LOWER_VALUE = "lower";
+
+ /** Fold to upper case */
+ String URL_CASE_FOLDING_UPPER_VALUE = "upper";
+
+ /** No folding set */
+ int URL_CASE_FOLDING_UNSET = 0;
+
+ /** Folding set to "no folding" */
+ int URL_CASE_FOLDING_NONE = 1;
+
+ /** Folding set to "lowercase" */
+ int URL_CASE_FOLDING_LOWER = 2;
+
+ /** Folding set to "uppercase" */
+ int URL_CASE_FOLDING_UPPER = 3;
+
+ /** Parse file upload items automatically */
+ String AUTOMATIC_KEY = "automaticUpload";
+
+ /**
+ * <p> The default value of 'automaticUpload' property
+ * (<code>false</code>). If set to <code>true</code>, parsing the
+ * multipart request will be performed automatically by {@link
+ * org.apache.fulcrum.parser.ParameterParser}. Otherwise, an {@link
+ * org.apache.turbine.modules.Action} may decide to parse the
+ * request by calling {@link #parseRequest(HttpServletRequest, String)
+ * parseRequest} manually.
+ */
+ boolean AUTOMATIC_DEFAULT = false;
+
+ /**
+ * Get the parameter encoding that has been configured as default for
+ * the ParserService.
+ */
+ String getParameterEncoding();
+
+ /**
+ * Trims the string data and applies the conversion specified in
+ * the property given by URL_CASE_FOLDING. It returns a new
+ * string so that it does not destroy the value data.
+ *
+ * @param value A String to be processed.
+ * @return A new String converted to lowercase and trimmed.
+ */
+ String convert(String value);
+
+ /**
+ * Convert a String value according to the url-case-folding property.
+ *
+ * @param value the String to convert
+ *
+ * @return a new String.
+ *
+ */
+ String convertAndTrim(String s);
+
+ /**
+ * A convert method, which trims the string data and applies the
+ * conversion specified in the parameter given. It returns a new
+ * string so that it does not destroy the value data.
+ *
+ * @param value A String to be processed.
+ * @param fold The parameter folding to be applied
+ * (see {@link ParserService})
+ * @return A new String converted to the correct case and trimmed.
+ */
+ String convertAndTrim(String value, int fold);
+
+ /**
+ * Gets the folding value from the configuration
+ *
+ * @return The current Folding Value
+ */
+ int getUrlFolding();
+
+ /**
+ * Gets the automaticUpload value from the configuration
+ *
+ * @return The current automaticUpload Value
+ */
+ boolean getAutomaticUpload();
+
+ /**
+ * Use the UploadService if available to parse the given request
+ * for uploaded files
+ *
+ * @return A list of {@link org.apache.commons.upload.FileItem}s
+ *
+ * @throws ServiceException if parsing fails or the UploadService
+ * is not available
+ */
+ List parseUpload(HttpServletRequest request) throws ServiceException;
+
+ /**
+ * Get a {@link ValueParser} instance from the service. Use the
+ * default imlementation.
+ *
+ * @return An object that implements ValueParser
+ *
+ * @throws InstantiationException if the instance could not be created
+ */
+ ValueParser getParser(Class ppClass) throws InstantiationException;
+
+ /**
+ * Return a used Parser to the service. This allows for
+ * pooling and recycling
+ *
+ * @param parser
+ */
+ void putParser(ValueParser parser);
+}
+
Added: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserServiceSupport.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserServiceSupport.java?rev=645892&view=auto
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserServiceSupport.java (added)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ParserServiceSupport.java Tue Apr 8 06:04:40 2008
@@ -0,0 +1,36 @@
+package org.apache.fulcrum.parser;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * ParserServiceSupport defines the methods which are needed by the parser objects
+ * to get access to the ParserService.
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id: ValueParser.java 535465 2007-05-05 06:58:06Z tv $
+ */
+public interface ParserServiceSupport
+{
+ /**
+ * Set a ParserService instance
+ */
+ void setParserService(ParserService parserService);
+}
Modified: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java (original)
+++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java Tue Apr 8 06:04:40 2008
@@ -27,8 +27,6 @@
import java.util.Date;
import java.util.Set;
-import javax.servlet.http.HttpServletRequest;
-
/**
* ValueParser is a base interface for classes that need to parse
@@ -51,52 +49,10 @@
*/
public interface ValueParser
{
- /** Avalon Identifier **/
- String ROLE = ValueParser.class.getName();
-
- /** Default Encoding for Parameter Parser */
- static final String PARAMETER_ENCODING_DEFAULT = "ISO-8859-1";
-
- /** Key for the Parameter Parser Encoding */
- static final String PARAMETER_ENCODING_KEY = "parameterEncoding";
-
- /** Property for setting the URL folding value */
- static final String URL_CASE_FOLDING_KEY = "urlCaseFolding";
-
- /** No folding */
- static final String URL_CASE_FOLDING_NONE_VALUE = "none";
-
- /** Fold to lower case */
- static final String URL_CASE_FOLDING_LOWER_VALUE = "lower";
-
- /** Fold to upper case */
- static final String URL_CASE_FOLDING_UPPER_VALUE = "upper";
-
- /** No folding set */
- static final int URL_CASE_FOLDING_UNSET = 0;
-
- /** Folding set to "no folding" */
- static final int URL_CASE_FOLDING_NONE = 1;
-
- /** Folding set to "lowercase" */
- static final int URL_CASE_FOLDING_LOWER = 2;
-
- /** Folding set to "uppercase" */
- static final int URL_CASE_FOLDING_UPPER = 3;
-
- /** Parse file upload items automatically */
- static final String AUTOMATIC_KEY = "automaticUpload";
-
/**
- * <p> The default value of 'automaticUpload' property
- * (<code>false</code>). If set to <code>true</code>, parsing the
- * multipart request will be performed automatically by {@link
- * org.apache.fulcrum.parser.ParameterParser}. Otherwise, an {@link
- * org.apache.turbine.modules.Action} may decide to parse the
- * request by calling {@link #parseRequest(HttpServletRequest, String)
- * parseRequest} manually.
+ * The default character encoding to use when converting to byte arrays
*/
- static final boolean AUTOMATIC_DEFAULT = false;
+ String DEFAULT_CHARACTER_ENCODING = "US-ASCII";
/**
* Clear all name/value pairs out of this object.
@@ -719,17 +675,10 @@
String convertAndTrim(String s, int folding);
/**
- * Gets the folding value from the configuration
+ * Gets the folding value from the ParserService configuration
*
* @return The current Folding Value
*/
int getUrlFolding();
-
- /**
- * Gets the automaticUpload value from the configuration
- *
- * @return The current automaticUpload Value
- */
- boolean getAutomaticUpload();
}
Modified: turbine/fulcrum/trunk/parser/src/test/TestComponentConfig.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/test/TestComponentConfig.xml?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/test/TestComponentConfig.xml (original)
+++ turbine/fulcrum/trunk/parser/src/test/TestComponentConfig.xml Tue Apr 8 06:04:40 2008
@@ -22,11 +22,5 @@
<parameterEncoding>utf-8</parameterEncoding>
<automaticUpload>true</automaticUpload>
</parser>
- <cookie>
- <parameterEncoding>utf-8</parameterEncoding>
- </cookie>
- <base>
- <parameterEncoding>utf-8</parameterEncoding>
- </base>
<upload repository="target" sizeMax="1048576" sizeThreshold="10240"/>
</componentConfig>
Modified: turbine/fulcrum/trunk/parser/src/test/TestRoleConfig.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/test/TestRoleConfig.xml?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/test/TestRoleConfig.xml (original)
+++ turbine/fulcrum/trunk/parser/src/test/TestRoleConfig.xml Tue Apr 8 06:04:40 2008
@@ -25,18 +25,18 @@
default-class="org.apache.fulcrum.upload.DefaultUploadService"
/>
<role
- name="org.apache.fulcrum.parser.ParameterParser"
+ name="org.apache.fulcrum.pool.PoolService"
shorthand="parser"
- default-class="org.apache.fulcrum.parser.DefaultParameterParser"
+ default-class="org.apache.fulcrum.pool.DefaultPoolService"
/>
<role
- name="org.apache.fulcrum.parser.CookieParser"
- shorthand="cookie"
- default-class="org.apache.fulcrum.parser.DefaultCookieParser"
+ name="org.apache.fulcrum.factory.FactoryService"
+ shorthand="parser"
+ default-class="org.apache.fulcrum.factory.DefaultFactoryService"
/>
<role
- name="org.apache.fulcrum.parser.ValueParser"
- shorthand="base"
- default-class="org.apache.fulcrum.parser.BaseValueParser"
+ name="org.apache.fulcrum.parser.ParserService"
+ shorthand="parser"
+ default-class="org.apache.fulcrum.parser.DefaultParserService"
/>
</role-list>
Modified: turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java (original)
+++ turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/BaseValueParserTest.java Tue Apr 8 06:04:40 2008
@@ -41,6 +41,8 @@
private BaseValueParser parser;
+ private ParserService parserService;
+
/**
* Performs any initialization that must happen before each test is run.
* @throws Exception
@@ -50,7 +52,8 @@
super.setUp();
try
{
- parser = (BaseValueParser) this.lookup(ValueParser.ROLE);
+ parserService = (ParserService)this.lookup(ParserService.ROLE);
+ parser = (BaseValueParser) parserService.getParser(BaseValueParser.class);
}
catch (ComponentException e)
{
@@ -64,7 +67,8 @@
*/
protected void tearDown()
{
- parser = null;
+ parserService.putParser(parser);
+ this.release(parserService);
}
public void testGetByte()
@@ -636,10 +640,11 @@
{
try
{
- BaseValueParser vp = (BaseValueParser)this.lookup(ValueParser.ROLE);
+ BaseValueParser vp = (BaseValueParser) parserService.getParser(BaseValueParser.class);
assertFalse(vp.isDisposed());
+ parserService.putParser(vp);
}
- catch (ComponentException e)
+ catch (InstantiationException e)
{
assertTrue("Could not instantiate ValueParser object", false);
}
Modified: turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/ParameterParserTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/ParameterParserTest.java?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/ParameterParserTest.java (original)
+++ turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/ParameterParserTest.java Tue Apr 8 06:04:40 2008
@@ -37,6 +37,8 @@
{
private ParameterParser parameterParser = null;
+ private ParserService parserService;
+
/**
* Defines the testcase name for JUnit.
*
@@ -52,7 +54,8 @@
super.setUp();
try
{
- parameterParser = (ParameterParser) this.lookup(ParameterParser.class.getName());
+ parserService = (ParserService)this.lookup(ParserService.ROLE);
+ parameterParser = (ParameterParser) parserService.getParser(DefaultParameterParser.class);
}
catch (ComponentException e)
{
@@ -63,7 +66,7 @@
public void testConfiguredUrlCaseFolding() throws Exception
{
- assertTrue(parameterParser.getUrlFolding() == ParameterParser.URL_CASE_FOLDING_NONE);
+ assertTrue(parameterParser.getUrlFolding() == ParserService.URL_CASE_FOLDING_NONE);
}
/**
@@ -73,9 +76,9 @@
*/
public void testRepositoryExists() throws Exception
{
- assertEquals("TRIMMED_and_Not_Modified",parameterParser.convertAndTrim(" TRIMMED_and_Not_Modified ", ParameterParser.URL_CASE_FOLDING_NONE));
- assertEquals("trimmed_and_lower_case",parameterParser.convertAndTrim(" TRIMMED_and_Lower_Case ", ParameterParser.URL_CASE_FOLDING_LOWER));
- assertEquals("TRIMMED_AND_UPPER_CASE",parameterParser.convertAndTrim(" TRIMMED_and_Upper_Case ", ParameterParser.URL_CASE_FOLDING_UPPER));
+ assertEquals("TRIMMED_and_Not_Modified",parameterParser.convertAndTrim(" TRIMMED_and_Not_Modified ", ParserService.URL_CASE_FOLDING_NONE));
+ assertEquals("trimmed_and_lower_case",parameterParser.convertAndTrim(" TRIMMED_and_Lower_Case ", ParserService.URL_CASE_FOLDING_LOWER));
+ assertEquals("TRIMMED_AND_UPPER_CASE",parameterParser.convertAndTrim(" TRIMMED_and_Upper_Case ", ParserService.URL_CASE_FOLDING_UPPER));
}
/**
Modified: turbine/fulcrum/trunk/parser/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/xdocs/changes.xml?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/xdocs/changes.xml (original)
+++ turbine/fulcrum/trunk/parser/xdocs/changes.xml Tue Apr 8 06:04:40 2008
@@ -25,6 +25,10 @@
<body>
<release version="1.0.2-dev" date="in SVN">
+ <action dev="tv" type="update" issue="TRB-39">
+ Totally refactored the component to provide correctly initialized
+ parser objects.
+ </action>
<action dev="tv" type="update" issue="TRB-32" due-to="Juergen Hofmann">
Port parser fixes of Turbine 2.3.2 over to the component.
</action>
Modified: turbine/fulcrum/trunk/parser/xdocs/index.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/xdocs/index.xml?rev=645892&r1=645891&r2=645892&view=diff
==============================================================================
--- turbine/fulcrum/trunk/parser/xdocs/index.xml (original)
+++ turbine/fulcrum/trunk/parser/xdocs/index.xml Tue Apr 8 06:04:40 2008
@@ -29,7 +29,7 @@
<section name="Overview">
<p>
- This Service functions as a Parser component.
+ This Service functions as a repository for Parser components.
</p>
<p>
@@ -47,10 +47,10 @@
<source>
<![CDATA[
- <role
- name="org.apache.fulcrum.parser.*"
- shorthand="cache"
- default-class="org.apache.fulcrum.parser.*"/>
+ <role
+ name="org.apache.fulcrum.parser.ParserService"
+ shorthand="parser"
+ default-class="org.apache.fulcrum.parser.DefaultParserService"/>
]]>
</source>
@@ -60,51 +60,36 @@
<source>
<![CDATA[
- <cache cacheInitialSize="20" cacheCheckFrequency="5"/>
+ <parser>
+ <parameterEncoding>utf-8</parameterEncoding>
+ <automaticUpload>true</automaticUpload>
+ </parser>
]]>
</source>
</section>
<section name="Usage">
+ <p>
+ You get a parser from the service like this:
+ </p>
<source><![CDATA[
+ // Get a ValueParser instance
+ ValueParser parser = (ValueParser) parserService.getParser(BaseValueParser.class);
- GlobalCacheService gs = null;
- try
- {
- /*
- * Look for the item in the cache.
- * If it doesn't exist or the item is stale,
- * the cache will throw an exception.
- */
- gs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE)
-
- CachedObject obj = gs.getObject("cached_object");
-
- data.setMessage( data.getScreen() + " Got " +
- obj.getContents().toString() + " from global cache!" );
- }
- catch(ObjectExpiredException gone)
- {
- /*
- * Add the item to the cache.
- */
- gs.addObject("cached_object",
- new CachedObject("in_the_cache",5000));
-
- data.setMessage( data.getScreen() +
- " Refreshed/or added new item to" +
- " the cache! Expires in 5 seconds" );
- }
-
+ parser.add(...);
+ Double d = parser.getDoubleObject(...);
+
+ // Return the instance to the service
+ parserService.putParser(parser);
]]></source>
<p>
- You can also place an expiration time on your objects so the Service will
- automatically remove them when they expire. If you don't specify an expiration
- time, the Service uses 5 seconds. To see an example, look at the
- test case <a href="xref-test/org/apache/fulcrum/cache/CacheTest.html">CacheTest</a>
+ Parsers are pooled inside the service. You can use this service to manage any types of
+ parsers, you just need to provide the class you wnat to manage. If the parser class
+ implements LogEnabled it will get an (Avalon) logger injected. If the parser class
+ implements ParserServiceSupport it will get an instance of this service injected.
</p>
</section>