StringFileProvider.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.message;
00018 
00019 import com.google.common.base.Preconditions;
00020 import com.google.common.collect.ImmutableMap;
00021 import com.google.common.collect.Lists;
00022 import com.google.common.collect.Maps;
00023 import com.google.common.collect.Sets;
00024 
00025 import org.apache.commons.io.DirectoryWalker;
00026 import org.apache.commons.io.FileUtils;
00027 import org.apache.commons.io.filefilter.FileFilterUtils;
00028 import org.apache.commons.io.filefilter.IOFileFilter;
00029 import org.ros.exception.RosMessageRuntimeException;
00030 
00031 import java.io.File;
00032 import java.io.FileFilter;
00033 import java.io.IOException;
00034 import java.util.Collection;
00035 import java.util.Map;
00036 import java.util.NoSuchElementException;
00037 import java.util.Set;
00038 
00042 public class StringFileProvider {
00043 
00044   private final Collection<File> directories;
00045   private final Map<File, String> strings;
00046   private final StringFileDirectoryWalker stringFileDirectoryWalker;
00047 
00048   private final class StringFileDirectoryWalker extends DirectoryWalker {
00049     
00050     private final Set<File> directories;
00051 
00052     private StringFileDirectoryWalker(FileFilter filter, int depthLimit) {
00053       super(filter, depthLimit);
00054       directories = Sets.newHashSet();
00055     }
00056     
00057     // TODO(damonkohler): Update Apache Commons IO to the latest version.
00058     @SuppressWarnings("rawtypes")
00059     @Override
00060     protected boolean handleDirectory(File directory, int depth, Collection results)
00061         throws IOException {
00062       File canonicalDirectory = directory.getCanonicalFile();
00063       if (directories.contains(canonicalDirectory)) {
00064         return false;
00065       }
00066       directories.add(canonicalDirectory);
00067       return true;
00068     }
00069 
00070     @SuppressWarnings("rawtypes")
00071     @Override
00072     protected void handleFile(File file, int depth, Collection results) {
00073       String content;
00074       try {
00075         content = FileUtils.readFileToString(file, "US-ASCII");
00076       } catch (IOException e) {
00077         throw new RosMessageRuntimeException(e);
00078       }
00079       strings.put(file, content);
00080     }
00081 
00082     public void update(File directory) {
00083       try {
00084         walk(directory, null);
00085       } catch (IOException e) {
00086         throw new RosMessageRuntimeException(e);
00087       }
00088     }
00089   }
00090 
00091   public StringFileProvider(IOFileFilter ioFileFilter) {
00092     directories = Lists.newArrayList();
00093     strings = Maps.newConcurrentMap();
00094     IOFileFilter directoryFilter = FileFilterUtils.directoryFileFilter();
00095     FileFilter fileFilter = FileFilterUtils.orFileFilter(directoryFilter, ioFileFilter);
00096     stringFileDirectoryWalker = new StringFileDirectoryWalker(fileFilter, -1);
00097   }
00098 
00099   public void update() {
00100     for (File directory : directories) {
00101       stringFileDirectoryWalker.update(directory);
00102     }
00103   }
00104 
00111   public void addDirectory(File directory) {
00112     Preconditions.checkArgument(directory.isDirectory());
00113     directories.add(directory);
00114   }
00115 
00116   public Map<File, String> getStrings() {
00117     return ImmutableMap.copyOf(strings);
00118   }
00119 
00120   public String get(File file) {
00121     if (!has(file)) {
00122       throw new NoSuchElementException("File does not exist: " + file);
00123     }
00124     return strings.get(file);
00125   }
00126 
00127   public boolean has(File file) {
00128     return strings.containsKey(file);
00129   }
00130 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44