You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2002/06/25 05:15:43 UTC
cvs commit: jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition DefinitionsFactory.java FactorySet.java I18nFactorySet.java XmlAttribute.java XmlDefinition.java XmlDefinitionsSet.java XmlListAttribute.java XmlParser.java
craigmcc 2002/06/24 20:15:43
Added: src/share/org/apache/struts/tiles/actions
DefinitionDispatcherAction.java NoOpAction.java
ReloadDefinitionsAction.java TilesAction.java
ViewDefinitionsAction.java
src/share/org/apache/struts/tiles/beans MenuItem.java
SimpleMenuItem.java
src/share/org/apache/struts/tiles/definition
ReloadableDefinitionsFactory.java
src/share/org/apache/struts/tiles/xmlDefinition
DefinitionsFactory.java FactorySet.java
I18nFactorySet.java XmlAttribute.java
XmlDefinition.java XmlDefinitionsSet.java
XmlListAttribute.java XmlParser.java
Log:
Migrate Tiles to main codebase (Part 2).
Revision Changes Path
1.1 jakarta-struts/src/share/org/apache/struts/tiles/actions/DefinitionDispatcherAction.java
Index: DefinitionDispatcherAction.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/actions/DefinitionDispatcherAction.java,v 1.1 2002/06/25 03:15:42 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:42 $
*
* ====================================================================
*
* 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.actions;
import org.apache.struts.tiles.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* <p>An <strong>Action</strong> that dispatches to a Tiles Definition
* that is named by the request parameter whose name is specified
* by the <code>parameter</code> property of the corresponding
* ActionMapping.
* This action is usefull in following situations :
* <li>
* <ul>To associate an Url to a definition</ul>
* <ul>To use Struts <link> tag on a definition</ul>
* </li>
* <p>To configure the use of this action in your
* <code>struts-config.xml</code> file, create an entry like this:</p>
*
* <code>
* <action path="/saveSubscription"
* type="org.apache.struts.tiles.actions.DefinitionDispatcherAction"
* parameter="def"/>
* <forward name="success" path="anything" //>
* <forward name="error" path="path.to.error.page" //>
* </code>
*
* <p>which will use the value of the request parameter named "def"
* to pick the appropriate definition name.
* <p> The value for success doesn't matter. The forward will forward to
* appropriate definition.
* <p> The value for error should denote a valid jsp path or definition name.
*
* @author Niall Pemberton <ni...@btInternet.com>
* @author Craig R. McClanahan
* @author Cedric Dumoulin
* @version $Revision: 1.1 $ $Date: 2002/06/25 03:15:42 $
*/
public final class DefinitionDispatcherAction extends Action {
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*
* @param servlet The ActionServlet making this request
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// Identify the request parameter containing the method name
// If none defined, use "def"
String parameter = mapping.getParameter();
if (parameter == null) {
parameter = "def";
}
// Identify the method name to be dispatched to
String name = request.getParameter(parameter);
if (name == null)
{
String msg = "Definition dispatcher action : can't get parameter '"
+ parameter + "'.";
printError( response, msg );
return (null);
}
// Try to dispatch to requested definition
try
{
// Read definition from factory, but we can create it here.
ComponentDefinition definition = DefinitionsUtil.getDefinition( name, request, getServlet().getServletContext() );
System.out.println("get Definition " + definition );
DefinitionsUtil.setActionDefinition( request, definition);
}
catch( FactoryNotFoundException ex )
{
printError( response, "Error - DefinitionDispatcherAction : Can't get definition factory.");
return (mapping.findForward("error"));
}
catch( NoSuchDefinitionException ex )
{
printError( response, "Error - DefinitionDispatcherAction : Can't get definition '" + name +"'.");
return (mapping.findForward("error"));
}
catch( DefinitionsFactoryException ex )
{
printError( response, "Error - DefinitionDispatcherAction : General Factory error '" + ex.getMessage() +"'.");
return (mapping.findForward("error"));
}
catch( Exception ex )
{
printError( response, "Error - DefinitionDispatcherAction : General error '" + ex.getMessage() +"'.");
return (mapping.findForward("error"));
}
return (mapping.findForward("success"));
}
protected void printError( HttpServletResponse response, String msg )
throws IOException
{
response.setContentType("text/plain");
PrintWriter writer = response.getWriter();
writer.println(msg);
writer.flush();
writer.close();
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/actions/NoOpAction.java
Index: NoOpAction.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/actions/NoOpAction.java,v 1.1 2002/06/25 03:15:42 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:42 $
*
* ====================================================================
*
* 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.actions;
import java.io.IOException;
import java.util.Locale;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;
/**
* Implementation of <strong>Action</strong> that populates an instance of
* <code>SubscriptionForm</code> from the currently specified subscription.
*
* @author Cedric Dumoulin
* @version $Revision: 1.1 $ $Date: 2002/06/25 03:15:42 $
*/
public final class NoOpAction extends Action {
// --------------------------------------------------------- Public Methods
/**
* No op action.
* Action doing nothing.
*
* @param servlet The ActionServlet making this request
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// No op action.
return (mapping.findForward("success"));
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/actions/ReloadDefinitionsAction.java
Index: ReloadDefinitionsAction.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/actions/ReloadDefinitionsAction.java,v 1.1 2002/06/25 03:15:42 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:42 $
*
* ====================================================================
*
* 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.actions;
import org.apache.struts.tiles.DefinitionsUtil;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.definition.ReloadableDefinitionsFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* <p>A standard <strong>Action</strong> that calls the
* <code>reload()</code> method of our controller servlet to
* reload its configuration information from the configuration
* files (which have presumably been updated) dynamically.</p>
*
* @author Craig R. McClanahan
* @author Cedric Dumoulin
* @version $Revision: 1.1 $ $Date: 2002/06/25 03:15:42 $
*/
public final class ReloadDefinitionsAction extends Action {
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain");
PrintWriter writer = response.getWriter();
try {
ServletContext context = getServlet().getServletContext();
ReloadableDefinitionsFactory factory = (ReloadableDefinitionsFactory)DefinitionsUtil.getDefinitionsFactory(context );
factory.reload(context);
writer.println("OK");
} catch (ClassCastException e) {
writer.println("FAIL - " + e.toString());
getServlet().log("ReloadAction", e);
} catch (DefinitionsFactoryException e) {
writer.println("FAIL - " + e.toString());
getServlet().log("ReloadAction", e);
}
writer.flush();
writer.close();
return (null);
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/actions/TilesAction.java
Index: TilesAction.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/actions/TilesAction.java,v 1.1 2002/06/25 03:15:42 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:42 $
*
* ====================================================================
*
* 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.actions;
import org.apache.struts.tiles.ComponentContext;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
/**
* Base class for Tiles Actions.
* This class as the same role as Struts Action. It provides a method perform(...)
* called when action is invoked. The difference is that the perform() method takes
* an additional parameter : tile context.
* This class extends Struts Action. Subclasses should implements new perform() method
* instead of Struts perform() method.
* @version $Revision: 1.1 $ $Date: 2002/06/25 03:15:42 $
*/
public abstract class TilesAction extends Action
{
/**
* Original Struts Action's method.
* Retrieve current Tile context, and call Tile's perform method.
*
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// Try to retrieve tile context
ComponentContext context = ComponentContext.getContext( request );
if( context == null )
{
throw new ServletException( "Can't find Tile context for '" + this.getClass().getName()
+ "'. TilesAction subclasses must be called from a Tile" );
}
return perform( context, mapping, form, request, response );
}
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
* This method should be implemented by subclasses.
*
* @param context The current Tile context, containing Tile attributes
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
abstract public ActionForward perform( ComponentContext context,
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException;
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/actions/ViewDefinitionsAction.java
Index: ViewDefinitionsAction.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/actions/ViewDefinitionsAction.java,v 1.1 2002/06/25 03:15:42 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:42 $
*
* ====================================================================
*
* 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.actions;
import org.apache.struts.tiles.DefinitionsUtil;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.ComponentDefinitionsFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* <p>A standard <strong>Action</strong> that calls the
* <code>reload()</code> method of our controller servlet to
* reload its configuration information from the configuration
* files (which have presumably been updated) dynamically.</p>
*
* @author Craig R. McClanahan
* @author Cedric Dumoulin
* @version $Revision: 1.1 $ $Date: 2002/06/25 03:15:42 $
*/
public final class ViewDefinitionsAction extends Action {
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain");
PrintWriter writer = response.getWriter();
try {
ServletContext context = getServlet().getServletContext();
ComponentDefinitionsFactory factory = DefinitionsUtil.getDefinitionsFactory(context );
writer.println( factory );
} catch (Exception e) {
writer.println("FAIL - " + e.toString());
getServlet().log("ReloadAction", e);
}
writer.flush();
writer.close();
return (null);
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/beans/MenuItem.java
Index: MenuItem.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/beans/MenuItem.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.beans;
import java.io.Serializable;
/**
* A bean MenuItem interface.
*
*/
public interface MenuItem extends Serializable
{
/**
* Set value property
*/
public void setValue(String value);
/**
* Get value property
*/
public String getValue();
/**
* Set link property
*/
public void setLink(String link);
/**
* Get link property
*/
public String getLink();
/**
* Set icon property
*/
public void setIcon(String link);
/**
* Get icon property
*/
public String getIcon();
/**
* Set tooltip property
*/
public void setTooltip(String link);
/**
* Get tooltip property
*/
public String getTooltip();
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/beans/SimpleMenuItem.java
Index: SimpleMenuItem.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/beans/SimpleMenuItem.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.beans;
import java.io.Serializable;
/**
* A bean MenuItem implementation.
* Used to read menu item in definitions.
*/
public class SimpleMenuItem implements MenuItem, Serializable
{
private String value;
private String link;
private String icon;
private String tooltip;
/**
* Constructor.
*/
public SimpleMenuItem()
{
}
/**
* Set value property
*/
public void setValue(String value)
{
this.value = value;
}
/**
* Get value property
*/
public String getValue()
{
return value;
}
/**
* Set link property
*/
public void setLink(String link)
{
this.link = link;
}
/**
* Get link property
*/
public String getLink()
{
return link;
}
/**
* Get icon property
*/
public void setIcon(String icon)
{
this.icon = icon;
}
/**
* Get icon property
*/
public String getIcon()
{
return icon;
}
/**
* Get tooltip property
*/
public void setTooltip(String tooltip)
{
this.tooltip = tooltip;
}
/**
* Get tooltip property
*/
public String getTooltip()
{
return tooltip;
}
/**
* toString method
*/
public String toString()
{
StringBuffer buff = new StringBuffer();
buff.append( "SimpleMenuItem[");
if( getValue() != null )
buff.append( "value=" ).append(getValue()).append( ", " );
if( getLink() != null )
buff.append( "link=" ).append(getLink()).append( ", " );
if( getTooltip() != null )
buff.append( "tooltip=" ).append(getTooltip()).append( ", " );
if( getIcon() != null )
buff.append( "icon=" ).append(getIcon()).append( ", " );
buff.append( "]");
return buff.toString();
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/definition/ReloadableDefinitionsFactory.java
Index: ReloadableDefinitionsFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/definition/ReloadableDefinitionsFactory.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.definition;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletRequest;
import java.util.Map;
import java.util.HashMap;
import java.util.Enumeration;
import org.apache.struts.tiles.*;
import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
/**
* A reloadable factory.
* This factory is the main entrance to any factory implementation. It takes in
* charge real implementation instance, and allows reload of it by creating a new
* instance.
*
*/
public class ReloadableDefinitionsFactory implements ComponentDefinitionsFactory
{
/** The real factory instance */
protected ComponentDefinitionsFactory factory;
/** Initialization parameters */
protected Map properties;
/** Name of init property carrying factory class name */
public static final String DEFINITIONS_FACTORY_CLASSNAME = "definitions-factory-class";
/**
* Constructor.
* Create a factory according to servlet settings.
* @throws DefinitionsFactoryException If factory creation fail.
*/
public ReloadableDefinitionsFactory( ServletContext servletContext, ServletConfig servletConfig )
throws DefinitionsFactoryException
{
properties = new ServletPropertiesMap( servletConfig );
factory = createFactory( servletContext, properties);
}
/**
* Constructor.
* Create a factory according to servlet settings.
* @throws DefinitionsFactoryException If factory creation fail.
*/
public ReloadableDefinitionsFactory( ServletContext servletContext, Map properties )
throws DefinitionsFactoryException
{
this.properties = properties;
factory = createFactory( servletContext, properties);
}
/**
* Create Definition factory from provided classname.
* If a factory class name is provided, a factory of this class is created. Otherwise,
* default factory is created.
* Factory must have a constructor taking ServletContext and Map as parameter.
* @param classname Class name of the factory to create.
* @param servletContext Servlet Context passed to newly created factory.
* @param properties Map of name/property passed to newly created factory.
* @return newly created factory.
* @throws DefinitionsFactoryException If an error occur while initializing factory
*/
public ComponentDefinitionsFactory createFactoryFromClassname(ServletContext servletContext, Map properties, String classname)
throws DefinitionsFactoryException
{
if( classname == null )
return createFactory( servletContext, properties );
// Try to create from classname
try
{
Class factoryClass = Class.forName(classname);
ComponentDefinitionsFactory factory = (ComponentDefinitionsFactory)factoryClass.newInstance();
factory.initFactory( servletContext, properties);
return factory;
}
catch( ClassCastException ex )
{ // Bad classname
throw new DefinitionsFactoryException( "Error - createDefinitionsFactory : Factory class '"
+ classname +" must implements 'ComponentDefinitionsFactory'.", ex );
}
catch( ClassNotFoundException ex )
{ // Bad classname
throw new DefinitionsFactoryException( "Error - createDefinitionsFactory : Bad class name '"
+ classname +"'.", ex );
}
catch( InstantiationException ex )
{ // Bad constructor or error
throw new DefinitionsFactoryException( ex );
}
catch( IllegalAccessException ex )
{ //
throw new DefinitionsFactoryException( ex );
}
}
/**
* Create default Definition factory.
* Factory must have a constructor taking ServletContext and Map as parameter.
* In this implementation, default factory is of class I18nFactorySet
* @param servletContext Servlet Context passed to newly created factory.
* @param properties Map of name/property passed to newly created factory.
* @return newly created factory.
* @throws DefinitionsFactoryException If an error occur while initializing factory
*/
public ComponentDefinitionsFactory createDefaultFactory(ServletContext servletContext, Map properties)
throws DefinitionsFactoryException
{
ComponentDefinitionsFactory factory = new I18nFactorySet(servletContext, properties); ;
return factory;
}
/**
* Create Definition factory.
* Convenience method. ServletConfig is wrapped into a Map allowing retrieval
* of init parameters. Factory classname is also retrieved, as well as debug level.
* Finally, approriate createDefinitionsFactory() is called.
* @param servletContext Servlet Context passed to newly created factory.
* @param servletConfig Servlet config containing parameters to be passed to newly created factory.
*/
public ComponentDefinitionsFactory createFactory(ServletContext servletContext, Map properties)
throws DefinitionsFactoryException
{
String classname = (String)properties.get(DEFINITIONS_FACTORY_CLASSNAME);
if(classname!=null)
return createFactoryFromClassname( servletContext, properties, classname);
return new I18nFactorySet( servletContext, properties );
}
/**
* Get a definition by its name.
* Call appropriate method on underlying factory instance.
* Throw appropriate exception if definition or definition factory is not found.
* @param name Name of requested definition.
* @param request Current servelet request
* @param servletContext current servlet context
* @throws FactoryNotFoundException Can't find definition factory.
* @throws DefinitionsFactoryException General error in factory while getting definition.
* @throws NoSuchDefinitionException No definition found for specified name
*/
public ComponentDefinition getDefinition(String definitionName, ServletRequest request, ServletContext servletContext)
throws FactoryNotFoundException, DefinitionsFactoryException
{
return factory.getDefinition(definitionName, (HttpServletRequest)request, servletContext);
}
/**
* Reload underlying factory.
* Reload is done creating a new factory instance, and replacing old instance
* by the new one.
* @param request Current servelet request
* @param servletContext current servlet context
* @throws DefinitionsFactoryException If factory creation fail.
*/
public void reload(ServletContext servletContext)
throws DefinitionsFactoryException
{
ComponentDefinitionsFactory newInstance = createFactory( servletContext, properties);
factory = newInstance;
}
/**
* Get underlying factory instance.
*/
public ComponentDefinitionsFactory getFactory()
{
return factory;
}
/**
* Init factory.
* This method is required by interface ComponentDefinitionsFactory. It is
* not used in this implementation, as it manage itself underlying creation
* and initialization.
* @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
{ // do nothing
}
/**
* Get this object as a String
*/
public String toString()
{
return factory.toString();
}
/**
* Inner class.
* Wrapper for ServletContext init parameters.
* Object of this class is an hashmap containing parameters and values
* defined in the servlet config file (web.xml).
*/
class ServletPropertiesMap extends HashMap {
/**
* Constructor.
*/
ServletPropertiesMap( ServletConfig config )
{
// This implementation is very simple.
// It is possible to avoid creation of a new structure, but this need
// imply writing all Map interface.
Enumeration enum = config.getInitParameterNames();
while( enum.hasMoreElements() )
{
String key = (String)enum.nextElement();
put( key, config.getInitParameter( key ) );
}
}
} // end inner class
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/DefinitionsFactory.java
Index: DefinitionsFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/DefinitionsFactory.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.ComponentDefinitionsFactory;
import org.apache.struts.tiles.ComponentDefinition;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.FactoryNotFoundException;
import org.apache.struts.tiles.NoSuchDefinitionException;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.Serializable;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
/**
* A factory for definitions.
* This factory allows to retrieve definitions by their keys.
*/
public class DefinitionsFactory implements Serializable
{
/** */
protected Map definitions;
/**
* Get a definition by its name.
* @throws DefinitionsFactoryException An error occur while getting
* definition.
* @throws NoSuchDefinitionException No definition found for specified name
* Implementation can throw more accurate exception as a subclass of this
* exception
*/
public ComponentDefinition getDefinition(String name, ServletRequest request, ServletContext servletContext)
throws NoSuchDefinitionException, DefinitionsFactoryException
{
return (ComponentDefinition)definitions.get(name);
}
/**
* Put definition in set
* @param definition
*/
public void putDefinition(ComponentDefinition definition)
{
definitions.put( definition.getName(), definition );
}
/**
* Constructor.
* Create a factory initialized with definitions from XmlDefinitionsSet.
* @param xmlDefinitions resolved definition from XmlDefinitionSet.
* @throws DefinitionsFactoryException If an error occurs while creating a definition.
* @throws NoSuchDefinitionException If an error occurs while resolving inheritance
*/
public DefinitionsFactory(XmlDefinitionsSet xmlDefinitions)
throws NoSuchDefinitionException
{
definitions = new HashMap();
// First, resolve inheritance
xmlDefinitions.resolveInheritances();
// Walk thru xml set and copy each definitions.
Iterator i = xmlDefinitions.getDefinitions().values().iterator();
while( i.hasNext() )
{
XmlDefinition xmlDefinition = (XmlDefinition)i.next();
putDefinition( new ComponentDefinition( xmlDefinition) );
} // end loop
}
/**
*
*/
public String toString()
{
return definitions.toString();
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/FactorySet.java
Index: FactorySet.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/FactorySet.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.ComponentDefinitionsFactory;
import org.apache.struts.tiles.ComponentDefinition;
import org.apache.struts.tiles.NoSuchDefinitionException;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.FactoryNotFoundException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletContext;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
/**
* Component Definitions factory.
* This factory contains several factories identified by a key. The
* getDefinition() method first look for the factory key, retrieve or create this
* factory and then call getDefinition() on the found factory.
*/
public abstract class FactorySet implements ComponentDefinitionsFactory
{
/** Loaded factories */
protected Map factories=null;
/**
* Extract key that will be used to get the sub factory.
* @param name Name of requested definition
* @param request Current servlet request.
* @param servletContext Current servlet context
*/
abstract protected Object getDefinitionsFactoryKey(String name, ServletRequest request, ServletContext servletContext);
/**
* Get default factory.
* @return Default factory
*/
abstract protected DefinitionsFactory getDefaultFactory();
/**
* Get a factory by its key.
* If key is null, return defaultFactory.
* Search in loaded factory. If not found, call create factory, and store return value in
* loaded factories.
* @param key Key of requested definition
* @param request Current servlet request.
* @param servletContext Current servlet context
* @throws DefinitionsFactoryException If an error occur while creating factory.
*/
protected DefinitionsFactory getFactory(Object key, ServletRequest request, ServletContext servletContext)
throws DefinitionsFactoryException
{
if(key == null )
return getDefaultFactory();
Object factory = factories.get( key );
if( factory == null )
{
// synchronize creation to avoid double creation by separate threads.
// Also, check if factory hasn't been created while waiting for synchronized
// section.
synchronized(factories)
{
factory = factories.get( key );
if( factory == null )
{
factory = createFactory( key, request, servletContext);
factories.put( key, factory );
} // end if
} // end synchronized
} // end if
return (DefinitionsFactory)factory;
}
/**
* Get a definition by its name.
* @throws DefinitionsFactoryException An error occur while getting
* definition.
* @param name Name of requested definition
* @param request Current servlet request.
* @param servletContext Current servlet context
* @throws NoSuchDefinitionException No definition found for specified name
* @throws DefinitionsFactoryException General exception
*/
public ComponentDefinition getDefinition(String name, ServletRequest request, ServletContext servletContext)
throws NoSuchDefinitionException, DefinitionsFactoryException
{
if( factories == null )
throw new FactoryNotFoundException( "No definitions factory defined" );
Object key = getDefinitionsFactoryKey( name, request, servletContext);
DefinitionsFactory factory = getFactory( key, request, servletContext);
return factory.getDefinition( name, request, servletContext );
}
/**
* Create a factory for specified key.
* This method is called by getFactory() when the requested factory doesn't already exist.
* Must return a factory, even a default one.
* Real implementation need to provide this method.
* @param key Key of requested definition
* @param request Current servlet request.
* @param servletContext Current servlet context
* @throws DefinitionsFactoryException If an error occur while creating factory.
*/
abstract protected DefinitionsFactory createFactory(Object key, ServletRequest request, ServletContext servletContext)
throws DefinitionsFactoryException;
/**
* Init factory set.
* @param servletContext Current servlet context
* @param properties properties used to initialized factory set;
*/
abstract public void initFactory(ServletContext servletContext, Map properties)
throws DefinitionsFactoryException;
/**
*
*/
public FactorySet()
{
factories = new HashMap();
}
/**
*
*/
public String toString()
{
Iterator i = factories.values().iterator();
StringBuffer buff = new StringBuffer( "all FactorySet's factory : \n" );
while( i.hasNext() )
{
buff.append( i.next().toString() ).append("\n");
}
return buff.toString();
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java
Index: I18nFactorySet.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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 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
* Locale found in jsp 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 : templateDefinitions_fr.xml). If no file is found under this name, use default file.
*/
public class I18nFactorySet extends FactorySet
{
/** Debug flag */
static public final boolean debug = false;
/** 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;
/** Does we want validating parser. Default is false.
* 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 initFactory must be called prior to any use of created factory.
*/
public I18nFactorySet()
{
}
/**
* Constructor.
* Init the factory by reading appropriate configuration file.
* @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 )
{
System.out.println( "Bad format for parameter '"
+ PARSER_DETAILS_PARAMETER_NAME
+ "'. Integer expected.");
servletContext.log( "Tiles factory init : 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( DefinitionsUtil.userDebugLevel > 0)
System.out.println( "Factory initialized from file '" + filename + "'." );
}
catch( FileNotFoundException ex )
{ // A filename is specified, throw appropriate error.
System.out.println( 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( DefinitionsUtil.userDebugLevel > 0)
{
System.out.println( "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(debug)
System.out.println( "default factory:" + defaultFactory );
}
/**
* Get default factory.
* @return Default factory
*/
protected DefinitionsFactory getDefaultFactory()
{
return defaultFactory;
}
/**
* Create default factory .
* Create InstancesMapper for specified Locale.
* If creation failed, use default mapper, and output an error message in
* console.
* @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(debug)
System.out.println( rootXmlConfig );
DefinitionsFactory factory = new DefinitionsFactory( rootXmlConfig );
if( DefinitionsUtil.userDebugLevel > DefinitionsUtil.NO_DEBUG )
System.out.println( "factory loaded : " + factory );
return factory;
}
/**
* Extract key that will be used to get the sub factory.
* @param name Name of requested definition
* @param request Current servlet request.
* @param servletContext Current servlet context
* @return the key or null if not found.
*/
protected Object getDefinitionsFactoryKey(String name, ServletRequest request, ServletContext servletContext)
{
Locale locale = null;
try
{
HttpSession session = ((HttpServletRequest)request).getSession(false);
if (session != null)
locale = (Locale)session.getAttribute(DefinitionsUtil.LOCALE_KEY);
}
catch( ClassCastException ex )
{ //
System.out.println( "Error - I18nFactorySet.getDefinitionsFactoryKey" );
ex.printStackTrace();
}
return locale;
}
/**
* Create a factory for specified key.
* If creation failed, return default factory, and output an error message in
* console.
* @param key
* @return Definition factory for specified key.
* @throws DefinitionsFactoryException If an error occur while creating factory.
*/
protected DefinitionsFactory createFactory(Object key, ServletRequest request, ServletContext servletContext)
throws DefinitionsFactoryException
{
if( key == null )
return getDefaultFactory();
// Build possible postfixes
List possiblePostfixes = calculatePostixes( "", (Locale)key );
// Search last postix corresponding to a config file to load.
// First check if something is loaded for this postfix.
// If not, try to load its config.
XmlDefinitionsSet lastXmlFile = null;;
DefinitionsFactory factory = null;
String curPostfix = null;
int i;
for(i=possiblePostfixes.size()-1; i>=0; i-- )
{
curPostfix = (String)possiblePostfixes.get(i);
// Already loaded ?
factory = (DefinitionsFactory)loaded.get( curPostfix );
if( factory != null )
{ // yes, stop search
return factory;
} // end if
// Try to load it. If success, stop search
lastXmlFile = parseXmlFiles( servletContext, curPostfix, null );
if( lastXmlFile != null )
break;
} // end loop
// Have we found a description file ?
// If no, return default one
if( lastXmlFile == null )
{
return getDefaultFactory();
}
// We found something. Need to load base and intermediate files
String lastPostfix = curPostfix;
XmlDefinitionsSet rootXmlConfig = parseXmlFiles( servletContext, "", null );
for( int j=0; j<i; j++ )
{
curPostfix = (String)possiblePostfixes.get(j);
parseXmlFiles( servletContext, curPostfix, rootXmlConfig);
} // end loop
rootXmlConfig.extend( lastXmlFile );
rootXmlConfig.resolveInheritances();
factory = new DefinitionsFactory(rootXmlConfig);
loaded.put( lastPostfix, factory );
// User help
if( DefinitionsUtil.userDebugLevel > DefinitionsUtil.NO_DEBUG )
System.out.println( "factory loaded : " + factory );
// return last available found !
return factory;
}
/**
* Calculate the postixes along the search path from the base bundle to the
* bundle specified by baseName and locale.
* Method copied from java.util.ResourceBundle
* @param baseName the base bundle name
* @param locale the locale
* @param names the vector used to return the names of the bundles along
* the search path.
*/
private static List calculatePostixes(String baseName, Locale locale) {
final List result = new ArrayList(MAX_BUNDLES_SEARCHED);
final String language = locale.getLanguage();
final int languageLength = language.length();
final String country = locale.getCountry();
final int countryLength = country.length();
if (languageLength + countryLength == 0) {
//The locale is "", "", "".
return result;
}
final StringBuffer temp = new StringBuffer(baseName);
temp.append('_');
temp.append(language);
result.add(temp.toString());
if (countryLength == 0) {
return result;
}
temp.append('_');
temp.append(country);
result.add(temp.toString());
return result;
}
/**
* Parse files associated to postix if they exist.
* For each name in filenames, append postfix before file extension,
* then try to load the corresponding file.
* If file doesn't exist, try next one. Each file description is added to
* the XmlDefinitionsSet description.
* The XmlDefinitionsSet description is created only if there is a definition file.
* Inheritance is not resolved in the returned XmlDefinitionsSet.
* If no description file can be opened, and no definiion set is provided, return null.
* @param postfix Postfix to add to each description file.
* @param xmlDefinitions Definitions set to which definitions will be added. If null, a definitions
* set is created on request.
* @return XmlDefinitionsSet The definitions set created or passed as parameter.
* @throws DefinitionsFactoryException If an error happen during file parsing.
*/
private XmlDefinitionsSet parseXmlFiles( ServletContext servletContext, String postfix, XmlDefinitionsSet xmlDefinitions )
throws DefinitionsFactoryException
{
if( postfix != null && postfix.length() == 0 )
postfix = null;
// Iterate throw each file name in list
Iterator i = filenames.iterator();
while( i.hasNext() )
{
String filename = concatPostfix((String)i.next(), postfix) ;
xmlDefinitions = parseXmlFile( servletContext, filename, xmlDefinitions );
} // end loop
return xmlDefinitions;
}
/**
* Parse specified xml file and add definition to specified definitions set.
* This method is used to load several description files in one instances list.
* If filename exist and definition set is null, create a new set. Otherwise, return
* passed definition set (can be null).
* @param servletContext Current servlet context. Used to open file.
* @param filename Name of file to parse.
* @param xmlDefinitions Definitions set to which definitions will be added. If null, a definitions
* set is created on request.
* @return XmlDefinitionsSet The definitions set created or passed as parameter.
* @throws DefinitionsFactoryException If an error happen during file parsing.
*/
private XmlDefinitionsSet parseXmlFile( ServletContext servletContext, String filename , XmlDefinitionsSet xmlDefinitions)
throws DefinitionsFactoryException
{
try
{
InputStream input = servletContext.getResourceAsStream(filename);
// Try to load using real path.
// This allow to load config file under websphere 3.5.x
// Patch proposed Houston, Stephen (LIT) on 5 Apr 2002
if (null == input)
{
try
{
input = new java.io.FileInputStream(servletContext.getRealPath(filename));
}
catch (Exception e)
{
}
}
// If still nothing found, this mean no config file is associated
if(input == null )
{
//if(debug)
//System.out.println( "Can't open file '" + filename + "'" );
return xmlDefinitions;
}
// Check if parser already exist.
// Doesn't seem to work yet.
//if( xmlParser == null )
if( true )
{ // create it
if(debug)
System.out.println( "Create xmlParser");
xmlParser = new XmlParser();
xmlParser.setValidating(isValidatingParser);
xmlParser.setDetailLevel(parserDetailLevel);
}
// Check if definition set already exist.
if( xmlDefinitions == null )
{ // create it
if(debug)
System.out.println( "Create xmlDefinitions");
xmlDefinitions = new XmlDefinitionsSet();
}
xmlParser.parse( input, xmlDefinitions );
}
catch( SAXException ex )
{
if( debug)
{
System.out.println( "Error while parsing file '" + filename + "'.");
ex.printStackTrace();
}
throw new DefinitionsFactoryException( "Error while parsing file '" + filename + "'. " + ex.getMessage(), ex );
}
catch( IOException ex )
{
throw new DefinitionsFactoryException( "IO Error while parsing file '" + filename + "'. " + ex.getMessage(), ex);
}
return xmlDefinitions;
}
/**
* Concat postfix to the name. Take care of existing filename extension.
* Transform the given name "name.ext" to have "name" + "postfix" + "ext".
* If there is no ext, return "name" + "postfix".
*/
private String concatPostfix( String name, String postfix )
{
if( postfix == null )
return name;
// Search file name extension.
// take care of Unix files starting with .
int dotIndex = name.lastIndexOf( "." );
int lastNameStart = name.lastIndexOf( java.io.File.pathSeparator );
if( dotIndex < 1 || dotIndex < lastNameStart )
return name + postfix;
String ext = name.substring( dotIndex );
name = name.substring( 0, dotIndex);
return name + postfix + ext;
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlAttribute.java
Index: XmlAttribute.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlAttribute.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.AttributeDefinition;
import org.apache.struts.tiles.DirectStringAttribute;
import org.apache.struts.tiles.PathAttribute;
import org.apache.struts.tiles.DefinitionAttribute;
import org.apache.struts.tiles.DefinitionNameAttribute;
import org.apache.struts.tiles.UntyppedAttribute;
/**
* A property key-value pair.
* This class is used to read configuration files.
*/
public class XmlAttribute
{
/**
* Attribute name or key.
*/
private String name;
/**
* Attribute value.
* Value read from description file.
*/
private Object value;
/**
* Attribute value.
*/
private String direct=null;
/**
* Attribute value.
*/
private String valueType=null;
/**
* Attribute value.
*/
private String role=null;
/**
* Real attribute value.
* Real value is the value after processing of valueType.
* I.e. if a type is defined, realValue contains wrapper for this type.
*/
private Object realValue;
/**
* Constructor.
*/
public XmlAttribute()
{
}
/**
* Constructor.
*/
public XmlAttribute( String name, Object value)
{
this.name = name;
this.value = value;
}
/**
* Access method for the name property.
*
* @return the current value of the name property
*/
public String getName()
{
return name;
}
/**
* @return void
* Sets the value of the name property.
*
* @param aName the new value of the name property
*/
public void setRole(String role)
{
this.role = role;
}
/**
* Access method for the name property.
*
* @return the current value of the name property
*/
public String getRole()
{
return role;
}
/**
* @return void
* Sets the value of the name property.
*
* @param aName the new value of the name property
*/
public void setName(String aName)
{
name = aName;
}
/**
* Another access method for the name property.
*
* @return the current value of the name property
*/
public String getAttribute()
{
return name;
}
/**
* Sets the value of the name property.
*
* @param aName the new value of the name property
*/
public void setAttribute(String aName)
{
name = aName;
}
/**
* Access method for the value property.
* Return the value or a QualifiedAttribute containing the value
* if 'direct' is set.
*
* @return the current value of the value property
*/
public Object getValue()
{
// Compatibility with JSP Template
if( realValue ==null )
realValue = computeRealValue();
return realValue;
}
/**
* Sets the value of the value property.
*
* @param aValue the new value of the value property
*/
public void setValue(Object aValue)
{
realValue=null;
value = aValue;
}
/**
* Sets the value of the value property.
*
* @param aValue the new value of the value property
*/
public void setContent(Object aValue)
{
setValue(aValue);
}
/**
* Sets the value of the value property.
*
* @param aValue the new value of the value property
*/
public void setBody(String body)
{
if( body.length() == 0 )
return;
//System.out.println("body set to'" + body + "'");
setValue(body);
}
/**
* Sets the value of the value property.
*
* @param aValue the new value of the value property
*/
public void setDirect(String value)
{
this.direct = value;
}
/**
* Sets the value of the value property.
*
* @param aValue the new value of the value property
*/
public void setType(String value)
{
this.valueType = value;
}
/**
* Compute real value from attributes setting.
*/
protected Object computeRealValue()
{
Object realValue = value;
// Is there a type set ?
// First check direct attribute, and translate it to a valueType.
// Then, evaluate valueType, and create requested typed attribute.
if( direct != null )
{
if( Boolean.valueOf(direct).booleanValue() == true )
valueType = "string";
else
valueType = "path";
} // end if
if( value != null && valueType!=null /* && !(value instanceof AttributeDefinition) */ )
{
String strValue = value.toString();
if( valueType.equalsIgnoreCase( "string" ) )
{
realValue = new DirectStringAttribute( strValue );
}
else if( valueType.equalsIgnoreCase( "page" ) )
{
realValue = new PathAttribute( strValue );
}
else if( valueType.equalsIgnoreCase( "template" ) )
{
realValue = new PathAttribute( strValue );
}
else if( valueType.equalsIgnoreCase( "instance" ) )
{
realValue = new DefinitionNameAttribute( strValue );
} // end if
// Set realValue's role value if needed
if( role !=null )
((UntyppedAttribute)realValue).setRole( role );
} // end if
// Create attribute wrapper to hold role if role is set and no type specified
if( role!=null && value != null && valueType==null )
{
realValue = new UntyppedAttribute( value.toString(), role );
} // end if
return realValue;
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlDefinition.java
Index: XmlDefinition.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlDefinition.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.ComponentDefinition;
import org.apache.struts.tiles.NoSuchDefinitionException;
import java.util.Iterator;
/**
*A definition red from an XML definitions file.
*/
public class XmlDefinition extends ComponentDefinition
{
/**
* Extends attribute value.
*/
private String inherit;
/** Debug flag */
static public final boolean debug = false;
/**
* Use for resolving inheritance.
*/
private boolean isVisited=false;
/**
* Constructor.
*/
public XmlDefinition()
{
super();
//if(debug)
//System.out.println( "create definition" );
}
/**
* add an attribute to this component
*
* @param attribute Attribute to add.
*/
public void addAttribute( XmlAttribute attribute)
{
putAttribute( attribute.getName(), attribute.getValue() );
}
/**
* Sets the value of the extend and path property.
*
* @param aPath the new value of the path property
*/
public void setExtends(String name)
{
inherit = name;
}
/**
* Access method for the path property.
*
* @return the current value of the path property
*/
public String getExtends()
{
return inherit;
}
/**
* Get the value of the extendproperty.
*
*/
public boolean isExtending( )
{
return inherit!=null;
}
/**
* Get the value of the extendproperty.
*
*/
public void setIsVisited( boolean isVisited )
{
this.isVisited = isVisited;
}
/**
* Resolve inheritance.
* First, resolve parent's inheritance, then set path to the parent's path.
* Also copy attributes setted in parent, and not set in child
* If instance doesn't extends something, do nothing.
* @throws NoSuchInstanceException If a inheritance can be solved.
*/
public void resolveInheritance( XmlDefinitionsSet definitionsSet )
throws NoSuchDefinitionException
{
// Already done, or not needed ?
if( isVisited || !isExtending() )
return;
if( debug)
System.out.println( "Resolve definition for child name='"
+ getName() + "' extends='"
+ getExtends() + "'." );
// Set as visited to avoid endless recurisvity.
setIsVisited( true );
// Resolve parent before itself.
XmlDefinition parent = definitionsSet.getDefinition( getExtends() );
if( parent == null )
{ // error
String msg = "Error while resolving definition inheritance: child '"
+ getName() + "' can't find its ancestor '"
+ getExtends() + "'. Please check your description file.";
System.out.println( msg );
// to do : find better exception
throw new NoSuchDefinitionException( msg );
}
parent.resolveInheritance( definitionsSet );
// Iterate on each parent's attribute, and add it if not defined in child.
Iterator parentAttributes = parent.getAttributes().keySet().iterator();
while( parentAttributes.hasNext() )
{
String name = (String)parentAttributes.next();
if( !getAttributes().containsKey(name) )
putAttribute( name, parent.getAttribute(name) );
}
// Set path and role if not setted
if( path == null )
setPath( parent.getPath() );
if( role == null )
setRole( parent.getRole() );
if( controller==null )
{
setController( parent.getController());
setControllerType( parent.getControllerType());
}
}
/**
* Overload this definition with passed child.
* All attributes from child are copied to this definition. Previous attribute with
* same name are disguarded.
* Special attribute 'path','role' and 'extends' are overloaded if defined in child.
* @param child Child used to overload this definition.
*/
public void overload( XmlDefinition child )
{
if( child.getPath() != null )
{
path = child.getPath();
}
if( child.getExtends() != null )
{
inherit = child.getExtends();
}
if( child.getRole() != null )
{
role = child.getRole();
}
if( child.getController()!=null )
{
controller = child.getController();
controllerType = child.getControllerType();
}
// put all child attributes in parent.
attributes.putAll( child.getAttributes());
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlDefinitionsSet.java
Index: XmlDefinitionsSet.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlDefinitionsSet.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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.NoSuchDefinitionException;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
/**
* A set of definitions red from XML definitions file
*/
public class XmlDefinitionsSet
{
/** defined definitions */
protected Map definitions;
/**
* Constructor.
*/
public XmlDefinitionsSet()
{
definitions = new HashMap();
}
/**
* Put definition in set
* @param definition
*/
public void putDefinition(XmlDefinition definition)
{
definitions.put( definition.getName(), definition );
}
/**
* Get requested definition
* @param name definitio name
*/
public XmlDefinition getDefinition(String name)
{
return (XmlDefinition)definitions.get( name );
}
/**
* Get definitions map
* @param name definitio name
*/
public Map getDefinitions()
{
return definitions;
}
/**
* Resolve extended instances.
*/
public void resolveInheritances() throws NoSuchDefinitionException
{
// Walks throw all definitions and resolve individual inheritance
Iterator i = definitions.values().iterator();
while( i.hasNext() )
{
XmlDefinition definition = (XmlDefinition)i.next();
definition.resolveInheritance( this );
} // end loop
}
/**
* add definitions from specified child definitions set.
* For each definition in child, look if it exist in this set.
* If not, add it, if yes, overload parent's definition with child definition.
* @param child definition used to overload this object.
* @return void
*/
public void extend( XmlDefinitionsSet child )
{
if(child==null)
return;
Iterator i = child.getDefinitions().values().iterator();
while( i.hasNext() )
{
XmlDefinition childInstance = (XmlDefinition)i.next();
XmlDefinition parentInstance = getDefinition(childInstance.getName() );
if( parentInstance != null )
{
parentInstance.overload( childInstance );
}
else
putDefinition( childInstance );
} // end loop
}
/**
*
*/
public String toString()
{
return "definitions=" + definitions.toString() ;
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlListAttribute.java
Index: XmlListAttribute.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlListAttribute.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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 java.util.List;
import java.util.ArrayList;
/**
* An attribute as a List.
* This attribute associates a name with a list. The list can be found by the
* property name.
* Element in list are retrieved using List methods.
* This class is used to read configuration files.
*/
public class XmlListAttribute extends XmlAttribute
{
/** List.
* We declare a list, to avoid cast.
* Parent "value" property point to the same list.
*/
private List list;
/**
* Constructor.
*/
public XmlListAttribute()
{
list = new ArrayList();
setValue(list);
}
/**
* Constructor.
*/
public XmlListAttribute( String name, List value)
{
super( name, value );
list = value;
}
/**
* Add an element in list.
* We use a Property, to avoid rewriting a new class.
*/
public void add( XmlAttribute element )
{
list.add( element.getValue() );
}
/**
* Add an element in list.
*/
public void add( Object value )
{
//list.add( value );
// To correct a bug in digester, we need to check the object type
// Digester doesn't call correct method according to object type ;-(
if(value instanceof XmlAttribute)
{
add((XmlAttribute)value);
return;
}
else
list.add( value );
}
/**
* Add an element in list.
*/
public void addObject( Object value )
{
list.add( value );
}
}
1.1 jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlParser.java
Index: XmlParser.java
===================================================================
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/xmlDefinition/XmlParser.java,v 1.1 2002/06/25 03:15:43 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:15:43 $
*
* ====================================================================
*
* 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 java.util.Map;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import java.util.HashMap;
import java.io.InputStream;
import java.io.IOException;
import java.io.Serializable;
import java.io.*;
import java.net.URL;
import org.apache.commons.digester.Digester;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import org.xml.sax.SAXException;
import org.apache.struts.tiles.NoSuchDefinitionException;
import java.io.Reader;
/**
*Parse an XML definitions file.
*/
public class XmlParser
{
/** Associated digester */
protected Digester digester;
/**
* Should we use a validating XML parser to read the configuration file.
* Default is false.
*/
protected boolean validating = false;
/**
* Digester debug level. DEfault = 0.
*/
protected int digesterDebugLevel = 0;
/**
* The set of public identifiers, and corresponding resource names, for
* the versions of the configuration file DTDs that we know about. There
* <strong>MUST</strong> be an even number of Strings in this list!
*/
protected String registrations[] = {
"-//Apache Software Foundation//DTD Tiles Configuration//EN",
"/org/apache/struts/tiles/resources/tiles-config.dtd",
"-//Apache Software Foundation//DTD Components Configuration//EN",
"/org/apache/struts/tiles/resources/tiles-config.dtd",
};
/**
* Constructor.
* Create a digester parser, and initialize syntax rules.
*/
public XmlParser()
{
digester = new Digester();
digester.setDebug(digesterDebugLevel);
digester.setValidating(validating);
digester.setNamespaceAware(true);
//digester.setUseContextClassLoader(true);
// Register our local copy of the DTDs that we can find
for (int i = 0; i < registrations.length; i += 2) {
URL url = this.getClass().getResource(registrations[i+1]);
if (url != null)
{
digester.register(registrations[i], url.toString());
}
}
// Init syntax rules
initDigester( digester );
}
/**
* Set digester validating flag.
*/
public void setValidating( boolean validating )
{
digester.setValidating( validating);
}
/**
* Set digester detail level.
*/
public void setDetailLevel( int detailLevel )
{
digester.setDebug( detailLevel);
}
/**
* Init digester for components syntax.
* This is an old set of rules, left for backward compatibilities.
*
*/
private void initDigesterForComponentsDefinitionsSyntax( Digester digester )
{
// Common constants
String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition";
String DEFINITION_TAG = "component-definitions/definition";
String definitionHandlerClass = PACKAGE_NAME + ".XmlDefinition";
String PUT_TAG = DEFINITION_TAG + "/put";
String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute";
String LIST_TAG = DEFINITION_TAG + "/putList";
String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute";
String ADD_LIST_ELE_TAG = LIST_TAG + "/add";
// syntax rules
digester.addObjectCreate( DEFINITION_TAG, definitionHandlerClass );
digester.addSetProperties( DEFINITION_TAG);
digester.addSetNext( DEFINITION_TAG, "putDefinition", definitionHandlerClass);
// put / putAttribute rules
digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass);
digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass);
digester.addSetProperties( PUT_TAG);
digester.addCallMethod( PUT_TAG, "setBody", 0);
// list rules
digester.addObjectCreate( LIST_TAG, listHandlerClass);
digester.addSetProperties( LIST_TAG);
digester.addSetNext( LIST_TAG, "addAttribute", putAttributeHandlerClass);
// list elements rules
// We use Attribute class to avoid rewriting a new class.
// Name part can't be used in listElement attribute.
digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass);
digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass);
digester.addSetProperties( ADD_LIST_ELE_TAG);
digester.addCallMethod( ADD_LIST_ELE_TAG, "setBody", 0);
}
/**
* Init digester for tiles syntax.
* Same as components, but with first element = tiles-definitions
*
*/
private void initDigesterForTilesDefinitionsSyntax( Digester digester )
{
// Common constants
String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition";
String DEFINITION_TAG = "tiles-definitions/definition";
String definitionHandlerClass = PACKAGE_NAME + ".XmlDefinition";
String PUT_TAG = DEFINITION_TAG + "/put";
String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute";
//String LIST_TAG = DEFINITION_TAG + "/putList";
// List tag value
String LIST_TAG = "putList";
String DEF_LIST_TAG = DEFINITION_TAG + "/" + LIST_TAG;
String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute";
// Tag value for adding an element in a list
String ADD_LIST_ELE_TAG = "*/" + LIST_TAG + "/add";
// syntax rules
digester.addObjectCreate( DEFINITION_TAG, definitionHandlerClass );
digester.addSetProperties( DEFINITION_TAG);
digester.addSetNext( DEFINITION_TAG, "putDefinition", definitionHandlerClass);
// put / putAttribute rules
// Rules for a same pattern are called in order, but rule.end() are called
// in reverse order.
// SetNext and CallMethod use rule.end() method. So, placing SetNext in
// first position ensure it will be called last (sic).
digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass);
digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass);
digester.addSetProperties( PUT_TAG);
digester.addCallMethod( PUT_TAG, "setBody", 0);
// Definition level list rules
// This is rules for lists nested in a definition
digester.addObjectCreate( DEF_LIST_TAG, listHandlerClass);
digester.addSetProperties( DEF_LIST_TAG);
digester.addSetNext( DEF_LIST_TAG, "addAttribute", putAttributeHandlerClass);
// list elements rules
// We use Attribute class to avoid rewriting a new class.
// Name part can't be used in listElement attribute.
digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass);
digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass);
digester.addSetProperties( ADD_LIST_ELE_TAG);
digester.addCallMethod( ADD_LIST_ELE_TAG, "setBody", 0);
// nested list elements rules
// Create a list handler, and add it to parent list
String NESTED_LIST = "*/" + LIST_TAG + "/" + LIST_TAG;
digester.addObjectCreate( NESTED_LIST, listHandlerClass);
digester.addSetProperties( NESTED_LIST);
digester.addSetNext( NESTED_LIST, "add", putAttributeHandlerClass);
// bean elements rules
// We use Attribute class to avoid rewriting a new class.
// Name part can't be used in listElement attribute.
//String ADD_WILDCARD = LIST_TAG + "/addItem";
// non String ADD_WILDCARD = LIST_TAG + "/addx*";
String ADD_WILDCARD = "*/item";
String menuItemDefaultClass = "org.apache.struts.tiles.beans.SimpleMenuItem";
digester.addObjectCreate( ADD_WILDCARD, menuItemDefaultClass, "classtype");
digester.addSetNext( ADD_WILDCARD, "add", "java.lang.Object");
digester.addSetProperties( ADD_WILDCARD);
}
/**
* Init digester in order to parse instances definition file syntax.
* Instances is an old name for "definition". This method is left for
* backward compatibility.
*
*/
private void initDigesterForInstancesSyntax( Digester digester )
{
// Build a digester to process our configuration resource
String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition";
String INSTANCE_TAG = "component-instances/instance";
String instanceHandlerClass = PACKAGE_NAME + ".XmlDefinition";
String PUT_TAG = INSTANCE_TAG + "/put";
String PUTATTRIBUTE_TAG = INSTANCE_TAG + "/putAttribute";
String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute";
String LIST_TAG = INSTANCE_TAG + "/putList";
String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute";
String ADD_LIST_ELE_TAG = LIST_TAG + "/add";
// component instance rules
digester.addObjectCreate( INSTANCE_TAG, instanceHandlerClass );
digester.addSetProperties( INSTANCE_TAG);
digester.addSetNext( INSTANCE_TAG, "putDefinition", instanceHandlerClass);
// put / putAttribute rules
digester.addObjectCreate( PUTATTRIBUTE_TAG, putAttributeHandlerClass);
digester.addSetProperties( PUTATTRIBUTE_TAG);
digester.addSetNext( PUTATTRIBUTE_TAG, "addAttribute", putAttributeHandlerClass);
// put / putAttribute rules
digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass);
digester.addSetProperties( PUT_TAG);
digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass);
// list rules
digester.addObjectCreate( LIST_TAG, listHandlerClass);
digester.addSetProperties( LIST_TAG);
digester.addSetNext( LIST_TAG, "addAttribute", putAttributeHandlerClass);
// list elements rules
// We use Attribute class to avoid rewriting a new class.
// Name part can't be used in listElement attribute.
digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass);
digester.addSetProperties( ADD_LIST_ELE_TAG);
digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass);
}
/**
* Init digester.
*
*/
protected void initDigester( Digester digester )
{
initDigesterForTilesDefinitionsSyntax( digester );
initDigesterForComponentsDefinitionsSyntax( digester );
initDigesterForInstancesSyntax( digester );
}
/**
* Parse input reader and add encounter definitions to definitions set.
* @param in Input stream
* @param definitions Xml Definitions set to which encountered definition are added.
* @throws IOException If an error occur during file parsing.
* @throws SAXException
*/
public void parse( InputStream in, XmlDefinitionsSet definitions ) throws IOException, SAXException
{
try
{
// set first object in stack
//digester.clear();
digester.push(definitions);
// parse
digester.parse(in);
in.close();
}
catch (SAXException e)
{
//throw new ServletException( "Error while parsing " + mappingConfig, e);
throw e;
}
}
/**
* Main method to check file syntax.
*/
public static void main(String[] args)
{
//String filename = "E:/programs/jakarta-tomcat/webapps/wtiles-struts/WEB-INF/tiles-examples-defs.xml";
String filename = "E:/programs/jakarta-tomcat-4.0.3/webapps/wtiles-struts/WEB-INF/tiles-examples-defs.xml";
//String filename = "E:/programs/jakarta-tomcat/webapps/wtiles-struts/WEB-INF/tilesDefinitions.xml";
//String filename = "E:/programs/jakarta-tomcat/webapps/wtiles-channel/WEB-INF/componentDefinitions.xml";
//String filename2 = "E:/programs/jakarta-tomcat/webapps/wtiles-tutorial/WEB-INF/componentDefinitions.xml";
if( args.length > 1 )
{
filename = args[1];
} // end if
System.out.println( "Read file '" + filename +"'" );
InputStream input = null;
InputStream input2 = null;
// Open file
try
{
input = new BufferedInputStream(
new FileInputStream( filename) );
// input2 = new BufferedInputStream(
// new FileInputStream( filename2) );
}
catch( IOException ex )
{
System.out.println( "can't open file '" + filename + "' : " + ex.getMessage() );
}
// Check file syntax
try
{
XmlParser parser = new XmlParser();
parser.setValidating(true);
parser.setDetailLevel(2);
XmlDefinitionsSet definitions = new XmlDefinitionsSet();
System.out.println( " Parse file" );
parser.parse( input, definitions);
// System.out.println( " Check file 2" );
//parser.parse( input2, definitions);
System.out.println( " done." );
System.out.println( " Result : " + definitions.toString() );
}
catch( Exception ex )
{
System.out.println( "Error during parsing '" + filename + "' : " + ex.getMessage() );
ex.printStackTrace();
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>