Example usage for org.apache.commons.io FileUtils toFile

List of usage examples for org.apache.commons.io FileUtils toFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils toFile.

Prototype

public static File toFile(URL url) 

Source Link

Document

Convert from a URL to a File.

Usage

From source file:net.pms.util.FileUtilTest.java

@Test
public void testGetFileCharset_UTF8_with_BOM() throws Exception {
    File file = FileUtils.toFile(CLASS.getResource("russian-utf8-with-bom.srt"));
    assertThat(FileUtil.getFileCharset(file)).isEqualTo(Constants.CHARSET_UTF_8);
}

From source file:com.itemanalysis.psychometrics.scaling.PercentileRankTest.java

public double[] getPercentileRank() {
    double[] x = new double[1000];
    try {//from  w  w w.  j av  a2s  . c o m
        File f = FileUtils.toFile(this.getClass().getResource("/testdata/scaling.txt"));
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line = "";
        String[] s = null;
        int row = 0;
        br.readLine();//eliminate column names by skipping first row
        while ((line = br.readLine()) != null) {
            s = line.split(",");
            x[row] = Double.parseDouble(s[51]);//percentile rank is in column 52
            row++;
        }
        br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return x;
}

From source file:net.sf.eclipsecs.core.config.configtypes.ProjectConfigurationType.java

/**
 * {@inheritDoc}/*from w  w  w .  ja  v a  2s  . c  om*/
 */
public boolean isConfigurable(ICheckConfiguration checkConfiguration) {
    boolean isConfigurable = true;

    boolean isProtected = Boolean.valueOf(checkConfiguration.getAdditionalData().get(KEY_PROTECT_CONFIG))
            .booleanValue();
    isConfigurable = !isProtected;

    if (!isProtected) {

        // The configuration can be changed when the external configuration
        // file can is writable
        try {
            isConfigurable = FileUtils.toFile(checkConfiguration.getResolvedConfigurationFileURL()).canWrite();
        } catch (CheckstylePluginException e) {
            CheckstyleLog.log(e);
            isConfigurable = false;
        }

    }
    return isConfigurable;
}

From source file:net.pms.formats.v2.SubtitleUtilsTest.java

@Test
public void testGetSubCpOptionForMencoder_withoutDetectedCharset() throws Exception {
    DLNAMediaSubtitle subtitle = new DLNAMediaSubtitle();
    File file_cp1251 = FileUtils.toFile(CLASS.getResource("../../util/russian-cp1251.srt"));
    subtitle.setType(VOBSUB);/*ww w.j av a  2s .c  o  m*/
    subtitle.setExternalFile(file_cp1251);
    assertThat(subtitle.getExternalFileCharacterSet()).isNull();
    assertThat(getSubCpOptionForMencoder(subtitle)).isNull();
}

From source file:net.pms.util.FileUtilTest.java

@Test
public void testGetFileCharset_UTF16_LE() throws Exception {
    File file = FileUtils.toFile(CLASS.getResource("russian-utf16-le.srt"));
    assertThat(FileUtil.getFileCharset(file)).isEqualTo(Constants.CHARSET_UTF_16LE);
}

From source file:com.itemanalysis.psychometrics.scaling.NormalizedScoreTest.java

public double[][] getNormScore() {
    double[][] x = new double[1000][2];
    try {// w ww. ja  v  a2s  .c  o  m
        File f = FileUtils.toFile(this.getClass().getResource("/testdata/scaling.txt"));
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line = "";
        String[] s = null;
        int row = 0;
        br.readLine();//eliminate column names by skipping first row
        while ((line = br.readLine()) != null) {
            s = line.split(",");
            x[row][0] = Double.parseDouble(s[53]);//percentile rank is in column 54
            x[row][1] = Double.parseDouble(s[54]);//percentile rank on IQ scale is in column 55
            row++;
        }
        br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return x;
}

From source file:com.itemanalysis.psychometrics.scaling.KelleyRegressedScoreTest.java

public double[] getKelleyScores() {
    double[] x = new double[1000];
    try {//w  w w . ja  va2s  . c o m
        File f = FileUtils.toFile(this.getClass().getResource("/testdata/scaling.txt"));
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line = "";
        String[] s = null;
        int row = 0;
        br.readLine();//eliminate column names by skipping first row
        while ((line = br.readLine()) != null) {
            s = line.split(",");
            x[row] = Double.parseDouble(s[52]);//kelley scores in column 53
            row++;
        }
        br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return x;
}

From source file:de.renew.workflow.connector.internal.cases.ScenarioCompatibilityHelper.java

public static boolean ensureBackwardsCompatibility(final ScenarioHandlingProjectNature nature) {
    // FIXME: this is dirty fix only for this release 2.3
    // should be implemented in other way, we just do not have any time now
    try {//from  w  w  w .  ja  v  a  2 s  .com
        if (nature == null
                || !nature.getProject().hasNature("org.kalypso.kalypso1d2d.pjt.Kalypso1D2DProjectNature")) //$NON-NLS-1$
            return true;

        // FIXME: the whole code here does not belong to this place -> this is a hidden dependency to 1d2d: bad!
        // TODO: instead implement an extension point mechanism
        final ProjectTemplate[] lTemplate = EclipsePlatformContributionsExtensions
                .getProjectTemplates("org.kalypso.kalypso1d2d.pjt.projectTemplate"); //$NON-NLS-1$
        try {
            // FIXME: this very probably does not work correctly or any more at all!

            /* Unpack project from template */
            final File destinationDir = nature.getProject().getLocation().toFile();
            final URL data = lTemplate[0].getData();
            final String location = data.toString();
            final String extension = FilenameUtils.getExtension(location);
            if ("zip".equalsIgnoreCase(extension)) //$NON-NLS-1$
            {
                // TODO: this completely overwrite the old project content, is this intended?
                ZipUtilities.unzip(data.openStream(), destinationDir, false);
            } else {
                final URL fileURL = FileLocator.toFileURL(data);
                final File dataDir = FileUtils.toFile(fileURL);
                if (dataDir == null) {
                    return false;
                }

                // FIXME: this only fixes the basic scenario, is this intended?

                final IOFileFilter lFileFilter = new WildcardFileFilter(new String[] { "wind.gml" }); //$NON-NLS-1$
                final IOFileFilter lDirFilter = TrueFileFilter.INSTANCE;
                final Collection<?> windFiles = FileUtils.listFiles(destinationDir, lFileFilter, lDirFilter);

                if (dataDir.isDirectory() && (windFiles == null || windFiles.size() == 0)) {
                    final WildcardFileFilter lCopyFilter = new WildcardFileFilter(
                            new String[] { "*asis", "models", "wind.gml" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    FileUtils.copyDirectory(dataDir, destinationDir, lCopyFilter);
                } else {
                    return true;
                }
            }
        } catch (final Throwable t) {
            t.printStackTrace();
            return false;
        }

        nature.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
    } catch (final CoreException e) {
        // FIXME: this is no error handling; the users are not informed and will stumble over following errors caued by
        // this problem

        WorkflowConnectorPlugin.getDefault().getLog().log(e.getStatus());
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:com.itemanalysis.jmetrik.data.JmetrikFileImporterTest.java

@Test
public void readJmetrikFileTest() {
    System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file");
    CSVParser parser = null;//from  www. j av  a  2s.  c om
    Reader reader = null;

    try {
        File dataFile = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik"));
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8");

        parser = new CSVParser(reader, CSVFormat.DEFAULT.withCommentMarker('#'));
        Iterator<CSVRecord> iter = parser.iterator();
        CSVRecord temp = null;

        boolean readAttributes = false;
        boolean readData = false;
        int attCount = 0;

        while (iter.hasNext()) {
            temp = iter.next();

            if ("VERSION".equals(temp.getComment())) {
                System.out.println("VERSION: " + temp.get(0));

            } else if ("METADATA".equals(temp.getComment())) {
                System.out.println("CASES: " + temp.get(0));
            } else if ("ATTRIBUTES".equals(temp.getComment())) {
                readAttributes = true;
            } else if ("DATA".equals(temp.getComment())) {
                readAttributes = false;
                readData = true;
            }

            if (readAttributes) {
                System.out.print("ATTRIBUTE-" + attCount + ": ");
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
                attCount++;
            }

            if (readData) {
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
            }

        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            parser.close();
            reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

From source file:com.garethahealy.camel.file.loadbalancer.example1.routes.HandlesOneFileMultipleReadersTest.java

@Test
public void handlesOneFileMultipleReaders() throws InterruptedException, MalformedURLException {
    MockEndpoint first = getMockEndpoint("mock:endFirst");
    first.setExpectedMessageCount(1);/*www. j  ava  2s  . c  o m*/
    first.expectedBodiesReceived("afile1.log");
    first.setResultWaitTime(TimeUnit.SECONDS.toMillis(15));
    first.setAssertPeriod(TimeUnit.SECONDS.toMillis(1));

    MockEndpoint second = getMockEndpoint("mock:endSecond");
    second.setExpectedMessageCount(0);
    second.setResultWaitTime(TimeUnit.SECONDS.toMillis(15));
    second.setAssertPeriod(TimeUnit.SECONDS.toMillis(1));

    MockEndpoint third = getMockEndpoint("mock:endThird");
    third.setExpectedMessageCount(0);
    third.setResultWaitTime(TimeUnit.SECONDS.toMillis(15));
    third.setAssertPeriod(TimeUnit.SECONDS.toMillis(1));

    first.assertIsSatisfied();
    second.assertIsSatisfied();
    third.assertIsSatisfied();

    File firstDirectory = FileUtils.toFile(new URL("file:" + rootDirectory + "/.camel0"));
    File secondDirectory = FileUtils.toFile(new URL("file:" + rootDirectory + "/.camel1"));
    File thirdDirectory = FileUtils.toFile(new URL("file:" + rootDirectory + "/.camel2"));

    Assert.assertTrue(".camel0 doesnt exist", firstDirectory.exists());
    Assert.assertFalse(".camel1 exists", secondDirectory.exists());
    Assert.assertFalse(".camel2 exists", thirdDirectory.exists());

    Collection<File> firstFiles = FileUtils.listFiles(firstDirectory,
            FileFilterUtils.prefixFileFilter("afile1.log"), null);

    Assert.assertNotNull(firstFiles);

    //Directory should of only copied one file
    Assert.assertEquals(new Integer(1), new Integer(firstFiles.size()));
}