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:com.google.code.docbook4j.renderer.FORenderer.java

@Override
protected FileObject postProcess(final FileObject xmlSource, final FileObject xslSource,
        final FileObject xsltResult, final FileObject userConfigXml) throws Docbook4JException {

    FileObject target = null;//from  w w w .  j ava 2 s .c  o  m
    try {

        final FopFactory fopFactory = FopFactory.newInstance();

        final FOUserAgent userAgent = fopFactory.newFOUserAgent();
        userAgent.setBaseURL(xmlSource.getParent().getURL().toExternalForm());
        userAgent.setURIResolver(new VfsURIResolver());

        enhanceFOUserAgent(userAgent);

        String tmpPdf = "tmp://" + UUID.randomUUID().toString();
        target = FileObjectUtils.resolveFile(tmpPdf);
        target.createFile();

        Configuration configuration = createFOPConfig(userConfigXml);
        if (configuration != null) {
            fopFactory.setUserConfig(configuration);
            fopFactory.setBaseURL(userConfigXml.getParent().getURL().toExternalForm());
            fopFactory.setFontBaseURL(userConfigXml.getParent().getURL().toExternalForm());
        }

        Fop fop = fopFactory.newFop(getMimeType(), userAgent, target.getContent().getOutputStream());

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity
        // transformer
        transformer.setParameter("use.extensions", "1");
        transformer.setParameter("fop.extensions", "0");
        transformer.setParameter("fop1.extensions", "1");

        Source src = new StreamSource(xsltResult.getContent().getInputStream());
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(src, res);
        return target;

    } catch (FileSystemException e) {
        throw new Docbook4JException("Error create filesystem manager!", e);
    } catch (TransformerException e) {
        throw new Docbook4JException("Error transforming fo to pdf!", e);
    } catch (FOPException e) {
        throw new Docbook4JException("Error transforming fo to pdf!", e);
    } catch (ConfigurationException e) {
        throw new Docbook4JException("Error loading user configuration!", e);
    } catch (SAXException e) {
        throw new Docbook4JException("Error loading user configuration!", e);
    } catch (IOException e) {
        throw new Docbook4JException("Error loading user configuration!", e);
    } finally {
        FileObjectUtils.closeFileObjectQuietly(target);
    }

}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignSource.java

private OverlayData loadOverlayData(FileObject syncInfo) {

    try {/*w  w w  .jav a2 s  .c o  m*/
        if (syncInfo == null || !syncInfo.exists()) {
            return null;
        }

        FileObject overlayData = syncInfo.getParent().resolveFile(SystemContainerManager.OVERLAY_DATA_PATH);
        if (overlayData.exists()) {
            return OverlayData.load(overlayData);
        } else {
            return null;
        }
    } catch (Exception e) {
        _core.getLog().error("Exception loading overlay data", e);
        return null;
    }
}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignSource.java

private CSConfig loadConfig(FileObject syncInfo) {

    try {//from ww w . j  a  v a 2 s  .c  o m
        if (syncInfo == null || !syncInfo.exists()) {
            return new CSConfig();
        }

        FileObject csConfig = syncInfo.getParent().resolveFile(SystemContainerManager.CSCONFIG_PATH);
        if (csConfig.exists()) {
            return CSConfig.load(csConfig);
        } else {
            return null;
        }
    } catch (Exception e) {
        _core.getLog().error("Exception loading design config", e);
        return null;
    }
}

From source file:com.flicklib.folderscanner.AdvancedFolderScanner.java

/**
 * Handle the one directory with several different movies case.
 * @param files/*from w  ww. j  ava 2s. c  om*/
 * @return true, if a movie found
 * @throws FileSystemException 
 */
private boolean genericMovieFindProcess(FileObject[] files) throws FileSystemException {
    Map<String, FileGroup> foundMovies = new HashMap<String, FileGroup>();
    for (FileObject f : files) {
        if (!f.getType().hasChildren()) {
            String extension = getExtension(f);
            if (MovieFileFilter.VIDEO_EXT_EXTENSIONS.contains(extension)) {
                String baseName = movieNameExtractor.removeCrap(f);
                FileGroup m = foundMovies.get(baseName);
                if (m == null) {
                    m = new FileGroup();
                    m.setTitle(baseName);
                    m.setAudioLanguage(movieNameExtractor.getLanguageSuggestion(f));
                    m.getLocations().add(new FileLocation(currentLabel, f.getParent().getURL()));
                    foundMovies.put(baseName, m);
                }
                m.getFiles().add(createFileMeta(f, MovieFileType.getTypeByExtension(extension)));
            }
        }
    }
    boolean foundMovie = false;
    for (FileGroup m : foundMovies.values()) {
        if (m.isValid()) {
            add(m);
            foundMovie = true;
        }
    }
    return foundMovie;
}

From source file:com.google.code.docbook4j.renderer.BaseRenderer.java

protected Transformer createTransformer(FileObject xmlSource, FileObject xslStylesheet)
        throws TransformerConfigurationException, IOException {

    TransformerFactory transformerFactory = createTransformerFactory();
    if (xslStylesheet != null) {
        transformerFactory.setURIResolver(new XslURIResolver());
    }/*w ww  . j a v a 2s.com*/
    FileObject xsl = xslStylesheet != null ? xslStylesheet : getDefaultXslStylesheet();

    Source source = new StreamSource(xsl.getContent().getInputStream(), xsl.getURL().toExternalForm());
    Transformer transformer = transformerFactory.newTransformer(source);

    transformer.setParameter("use.extensions", "1");
    transformer.setParameter("callout.graphics", "0");
    transformer.setParameter("callout.unicode", "1");
    transformer.setParameter("callouts.extension", "1");
    transformer.setParameter("base.dir", xmlSource.getParent().getURL().toExternalForm());

    for (Map.Entry<String, String> entry : this.params.entrySet()) {
        transformer.setParameter(entry.getKey(), entry.getValue());
    }

    return transformer;
}

From source file:com.sshtools.appframework.ui.IconStore.java

public void addThemeJar(String themeName) throws IOException {
    FileObject obj = null;
    try {/*from   w ww .  j  a v  a2s . co m*/
        obj = VFS.getManager().resolveFile("res:" + themeName + "/index.theme");
    } catch (Exception e) {
        URL loc = getClass().getClassLoader().getResource(themeName + "/index.theme");
        try {
            String sloc = loc.toURI().toString();
            if (sloc.startsWith("jar:file:/") || !sloc.startsWith("jar:file://")) {
                sloc = "jar:jar:/" + System.getProperty("user.dir") + sloc.substring(9);
                FileObject resolveFile = VFS.getManager().resolveFile(System.getProperty("user.dir"));
                obj = VFS.getManager().resolveFile(resolveFile, sloc);
            } else {
                obj = VFS.getManager().resolveFile(sloc);

            }
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }
    }
    if (obj != null) {
        iconService.addBase(obj.getParent().getParent());
    }
}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Refresh ftp cache./* w ww.jav  a  2 s. c om*/
 * 
 * @param fileObject the file object
 * @throws FileSystemException
 */
public void refreshFtpCache(FileObject fileObject) throws FileSystemException {

    if (fileObject == null) {
        return;
    }

    if (!(fileObject.getFileSystem() instanceof FtpFileSystem)) {
        return;
    }
    if (fileObject.exists()) {
        return;
    }
    FileObject fileObjectParent = fileObject.getParent();
    if (fileObjectParent == null) {
        return;
    }

    fileObjectParent.getType(); // force to attach : needed for force refresh
    fileObjectParent.refresh();

}

From source file:net.sf.jabb.web.action.VfsTreeAction.java

/**
 * AJAX tree functions//from w w w.ja  va 2s .  c o m
 */
public String execute() {
    normalizeTreeRequest();
    JsTreeResult result = new JsTreeResult();

    final AllFileSelector ALL_FILES = new AllFileSelector();
    FileSystemManager fsManager = null;
    FileObject rootFile = null;
    FileObject file = null;
    FileObject referenceFile = null;
    try {
        fsManager = VfsUtility.getManager();
        rootFile = fsManager.resolveFile(rootPath, fsOptions);

        if (JsTreeRequest.OP_GET_CHILDREN.equalsIgnoreCase(requestData.getOperation())) {
            String parentPath = requestData.getId();
            List<JsTreeNodeData> nodes = null;
            try {
                nodes = getChildNodes(rootFile, parentPath);
                if (requestData.isRoot() && rootNodeName != null) { // add root node
                    JsTreeNodeData rootNode = new JsTreeNodeData();
                    rootNode.setData(rootNodeName);
                    Map<String, Object> attr = new HashMap<String, Object>();
                    rootNode.setAttr(attr);
                    attr.put("id", ".");
                    attr.put("rel", "root");
                    attr.put("fileType", FileType.FOLDER.toString());
                    rootNode.setChildren(nodes);
                    rootNode.setState(JsTreeNodeData.STATE_OPEN);
                    nodes = new LinkedList<JsTreeNodeData>();
                    nodes.add(rootNode);
                }
            } catch (Exception e) {
                log.error("Cannot get child nodes for: " + parentPath, e);
                nodes = new LinkedList<JsTreeNodeData>();
            }
            responseData = nodes;
        } else if (JsTreeRequest.OP_REMOVE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String path = requestData.getId();
            try {
                file = rootFile.resolveFile(path, NameScope.DESCENDENT);
                boolean wasDeleted = false;
                if (file.getType() == FileType.FILE) {
                    wasDeleted = file.delete();
                } else {
                    wasDeleted = file.delete(ALL_FILES) > 0;
                }
                result.setStatus(wasDeleted);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot delete: " + path, e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_CREATE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String parentPath = requestData.getReferenceId();
            String name = requestData.getTitle();
            try {
                referenceFile = rootFile.resolveFile(parentPath, NameScope.DESCENDENT_OR_SELF);
                file = referenceFile.resolveFile(name, NameScope.CHILD);
                file.createFolder();
                result.setStatus(true);
                result.setId(rootFile.getName().getRelativeName(file.getName()));
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot create folder '" + name + "' under '" + parentPath + "'", e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_RENAME_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String path = requestData.getId();
            String name = requestData.getTitle();
            try {
                referenceFile = rootFile.resolveFile(path, NameScope.DESCENDENT);
                file = referenceFile.getParent().resolveFile(name, NameScope.CHILD);
                referenceFile.moveTo(file);
                result.setStatus(true);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot rename '" + path + "' to '" + name + "'", e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_MOVE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String newParentPath = requestData.getReferenceId();
            String originalPath = requestData.getId();
            try {
                referenceFile = rootFile.resolveFile(originalPath, NameScope.DESCENDENT);
                file = rootFile.resolveFile(newParentPath, NameScope.DESCENDENT_OR_SELF)
                        .resolveFile(referenceFile.getName().getBaseName(), NameScope.CHILD);
                if (requestData.isCopy()) {
                    file.copyFrom(referenceFile, ALL_FILES);
                } else {
                    referenceFile.moveTo(file);
                }
                result.setStatus(true);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot move '" + originalPath + "' to '" + newParentPath + "'", e);
            }
            responseData = result;
        }
    } catch (FileSystemException e) {
        log.error("Cannot perform file operation.", e);
    } finally {
        VfsUtility.close(fsManager, file, referenceFile, rootFile);
    }
    return SUCCESS;
}

From source file:org.aludratest.service.file.impl.AbstractFileAction.java

protected void getOrCreateDirectory(FileObject directory) {
    FileObject root = configuration.getRootFolder();
    if (!root.equals(directory)) {
        try {//ww  w.j  a va 2  s. co  m
            getOrCreateDirectory(directory.getParent());
            directory.createFolder();
        } catch (IOException e) {
            throw new TechnicalException("Error creating directory", e);
        }
    }
}

From source file:org.aludratest.service.file.impl.FileActionImpl.java

/** Renames or moves a file or folder.
 * @param fromPath the file/folder to rename/move
 * @param toPath the new name/location of the file/folder
 * @param overwrite flag which indicates if an existing file may be overwritten by the operation
 * @return true if a formerly existing file was overwritten.
 * @throws FilePresentException if a file was already present and overwriting was disabled. */
@Override/* w  w w .j a  v a2 s .c o  m*/
public boolean move(String fromPath, String toPath, boolean overwrite) {
    assertWritingPermitted("move()");
    FileUtil.verifyFilePath(fromPath);
    FileUtil.verifyFilePath(toPath);
    FileObject target = getFileObject(toPath);
    boolean existedBefore = checkWritable(target, overwrite);
    try {
        getOrCreateDirectory(target.getParent());
        getFileObject(fromPath).moveTo(target);
        LOGGER.info("Moved {} to {}", fromPath, toPath);
        return existedBefore;
    } catch (FileSystemException e) {
        throw new TechnicalException("Error moving file" + fromPath + " -> " + toPath, e);
    }
}