

/*****************************************************************************/
/* Copyright (C) The Apache Software Foundation. All rights reserved.        */
/* _________________________________________________________________________ */
/* This software is published under the terms of the Apache Software License */
/* version 1.1, a copy of which has been included  with this distribution in */
/* the LICENSE file.                                                         */
/*****************************************************************************/
package org.apache.cocoon.www.file_.C_.java.tomcat.webapps.gwvas.WEB_INF.conf;

import java.io.OutputStream;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Stack;
import java.util.StringTokenizer;

import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.parameters.Parameters;

import org.apache.cocoon.Constants;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.ResourceNotFoundException;
import org.apache.cocoon.ConnectionResetException;
import org.apache.cocoon.acting.Action;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.matching.Matcher;
import org.apache.cocoon.matching.PreparableMatcher;
import org.apache.cocoon.selection.Selector;
import org.apache.cocoon.sitemap.AbstractSitemap;
import org.apache.cocoon.components.pipeline.StreamPipeline;
import org.apache.cocoon.components.pipeline.EventPipeline;
import org.apache.cocoon.sitemap.Sitemap;
import org.apache.cocoon.sitemap.ErrorNotifier;
import org.apache.cocoon.sitemap.ContentAggregator;
import org.apache.cocoon.sitemap.Manager;
import org.apache.cocoon.sitemap.SitemapRedirector;
import org.apache.cocoon.components.language.markup.xsp.XSPRequestHelper;
import org.apache.cocoon.components.language.markup.xsp.XSPResponseHelper;

/**
 * This is the automatically generated class from the sitemap definitions
 *
 * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
 * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
 * @version CVS $Id: sitemap.xsl,v 1.47 2001/11/14 22:43:43 giacomo Exp $
 */
public class sitemap_xmap extends AbstractSitemap {
  static final String LOCATION = "org.apache.cocoon.www.file_.C_.java.tomcat.webapps.gwvas.WEB_INF.conf.sitemap_xmap";

  static {
    dateCreated = 1006800329685L;
  }

  /** An empty <code>Parameter</code> used to pass to the sitemap components */
  private Parameters emptyParam = new Parameters();

  /** HashMap relating labels to view names */
  private HashMap view_label_map = new HashMap(1);



  //line numbers not supported with xalan
  // Pattern for "home" (either String or prepared pattern)
  private Object matcher_wildcard_N400047_expr;


  /**
   * Method that handles non-factory selectors.
   */
  private boolean isSelected(String hint, String testValue,
                             List listOfMaps, Parameters params,
                             Map objectModel) throws Exception {
    Selector selector = (Selector) this.selectors.select(hint);
    try {
      return selector.select(substitute(listOfMaps, testValue),
                             objectModel, params);
    }
    finally { this.selectors.release(selector);
            } }

  /**
    * Method that handles non-factory matchers.
    */
  private Map matches(String hint, Object preparedPattern,
                      String pattern, List listOfMaps, Parameters params,
                      Map objectModel) throws Exception {
    Component matcher = (Component) this.matchers.select(hint);
    try {
      if (preparedPattern == null) {
        return ((Matcher) matcher).match(
                 substitute(listOfMaps, pattern), objectModel, params);
      } else {
        return ((PreparableMatcher) matcher).preparedMatch(
                 preparedPattern, objectModel, params);
      }
    }
    finally { this.matchers.release(matcher);
            } }


  /**
    * Pass a <code>Configuration</code> instance to this
    * <code>Configurable</code> class.
    */
  public void configure(Configuration conf) throws ConfigurationException {
    this.sitemapManager = new Manager();
    this.sitemapManager.setLogger(getLogger());
    this.sitemapManager.compose(this.manager);
    this.sitemapManager.configure(conf);



    view_label_map.put("xsltran", "xsltran");


    try {

      load_component (Sitemap.GENERATOR, "!error-notifier!", "org.apache.cocoon.sitemap.ErrorNotifier",
                      new DefaultConfiguration("", LOCATION), null);
      load_component (Sitemap.GENERATOR, "!content-aggregator!", "org.apache.cocoon.sitemap.ContentAggregator",
                      new DefaultConfiguration("", LOCATION), null);
      load_component (Sitemap.TRANSFORMER, "!link-translator!", "org.apache.cocoon.sitemap.LinkTranslator",
                      new DefaultConfiguration("", LOCATION), null);

      Configurer configurer = new Configurer(this, LOCATION);
      configurer.configGenerators();
      configurer.configTransformers();
      configurer.configReaders();
      configurer.configSerializers();
      configurer.configMatchers();
      configurer.configSelectors();
      configurer.configActions();
      configurer = null;

      this.generators.initialize();
      this.transformers.initialize();
      this.serializers.initialize();
      this.readers.initialize();
      this.actions.initialize();
      this.matchers.initialize();
      this.prepareMatchers();
      this.selectors.initialize();

      this.manager.initialize();
      /* catch any exception thrown by a component during configuration */
    } catch (Exception e) {
      getLogger().warn(e.getMessage(), e);
      throw new ConfigurationException (
        "Error in sitemap configuration : " + e.getMessage(), e);
    }
  }


  class Configurer {
    sitemap_xmap sitemap;
    String LOCATION;
    public Configurer (sitemap_xmap sitemap, String location) {
      this.sitemap = sitemap;
      this.LOCATION = location;
    }

    /** Configure generators */
    public void configGenerators() throws Exception {

      //line numbers not supported with xalan
      {
        DefaultConfiguration cconf1 =
          new DefaultConfiguration("file", LOCATION);

        cconf1.addAttribute ("name", "file");

        cconf1.addAttribute ("src", "org.apache.cocoon.generation.FileGenerator");

        sitemap.load_component (Sitemap.GENERATOR, "file", "org.apache.cocoon.generation.FileGenerator",
                                cconf1, null);

      }

    }

    /** Configure transformers */
    public void configTransformers() throws Exception {

      //line numbers not supported with xalan
      {
        DefaultConfiguration cconf1 =
          new DefaultConfiguration("xslt", LOCATION);

        cconf1.addAttribute ("name", "xslt");

        cconf1.addAttribute ("src", "org.apache.cocoon.transformation.TraxTransformer");

        //line numbers not supported with xalan
        {
          DefaultConfiguration cconf2 =
            new DefaultConfiguration("use-store", LOCATION);

          cconf2.appendValueData("true");

          cconf1.addChild(cconf2);


        }

        //line numbers not supported with xalan
        { DefaultConfiguration cconf2 =
            new DefaultConfiguration("use-request-parameters",
                                     LOCATION);

          cconf2.appendValueData("false");

          cconf1.addChild(cconf2);


        }
        //line numbers not supported with xalan
        { DefaultConfiguration cconf2 =
            new DefaultConfiguration("use-browser-capabilities-db",
                                     LOCATION);

          cconf2.appendValueData("false");

          cconf1.addChild(cconf2);


        }
        sitemap.load_component (Sitemap.TRANSFORMER, "xslt", "org.apache.cocoon.transformation.TraxTransformer",
                                cconf1, null);

      }

    }

    /** Configure readers */
    public void configReaders() throws Exception {

    }

    /* Configure serializers */
    public void configSerializers() throws Exception {

      //line numbers not supported with xalan
      {
        DefaultConfiguration cconf1 =
          new DefaultConfiguration("html", LOCATION);

        cconf1.addAttribute ("mime-type", "text/html");

        cconf1.addAttribute ("name", "html");

        cconf1.addAttribute ("src", "org.apache.cocoon.serialization.HTMLSerializer");

        sitemap.load_component (Sitemap.SERIALIZER, "html", "org.apache.cocoon.serialization.HTMLSerializer",
                                cconf1, "text/html");

      }

    }

    /** Configure matchers */
    public void configMatchers() throws Exception {

      //line numbers not supported with xalan
      {
        DefaultConfiguration cconf1 =
          new DefaultConfiguration("wildcard", LOCATION);

        cconf1.addAttribute ("name", "wildcard");

        cconf1.addAttribute ("src", "org.apache.cocoon.matching.WildcardURIMatcherFactory");

        sitemap.load_component (Sitemap.MATCHER, "wildcard", "org.apache.cocoon.matching.WildcardURIMatcherFactory",
                                cconf1, null);

      }

    }

    /** Configure selectors */
    public void configSelectors() throws Exception {

    }

    /** Configure actions */
    public void configActions() throws Exception {

    }
  }

  public Object preparePattern(String type, String pattern) throws Exception {
    Component matcher = this.matchers.select(type);
    try {
      if (matcher instanceof PreparableMatcher) {
        // Prepare pattern ('{' have been unescaped)
        return ((PreparableMatcher) matcher).preparePattern(pattern);
      } else {
        // Return null : regular Matcher will be used.
        return null;
      }
    }
    finally { this.matchers.release(matcher);
            } }
  /** Prepare patterns of PreparableMatchers. */
  public void prepareMatchers() throws Exception {



    // Prepare the pattern for "home"
    this.matcher_wildcard_N400047_expr =
      this.preparePattern("wildcard", "home");

  }



  //line numbers not supported with xalan
  /**
   * This is the method to produce the "xsltran" view of the requested resource
   * @param pipeline A <code>StreamPipeline</code> holding the sitemap component collected so far
   * @param listOfMaps A <code>List</code> of Maps holding replacement values for src attributes
   * @param environment The <code>Environment</code> requesting a resource
   * @return Wether the request has been processed or not
   * @exception Exception If an error occurs during request evaluation and production
   */
  private boolean view_xsltran (StreamPipeline pipeline,
                                EventPipeline eventPipeline, List listOfMaps,
                                Environment environment, boolean internalRequest) throws Exception {
    Map map = null;
    Parameters param = null;
    SitemapRedirector redirector = new SitemapRedirector(environment);


    if (getLogger().isDebugEnabled())
      getLogger().debug("Component transformer:xslt(emptyParam)");


    if (getLogger().isDebugEnabled())
      getLogger().debug("Source=stylesheets/xsl/welcome.xsl");
    eventPipeline.addTransformer ("xslt",
                                  substitute(listOfMaps, "stylesheets/xsl/welcome.xsl"),
                                  emptyParam);


    if (getLogger().isDebugEnabled())
      getLogger().debug("Component serializer:html(emptyParam)");

    pipeline.setSerializer ("html", null, emptyParam);

    if (!internalRequest) {
      return pipeline.process(environment);
    }


    if (true)
      return true;

    return internalRequest;
  }



  private String contains_view(String labels, String cocoon_view) {
    if (getLogger().isDebugEnabled())
      getLogger().debug("contains_view(\"" + labels + "\", \"" +
                        cocoon_view + "\")");
    StringTokenizer st = new StringTokenizer(labels, " ,", false);
    while (st.hasMoreTokens()) {
      String token = st.nextToken();
      String view = (String) view_label_map.get(token);
      if (getLogger().isDebugEnabled())
        getLogger().debug("contains_view: examining token \"" +
                          token + "\" against view \"" + view + "\"");
      if (view != null && view.equals(cocoon_view)) {
        if (getLogger().isDebugEnabled())
          getLogger().debug("contains_view: view \"" + view + "\" selected");
        return view;
      }
    }
    return null;
  }


  private boolean call_view(String view_name, StreamPipeline pipeline,
                            EventPipeline eventPipeline, List listOfMaps,
                            Environment environment, boolean internalRequest) throws Exception {
    if (getLogger().isDebugEnabled())
      getLogger().debug("call_view(\"" + view_name + "\")");

    if ("xsltran".equals(view_name)) {
      return view_xsltran(pipeline, eventPipeline, listOfMaps,
                          environment, internalRequest);
    }

    return internalRequest;
  }




  /**
    * Process to producing the output to the specified <code>OutputStream</code>.
    */
  public boolean process(Environment environment) throws Exception {
    /* the <code>EventPipeline</code> is used to collect the xml producing sitemap
        components and the <code>StreamPipeline</code> to produce the requested resource */
    EventPipeline eventPipeline = null;
    StreamPipeline pipeline = null;
    boolean result = false;
    try {
      try {
        eventPipeline = (EventPipeline) this.manager.lookup(
                          EventPipeline.ROLE);
        pipeline = (StreamPipeline) this.manager.lookup(
                     StreamPipeline.ROLE);
        pipeline.setEventPipeline(eventPipeline);
      } catch (Exception e) {
        if (getLogger().isErrorEnabled())
          getLogger().error("processing of resource failed", e);
        throw e;
      }

      result = process (environment, pipeline, eventPipeline, false);

    }
    finally { if (eventPipeline != null)
                this.manager.release(eventPipeline);
              if (pipeline != null)
                  this.manager.release(pipeline);
                } return result;
  }

  /**
    * Process to producing the output to the specified <code>OutputStream</code>.
    */
  public boolean process(Environment environment, StreamPipeline pipeline,
                         EventPipeline eventPipeline) throws Exception {
    if (getLogger().isDebugEnabled())
      getLogger().debug("processing internal sitemap request");
    // set the correct component manager
    pipeline.compose(this.manager);
    eventPipeline.compose(this.manager);
    return process (environment, pipeline, eventPipeline, true);
  }

  /**
    * Process to producing the output to the specified <code>OutputStream</code>.
    */
  private final boolean process(Environment environment,
                                StreamPipeline pipeline, EventPipeline eventPipeline,
                                boolean internalRequest) throws Exception {
    /* the <code>List</code> objects to hold the replacement values
        delivered from matchers and selectors to replace occurences of
        XPath kind expressions in values of src attribute used with
        generate and transform elements */
    List listOfMaps = (List) new ArrayList();
    SitemapRedirector redirector = new SitemapRedirector(environment);



    //line numbers not supported with xalan
    try {

      // method for handling "home"
      if (wildcardMatchN400047(redirector, environment, pipeline,
                               eventPipeline, internalRequest, listOfMaps))
        return true;

      throw new ResourceNotFoundException(
        "No pipeline matched request: " +
        environment.getURIPrefix() + '/'+environment.getURI());

    } catch (ConnectionResetException cre) {
      // Will be reported by CocoonServlet, rethrowing
      throw cre;
    }
    catch (ResourceNotFoundException e) {

      // Will be reported by CocoonServlet, rethrowing
      if (true)
        throw e;

    }
    catch (Exception e) {

      if (getLogger().isErrorEnabled())
        getLogger().error("Sitemap", e);
      if (true)
        throw e;

    }


    return false;
  }



  //line numbers not supported with xalan


  // method for handling "home"
  private boolean wildcardMatchN400047(SitemapRedirector redirector,
                                       Environment environment, StreamPipeline pipeline,
                                       EventPipeline eventPipeline, boolean internalRequest,
                                       List listOfMaps) throws ConnectionResetException,
  ResourceNotFoundException, Exception {
    Map map;
    Parameters param;
    Map objectModel = environment.getObjectModel();
    String cocoon_view = environment.getView();
    String cocoon_action = environment.getAction();

    if ((map = matches("wildcard", matcher_wildcard_N400047_expr, "home",
                       listOfMaps, emptyParam, objectModel)) != null) {
      if (getLogger().isDebugEnabled())
        getLogger().debug("Matched wildcardmatcher_wildcard_N400047 pattern:home");
      listOfMaps.add (map);



      if (getLogger().isDebugEnabled())
        getLogger().debug("Component generator:file(emptyParam)");


      if (getLogger().isDebugEnabled())
        getLogger().debug("Source=content/xml/welcome.xml");
      eventPipeline.setGenerator ("file",
                                  substitute(listOfMaps, "content/xml/welcome.xml"),
                                  emptyParam);

      {
        String view_name = null;
        if ((view_name = contains_view("xsltran", cocoon_view)) !=
            null) {
          return call_view(view_name, pipeline, eventPipeline,
                           listOfMaps, environment, internalRequest);
        }
      }

      {
        String view_name = null;
      if ((view_name = contains_view("", cocoon_view)) != null) {
          return call_view(view_name, pipeline, eventPipeline,
                           listOfMaps, environment, internalRequest);
        }
      }



      if (getLogger().isDebugEnabled())
        getLogger().debug("Component transformer:xslt(emptyParam)");


      if (getLogger().isDebugEnabled())
        getLogger().debug("Source=stylesheets/xsl/welcome1.xsl");
      eventPipeline.addTransformer ("xslt",
                                    substitute(listOfMaps,
                                               "stylesheets/xsl/welcome1.xsl"), emptyParam);

      {
        String view_name = null;
        if ((view_name = contains_view("", cocoon_view)) != null) {
          return call_view(view_name, pipeline, eventPipeline,
                           listOfMaps, environment, internalRequest);
        }
      }


      // performing link translation
      if (environment.getObjectModel().containsKey(
            Constants.LINK_OBJECT)) {
        eventPipeline.addTransformer ("!link-translator!", null,
                                      emptyParam);
      }


      if (getLogger().isDebugEnabled())
        getLogger().debug("Component serializer:html(emptyParam)");

      pipeline.setSerializer ("html", null, emptyParam);

      {
        String view_name = null;
        if ((view_name = contains_view("", cocoon_view)) != null) {
          return call_view(view_name, pipeline, eventPipeline,
                           listOfMaps, environment, internalRequest);
        }
      }

      if (!internalRequest) {
        return pipeline.process(environment);
      }


      if (true)
        return true;


      listOfMaps.remove (listOfMaps.size() - 1);
    }
    return false;
  }

}



