<commit_before>package com.prairie.eevernote.util;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.w3c.dom.DOMException;

import com.prairie.eevernote.enml.FontStyle;
import com.prairie.eevernote.enml.StyleText;
import com.prairie.eevernote.ui.ConfigContentProposalProvider;
import com.prairie.eevernote.ui.ConfigTextContentAdapter;

public class EclipseUtil implements ConstantsUtil {

    public static List<File> getSelectedFiles(final ExecutionEvent event) {
        ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();

        final List<File> files = ListUtil.list();

        if (selection instanceof IStructuredSelection) {
            Iterator<?> iterator = ((StructuredSelection) selection).iterator();
            while (iterator.hasNext()) {
                IFile iFile;
                Object object = iterator.next();
                if (object instanceof IFile) {
                    iFile = (IFile) object;
                } else if (object instanceof ICompilationUnit) {
                    ICompilationUnit compilationUnit = (ICompilationUnit) object;
                    IResource resource = compilationUnit.getResource();
                    if (resource instanceof IFile) {
                        iFile = (IFile) resource;
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                File file = iFile.getLocation().makeAbsolute().toFile();
                files.add(file);
            }
        } else if (selection instanceof ITextSelection) {
            IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
            IFile iFile = (IFile) editorPart.getEditorInput().getAdapter(IFile.class);
            if (iFile != null) {// TODO iFile == null: how to handle this
                // case in XML file
                File file = iFile.getLocation().makeAbsolute().toFile();
                files.add(file);
            }
        }

        return files;
    }

    public static List<List<StyleText>> getSelectedStyleText(final StyledText styledText) throws DOMException, ParserConfigurationException {
        Point selection = styledText.getSelection();
        String selectionText = styledText.getSelectionText();

        String face = StringUtils.EMPTY;
        int size = TEN;
        FontData[] fontDatas = styledText.getFont().getFontData();
        if (fontDatas != null && fontDatas.length > ZERO) {
            face = fontDatas[ZERO].getName();
            size = fontDatas[ZERO].getHeight();
        }

        String[] lines = StringUtil.splitByMultipleSeparatorsPreserveAllTokens(selectionText, new String[] { StringUtil.CRLF, StringUtils.CR, StringUtils.LF });<commit_msg>use utility as much as possible<commit_after>package com.prairie.eevernote.util;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.w3c.dom.DOMException;

import com.prairie.eevernote.enml.FontStyle;
import com.prairie.eevernote.enml.StyleText;
import com.prairie.eevernote.ui.ConfigContentProposalProvider;
import com.prairie.eevernote.ui.ConfigTextContentAdapter;

public class EclipseUtil implements ConstantsUtil {

    public static List<File> getSelectedFiles(final ExecutionEvent event) {
        ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();

        final List<File> files = ListUtil.list();

        if (selection instanceof IStructuredSelection) {
            Iterator<?> iterator = ((StructuredSelection) selection).iterator();
            while (iterator.hasNext()) {
                IFile iFile;
                Object object = iterator.next();
                if (object instanceof IFile) {
                    iFile = (IFile) object;
                } else if (object instanceof ICompilationUnit) {
                    ICompilationUnit compilationUnit = (ICompilationUnit) object;
                    IResource resource = compilationUnit.getResource();
                    if (resource instanceof IFile) {
                        iFile = (IFile) resource;
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                File file = iFile.getLocation().makeAbsolute().toFile();
                files.add(file);
            }
        } else if (selection instanceof ITextSelection) {
            IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
            IFile iFile = (IFile) editorPart.getEditorInput().getAdapter(IFile.class);
            if (iFile != null) {// TODO iFile == null: how to handle this
                // case in XML file
                File file = iFile.getLocation().makeAbsolute().toFile();
                files.add(file);
            }
        }

        return files;
    }

    public static List<List<StyleText>> getSelectedStyleText(final StyledText styledText) throws DOMException, ParserConfigurationException {
        Point selection = styledText.getSelection();
        String selectionText = styledText.getSelectionText();

        String face = StringUtils.EMPTY;
        int size = TEN;
        FontData[] fontDatas = styledText.getFont().getFontData();
        if (fontDatas != null && fontDatas.length > ZERO) {
            face = fontDatas[ZERO].getName();
            size = fontDatas[ZERO].getHeight();
        }

        String[] lines = StringUtil.splitByMultipleSeparatorsPreserveAllTokens(selectionText, ArrayUtils.toArray(StringUtil.CRLF, StringUtils.CR, StringUtils.LF));
{'fraction_non_alphanumeric': 0.09700629577283824, 'fraction_numerical': 0.0007709109597841449, 'mean_word_length': 2.8288243974422036, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1464376', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2079, 'n_words': 411}