public Map act()

in core/cocoon-sitemap/cocoon-sitemap-components/src/main/java/org/apache/cocoon/acting/RequestParameterExistsAction.java [77:318]


    public Map act( Redirector redirector,
            SourceResolver resolver,
            Map objectModel,
            String source,
            Parameters parameters
            )
    throws Exception {
        Request request = ObjectModelHelper.getRequest(objectModel);
        HashMap results = new HashMap();
        HashMap items = new HashMap();
        int wildcards = 0;

        // check default parameters for existence
        if (this.getLogger().isDebugEnabled()) {
            getLogger().debug("checking default parameters");
        }
        Iterator reqParams = settings.values().iterator();
        while (reqParams.hasNext()) {
            String paramName = (String) reqParams.next();
            StringParts sp = splitParameter(paramName);
            if (sp != null) {
                // wildcard: special care required (deferred until later)
                items.put(new Integer(wildcards++), sp);
                if (this.getLogger().isDebugEnabled()) {
                    getLogger().debug("(default) deferring " + paramName);
                }
            } else {
                String paramValue = request.getParameter(paramName);
                if (paramValue == null) {
                    return null;
                }
                results.put(paramName, paramValue);
            }
        }

        // check parameters for existence
        if (this.getLogger().isDebugEnabled()) {
            getLogger().debug("checking sitemap parameters");
        }
        String params = parameters.getParameter("parameters", null);
        if (params != null) {
            StringTokenizer st = new StringTokenizer(params);
            while (st.hasMoreTokens()) {
                String paramName = st.nextToken();
                StringParts sp = splitParameter(paramName);
                if (sp != null) {
                    // wildcard: special care required (deferred until later)
                    items.put(new Integer(wildcards++), sp);
                    if (this.getLogger().isDebugEnabled()) {
                        getLogger().debug("deferring " + paramName);
                    }
                } else {

                    String paramValue = request.getParameter(paramName);
                    if (paramValue == null) {
                        return null;
                    }
                    results.put(paramName, paramValue);
                }
            }
        }

        if (wildcards != 0) {
            // special care for parameters with wildcard
            //
            if (this.getLogger().isDebugEnabled()) {
                getLogger().debug("deferred checking for parameters: " + wildcards);
            }

            // first one
            //
            if (this.getLogger().isDebugEnabled()) {
                getLogger().debug(" checking first");
            }
            HashMap values = new HashMap();
            StringParts sp1 = (StringParts) items.get(new Integer(0));
            if (this.getLogger().isDebugEnabled()) {
                getLogger().debug(
                    "  Parameter is [" + sp1.prefix + " * " + sp1.pstfix + "] ");
            }
            Enumeration requestParams = request.getParameterNames();
            Boolean dummy = Boolean.TRUE;
            while (requestParams.hasMoreElements()) {
                String paramName = (String) requestParams.nextElement();
                String match = getMatch(paramName, sp1);
                if (match != null) {
                    if (this.getLogger().isDebugEnabled()) {
                        getLogger().debug(
                            "  value is >"
                            + match
                            + "< "
                            + sp1.prefix.length()
                            + " "
                            + paramName.length()
                            + " "
                            + sp1.pstfix.length());
                    }
                    values.put(match, dummy);
                    sp1.count++;
                    if (this.getLogger().isDebugEnabled()) {
                        getLogger().debug(
                            "   Parameter "
                            + sp1.prefix
                            + "*"
                            + sp1.pstfix
                            + " matches "
                            + paramName
                            + " ("
                            + sp1.count
                            + " so far)");
                    }
                    String paramValue = request.getParameter(paramName);
                    if (paramValue == null) {
                        return null;
                    }
                    results.put(paramName, paramValue);
                }
            }

            if (sp1.count == 0) {
                if (this.getLogger().isDebugEnabled()) {
                    getLogger().debug(
                       "   Parameter "
                        + sp1.prefix
                        + "*"
                        + sp1.pstfix
                        + " matches "
                        + sp1.count);
                }
                return null;
            }

            // all other ones
            //
            if (this.getLogger().isDebugEnabled()) {
                getLogger().debug(" checking others");
            }
            requestParams = request.getParameterNames();
            while (requestParams.hasMoreElements()) {
                String paramName = (String) requestParams.nextElement();
                if (this.getLogger().isDebugEnabled()) {
                    getLogger().debug("  checking request parameter " + paramName);
                }
                for (int i = wildcards - 1; i > 0; i--) {
                    if (this.getLogger().isDebugEnabled()) {
                        getLogger().debug("   checking against " + i);
                    }
                    StringParts sp = (StringParts) items.get(new Integer(i));
                    String match = getMatch(paramName, sp);
                    if (this.getLogger().isDebugEnabled()) {
                        getLogger().debug(
                            "   Parameter is ["
                            + sp.prefix
                            + " * "
                            + sp.pstfix
                            + "] ");
                    }
                    if (match != null) {
                        if (this.getLogger().isDebugEnabled()) {
                            getLogger().debug(
                                "   Parameter "
                                    + sp.prefix
                                    + "*"
                                    + sp.pstfix
                                    + " matches "
                                    + paramName
                                    + " ("
                                    + sp.count
                                    + " so far)");
                        }
                        if (values.containsKey(match)) {
                            sp.count++;
                            if (this.getLogger().isDebugEnabled()) {
                                getLogger().debug(
                                "   " + paramName + " (verified)");
                            }
                            String paramValue = request.getParameter(paramName);
                            if (paramValue == null) {
                                return null;
                            }
                            results.put(paramName, paramValue);

                        } else {
                            if (this.getLogger().isDebugEnabled()) {
                                getLogger().debug(
                                    "Match "
                                    + match
                                    + "not found for "
                                    + sp1.prefix
                                    + "*"
                                    + sp1.pstfix
                                    + " but for "
                                    + sp.prefix
                                    + "*"
                                    + sp.pstfix);
                            }
                            return null;
                        }
                    }
                }
            }

            // since we enforce that only matches are counted, that exist for
            // the first parameter as well, check if every parameter has an
            // equal number of matches.
            //
            if (this.getLogger().isDebugEnabled()) {
                getLogger().debug("checking number of matches");
            }
            for (int i = wildcards - 1; i > 0; i--) {
                StringParts sp = (StringParts) items.get(new Integer(i));
                if (sp.count != sp1.count) {
                    if (this.getLogger().isDebugEnabled()) {
                        getLogger().debug(
                            "Found "
                            + sp.count
                            + " matches for "
                            + sp.prefix
                            + "*"
                            + sp.pstfix
                            + " but expected "
                            + sp1.count);
                    }
                    return null;
                } else {
                    if (this.getLogger().isDebugEnabled()) {
                        getLogger().debug(
                            "Found "
                            + sp.count
                            + " matches for "
                            + sp.prefix
                            + "*"
                            + sp.pstfix
                            + " as expected");
                    }
                }
            }

        }

        return Collections.unmodifiableMap(results);
    }