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:org.kalypso.grid.GeoGridUtilities.java

/**
 * Opens a {@link IGeoGrid} for a resource of a given mime-type.
 * //from w  w w. j a  va  2  s  .  c o m
 * @param writeable
 *          if <code>true</code>, the grid is opened for write-access. In that case a {@link IWriteableGeoGrid} will
 *          be returned.
 * @throws UnsupportedOperationException
 *           If a grid is opened for write access that does not support it.
 */
public static IGeoGrid openGrid(final String mimeType, final URL url, final Coordinate origin,
        final Coordinate offsetX, final Coordinate offsetY, final String sourceCRS, final boolean writeable)
        throws IOException, GeoGridException {
    // HACK: internal binary grid
    if (mimeType.endsWith("/bin"))
        return BinaryGeoGrid.openGrid(url, origin, offsetX, offsetY, sourceCRS, writeable);

    if (mimeType.endsWith("/tiff")) {
        /* If it is a tiff, it may be opened writable, but this works only with a file. */
        /* If the URL cannot be converted to a file, fall through and use the image geo grid instead. */
        final File file = FileUtils.toFile(url);
        if (file != null)
            return new TiffGeoGrid(origin, offsetX, offsetY, sourceCRS, file, -1, -1);
    }

    if (writeable)
        throw new UnsupportedOperationException("Cannot open this grid for write access.");

    if (mimeType.endsWith("/asc") || mimeType.endsWith("/asg"))
        return new AsciiRandomAccessGeoGrid(url, origin, offsetX, offsetY, sourceCRS);

    if (mimeType.startsWith("image"))
        return new ImageGeoGrid(url, origin, offsetX, offsetY, sourceCRS);

    throw new UnsupportedOperationException("Unknown file type: " + mimeType);
}

From source file:org.kalypso.grid.parallel.SequentialBinaryGeoGridReader.java

public SequentialBinaryGeoGridReader(final IGeoGrid templateGrid, final URL pUrl)
        throws IOException, GeoGridException {
    super(templateGrid.getOrigin(), templateGrid.getOffsetX(), templateGrid.getOffsetY(),
            templateGrid.getSourceCRS());

    /* Tries to find a file from the given url. */
    File gridFile = ResourceUtilities.findJavaFileFromURL(pUrl);
    if (gridFile == null)
        gridFile = FileUtils.toFile(pUrl);

    // REMARK using same buffer size as block size, so stream can be read in one go
    m_gridStream = new DataInputStream(new BufferedInputStream(new FileInputStream(gridFile), m_blockSize));

    m_header = BinaryGeoGridHeader.read(m_gridStream);

    final long length = getSizeX() * getSizeY();
    m_blockAmount = (length / m_blockSize) + 1;

    m_scalePotence = (int) Math.pow(10, m_header.getScale());
}

From source file:org.kalypso.kalypsomodel1d2d.conv.results.SWANResultsReader.java

public Map<String, Map<GM_Position, Double>> readMatResultsFile(final String pStrFilter) {
    MatFileReader lMatFileReader = null;
    final MatFileFilter lMatFilter = new MatFileFilter();
    Map<String, Map<GM_Position, Double>> lRes = null;
    try {//from  w ww  .  ja v  a  2s  .c om
        if (pStrFilter != null) {
            lMatFilter.addArrayName(STR_XP_NAME);
            lMatFilter.addArrayName(STR_YP_NAME);
            lMatFilter.addArrayName(pStrFilter);
        }
        if (m_strResultsFile == null) {
            lMatFileReader = new MatFileReader(FileUtils.toFile(m_resultsFile.getURL()), lMatFilter);
        } else {
            lMatFileReader = new MatFileReader(m_strResultsFile, lMatFilter);
        }
    } catch (final Exception e) {
        // throw new MatlabIOException( "Cannot open or read SWAN result matlab file." );
    }
    if (lMatFileReader != null) {
        try {
            final GM_Position lShiftPosition = SWANDataConverterHelper.readCoordinateShiftValues(m_resultsFile);
            m_doubleShiftX = lShiftPosition.getX();
            m_doubleShiftY = lShiftPosition.getY();
            final Map<String, MLArray> lMapData = lMatFileReader.getContent();
            // printDebugParsedSWANRawData( lMatFileReader, null );
            lRes = getValuesFormatedNameDatePosition(lMapData);
            // printDebugResultData( lRes, null );
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
    return lRes;
}

From source file:org.kalypso.kalypsomodel1d2d.conv.results.TelemacResultsReader.java

public Map<String, Map<GM_Position, Double>> readMatResultsFile(final String pStrFilter) {
    MatFileReader lMatFileReader = null;
    MatFileFilter lMatFilter = new MatFileFilter();
    Map<String, Map<GM_Position, Double>> lRes = null;
    try {/* w ww.j a v  a  2 s. c  o m*/
        if (pStrFilter != null) {
            lMatFilter.addArrayName(STR_XP_NAME);
            lMatFilter.addArrayName(STR_YP_NAME);
            lMatFilter.addArrayName(pStrFilter);
        }
        if (m_strResultsFile == null) {
            lMatFileReader = new MatFileReader(FileUtils.toFile(m_resultsFile.getURL()), lMatFilter);
        } else {
            lMatFileReader = new MatFileReader(m_strResultsFile, lMatFilter);
        }
    } catch (Exception e) {
        // throw new MatlabIOException( "Cannot open or read SWAN result matlab file." );
    }
    if (lMatFileReader != null) {
        try {
            GM_Position lShiftPosition = SWANDataConverterHelper.readCoordinateShiftValues(m_resultsFile);
            m_doubleShiftX = lShiftPosition.getX();
            m_doubleShiftY = lShiftPosition.getY();
            Map<String, MLArray> lMapData = lMatFileReader.getContent();
            // printDebugParsedSWANRawData( lMatFileReader, null );
            lRes = getValuesFormatedNameDatePosition(lMapData);
            // printDebugResultData( lRes, null );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return lRes;
}

From source file:org.kalypso.kalypsomodel1d2d.conv.wind.Primitives2GeoGridConverter.java

public IGeoGrid createGeoGrid() throws Exception {
    final File lOutputDir = FileUtils.toFile(m_urlOutputGridDirectory);
    if (!lOutputDir.exists() || !lOutputDir.isDirectory()) {
        return null;
    }//from ww w  . j  a va  2  s  . co m
    final File lNewFile = new File(lOutputDir, m_strFileName);
    if (lNewFile.exists()) {
        logger.warning("File with this name was found on disk: " + lNewFile.getAbsolutePath() //$NON-NLS-1$
                + "; this file will be overwritten."); //$NON-NLS-1$
        //      return null;
    }
    lNewFile.createNewFile();
    /*
     * use BinaryGeoGridWriter for better performance by writing the output files
     * RandomAccessFile lRandomAccessFile = new RandomAccessFile( lNewFile, "rw" );
     * String lStrCoordinateSystem = KalypsoDeegreePlugin.getDefault().getCoordinateSystem();
     * GM_Point lPointOrigin = m_gridDescriptor.getOrigin( lStrCoordinateSystem );
     * Coordinate lCoordinateOrigin = new Coordinate( lPointOrigin.getX(), lPointOrigin.getY(), lPointOrigin.getZ() );
     * Coordinate lCoordinateOffsetX = new Coordinate( m_gridDescriptor.getOffsetX().getGeoX(), m_gridDescriptor.getOffsetX().getGeoY() );
     * Coordinate lCoordinateOffsetY = new Coordinate( m_gridDescriptor.getOffsetY().getGeoX(), m_gridDescriptor.getOffsetY().getGeoY() );
     * BinaryGeoGridWrapperForPairsModel lGrid = new BinaryGeoGridWrapperForPairsModel( lRandomAccessFile, m_gridDescriptor.getNumColumns(), m_gridDescriptor.getNumRows(), m_intScale ,
     * lCoordinateOrigin, lCoordinateOffsetX, lCoordinateOffsetY, lStrCoordinateSystem, true );
     */
    final int xSize = m_gridDescriptor.getNumColumns() * 2;
    final int ySize = m_gridDescriptor.getNumRows();
    final BinaryGeoGridWriter lGrid = new BinaryGeoGridWriter(lNewFile.getAbsolutePath(), xSize, ySize,
            m_intScale);
    m_urlGeoGridDataFile = lNewFile.toURI().toURL();

    for (int j = 0; j < ySize; ++j) {
        for (int i = 0; i < xSize; i += 2) {
            lGrid.setValue(i, j, (Double) m_arrayData[i / 2][j].first);
            lGrid.setValue(i + 1, j, (Double) m_arrayData[i / 2][j].second);
        }
    }

    lGrid.dispose();
    return lGrid;
}

From source file:org.kalypso.kalypsomodel1d2d.sim.RMAKalypsoSimulation.java

private File findRma10skExe(final String version) throws CoreException {
    if (version == null || version.length() == 0)
        // REMARK: maybe could instead use a default or the one with the biggest version number?
        throw new CoreException(new Status(IStatus.ERROR, KalypsoModel1D2DPlugin.PLUGIN_ID,
                Messages.getString("org.kalypso.kalypsomodel1d2d.sim.RMA10Calculation.23"))); //$NON-NLS-1$

    // REMARK: This is OS dependent; we use should use a pattern according to OS
    final String exeName = ISimulation1D2DConstants.SIM_RMA10_EXE_FILE_PREFIX + version + ".exe"; //$NON-NLS-1$

    final Location installLocation = Platform.getInstallLocation();
    final File installDir = FileUtils.toFile(installLocation.getURL());
    final File exeDir = new File(installDir, "bin"); //$NON-NLS-1$
    final File exeFile = new File(exeDir, exeName);
    if (exeFile.exists())
        return exeFile;

    final String exeMissingMsg = String.format(
            Messages.getString("org.kalypso.kalypsomodel1d2d.sim.RMA10Calculation.26"), //$NON-NLS-1$
            exeFile.getAbsolutePath());//from   www .  j a v a  2 s.c o m
    throw new CoreException(new Status(IStatus.ERROR, KalypsoModel1D2DPlugin.PLUGIN_ID, exeMissingMsg));
}

From source file:org.kalypso.kalypsomodel1d2d.sim.SWANKalypsoSimulation.java

private File findSWANExe(final String exeVersionName) throws CoreException {
    // REMARK: This is OS dependent; we use should use a pattern according to OS
    String exeName = exeVersionName;
    if (exeVersionName == null || "".equals(exeVersionName.trim())) //$NON-NLS-1$
    {//from   ww w.j a v a2s.c o m
        exeName = ISimulation1D2DConstants.SIM_SWAN_EXE_FILE_PREFIX + ".exe"; //$NON-NLS-1$
    } else {
        exeName = ISimulation1D2DConstants.SIM_SWAN_EXE_FILE_PREFIX + exeVersionName + ".exe"; //$NON-NLS-1$

    }

    final Location installLocation = Platform.getInstallLocation();
    final File installDir = FileUtils.toFile(installLocation.getURL());
    final File exeDir = new File(installDir, "bin"); //$NON-NLS-1$
    final File exeFile = new File(exeDir, exeName);
    if (exeFile.exists())
        return exeFile;

    final String exeMissingMsg = String.format(
            Messages.getString("org.kalypso.kalypsomodel1d2d.sim.SWANCalculation.26"), //$NON-NLS-1$
            exeFile.getAbsolutePath());
    throw new CoreException(new Status(IStatus.ERROR, KalypsoModel1D2DPlugin.PLUGIN_ID, exeMissingMsg));
}

From source file:org.kalypso.kalypsosimulationmodel.core.wind.BinaryGeoGridWrapperForPairsModel.java

/**
 * Opens an existing grid.<br>/*from   www .  jav  a 2s . c  o m*/
 * Dispose the grid after it is no more needed in order to release the given resource.
 *
 * @param writeable
 *          If <code>true</code>, the grid is opened for writing and a {@link IWriteableGeoGrid} is returned.
 */
public static BinaryGeoGridWrapperForPairsModel openGrid(final URL url, final Coordinate origin,
        final Coordinate offsetX, final Coordinate offsetY, final String sourceCRS, final boolean writeable)
        throws IOException {
    /* Tries to find a file from the given url. */
    File fileFromUrl = ResourceUtilities.findJavaFileFromURL(url);
    File binFile = null;
    if (fileFromUrl == null)
        fileFromUrl = FileUtils.toFile(url);

    if (fileFromUrl == null) {
        /*
         * If url cannot be converted to a file, write its contents to a temporary file which will be deleted after the
         * grid gets disposed.
         */
        fileFromUrl = File.createTempFile("local", ".bin"); //$NON-NLS-1$ //$NON-NLS-2$
        fileFromUrl.deleteOnExit();
        FileUtils.copyURLToFile(url, fileFromUrl);
        binFile = fileFromUrl; // set in order to delete on dispose
    }

    FileChannel channel;
    if (writeable)
        channel = FileChannel.open(fileFromUrl.toPath(), StandardOpenOption.WRITE, StandardOpenOption.READ);
    else
        channel = FileChannel.open(fileFromUrl.toPath(), StandardOpenOption.READ);

    return new BinaryGeoGridWrapperForPairsModel(channel, binFile, origin, offsetX, offsetY, sourceCRS);
}

From source file:org.kalypso.kalypsosimulationmodel.ui.calccore.CalcCoreUtils.java

public static File getExecutablesDirectory() {
    final Location installLocation = Platform.getInstallLocation();
    final File installDir = FileUtils.toFile(installLocation.getURL());
    return new File(installDir, BIN_DIR);
}

From source file:org.kalypso.mapserver.servlets.CGIServlet.java

@Override
public String getInitParameter(final String name) {
    /* If the parameter has a value, always return it. */
    final String initParameter = super.getInitParameter(name);
    if (initParameter != null)
        return initParameter;

    /* If not (==null) return null, except if it is one of the following parameter. */
    if (!"cgibinResourceBase".equals(name) && !"Path".equals(name) && !"ENV_PROJ_LIB".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        return null;

    try {/*from w  ww  .  j  av a 2 s . c om*/
        final URL cgibinResourceBase = Platform.getInstallLocation().getURL();
        final File file = FileUtils.toFile(cgibinResourceBase);

        if (!"ENV_PROJ_LIB".equals(name)) //$NON-NLS-1$
            return file.toString();

        return new File(file, "cgi-bin").toString(); //$NON-NLS-1$
    } catch (final Exception ex) {
        ex.printStackTrace();
        return null;
    }
}