当前位置: 首页>>代码示例>>Java>>正文


Java FileUtil.normalizePath方法代码示例

本文整理汇总了Java中org.apache.camel.util.FileUtil.normalizePath方法的典型用法代码示例。如果您正苦于以下问题:Java FileUtil.normalizePath方法的具体用法?Java FileUtil.normalizePath怎么用?Java FileUtil.normalizePath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.camel.util.FileUtil的用法示例。


在下文中一共展示了FileUtil.normalizePath方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: configureMessage

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
/**
 * Configures the given message with the file which sets the body to the
 * file object.
 */
public void configureMessage(GenericFile<T> file, Message message) {
    message.setBody(file);

    if (flatten) {
        // when flatten the file name should not contain any paths
        message.setHeader(Exchange.FILE_NAME, file.getFileNameOnly());
    } else {
        // compute name to set on header that should be relative to starting directory
        String name = file.isAbsolute() ? file.getAbsoluteFilePath() : file.getRelativeFilePath();

        // skip leading endpoint configured directory
        String endpointPath = getConfiguration().getDirectory() + getFileSeparator();

        // need to normalize paths to ensure we can match using startsWith
        endpointPath = FileUtil.normalizePath(endpointPath);
        String copyOfName = FileUtil.normalizePath(name);
        if (ObjectHelper.isNotEmpty(endpointPath) && copyOfName.startsWith(endpointPath)) {
            name = name.substring(endpointPath.length());
        }

        // adjust filename
        message.setHeader(Exchange.FILE_NAME, name);
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:29,代码来源:GenericFileEndpoint.java

示例2: setUp

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected void setUp() throws Exception {
    deleteDirectory("target/fileidempotent");
    createDirectory("target/fileidempotent");

    File file = new File("target/fileidempotent/.filestore.dat");
    FileOutputStream fos = new FileOutputStream(file);

    // insert existing name to the file repo, so we should skip this file
    String name = FileUtil.normalizePath(new File("target/fileidempotent/report.txt").getAbsolutePath());
    fos.write(name.getBytes());
    fos.write(LS.getBytes());

    fos.close();

    super.setUp();

    // add a file to the repo
    repo = context.getRegistry().lookupByNameAndType("fileStore", IdempotentRepository.class);
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:22,代码来源:FileConsumerIdempotentLoadStoreTest.java

示例3: testIdempotentLoad

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
public void testIdempotentLoad() throws Exception {
    // send two files (report.txt exists already in idempotent repo)
    template.sendBodyAndHeader("file://target/fileidempotent/", "Hello World", Exchange.FILE_NAME, "report.txt");
    template.sendBodyAndHeader("file://target/fileidempotent/", "Bye World", Exchange.FILE_NAME, "report2.txt");

    // consume the file the first time
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Bye World");

    assertMockEndpointsSatisfied();
    // wait for the exchange to be done, as it only append to idempotent repo after success
    oneExchangeDone.matchesMockWaitTime();

    String name = FileUtil.normalizePath(new File("target/fileidempotent/report.txt").getAbsolutePath());
    assertTrue("Should contain file: " + name, repo.contains(name));

    String name2 = FileUtil.normalizePath(new File("target/fileidempotent/report2.txt").getAbsolutePath());
    assertTrue("Should contain file: " + name2, repo.contains(name2));
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:20,代码来源:FileConsumerIdempotentLoadStoreTest.java

示例4: expectedFileExists

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
/**
 * Adds an expectation that a file exists with the given name
 * <p/>
 * Will wait at most 5 seconds while checking for the existence of the file.
 *
 * @param name name of file, will cater for / and \ on different OS platforms
 * @param content content of file to compare, can be <tt>null</tt> to not compare content
 */
public void expectedFileExists(final String name, final String content) {
    final File file = new File(FileUtil.normalizePath(name));

    expects(new Runnable() {
        public void run() {
            // wait at most 5 seconds for the file to exists
            final long timeout = System.currentTimeMillis() + 5000;

            boolean stop = false;
            while (!stop && !file.exists()) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // ignore
                }
                stop = System.currentTimeMillis() > timeout;
            }

            assertTrue("The file should exists: " + name, file.exists());

            if (content != null) {
                String body = getCamelContext().getTypeConverter().convertTo(String.class, file);
                assertEquals("Content of file: " + name, content, body);
            }
        }
    });
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:36,代码来源:MockEndpoint.java

示例5: writeFile

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
public void writeFile(Exchange exchange, String fileName) throws GenericFileOperationFailedException {
    // build directory if auto create is enabled
    if (endpoint.isAutoCreate()) {
        // we must normalize it (to avoid having both \ and / in the name which confuses java.io.File)
        String name = FileUtil.normalizePath(fileName);

        // use java.io.File to compute the file path
        File file = new File(name);
        String directory = file.getParent();
        boolean absolute = FileUtil.isAbsolute(file);
        if (directory != null) {
            if (!operations.buildDirectory(directory, absolute)) {
                log.debug("Cannot build directory [{}] (could be because of denied permissions)", directory);
            }
        }
    }

    // upload
    if (log.isTraceEnabled()) {
        log.trace("About to write [{}] to [{}] from exchange [{}]", new Object[]{fileName, getEndpoint(), exchange});
    }

    boolean success = operations.storeFile(fileName, exchange);
    if (!success) {
        throw new GenericFileOperationFailedException("Error writing file [" + fileName + "]");
    }
    log.debug("Wrote [{}] to [{}]", fileName, getEndpoint());
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:29,代码来源:GenericFileProducer.java

示例6: setDirectory

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
public void setDirectory(String directory) {
    this.directory = needToNormalize()
        // must normalize path to cater for Windows and other OS
        ? FileUtil.normalizePath(directory)
        // for the remote directory we don't need to do that
        : directory;

    // endpoint directory must not be null
    if (this.directory == null) {
        this.directory = "";
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:13,代码来源:GenericFileConfiguration.java

示例7: createDoneFileName

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
/**
 * Creates the associated name of the done file based on the given file name.
 * <p/>
 * This method should only be invoked if a done filename property has been set on this endpoint.
 *
 * @param fileName the file name
 * @return name of the associated done file name
 */
protected String createDoneFileName(String fileName) {
    String pattern = getDoneFileName();
    ObjectHelper.notEmpty(pattern, "doneFileName", pattern);

    // we only support ${file:name} or ${file:name.noext} as dynamic placeholders for done files
    String path = FileUtil.onlyPath(fileName);
    String onlyName = FileUtil.stripPath(fileName);

    pattern = pattern.replaceFirst("\\$\\{file:name\\}", onlyName);
    pattern = pattern.replaceFirst("\\$simple\\{file:name\\}", onlyName);
    pattern = pattern.replaceFirst("\\$\\{file:name.noext\\}", FileUtil.stripExt(onlyName));
    pattern = pattern.replaceFirst("\\$simple\\{file:name.noext\\}", FileUtil.stripExt(onlyName));

    // must be able to resolve all placeholders supported
    if (StringHelper.hasStartToken(pattern, "simple")) {
        throw new ExpressionIllegalSyntaxException(fileName + ". Cannot resolve reminder: " + pattern);
    }

    String answer = pattern;
    if (ObjectHelper.isNotEmpty(path) && ObjectHelper.isNotEmpty(pattern)) {
        // done file must always be in same directory as the real file name
        answer = path + getFileSeparator() + pattern;
    }

    if (getConfiguration().needToNormalize()) {
        // must normalize path to cater for Windows and other OS
        answer = FileUtil.normalizePath(answer);
    }

    return answer;
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:40,代码来源:GenericFileEndpoint.java

示例8: testIdempotent

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
public void testIdempotent() throws Exception {
    // send a file
    template.sendBodyAndHeader("file://target/fileidempotent/", "Hello World", Exchange.FILE_NAME, "report.txt");

    // consume the file the first time
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);

    assertMockEndpointsSatisfied();

    // reset mock and set new expectations
    mock.reset();
    mock.expectedMessageCount(0);

    // move file back
    File file = new File("target/fileidempotent/done/report.txt");
    File renamed = new File("target/fileidempotent/report.txt");
    file.renameTo(renamed);

    // sleep to let the consumer try to poll the file
    Thread.sleep(2000);

    // should NOT consume the file again, let 2 secs pass to let the consumer try to consume it but it should not
    assertMockEndpointsSatisfied();

    String name = FileUtil.normalizePath(new File("target/fileidempotent/report.txt").getAbsolutePath());
    assertTrue("Should contain file: " + name, repo.contains(name));
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:29,代码来源:FileConsumerIdempotentTest.java

示例9: normalizePath

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
protected String normalizePath(String name) {
    return FileUtil.normalizePath(name);
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:4,代码来源:GenericFile.java

示例10: changeFileName

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
/**
 * Changes the name of this remote file. This method alters the absolute and
 * relative names as well.
 *
 * @param newName the new name
 */
public void changeFileName(String newName) {
    LOG.trace("Changing name to: {}", newName);

    // Make sure the names is normalized.
    String newFileName = FileUtil.normalizePath(newName);
    String newEndpointPath = FileUtil.normalizePath(endpointPath.endsWith("" + File.separatorChar) ? endpointPath : endpointPath + File.separatorChar);

    LOG.trace("Normalized endpointPath: {}", newEndpointPath);
    LOG.trace("Normalized newFileName: ()", newFileName);

    File file = new File(newFileName);
    if (!absolute) {
        // for relative then we should avoid having the endpoint path duplicated so clip it
        if (ObjectHelper.isNotEmpty(newEndpointPath) && newFileName.startsWith(newEndpointPath)) {
            // clip starting endpoint in case it was added
            // use File.separatorChar as the normalizePath uses this as path separator so we should use the same
            // in this logic here
            if (newEndpointPath.endsWith("" + File.separatorChar)) {
                newFileName = ObjectHelper.after(newFileName, newEndpointPath);
            } else {
                newFileName = ObjectHelper.after(newFileName, newEndpointPath + File.separatorChar);
            }

            // reconstruct file with clipped name
            file = new File(newFileName);
        }
    }

    // store the file name only
    setFileNameOnly(file.getName());
    setFileName(file.getName());

    // relative path
    if (file.getParent() != null) {
        setRelativeFilePath(file.getParent() + getFileSeparator() + file.getName());
    } else {
        setRelativeFilePath(file.getName());
    }

    // absolute path
    if (isAbsolute(newFileName)) {
        setAbsolute(true);
        setAbsoluteFilePath(newFileName);
    } else {
        setAbsolute(false);
        // construct a pseudo absolute filename that the file operations uses even for relative only
        String path = ObjectHelper.isEmpty(endpointPath) ? "" : endpointPath + getFileSeparator();
        setAbsoluteFilePath(path + getRelativeFilePath());
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("FileNameOnly: {}", getFileNameOnly());
        LOG.trace("FileName: {}", getFileName());
        LOG.trace("Absolute: {}", isAbsolute());
        LOG.trace("Relative path: {}", getRelativeFilePath());
        LOG.trace("Absolute path: {}", getAbsoluteFilePath());
        LOG.trace("Name changed to: {}", this);
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:66,代码来源:GenericFile.java

示例11: doMoveExistingFile

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
/**
 * Moves any existing file due fileExists=Move is in use.
 */
private void doMoveExistingFile(String fileName) throws GenericFileOperationFailedException {
    // need to evaluate using a dummy and simulate the file first, to have access to all the file attributes
    // create a dummy exchange as Exchange is needed for expression evaluation
    // we support only the following 3 tokens.
    Exchange dummy = endpoint.createExchange();
    String parent = FileUtil.onlyPath(fileName);
    String onlyName = FileUtil.stripPath(fileName);
    dummy.getIn().setHeader(Exchange.FILE_NAME, fileName);
    dummy.getIn().setHeader(Exchange.FILE_NAME_ONLY, onlyName);
    dummy.getIn().setHeader(Exchange.FILE_PARENT, parent);

    String to = endpoint.getMoveExisting().evaluate(dummy, String.class);
    // we must normalize it (to avoid having both \ and / in the name which confuses java.io.File)
    to = FileUtil.normalizePath(to);
    if (ObjectHelper.isEmpty(to)) {
        throw new GenericFileOperationFailedException("moveExisting evaluated as empty String, cannot move existing file: " + fileName);
    }

    // ensure any paths is created before we rename as the renamed file may be in a different path (which may be non exiting)
    // use java.io.File to compute the file path
    File toFile = new File(to);
    String directory = toFile.getParent();
    boolean absolute = FileUtil.isAbsolute(toFile);
    if (directory != null) {
        if (!buildDirectory(directory, absolute)) {
            LOG.debug("Cannot build directory [{}] (could be because of denied permissions)", directory);
        }
    }

    // deal if there already exists a file
    if (existsFile(to)) {
        if (endpoint.isEagerDeleteTargetFile()) {
            LOG.trace("Deleting existing file: {}", to);
            if (!deleteFile(to)) {
                throw new GenericFileOperationFailedException("Cannot delete file: " + to);
            }
        } else {
            throw new GenericFileOperationFailedException("Cannot moved existing file from: " + fileName + " to: " + to + " as there already exists a file: " + to);
        }
    }

    LOG.trace("Moving existing file: {} to: {}", fileName, to);
    if (!renameFile(fileName, to)) {
        throw new GenericFileOperationFailedException("Cannot rename file from: " + fileName + " to: " + to);
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:50,代码来源:FileOperations.java

示例12: normalizePath

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
public String normalizePath(String name) {
    return FileUtil.normalizePath(name);
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:4,代码来源:GenericFileProducer.java

示例13: acceptFile

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private static boolean acceptFile(File file, String endpointPath, GenericFileFilter filter, GenericFileFilter antFilter,
                                  Pattern excludePattern, Pattern includePattern) {
    GenericFile gf = new GenericFile();
    gf.setEndpointPath(endpointPath);
    gf.setFile(file);
    gf.setFileNameOnly(file.getName());
    gf.setFileLength(file.length());
    gf.setDirectory(file.isDirectory());
    // must use FileUtil.isAbsolute to have consistent check for whether the file is
    // absolute or not. As windows do not consider \ paths as absolute where as all
    // other OS platforms will consider \ as absolute. The logic in Camel mandates
    // that we align this for all OS. That is why we must use FileUtil.isAbsolute
    // to return a consistent answer for all OS platforms.
    gf.setAbsolute(FileUtil.isAbsolute(file));
    gf.setAbsoluteFilePath(file.getAbsolutePath());
    gf.setLastModified(file.lastModified());

    // compute the file path as relative to the starting directory
    File path;
    String endpointNormalized = FileUtil.normalizePath(endpointPath);
    if (file.getPath().startsWith(endpointNormalized + File.separator)) {
        // skip duplicate endpoint path
        path = new File(ObjectHelper.after(file.getPath(), endpointNormalized + File.separator));
    } else {
        path = new File(file.getPath());
    }

    if (path.getParent() != null) {
        gf.setRelativeFilePath(path.getParent() + File.separator + file.getName());
    } else {
        gf.setRelativeFilePath(path.getName());
    }

    // the file name should be the relative path
    gf.setFileName(gf.getRelativeFilePath());

    if (filter != null) {
        if (!filter.accept(gf)) {
            return false;
        }
    }

    if (antFilter != null) {
        if (!antFilter.accept(gf)) {
            return false;
        }
    }

    // exclude take precedence over include
    if (excludePattern != null)  {
        if (excludePattern.matcher(file.getName()).matches()) {
            return false;
        }
    }
    if (includePattern != null)  {
        if (!includePattern.matcher(file.getName()).matches()) {
            return false;
        }
    }

    return true;
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:64,代码来源:MarkerFileExclusiveReadLockStrategy.java

示例14: asGenericFile

import org.apache.camel.util.FileUtil; //导入方法依赖的package包/类
/**
 * Creates a new GenericFile<File> based on the given file.
 *
 * @param endpointPath the starting directory the endpoint was configured with
 * @param file the source file
 * @param probeContentType whether to probe the content type of the file or not
 * @return wrapped as a GenericFile
 */
public static GenericFile<File> asGenericFile(String endpointPath, File file, String charset, boolean probeContentType) {
    GenericFile<File> answer = new GenericFile<File>(probeContentType);
    // use file specific binding
    answer.setBinding(new FileBinding());

    answer.setCharset(charset);
    answer.setEndpointPath(endpointPath);
    answer.setFile(file);
    answer.setFileNameOnly(file.getName());
    answer.setFileLength(file.length());
    answer.setDirectory(file.isDirectory());
    // must use FileUtil.isAbsolute to have consistent check for whether the file is
    // absolute or not. As windows do not consider \ paths as absolute where as all
    // other OS platforms will consider \ as absolute. The logic in Camel mandates
    // that we align this for all OS. That is why we must use FileUtil.isAbsolute
    // to return a consistent answer for all OS platforms.
    answer.setAbsolute(FileUtil.isAbsolute(file));
    answer.setAbsoluteFilePath(file.getAbsolutePath());
    answer.setLastModified(file.lastModified());

    // compute the file path as relative to the starting directory
    File path;
    String endpointNormalized = FileUtil.normalizePath(endpointPath);
    if (file.getPath().startsWith(endpointNormalized + File.separator)) {
        // skip duplicate endpoint path
        path = new File(ObjectHelper.after(file.getPath(), endpointNormalized + File.separator));
    } else {
        path = new File(file.getPath());
    }

    if (path.getParent() != null) {
        answer.setRelativeFilePath(path.getParent() + File.separator + file.getName());
    } else {
        answer.setRelativeFilePath(path.getName());
    }

    // the file name should be the relative path
    answer.setFileName(answer.getRelativeFilePath());

    // use file as body as we have converters if needed as stream
    answer.setBody(file);
    return answer;
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:52,代码来源:FileConsumer.java


注:本文中的org.apache.camel.util.FileUtil.normalizePath方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。