📄 i18nfactoryset.java
字号:
/*
* $Header: /sfroot/cvs/esimple/src/core/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java,v 1.1.1.1 2004/09/08 06:38:39 lava Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/09/08 06:38:39 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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", "Struts", 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. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.tiles.xmlDefinition;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.FactoryNotFoundException;
import org.apache.struts.tiles.DefinitionsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.ServletRequest;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Locale;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import org.xml.sax.SAXException;
/**
* Definitions factory.
* This implementation allows to have a set of definition factories.
* There is a main factory and one factory for each file associated to a Locale.
*
* To retrieve a definition, we first search for the appropriate factory using
* the Locale found in session context. If no factory is found, use the
* default one. Then we ask the factory for the definition.
*
* A definition factory file is loaded using main filename extended with locale code
* (ex : <code>templateDefinitions_fr.xml</code>). If no file is found under this name, use default file.
*/
public class I18nFactorySet extends FactorySet
{
/** Commons Logging instance. */
protected static Log log = LogFactory.getLog(I18nFactorySet.class);
/** Config file parameter name.
* @deprecated use DEFINITIONS_CONFIG_PARAMETER_NAME
*/
public static final String INSTANCES_CONFIG_PARAMETER_NAME = "instances-config";
/** Default name */
//public static final String DEFAULT_DEFINITIONS_FILE_NAME = "/WEB-INF/componentDefinitions.xml";
/** Config file parameter name. */
public static final String DEFINITIONS_CONFIG_PARAMETER_NAME = "definitions-config";
/** Config file parameter name. */
public static final String PARSER_DETAILS_PARAMETER_NAME = "definitions-parser-details";
/** Config file parameter name. */
public static final String PARSER_VALIDATE_PARAMETER_NAME = "definitions-parser-validate";
/** Possible definition filenames. */
public static final String DEFAULT_DEFINITION_FILENAMES[] = {
"/WEB-INF/tileDefinitions.xml",
"/WEB-INF/componentDefinitions.xml",
"/WEB-INF/instanceDefinitions.xml"};
/** Default factory. */
protected DefinitionsFactory defaultFactory;
/** Xml parser used.
* Attribute is transient to allow serialization. In this implementaiton,
* xmlParser is created each time we need it ;-(.
*/
protected transient XmlParser xmlParser;
/** Do we want validating parser. Default is <code>false</code>.
* Can be set from servlet config file.
*/
protected boolean isValidatingParser = false;
/** Parser detail level. Default is 0.
* Can be set from servlet config file.
*/
protected int parserDetailLevel = 0;
/**
* Maximum length of one branch of the resource search path tree.
* Used in getBundle().
*/
private static final int MAX_BUNDLES_SEARCHED = 2;
/** Default filenames extension. */
public static final String FILENAME_EXTENSION = ".xml";
/** Names of files containing instances descriptions. */
private List filenames;
/** Collection of already loaded definitions set, referenced by their suffix. */
private Map loaded;
/**
* Parameterless Constructor.
* Method {@link #initFactory} must be called prior to any use of created factory.
*/
public I18nFactorySet()
{
}
/**
* Constructor.
* Init the factory by reading appropriate configuration file.
* @param servletContext Servlet context.
* @param properties Map containing all properties.
* @throws FactoryNotFoundException Can't find factory configuration file.
*/
public I18nFactorySet(ServletContext servletContext, Map properties )
throws DefinitionsFactoryException
{
initFactory( servletContext, properties);
}
/**
* Initialization method.
* Init the factory by reading appropriate configuration file.
* This method is called exactly once immediately after factory creation in
* case of internal creation (by DefinitionUtil).
* @param servletContext Servlet Context passed to newly created factory.
* @param properties Map of name/property passed to newly created factory. Map can contains
* more properties than requested.
* @throws DefinitionsFactoryException An error occur during initialization.
*/
public void initFactory(ServletContext servletContext, Map properties )
throws DefinitionsFactoryException
{
// Set some property values
String value = (String)properties.get(PARSER_VALIDATE_PARAMETER_NAME);
if( value != null )
{
isValidatingParser = Boolean.valueOf( value ).booleanValue();
} // end if
value = (String)properties.get(PARSER_DETAILS_PARAMETER_NAME);
if( value != null )
{
try {
parserDetailLevel = Integer.valueOf( value ).intValue();
}
catch( NumberFormatException ex )
{
log.error( "Bad format for parameter '"
+ PARSER_DETAILS_PARAMETER_NAME
+ "'. Integer expected.");
}
} // end if
// init factory withappropriate configuration file
// Try to use provided filename, if any.
// If no filename are provided, try to use default ones.
String filename = (String)properties.get(DEFINITIONS_CONFIG_PARAMETER_NAME);
if( filename != null )
{ // Use provided filename
try
{
initFactory( servletContext, filename );
if(log.isDebugEnabled())
log.debug( "Factory initialized from file '" + filename + "'." );
}
catch( FileNotFoundException ex )
{ // A filename is specified, throw appropriate error.
log.error( ex.getMessage() + " : Can't find file '" +filename + "'" );
throw new FactoryNotFoundException( ex.getMessage() + " : Can't find file '" +filename + "'" ) ;
} // end catch
}
else
{ // try each default file names
for( int i=0; i<DEFAULT_DEFINITION_FILENAMES.length; i++ )
{
filename = DEFAULT_DEFINITION_FILENAMES[i];
try
{
initFactory( servletContext, filename );
if(log.isInfoEnabled())
{
log.info( "Factory initialized from file '" + filename + "'." );
}
}
catch( FileNotFoundException ex )
{ // Do nothing
} // end catch
} // end loop
} // end if
}
/**
* Initialization method.
* Init the factory by reading appropriate configuration file.
* This method is called exactly once immediately after factory creation in
* case of internal creation (by DefinitionUtil).
* @param servletContext Servlet Context passed to newly created factory.
* @param proposedFilename File names, comma separated, to use as base file names.
* @throws DefinitionsFactoryException An error occur during initialization.
*/
protected void initFactory(ServletContext servletContext, String proposedFilename )
throws DefinitionsFactoryException, FileNotFoundException
{
// Init list of filenames
StringTokenizer tokenizer = new StringTokenizer( proposedFilename, "," );
this.filenames = new ArrayList(tokenizer.countTokens());
while( tokenizer.hasMoreTokens() )
{
this.filenames.add(tokenizer.nextToken().trim());
}
loaded = new HashMap();
defaultFactory = createDefaultFactory( servletContext );
if(log.isDebugEnabled())
log.debug( "default factory:" + defaultFactory );
}
/**
* Get default factory.
* @return Default factory
*/
protected DefinitionsFactory getDefaultFactory()
{
return defaultFactory;
}
/**
* Create default factory .
* Create InstancesMapper for specified Locale.
* If creation failes, use default mapper and log error message.
* @param servletContext Current servlet context. Used to open file.
* @return Created default definition factory.
* @throws DefinitionsFactoryException If an error occur while creating factory.
* @throws FileNotFoundException if factory can't be loaded from filenames.
*/
protected DefinitionsFactory createDefaultFactory(ServletContext servletContext)
throws DefinitionsFactoryException, FileNotFoundException
{
XmlDefinitionsSet rootXmlConfig = parseXmlFiles( servletContext, "", null );
if( rootXmlConfig == null )
throw new FileNotFoundException();
rootXmlConfig.resolveInheritances();
if(log.isDebugEnabled())
log.debug( rootXmlConfig );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -