<commit_before>/*
 * Copyright 2007,2008,2009 Wyona
 */

package org.wyona.yanel.impl.resources.tinymce;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
import org.wyona.yanel.core.Environment;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.ResourceConfiguration;
import org.wyona.yanel.core.api.attributes.CreatableV2;
import org.wyona.yanel.core.api.attributes.ModifiableV2;
import org.wyona.yanel.core.api.attributes.VersionableV2;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.map.Realm;
import org.wyona.yanel.core.navigation.Node;
import org.wyona.yanel.core.navigation.Sitetree;
import org.wyona.yanel.core.source.SourceResolver;
import org.wyona.yanel.core.util.PathUtil;
import org.wyona.yanel.core.util.ResourceAttributeHelper;

import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.yanel.impl.resources.BasicXMLResource;
import org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource;
import org.wyona.yanel.impl.resources.usecase.UsecaseException;
import org.wyona.yanel.impl.resources.xml.ConfigurableViewDescriptor;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.io.IOUtils;
import org.apache.xml.resolver.tools.CatalogResolver;


/**
 * Resource to edit another modifiable resource with TinyMCE
 */
public class TinyMCEResource extends ExecutableUsecaseResource {
    
    private static Logger log = Logger.getLogger(TinyMCEResource.class);
    
    private static final String PARAMETER_EDIT_PATH = "edit-path";
    private static final String PARAMETER_CONTINUE_PATH = "continue-path";
    private static final String PARAM_SUBMIT_TIDY = "submit-tidy";
    private static final String PARAM_SUBMIT_TIDY_SAVE = "submit-tidy-save";
    private static final String VIEW_FIX_WELLFORMNESS = "fix-wellformness";
    private static final String CONFIG_PROPERTY_MATCHER_EXTENSION = "matcher-extension";    
    
    private String editorContent;
    private String resourceContent;
    private String editPath;
    private String contentToEdit;
    private Resource resToEdit;

    /* (non-Javadoc)
     * @see org.wyona.yanel.impl.resources.usecase.UsecaseResource#init()
     */
    protected void init() throws UsecaseException {
        log.warn("DEBUG: Init resource ...");
        try {
            String matcherExtension = getResourceConfigProperty(CONFIG_PROPERTY_MATCHER_EXTENSION);
            if (matcherExtension != null && matcherExtension.length() > 0) {
                editPath = getPath().substring(0, getPath().length() - matcherExtension.length());
            } else {
                log.warn("Could not get Resource Configuration Property 'matcher-extension'. Fallback to previous version ...");
                editPath = getParameterAsString(PARAMETER_EDIT_PATH); 
            }
        } catch(Exception e) {
            log.error(e, e);
            throw new UsecaseException(e.getMessage());
        }

        if (editPath == null || editPath.equals("")) {
            addError("Could not get paramter edit-path. Don't know what to edit.");
            return;
        } else {
        }

        try {
            resToEdit = getYanel().getResourceManager().getResource(getEnvironment(), getRealm(), editPath);
        } catch (Exception e) {
            log.error("Exception: " + e);
        }
        if (resToEdit == null) {
            addError("Could not get Resource-Type to edit.");
            return;
        }
        
        log.warn("DEBUG: Edit path: " + editPath);
        editorContent = getParameterAsString(editPath); 
        log.warn("DEBUG: Editor content: " + editorContent);

        if (ResourceAttributeHelper.hasAttributeImplemented(resToEdit, "Modifiable", "2")) {
            try {
                InputStream is = ((ModifiableV2) resToEdit).getInputStream();
                resourceContent = IOUtils.toString(is);
            } catch (Exception e) {
                log.error(e, e);
                addError("Could not get Resource-Type content.");
            }
        } else {
            addError("This resource can not be edited. ");
        }
    }<commit_msg>log message added to output<commit_after>/*
 * Copyright 2007,2008,2009 Wyona
 */

package org.wyona.yanel.impl.resources.tinymce;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
import org.wyona.yanel.core.Environment;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.ResourceConfiguration;
import org.wyona.yanel.core.api.attributes.CreatableV2;
import org.wyona.yanel.core.api.attributes.ModifiableV2;
import org.wyona.yanel.core.api.attributes.VersionableV2;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.map.Realm;
import org.wyona.yanel.core.navigation.Node;
import org.wyona.yanel.core.navigation.Sitetree;
import org.wyona.yanel.core.source.SourceResolver;
import org.wyona.yanel.core.util.PathUtil;
import org.wyona.yanel.core.util.ResourceAttributeHelper;

import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.yanel.impl.resources.BasicXMLResource;
import org.wyona.yanel.impl.resources.usecase.ExecutableUsecaseResource;
import org.wyona.yanel.impl.resources.usecase.UsecaseException;
import org.wyona.yanel.impl.resources.xml.ConfigurableViewDescriptor;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.io.IOUtils;
import org.apache.xml.resolver.tools.CatalogResolver;


/**
 * Resource to edit another modifiable resource with TinyMCE
 */
public class TinyMCEResource extends ExecutableUsecaseResource {
    
    private static Logger log = Logger.getLogger(TinyMCEResource.class);
    
    private static final String PARAMETER_EDIT_PATH = "edit-path";
    private static final String PARAMETER_CONTINUE_PATH = "continue-path";
    private static final String PARAM_SUBMIT_TIDY = "submit-tidy";
    private static final String PARAM_SUBMIT_TIDY_SAVE = "submit-tidy-save";
    private static final String VIEW_FIX_WELLFORMNESS = "fix-wellformness";
    private static final String CONFIG_PROPERTY_MATCHER_EXTENSION = "matcher-extension";    
    
    private String editorContent;
    private String resourceContent;
    private String editPath;
    private String contentToEdit;
    private Resource resToEdit;

    /* (non-Javadoc)
     * @see org.wyona.yanel.impl.resources.usecase.UsecaseResource#init()
     */
    protected void init() throws UsecaseException {
        log.warn("DEBUG: Init resource ...");
        try {
            String matcherExtension = getResourceConfigProperty(CONFIG_PROPERTY_MATCHER_EXTENSION);
            if (matcherExtension != null && matcherExtension.length() > 0) {
                editPath = getPath().substring(0, getPath().length() - matcherExtension.length());
            } else {
                log.warn("Could not get Resource Configuration Property 'matcher-extension'. Fallback to previous version ...");
                editPath = getParameterAsString(PARAMETER_EDIT_PATH); 
            }
        } catch(Exception e) {
            log.error(e, e);
            throw new UsecaseException(e.getMessage());
        }

        if (editPath == null || editPath.equals("")) {
            addError("Could not get paramter edit-path. Don't know what to edit.");
            return;
        } else {
        }

        try {
            resToEdit = getYanel().getResourceManager().getResource(getEnvironment(), getRealm(), editPath);
        } catch (Exception e) {
            log.error("Exception: " + e);
        }
        if (resToEdit == null) {
            addError("Could not get Resource-Type to edit.");
            return;
        }
        
        log.warn("DEBUG: Edit path: " + editPath);
        editorContent = getParameterAsString(editPath); 
        log.warn("DEBUG: Editor content: " + editorContent);

        if (ResourceAttributeHelper.hasAttributeImplemented(resToEdit, "Modifiable", "2")) {
            try {
                InputStream is = ((ModifiableV2) resToEdit).getInputStream();
                resourceContent = IOUtils.toString(is);
            } catch (Exception e) {
                log.error(e, e);
                addError("Could not get Resource-Type content: " + e.getMessage());
            }
        } else {
            addError("This resource can not be edited. ");
        }
    }
{'fraction_non_alphanumeric': 0.10670926517571885, 'fraction_numerical': 0.005111821086261981, 'mean_word_length': 3.2112107623318384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492247', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2756, 'n_words': 541}