<commit_before>/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
package org.gridlab.gridsphere.provider.portletui.beans;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.DiskFileUpload;
import org.gridlab.gridsphere.portlet.PortletRequest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

/**
 * A <code>FileInputBean</code> provides a file upload element
 */
public class FileInputBean extends InputBean implements TagBean {

    public static final int MAX_UPLOAD_SIZE = 1024 * 1024;
    public static final String TEMP_DIR = "/tmp";

    public static final String SUBMIT_STYLE = "portlet-form-button";

    public static String NAME = "fi";

    private FileItem savedFileItem = null;

    /**
     * Constructs a default file input bean
     */
    public FileInputBean() {
        super(NAME);
        this.cssStyle = SUBMIT_STYLE;
        this.inputtype = "file";
    }

    /**
     * Constructs a file input bean from a portlet request and bean identifier
     *
     * @param request the portlet request
     * @param beanId the bean identifier
     * @throws IOException if an I/O exception occurs
     */
    public FileInputBean(PortletRequest request, String beanId) throws IOException {
        super(NAME);
        this.cssStyle = SUBMIT_STYLE;
        this.inputtype = "file";
        this.request = request;
        this.beanId = beanId;
        createFileUpload();
    }

    public FileInputBean(PortletRequest request, String beanId, FileItem fileItem) throws IOException {
        super(NAME);
        this.cssStyle = SUBMIT_STYLE;
        this.inputtype = "file";
        this.request = request;
        this.beanId = beanId;
        savedFileItem = fileItem;
        createFileUpload();
    }


    /**
     * Creates a file upload handler
     *
     * @throws IOException
     */
    protected void createFileUpload() throws IOException {

        if (savedFileItem != null) {
            // Create a new file upload handler
            DiskFileUpload upload = new DiskFileUpload();

            // Set upload parameters
            upload.setSizeMax(MAX_UPLOAD_SIZE);
            upload.setRepositoryPath(TEMP_DIR);

            // Parse the request
            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                name = "<b>Unable to parse uploaded file!</b>";
            }
            // Process the uploaded fields
            Iterator iter = items.iterator();

            try {
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (!item.isFormField()) {
                        savedFileItem = item;

                    }
                }
            } catch (Exception e) {
                throw new IOException("Unable to save file: " + e);
            }
        }
        if (savedFileItem == null) throw new IOException("No file has been saved!");

        value = savedFileItem.getName();
        //savedFileItem.getStoreLocation();

        System.err.println("saved file :" + value);
    }

    /**
     * store Uploded file into file
     *
     */
    public void storeFile(File file) throws Exception {
        if (savedFileItem != null) {
            savedFileItem.write(file);
        }
    }

    /**
     * Returns the uploaded file name
     *
     * @return the uploaded file name
     */
    public String getFileName() {
        if (savedFileItem != null) {
            return savedFileItem.getName();
        } else {
            return "";
        }
    }
<commit_msg>*** empty log message ***<commit_after>/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
package org.gridlab.gridsphere.provider.portletui.beans;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.DiskFileUpload;
import org.gridlab.gridsphere.portlet.PortletRequest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

/**
 * A <code>FileInputBean</code> provides a file upload element
 */
public class FileInputBean extends InputBean implements TagBean {

    public static final int MAX_UPLOAD_SIZE = 1024 * 1024;
    public static final String TEMP_DIR = "/tmp";

    public static final String SUBMIT_STYLE = "portlet-form-button";

    public static String NAME = "fi";

    private FileItem savedFileItem = null;

    /**
     * Constructs a default file input bean
     */
    public FileInputBean() {
        super(NAME);
        this.cssStyle = SUBMIT_STYLE;
        this.inputtype = "file";
    }

    /**
     * Constructs a file input bean from a portlet request and bean identifier
     *
     * @param request the portlet request
     * @param beanId the bean identifier
     * @throws IOException if an I/O exception occurs
     */
    public FileInputBean(PortletRequest request, String beanId) throws IOException {
        super(NAME);
        this.cssStyle = SUBMIT_STYLE;
        this.inputtype = "file";
        this.request = request;
        this.beanId = beanId;
        createFileUpload();
    }

    public FileInputBean(PortletRequest request, String beanId, FileItem fileItem) throws IOException {
        super(NAME);
        this.cssStyle = SUBMIT_STYLE;
        this.inputtype = "file";
        this.request = request;
        this.beanId = beanId;
        savedFileItem = fileItem;
        createFileUpload();
    }


    /**
     * Creates a file upload handler
     *
     * @throws IOException
     */
    protected void createFileUpload() throws IOException {

        if (savedFileItem != null) {
            // Create a new file upload handler
            DiskFileUpload upload = new DiskFileUpload();

            // Set upload parameters
            upload.setSizeMax(MAX_UPLOAD_SIZE);
            upload.setRepositoryPath(TEMP_DIR);

            // Parse the request
            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                name = "<b>Unable to parse uploaded file!</b>";
            }
            // Process the uploaded fields
            Iterator iter = items.iterator();

            try {
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (!item.isFormField()) {
                        savedFileItem = item;

                    }
                }
            } catch (Exception e) {
                throw new IOException("Unable to save file: " + e);
            }
        }
        if (savedFileItem == null) throw new IOException("No file has been saved!");

        value = savedFileItem.getName();
        //savedFileItem.getStoreLocation();

        System.err.println("saved file :" + value);
    }


    /**
     * Returns the uploaded file name
     *
     * @return the uploaded file name
     */
    public String getFileName() {
        if (savedFileItem != null) {
            return savedFileItem.getName();
        } else {
            return "";
        }
    }

{'fraction_non_alphanumeric': 0.09836287376539034, 'fraction_numerical': 0.0021647950209714517, 'mean_word_length': 2.010997963340122, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3836487', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1941, 'n_words': 539}