All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ocpsoft.pretty.faces.config.spi.ParentingPostProcessor Maven / Gradle / Ivy

There is a newer version: 3.3.3
Show newest version
/*
 * Copyright 2010 Lincoln Baxter, III
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ocpsoft.pretty.faces.config.spi;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import com.ocpsoft.pretty.PrettyException;
import com.ocpsoft.pretty.faces.config.PrettyConfig;
import com.ocpsoft.pretty.faces.config.mapping.PathValidator;
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
import com.ocpsoft.pretty.faces.spi.ConfigurationPostProcessor;

/**
 * @author Lincoln Baxter, III
 */
public class ParentingPostProcessor implements ConfigurationPostProcessor
{
   public static final String HIERARCHY_ENABLED_PARAM = "com.ocpsoft.pretty.INHERITABLE_CONFIG";
   private final List seen = new ArrayList();

   public PrettyConfig processConfiguration(ServletContext context, PrettyConfig config)
   {
      String enabled = context.getInitParameter(HIERARCHY_ENABLED_PARAM);
      if ((enabled != null) && "false".equalsIgnoreCase(enabled.trim()))
      {
         return config;
      }

      List mappings = config.getMappings();
      for (UrlMapping m : mappings)
      {
         createAncestry(config, m);
      }
      return config;
   }

   private void createAncestry(PrettyConfig config, UrlMapping m)
   {
      if (m.hasParent() && !seen.contains(m))
      {
         UrlMapping parent = config.getMappingById(m.getParentId());
         if (parent == null)
         {
            throw new PrettyException("Error when building configuration for URL-mapping [" + m.getId() + ":"
                  + m.getPattern() + "] - the requested parentId [" + m.getParentId() + "] does not exist in the configuration.");
         }
         if (parent.hasParent())
         {
            createAncestry(config, parent);
         }
         m.setPattern(parent.getPattern() + m.getPattern());
         mergeValidators(parent, m);
         seen.add(m);
      }
   }

   private void mergeValidators(UrlMapping parent, UrlMapping child)
   {
      List result = new ArrayList();
      List validators = new ArrayList();

      validators.addAll(parent.getPathValidators());
      validators.addAll(child.getPathValidators());

      int i = 0;
      for (PathValidator pv : validators)
      {
         PathValidator temp = copy(pv);
         temp.setIndex(i++);
         result.add(temp);
      }

      child.setPathValidators(result);
   }

   private PathValidator copy(PathValidator pathValidator)
   {
      PathValidator result = new PathValidator();
      result.setIndex(pathValidator.getIndex());
      result.setOnError(pathValidator.getOnError());
      result.setValidatorIds(pathValidator.getValidatorIds());
      result.setValidatorExpression(pathValidator.getValidatorExpression());
      return result;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy