<commit_before>/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  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.
 *
 */

/* $Id$  */

package org.apache.lenya.cms.cocoon.components.modules.input;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.commons.lang.StringUtils;
import org.apache.lenya.cms.publication.DocumentException;
import org.apache.lenya.cms.publication.DocumentType;
import org.apache.lenya.cms.publication.DocumentTypeResolver;
import org.apache.lenya.cms.publication.PageEnvelope;

/**
 * Input module wrapping the page envelope. This module provides publication
 * related information such as document-id, area, publication-id.
 * 
 * @see org.apache.lenya.cms.publication.PageEnvelope
 */
public class PageEnvelopeModule extends AbstractPageEnvelopeModule {

    protected static final String URI_PARAMETER_DOCTYPE = "doctype";

    /**
     * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
     *      org.apache.avalon.framework.configuration.Configuration,
     *      java.util.Map)
     */
    public Object getAttribute(String name, Configuration modeConf, Map objectModel)
            throws ConfigurationException {
        PageEnvelope envelope = getEnvelope(objectModel);
        Object value = null;

        try {
            if (name.equals(PageEnvelope.AREA)) {
                value = envelope.getDocument().getArea();
            } else if (name.equals(PageEnvelope.CONTEXT)) {
                value = envelope.getContext();
            } else if (name.equals(PageEnvelope.PUBLICATION_ID)) {
                value = envelope.getPublication().getId();
            } else if (name.equals(PageEnvelope.PUBLICATION)) {
                value = envelope.getPublication();
            } else if (name.equals(PageEnvelope.PUBLICATION_LANGUAGES_CSV)) {
                value = StringUtils.join(envelope.getPublication().getLanguages(), ',');
            } else if (name.equals(PageEnvelope.DOCUMENT)) {
                value = envelope.getDocument();
            } else if (name.equals(PageEnvelope.DOCUMENT_ID)) {
                value = envelope.getDocument().getId();
            } else if (name.equals(PageEnvelope.DOCUMENT_NAME)) {
                value = envelope.getDocument().getName();
            } else if (name.equals(PageEnvelope.DOCUMENT_LABEL)) { // FIXME: Why
                                                                   // is this
                                                                   // here?
                value = envelope.getDocument().getLabel();
            } else if (name.equals(PageEnvelope.DOCUMENT_URL)) {
                value = envelope.getDocument().getCanonicalDocumentURL();
            } else if (name.equals(PageEnvelope.DOCUMENT_URL_WITHOUT_LANGUAGE)) {
                value = envelope.getDocument().getCanonicalWebappURL();
            } else if (name.equals(PageEnvelope.DOCUMENT_PATH)) {
                value = envelope.getDocumentPath();
            } else if (name.equals(PageEnvelope.DOCUMENT_FILE)) {
                value = envelope.getDocument().getFile();
            } else if (name.equals(PageEnvelope.DOCUMENT_EXTENSION)) {
                value = envelope.getDocument().getExtension();
            } else if (name.equals(PageEnvelope.DEFAULT_LANGUAGE)) {
                value = envelope.getPublication().getDefaultLanguage();
            } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGE)) {
                value = envelope.getDocument().getLanguage();
            } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES)) {
                value = envelope.getDocument().getLanguages();
            } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES_CSV)) {
                value = StringUtils.join(envelope.getDocument().getLanguages(), ',');
            } else if (name.equals(PageEnvelope.DOCUMENT_LASTMODIFIED)) {
                Date date = envelope.getDocument().getLastModified();
                value = new SimpleDateFormat(DATE_FORMAT).format(date);
            } else if (name.equals(PageEnvelope.BREADCRUMB_PREFIX)) {
                value = envelope.getPublication().getBreadcrumbPrefix();
            } else if (name.equals(PageEnvelope.DOCUMENT_TYPE)) {
                value = getDocumentType(envelope);
            } else {
                throw new ConfigurationException("The attribute [" + name + "] is not supported!");
            }
        } catch (final DocumentException e) {
            throw new ConfigurationException("Getting attribute for name [" + name + "] failed: ",
                    e);
        } catch (final ServiceException e) {
            throw new ConfigurationException("Getting attribute for name [" + name + "] failed: ",
                    e);
        } catch (final ConfigurationException e) {
            throw new ConfigurationException("Getting attribute for name [" + name + "] failed: ",
                    e);
        }

        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Returning [" + name + "] = [" + value + "]");
        }

        return value;
    }

    /**
     * <code>DATE_FORMAT</code> The date format
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * Returns the document type.<commit_msg>check if current URL represents document before resolving according values<commit_after>/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  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.
 *
 */

/* $Id$  */

package org.apache.lenya.cms.cocoon.components.modules.input;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.commons.lang.StringUtils;
import org.apache.lenya.cms.publication.Document;
import org.apache.lenya.cms.publication.DocumentException;
import org.apache.lenya.cms.publication.DocumentType;
import org.apache.lenya.cms.publication.DocumentTypeResolver;
import org.apache.lenya.cms.publication.PageEnvelope;

/**
 * Input module wrapping the page envelope. This module provides publication
 * related information such as document-id, area, publication-id.
 * 
 * @see org.apache.lenya.cms.publication.PageEnvelope
 */
public class PageEnvelopeModule extends AbstractPageEnvelopeModule {

    protected static final String URI_PARAMETER_DOCTYPE = "doctype";

    /**
     * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
     *      org.apache.avalon.framework.configuration.Configuration,
     *      java.util.Map)
     */
    public Object getAttribute(String name, Configuration modeConf, Map objectModel)
            throws ConfigurationException {

        if (!Arrays.asList(PageEnvelope.PARAMETER_NAMES).contains(name)) {
            throw new ConfigurationException("The attribute [" + name + "] is not supported!");
        }

        PageEnvelope envelope = getEnvelope(objectModel);
        Object value = null;

        try {
            if (name.equals(PageEnvelope.AREA)) {
                value = envelope.getArea();
            } else if (name.equals(PageEnvelope.CONTEXT)) {
                value = envelope.getContext();
            } else if (name.equals(PageEnvelope.PUBLICATION_ID)) {
                value = envelope.getPublication().getId();
            } else if (name.equals(PageEnvelope.PUBLICATION)) {
                value = envelope.getPublication();
            } else if (name.equals(PageEnvelope.PUBLICATION_LANGUAGES_CSV)) {
                value = StringUtils.join(envelope.getPublication().getLanguages(), ',');
            } else if (name.equals(PageEnvelope.DEFAULT_LANGUAGE)) {
                value = envelope.getPublication().getDefaultLanguage();
            } else if (name.equals(PageEnvelope.BREADCRUMB_PREFIX)) {
                value = envelope.getPublication().getBreadcrumbPrefix();
            }

            Document document = envelope.getDocument();
            if (document != null) {
                if (name.equals(PageEnvelope.DOCUMENT)) {
                    value = document;
                } else if (name.equals(PageEnvelope.DOCUMENT_ID)) {
                    value = document.getId();
                } else if (name.equals(PageEnvelope.DOCUMENT_NAME)) {
                    value = document.getName();
                } else if (name.equals(PageEnvelope.DOCUMENT_LABEL)) {
                    value = document.getLabel();
                } else if (name.equals(PageEnvelope.DOCUMENT_URL)) {
                    value = document.getCanonicalDocumentURL();
                } else if (name.equals(PageEnvelope.DOCUMENT_URL_WITHOUT_LANGUAGE)) {
                    value = document.getCanonicalWebappURL();
                } else if (name.equals(PageEnvelope.DOCUMENT_PATH)) {
                    value = envelope.getDocumentPath();
                } else if (name.equals(PageEnvelope.DOCUMENT_FILE)) {
                    value = document.getFile();
                } else if (name.equals(PageEnvelope.DOCUMENT_EXTENSION)) {
                    value = document.getExtension();
                } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGE)) {
                    value = document.getLanguage();
                } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES)) {
                    value = document.getLanguages();
                } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES_CSV)) {
                    value = StringUtils.join(document.getLanguages(), ',');
                } else if (name.equals(PageEnvelope.DOCUMENT_LASTMODIFIED)) {
                    Date date = document.getLastModified();
                    value = new SimpleDateFormat(DATE_FORMAT).format(date);
                } else if (name.equals(PageEnvelope.DOCUMENT_TYPE)) {
                    value = getDocumentType(envelope);
                }
            }
        } catch (final DocumentException e) {
            throw new ConfigurationException("Getting attribute for name [" + name + "] failed: ",
                    e);
        } catch (final ServiceException e) {
            throw new ConfigurationException("Getting attribute for name [" + name + "] failed: ",
                    e);
        }

        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Returning [" + name + "] = [" + value + "]");
        }

        return value;
    }

    /**
     * <code>DATE_FORMAT</code> The date format
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * Returns the document type.
{'fraction_non_alphanumeric': 0.10607689127738736, 'fraction_numerical': 0.0019842910293509713, 'mean_word_length': 2.3929873772791024, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683707', 'n_tokens_mistral': 3241, 'n_tokens_neox': 3091, 'n_words': 694}