Example usage for org.apache.commons.vfs2 FileObject getParent

List of usage examples for org.apache.commons.vfs2 FileObject getParent

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getParent.

Prototype

FileObject getParent() throws FileSystemException;

Source Link

Document

Returns the folder that contains this file.

Usage

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

private void processSWANTabFile(final FileObject swanResOutTabFile, final FileObject swanResShiftFile) {
    final GM_Position lShiftPosition = SWANDataConverterHelper.readCoordinateShiftValues(swanResShiftFile);
    if (lShiftPosition == null) {
        return;//from  w  ww .  j a v  a  2s  .  c  om
    }
    try {
        if (swanResOutTabFile.isContentOpen()) {
            swanResOutTabFile.close();
        }
        final FileObject swanResOutTabFileBackUp = swanResOutTabFile.getParent()
                .resolveFile(swanResOutTabFile.getName().getBaseName() + ".bck"); //$NON-NLS-1$
        swanResOutTabFile.moveTo(swanResOutTabFileBackUp);

        // int lIntLinesCounter = 0;
        final OutputStream lOutStream = swanResOutTabFile.getContent().getOutputStream();
        final DataInputStream lInDataStream = new DataInputStream(
                swanResOutTabFileBackUp.getContent().getInputStream());
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(lInDataStream));
        final Formatter lFormatter = new Formatter(lOutStream, Charset.defaultCharset().name(), Locale.US);
        while (lInDataStream.available() != 0) {
            final String lStrTmpLine = streamReader.readLine().trim();
            // ++lIntLinesCounter;
            if (lStrTmpLine.startsWith("%")) { //$NON-NLS-1$
                lFormatter.format("%s\n", lStrTmpLine); //$NON-NLS-1$
                continue;
            }
            final StringTokenizer lStrTokenizer = new StringTokenizer(lStrTmpLine, " "); //$NON-NLS-1$
            int lIntTokenCounter = 0;
            String lStrNewLine = ""; //$NON-NLS-1$
            while (lStrTokenizer.hasMoreTokens()) {
                final String lStrToken = lStrTokenizer.nextToken();
                if (lIntTokenCounter == 1) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getX());
                } else if (lIntTokenCounter == 2) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getY());
                } else {
                    lStrNewLine += lStrToken + "\t"; //$NON-NLS-1$
                }
                lIntTokenCounter++;
            }
            lFormatter.format("%s\n", lStrNewLine); //$NON-NLS-1$

        }
        lFormatter.close();
        lInDataStream.close();
        lOutStream.close();
    } catch (final Exception e) {
        return;
    }

    return;
}

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

private IStatus processResultFile(final FileObject file, final ICalcUnitResultMeta calcUnitResultMeta,
        final IProgressMonitor monitor, final boolean doFullEvaluate) throws CoreException {
    try {/* www. ja  v  a 2  s  .co m*/
        final String filename = file.getName().getBaseName();

        if (ISimulation1D2DConstants.MODEL_2D.equals(filename))
            return Status.OK_STATUS;
        Date stepDate = null;
        String resultFileName = FileUtilities.nameWithoutExtension(filename);

        // check if the given result file is already compressed
        if (filename != null && filename.endsWith(".2d.zip")) //$NON-NLS-1$
        {
            resultFileName = filename;
            if (file.toString().startsWith(STEADY_PREFIX))
                stepDate = STEADY_DATE;
            else if (file.toString().startsWith(MAXI_PREFIX))
                stepDate = MAXI_DATE;
            else
                stepDate = ResultMeta1d2dHelper.resolveDateFromResultStep(file);

            if (m_resultDirSWAN == null) {
                final IPath lPath = ResultMeta1d2dHelper.getSavedPathFromResultData(calcUnitResultMeta,
                        ResultMeta1d2dHelper.SWAN_RAW_DATA_META_NAME);
                if (lPath != null && lPath.toFile().exists()) {
                    try {
                        m_resultDirSWAN = file.getParent().getParent().resolveFile(lPath.toOSString());
                    } catch (final Exception e) {
                        m_geoLog.formatLog(IStatus.INFO, CODE_RUNNING_FINE,
                                Messages.getString("org.kalypso.kalypsomodel1d2d.sim.ResultManager.15"), //$NON-NLS-1$
                                resultFileName);
                    }
                }
            }
        } else
            stepDate = findStepDate(file);

        if (stepDate == null)
            return Status.OK_STATUS;

        m_geoLog.formatLog(IStatus.INFO, CODE_RUNNING_FINE,
                Messages.getString("org.kalypso.kalypsomodel1d2d.sim.ResultManager.14"), resultFileName); //$NON-NLS-1$

        // start a job for each unknown 2d file.
        final String outDirName = NodeResultHelper.createOutDirName(stepDate);

        final File resultOutputDir = new File(m_outputDir, outDirName);
        resultOutputDir.mkdirs();
        final ProcessResult2DOperation processResultsJob = new ProcessResult2DOperation(file, m_resultDirSWAN,
                resultOutputDir, m_flowModel, m_controlModel, m_discModel, m_parameters, stepDate,
                calcUnitResultMeta, doFullEvaluate, m_geoLog);
        final IStatus result = processResultsJob.execute(monitor);

        m_minMaxCatcher.addNodeResultMinMaxCatcher(processResultsJob.getMinMaxData());

        // TODO: set this status as step result status?
        return result;
    } finally {
        ProgressUtilities.done(monitor);
    }
}

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

private FileObject findTelemacResultFile(final FileObject resultDir) {
    if (resultDir == null)
        return null;

    FileObject[] children = null;
    try {/*from  w w  w .j  a v  a2  s.c o  m*/
        children = resultDir.getChildren();
    } catch (FileSystemException e) {
        e.printStackTrace();
    }
    for (final FileObject child : children) {
        final FileName childName = child.getName();
        final String baseName = childName.getBaseName();
        if (FilenameUtils.wildcardMatch(baseName, "*res*.slf*")) //$NON-NLS-1$ 
        {
            return child;
        }
    }

    final IPath path = ResultMeta1d2dHelper.getSavedPathFromResultData(m_calcUnitMeta,
            ResultMeta1d2dHelper.TELEMAC_RAW_DATA_META_NAME);
    if (path != null) {
        try {
            return resultDir.getParent().resolveFile(path.toOSString());
        } catch (final Exception e) {
            m_geoLog.formatLog(IStatus.INFO, CODE_RUNNING_FINE,
                    Messages.getString("org.kalypso.kalypsomodel1d2d.sim.ResultManager.15"), //$NON-NLS-1$
                    resultDir.getName().getBaseName());
            return null;
        }
    }
    return null;
}

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

private void processSWANTabFile(final FileObject swanResOutTabFile, final FileObject swanResShiftFile) {
    final GM_Position lShiftPosition = SWANDataConverterHelper.readCoordinateShiftValues(swanResShiftFile);
    if (lShiftPosition == null)
        return;//from  w ww  .j av  a  2 s .  com

    try {
        // FIXME: why?! should never happen...!
        if (swanResOutTabFile.isContentOpen())
            swanResOutTabFile.close();

        final FileObject swanResOutTabFileBackUp = swanResOutTabFile.getParent()
                .resolveFile(swanResOutTabFile.getName().getBaseName() + ".bck"); //$NON-NLS-1$
        swanResOutTabFile.moveTo(swanResOutTabFileBackUp);

        final OutputStream lOutStream = swanResOutTabFile.getContent().getOutputStream();
        final Formatter lFormatter = new Formatter(lOutStream, Charset.defaultCharset().name(), Locale.US);

        final BufferedReader lInDataStream = new BufferedReader(
                new InputStreamReader(swanResOutTabFileBackUp.getContent().getInputStream()));

        while (lInDataStream.ready()) {
            final String lStrTmpLine = lInDataStream.readLine().trim();
            if (lStrTmpLine.startsWith("%")) //$NON-NLS-1$
            {
                lFormatter.format("%s\n", lStrTmpLine); //$NON-NLS-1$
                continue;
            }

            final StringTokenizer lStrTokenizer = new StringTokenizer(lStrTmpLine, " "); //$NON-NLS-1$
            int lIntTokenCounter = 0;
            String lStrNewLine = ""; //$NON-NLS-1$
            while (lStrTokenizer.hasMoreTokens()) {
                final String lStrToken = lStrTokenizer.nextToken();
                if (lIntTokenCounter == 1) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getX());
                } else if (lIntTokenCounter == 2) {
                    lStrNewLine += String.format(Locale.US, "%.5f\t", //$NON-NLS-1$
                            NumberUtils.parseQuietDouble(lStrToken) + lShiftPosition.getY());
                } else {
                    lStrNewLine += lStrToken + "\t"; //$NON-NLS-1$
                }
                lIntTokenCounter++;
            }
            lFormatter.format("%s\n", lStrNewLine); //$NON-NLS-1$
        }

        // FIXME: not closed in a save way!
        lFormatter.close();
        lInDataStream.close();
        lOutStream.close();
    } catch (final Exception e) {
        // FIXME: this is no way to handle an error !
    }
}

From source file:org.kalypso.project.database.server.ProjectDatabaseHelper.java

protected static Boolean removeBean(final Session session, final KalypsoProjectBean bean) {
    try {//  ww  w . java  2 s.c o m
        final FileSystemManager manager = VFSUtilities.getManager();

        /* remove file from server */
        // destination webdav url of bean
        final String urlBean = ProjectDatabaseHelper.resolveDestinationUrl(bean);
        final FileObject fileBean = manager.resolveFile(urlBean);
        if (fileBean.exists()) {
            fileBean.delete();
        }

        final FileObject parentFolder = fileBean.getParent();
        if (parentFolder.exists()) {
            parentFolder.delete();
        }

        /* delete database entry */
        final Transaction tx = session.beginTransaction();
        session.delete(bean);
        tx.commit();

        return true;
    } catch (final FileSystemException e) {
        KalypsoProjectDatabase.getDefault().getLog().log(StatusUtilities.statusFromThrowable(e));
    }

    return false;

}

From source file:org.mycore.datamodel.ifs2.MCRStore.java

/**
 * Deletes the data stored in the given file object from the store
 * /*from  www  .j av a2s .  c om*/
 * @param fo
 *            the file object to be deleted
 */
void delete(FileObject fo) throws IOException {
    FileObject parent = fo.getParent();
    fo.delete(Selectors.SELECT_ALL);

    while (!parent.equals(baseDirectory)) {
        final FileObject[] children = parent.getChildren();
        if (children.length > 0) {
            break;
        }
        fo = parent;
        parent = fo.getParent();
        fo.delete();
    }
}

From source file:org.obiba.opal.shell.commands.CopyCommand.java

/**
 * Resolves the output file based on the command parameter. Creates the necessary parent folders (when required).
 *
 * @return A FileObject representing the output file.
 * @throws FileSystemException/*from   www  .  ja  v  a 2s .c  o  m*/
 */
private FileObject resolveOutputFileAndCreateParentFolders() throws FileSystemException {
    FileObject outputFile;
    outputFile = getFile(options.getOut());

    // Create the parent directory, if it doesn't already exist.
    FileObject directory = outputFile.getParent();
    if (directory != null) {
        directory.createFolder();
    }

    if (Strings.isNullOrEmpty(outputFile.getName().getExtension())) {
        outputFile.createFolder();
    }

    if ("xls".equals(outputFile.getName().getExtension())) {
        getShell().printf(
                "WARNING: Writing to an Excel 97 spreadsheet. These are limited to 256 columns and 65536 rows "
                        + "which may not be sufficient for writing large tables.\nUse an 'xlsx' extension to use Excel 2007 format "
                        + "which supports 16K columns.\n");
    }
    return outputFile;
}

From source file:org.obiba.opal.web.FilesResource.java

@Nullable
private Response validateFile(FileObject file) throws FileSystemException {
    // Folder or file already exist at specified path.
    if (file.exists()) {
        return Response.status(Status.FORBIDDEN).entity("cannotCreateFolderPathAlreadyExist").build();
    }//from   w ww.  j a  v  a  2  s  .c o  m

    // Parent folder is read-only.
    if (!file.getParent().isWriteable()) {
        return Response.status(Status.FORBIDDEN).entity("cannotCreateFolderParentIsReadOnly").build();
    }

    return null;
}

From source file:org.obiba.opal.web.FilesResource.java

private void addFolder(FileObject folder, ZipOutputStream outputStream, Collection<String> children)
        throws IOException {
    addFolder(folder.getParent().getName().getPath(), folder, outputStream, children);
}

From source file:org.obiba.opal.web.FilesResourceTest.java

@Test
public void testCreateFolder_CannotCreateFolderParentIsReadOnly()
        throws FileSystemException, URISyntaxException {
    expect(fileObjectMock.getType()).andReturn(FileType.FOLDER).atLeastOnce();
    expect(fileObjectMock.exists()).andReturn(true).atLeastOnce();
    FileObject childFolderMock = createMock(FileObject.class);
    expect(childFolderMock.exists()).andReturn(false).atLeastOnce();
    FileObject parentFolderMock = createMock(FileObject.class);
    expect(childFolderMock.getParent()).andReturn(parentFolderMock).atLeastOnce();
    expect(parentFolderMock.isWriteable()).andReturn(false).atLeastOnce();
    expect(fileObjectMock.resolveFile("folder")).andReturn(childFolderMock).atLeastOnce();

    replay(fileObjectMock, parentFolderMock, childFolderMock);

    Response response = getFileResource().createFolder("folder1", "folder", uriInfoMock);
    assertThat(response.getStatus()).isEqualTo(Status.FORBIDDEN.getStatusCode());
    assertThat(response.getEntity()).isEqualTo("cannotCreateFolderParentIsReadOnly");
    verify(fileObjectMock, parentFolderMock, childFolderMock);
}