Esempio n. 1
1
  public void writeTasks(OutputStream output) {
    try {
      output.write("[\n".getBytes());
      boolean needComma = false;
      File[] files = tasksDirectory.listFiles((FileFilter) FileFilterUtils.directoryFileFilter());
      List<Integer> numbers = new ArrayList<>();
      for (File directory : files) {
        try {
          numbers.add(Integer.valueOf(directory.getName()));
        } catch (Throwable ignored) {

        }
      }
      numbers.sort(Comparator.<Integer>reverseOrder());
      numbers = numbers.subList(0, Math.min(100, numbers.size()));
      for (int taskId : numbers) {
        File infoFile = new File(new File(tasksDirectory, String.valueOf(taskId)), "info.json");
        if (!infoFile.exists()) {
          continue;
        }
        if (needComma) {
          output.write(",\n".getBytes());
        } else {
          needComma = true;
        }
        try (FileInputStream fis = new FileInputStream(infoFile)) {
          IOUtils.copy(fis, output);
        }
      }
      output.write("]\n".getBytes());
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  private void testProcessLeftBelowFilesAllClean() throws Exception {
    final HadoopWriterFactory factory = new NoWriteHadoopWriterFactory(null, config);

    FileUtils.touch(new File(lockDirectory.getPath() + "/some_file_which_should_be_sent_1"));
    FileUtils.touch(new File(lockDirectory.getPath() + "/some_file_which_should_be_sent_2"));
    FileUtils.touch(
        new File(quarantineDirectory.getPath() + "/some_other_file_which_should_be_sent"));

    Assert.assertEquals(
        FileUtils.listFiles(
                spoolDirectory, FileFilterUtils.trueFileFilter(), FileFilterUtils.trueFileFilter())
            .size(),
        3);
    Assert.assertTrue(spoolDirectory.exists());
    Assert.assertTrue(tmpDirectory.exists());
    Assert.assertTrue(lockDirectory.exists());
    Assert.assertTrue(quarantineDirectory.exists());

    Thread.sleep(2 * CUTOFF_TIME);
    factory.processLeftBelowFiles();

    // All files should have been sent
    Assert.assertFalse(spoolDirectory.exists());
    Assert.assertFalse(tmpDirectory.exists());
    Assert.assertFalse(lockDirectory.exists());
    Assert.assertFalse(quarantineDirectory.exists());

    // We could even test the mapping in HDFS here (with the keys)
    Assert.assertEquals(hdfs.values().size(), 3);
  }
  @Override
  protected boolean handleDirectory(File directory, int depth, Collection<VcmFileData> results)
      throws IOException {
    try {
      if (directory.getCanonicalPath().equals(outputRootFile.getCanonicalPath())) {
        log.debug(String.format("Ignoring %s", directory.getCanonicalPath()));
        return false;
      }

      log.info(String.format("Indexing %s", directory.getCanonicalPath()));
      String relativeParentPaths =
          StringUtils.remove(
              directory.getCanonicalPath(), new File(componentRootPath).getCanonicalPath());
      String outputPath = outputRootPath + relativeParentPaths;
      File outputDirectory = new File(outputPath);
      if (!outputDirectory.exists()) {
        if (!outputDirectory.mkdir()) {
          throw new CommandExecutionException(
              2, String.format("Could not create the %s directory", outputDirectory));
        }
      }

      VelocityContext velocityContext = new VelocityContext(velocityToolManager.createContext());
      velocityContext.put("baseURL", baseUrl);
      velocityContext.put(
          "folders",
          directory.listFiles(
              (FileFilter)
                  FileFilterUtils.and(
                      FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE)));
      velocityContext.put("parentFolders", getParentFolders(relativeParentPaths));
      List<VcmFileData> files = getFiles(directory, outputPath);
      velocityContext.put("files", files);
      if (depth == 0) {
        velocityContext.put("rootPath", ".");
      } else {
        velocityContext.put("rootPath", StringUtils.repeat("../", depth));
      }
      velocityContext.put("depth", depth);

      OutputStreamWriter writer = null;
      try {
        writer =
            new OutputStreamWriter(
                new FileOutputStream(new File(outputDirectory, "index.htm")), "UTF-8");
        velocityTemplate.merge(velocityContext, writer);
      } finally {
        IOUtils.closeQuietly(writer);
      }

      results.addAll(files);
      return true;
    } catch (ZipException e) {
      throw new CommandExecutionException(3, e);
    }
  }
 private File copyProject(String path) throws Exception {
   File projectDir = temp.newFolder();
   File originalProjectDir =
       new File(IssueModeAndReportsMediumTest.class.getResource(path).toURI());
   FileUtils.copyDirectory(
       originalProjectDir,
       projectDir,
       FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter(".sonar")));
   return projectDir;
 }
Esempio n. 5
0
 /**
  * Creates a filter that returns everything in the "mods" folder except predefined cubes which are
  * distributed with each new release.
  */
 private FileFilter getModsFileFilter() {
   final String[] excludedCubes =
       new String[] {
         "legacy_cube.txt",
         "modern_cube.txt",
         "standard_cube.txt",
         "extended_cube.txt",
         "ubeefx_cube.txt"
       };
   final IOFileFilter excludedFiles = new NameFileFilter(excludedCubes, IOCase.INSENSITIVE);
   final IOFileFilter excludeFilter = FileFilterUtils.notFileFilter(excludedFiles);
   return FileFilterUtils.or(DirectoryFileFilter.DIRECTORY, excludeFilter);
 }
Esempio n. 6
0
 /**
  * Construct an instance with a directory and a file filter and an optional limit on the
  * <i>depth</i> navigated to.
  *
  * <p>The filters control which files and directories will be navigated to as part of the walk.
  * This constructor uses {@link FileFilterUtils#makeDirectoryOnly(IOFileFilter)} and {@link
  * FileFilterUtils#makeFileOnly(IOFileFilter)} internally to combine the filters. A {@code null}
  * filter means that no filtering should occur.
  *
  * @param directoryFilter the filter to apply to directories, null means visit all directories
  * @param fileFilter the filter to apply to files, null means visit all files
  * @param depthLimit controls how <i>deep</i> the hierarchy is navigated to (less than 0 means
  *     unlimited)
  */
 protected DirectoryWalker(
     IOFileFilter directoryFilter, IOFileFilter fileFilter, final int depthLimit) {
   if (directoryFilter == null && fileFilter == null) {
     this.filter = null;
   } else {
     directoryFilter = directoryFilter != null ? directoryFilter : TrueFileFilter.TRUE;
     fileFilter = fileFilter != null ? fileFilter : TrueFileFilter.TRUE;
     directoryFilter = FileFilterUtils.makeDirectoryOnly(directoryFilter);
     fileFilter = FileFilterUtils.makeFileOnly(fileFilter);
     this.filter = FileFilterUtils.or(directoryFilter, fileFilter);
   }
   this.depthLimit = depthLimit;
 }
  /**
   * Find the Java File object based off of a starting directory.
   *
   * @param filename the filename to search for
   * @param startingDirectory the directory to start searching at
   * @return the File discovered
   * @throws FileNotFoundException will throw an exception if no file is found matching the criteria
   */
  public static File findAbsoluteFile(final String filename, final String startingDirectory)
      throws FileNotFoundException {
    File returnFile = null;
    try {
      File startingDirFile = new File(startingDirectory);
      Collection<File> files =
          FileUtils.listFiles(
              startingDirFile, FileFilterUtils.nameFileFilter(filename), TrueFileFilter.INSTANCE);

      if (files.size() == 0) {
        throw new FileNotFoundException(
            "file '" + filename + "' not found in directory '" + startingDirectory);
      } else if (files.size() > 1) {
        throw new FileNotFoundException(
            "multiple files with filename '"
                + filename
                + "' found in directory '"
                + startingDirectory);
      } else {
        for (File f : files) {
          returnFile = f;
        }
      }
    } catch (FileNotFoundException fnfe) {
      throw fnfe;
    } catch (Exception e) {
      throw new FileNotFoundException(
          "'" + filename + "' not found in directory '" + startingDirectory + "'");
    }
    return returnFile;
  }
Esempio n. 8
0
 private File[] getScriptFiles() {
   final String starts = this.prefix + "_";
   if (AppConstants.SCRIPT_DIR.exists() == false) {
     return new File[0];
   }
   File[] array =
       FileUtils.listFiles(
               AppConstants.SCRIPT_DIR,
               new AbstractFileFilter() {
                 @Override
                 public boolean accept(File file) {
                   String name = file.getName();
                   return name.endsWith(".js") && name.startsWith(starts);
                 }
               },
               FileFilterUtils.trueFileFilter())
           .toArray(new File[0]);
   Arrays.sort(
       array,
       new Comparator<File>() {
         @Override
         public int compare(File arg0, File arg1) {
           return arg0.getPath().compareTo(arg1.getPath());
         }
       });
   return array;
 }
  /** singleton */
  public static final class MusicTraversalRule extends TraversalRule {

    public static final TraversalRule INSTANCE = new MusicTraversalRule();

    private static final IOFileFilter FILTER =
        FileFilterUtils.or(
            FileFilterUtils.suffixFileFilter(".mp3"), FileFilterUtils.suffixFileFilter(".flac"));

    private MusicTraversalRule() {
      super(FILTER, null, -1);
    }

    @Override
    protected void handleFile(File file, int depth, Collection<File> results) {
      results.add(file);
    }
  }
Esempio n. 10
0
  @Override
  protected final void parse() {
    for (File dir : getXMLDir()) {
      if (!dir.exists()) {
        warn("Dir " + dir.getAbsolutePath() + " not exists");
        return;
      }

      File dtd = new File(getXMLDir().get(0), getDTDFileName());
      if (!dtd.exists()) {
        info("DTD file: " + dtd.getName() + " not exists.");
        return;
      }

      initDTD(dtd);

      try {
        Collection<File> files =
            FileUtils.listFiles(
                dir,
                FileFilterUtils.suffixFileFilter(".xml"),
                FileFilterUtils.directoryFileFilter());

        for (File f : files) {
          if (!f.isHidden()) {
            if (!isIgnored(f)) {
              // if(!Config.ExcludeDataFiles.contains(f.getName()))
              // z{
              try {
                parseDocument(new FileInputStream(f), f.getName());
              } catch (Exception e) {
                info("Exception: " + e + " in file: " + f.getName(), e);
              }
              // }
            }
          }
        }
      } catch (Exception e) {
        warn("Exception: " + e, e);
      }
    }
  }
Esempio n. 11
0
    @Override
    protected Object[] doInBackground() throws Exception {
      IOFileFilter pdfFilter = FileFilterUtils.asFileFilter(this);
      IOFileFilter suffixFilter = FileFilterUtils.notFileFilter(new SuffixFileFilter(".fo"));
      IOFileFilter sheetFilter =
          FileFilterUtils.prefixFileFilter(Constants.CHARACTER_TEMPLATE_PREFIX);
      IOFileFilter fileFilter = FileFilterUtils.and(pdfFilter, suffixFilter, sheetFilter);

      IOFileFilter dirFilter = FileFilterUtils.makeSVNAware(TrueFileFilter.INSTANCE);
      File dir = new File(ConfigurationSettings.getOutputSheetsDir());
      Collection<File> files = FileUtils.listFiles(dir, fileFilter, dirFilter);
      URI osPath = new File(ConfigurationSettings.getOutputSheetsDir()).toURI();
      Object[] uriList = new Object[files.size()];
      int i = 0;
      for (File file : files) {
        uriList[i] = osPath.relativize(file.toURI());
        i++;
      }
      return uriList;
    }
Esempio n. 12
0
 /**
  * Merges top level "decks" folder only. Does not import sub-folders (prebuilt, firemind, etc). If
  * file already exists then imported version takes precedence.
  */
 private void importCustomDecks() throws IOException {
   setProgressNote(UiString.get(_S7));
   final String directoryName = "decks";
   final Path sourcePath = importDataPath.resolve(directoryName);
   if (sourcePath.toFile().exists()) {
     final Path targetPath = MagicFileSystem.getDataPath().resolve(directoryName);
     final IOFileFilter deckSuffixFilter = FileFilterUtils.suffixFileFilter(".dec");
     FileUtils.copyDirectory(sourcePath.toFile(), targetPath.toFile(), deckSuffixFilter);
   }
   setProgressNote(OK_STRING);
 }
Esempio n. 13
0
  private void copyLocalNativeLibraries(
      final File localNativeLibrariesDirectory, final File destinationDirectory)
      throws MojoExecutionException {
    getLog().debug("Copying existing native libraries from " + localNativeLibrariesDirectory);
    try {

      IOFileFilter libSuffixFilter = FileFilterUtils.suffixFileFilter(".so");

      IOFileFilter gdbserverNameFilter = FileFilterUtils.nameFileFilter("gdbserver");
      IOFileFilter orFilter = FileFilterUtils.or(libSuffixFilter, gdbserverNameFilter);

      IOFileFilter libFiles = FileFilterUtils.and(FileFileFilter.FILE, orFilter);
      FileFilter filter = FileFilterUtils.or(DirectoryFileFilter.DIRECTORY, libFiles);
      org.apache.commons.io.FileUtils.copyDirectory(
          localNativeLibrariesDirectory, destinationDirectory, filter);

    } catch (IOException e) {
      getLog().error("Could not copy native libraries: " + e.getMessage(), e);
      throw new MojoExecutionException("Could not copy native dependency.", e);
    }
  }
Esempio n. 14
0
 /**
  * Utility that deletes any temporary uploads that were performed during editing, but were not
  * saved.
  *
  * @throws IOException if there was a problem reading the uploads directory or deleting any
  *     temporary uploads
  */
 private void deleteTempUploads() throws IOException {
   if (selected != null) {
     for (Variant variant : selected.getVariants()) {
       Collection<File> tmpFiles =
           FileUtils.listFiles(
               UploadsServlet.getUploadsDirectory(),
               FileFilterUtils.prefixFileFilter("tmp-" + variant.getUuid()),
               null);
       for (File tmpFile : tmpFiles) {
         FileUtils.forceDelete(tmpFile);
       }
     }
   }
 }
Esempio n. 15
0
 public TaskPersistence() {
   try {
     tasksDirectory = new File(System.getProperty("user.home") + "/.floto/tasks");
     FileUtils.forceMkdir(tasksDirectory);
     long numberOfTasks =
         tasksDirectory.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()).length;
     nextTaskId = new AtomicLong(numberOfTasks + 1);
     objectMapper = new ObjectMapper();
     objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
     objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
     objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
     objectMapper.registerModule(new JSR310Module());
   } catch (IOException e) {
     throw Throwables.propagate(e);
   }
 }
Esempio n. 16
0
 private String getWallPaperURL(String spaceId) {
   String id = getShortSpaceId(spaceId);
   String basePath = getSpaceBasePath(id);
   File dir = new File(basePath);
   if (dir.exists() && dir.isDirectory()) {
     Collection<File> wallpapers =
         FileUtils.listFiles(
             dir, FileFilterUtils.prefixFileFilter("wallPaper", IOCase.INSENSITIVE), null);
     for (File wallpaper : wallpapers) {
       if (wallpaper.isFile() && FileUtil.isImage(wallpaper.getName())) {
         return getURLOfElement(id, wallpaper.getName());
       }
     }
   }
   return null;
 }
  /**
   * Suffix <br>
   * Created on: Nov 28, 2012 1:50:50 PM
   *
   * @param filePath
   * @param nameFix
   * @param newerTime
   * @param isOlder true : all file ,false : newer file
   * @return
   */
  public static Collection<File> getSuffixAndNewerFiles(
      String filePath, String nameFix, long newerTime, boolean isOlder) {

    IOFileFilter wildcardFileFilter = new SuffixFileFilter(nameFix);
    IOFileFilter ageFileFilter = new AgeFileFilter(newerTime, isOlder);
    IOFileFilter andFilter = FileFilterUtils.and(wildcardFileFilter, ageFileFilter);

    NameFileComparator comp = new NameFileComparator();
    File[] fileArr =
        comp.sort(
            FileUtils.convertFileCollectionToFileArray(
                FileUtils.listFiles((new File(filePath)), andFilter, TrueFileFilter.INSTANCE)));
    comp = null;

    return new LinkedList<File>(Arrays.asList(fileArr));
  }
Esempio n. 18
0
  // (2)
  public void cleanup() {

    // calculate cutoff date.
    Date cutoffDate = dateFactory.newDateTime().minusMinutes(savedPeriodMinutes).toDate();

    // collect target files.
    IOFileFilter fileFilter = FileFilterUtils.ageFileFilter(cutoffDate);
    Collection<File> targetFiles = FileUtils.listFiles(targetDirectory, fileFilter, null);

    if (targetFiles.isEmpty()) {
      return;
    }

    // delete files.
    for (File targetFile : targetFiles) {
      FileUtils.deleteQuietly(targetFile);
    }
  }
Esempio n. 19
0
 /**
  * Returns a list of available, valid (contains 'valid' file) checkpoint directories, as File
  * instances, with the more recently-written appearing first.
  *
  * @return List of valid checkpoint directory File instances
  */
 @SuppressWarnings("unchecked")
 public List<File> findAvailableCheckpointDirectories() {
   File[] dirs =
       getCheckpointsDir().getFile().listFiles((FileFilter) FileFilterUtils.directoryFileFilter());
   if (dirs == null) {
     return Collections.EMPTY_LIST;
   }
   Arrays.sort(dirs, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
   LinkedList<File> dirsList = new LinkedList<File>(Arrays.asList(dirs));
   Iterator<File> iter = dirsList.iterator();
   while (iter.hasNext()) {
     File cpDir = iter.next();
     if (!Checkpoint.hasValidStamp(cpDir)) {
       LOGGER.warning("checkpoint '" + cpDir + "' missing validity stamp file; ignoring");
       iter.remove();
     }
   }
   return dirsList;
 }
 /**
  * Cleaning up the generated files (shape and properties so that we recreate them).
  *
  * @throws FileNotFoundException
  * @throws IOException
  */
 private void cleanUp() throws FileNotFoundException, IOException {
   File dir = TestData.file(this, "heterogeneous/");
   File[] files =
       dir.listFiles(
           (FilenameFilter)
               FileFilterUtils.notFileFilter(
                   FileFilterUtils.or(
                       FileFilterUtils.or(
                           FileFilterUtils.suffixFileFilter("tif"),
                           FileFilterUtils.suffixFileFilter("aux")),
                       FileFilterUtils.nameFileFilter("datastore.properties"))));
   for (File file : files) {
     file.delete();
   }
 }
 private List<VcmFileData> getFiles(File directory, String outputPath)
     throws IOException, ZipException {
   ArrayList<VcmFileData> list = new ArrayList<VcmFileData>();
   for (File file :
       directory.listFiles(
           (FileFilter) FileFilterUtils.suffixFileFilter("vcm", IOCase.INSENSITIVE))) {
     log.debug(String.format("Opening %s", file.getCanonicalPath()));
     File destFile = new File(outputPath, file.getName());
     String relativePath =
         destFile
             .getCanonicalPath()
             .substring(new File(outputRootPath).getCanonicalPath().length() + 1);
     relativePath = StringUtils.replace(FilenameUtils.getPath(relativePath), "\\", "/");
     VcmFileData vcmFile = new VcmFileData(file, relativePath);
     list.add(vcmFile);
     log.debug(String.format("Generating thumbnail for %s", file.getCanonicalPath()));
     createThumbnail(vcmFile.getModel(), new File(outputPath, vcmFile.getThumbnailName()));
     log.debug(String.format("Copying %s", file.getCanonicalPath()));
     FileUtils.copyFile(file, destFile);
   }
   return list;
 }
Esempio n. 22
0
/**
 * Commons-based FileSystem implementation
 *
 * @author Joe Walnes
 * @author Mauro Talevi
 */
public class CommonsFileSystem implements FileSystem {

  private static final IOFileFilter SVN_AWARE_FILTER = FileFilterUtils.makeSVNAware(null);

  public String readFile(File file) {
    try {
      return readFileToString(file);
    } catch (Exception e) {
      throw new FileSystemException("Cannot read content from file " + file, e);
    }
  }

  public void copyFile(File source, File destination) {
    try {
      copyFile(source, destination);
    } catch (Exception e) {
      throw new FileSystemException("Failed to copy file " + source + " to " + destination, e);
    }
  }

  public void copyDirectory(File sourceDirectory, File targetDirectory, boolean recurse) {
    if (!sourceDirectory.isDirectory()) {
      throw new FileSystemException("Source must be a directory " + sourceDirectory, null);
    }
    try {
      Collection<File> relativeFiles =
          filterRelativeFiles(sourceDirectory, getFileFilter(), recurse);
      for (File relativeFile : relativeFiles) {
        File sourceFile = new File(sourceDirectory, relativeFile.getPath());
        File relativeDirectory = new File(targetDirectory, relativeFile.getParent());
        copyFileToDirectory(sourceFile, relativeDirectory);
      }
    } catch (Exception e) {
      throw new FileSystemException(
          "Failed to copy directory " + sourceDirectory + " to " + targetDirectory, e);
    }
  }

  @SuppressWarnings("unchecked")
  private Collection<File> filterRelativeFiles(
      File sourceDirectory, IOFileFilter filter, boolean recurse) {
    Collection<File> files =
        listFiles(
            sourceDirectory, makeFileOnly(filter), (recurse ? makeDirectoryOnly(filter) : null));
    Collection<File> relativeFiles = new ArrayList<File>();
    String sourceDirectoryPath = sourceDirectory.getPath();
    for (File file : files) {
      String filePath = file.getPath();
      String relativePath = difference(sourceDirectoryPath, filePath);
      relativeFiles.add(new File(relativePath));
    }
    return relativeFiles;
  }

  /**
   * Specifies the file filter used in the #copyDirectory() method.
   *
   * @return An IOFileFilter
   */
  protected IOFileFilter getFileFilter() {
    return SVN_AWARE_FILTER;
  }

  @SuppressWarnings("serial")
  public static class FileSystemException extends RuntimeException {
    public FileSystemException(String message, Throwable throwable) {
      super(message, throwable);
    }
  }
}
Esempio n. 23
0
/**
 * Generate the "Source view" documentation from the sources, like Flex/Flash Builder does for the
 * release builds. Users can they right click the application and view the sources.
 *
 * <p>This goal produces a syntax highlighted version of the as, mxml and html documents in the
 * sources, and just copies other types of files. It also generates a navigation to browse the
 * sources.
 *
 * @goal source-view
 * @phase prepare-package
 */
public class SourceViewMojo extends AbstractIrvinMojo {
  /**
   * The Maven project.
   *
   * @parameter expression="${project}"
   * @required
   * @readonly
   */
  protected MavenProject project;

  /**
   * The name of the directory containing the "View source" documentation.
   *
   * <p>It must be the same as the one declared in the Flex application:
   *
   * <pre>
   * &lt;mx:Application [...] viewSourceURL="srcview"&gt;
   *    [...]
   * &lt;/mx:Application&gt;
   * </pre>
   *
   * @parameter default-value="srcview"
   */
  protected String sourceViewDirectoryName;

  /**
   * Encoding to use for the generated documentation.
   *
   * @parameter default-value="UTF-8"
   */
  protected String outputEncoding;

  /** The instance of {@link VelocityEngine}. */
  protected VelocityEngine velocityEngine;

  /** The instance of {@link VelocityContext}. */
  protected VelocityContext velocityContext;

  /** The file filter to use when processing source files. */
  protected IOFileFilter filter = FileFilterUtils.makeCVSAware(FileFilterUtils.makeSVNAware(null));

  /** {@inheritDoc} */
  @Override
  protected void setUp() throws MojoExecutionException, MojoFailureException {
    // Initialize a Velocity engine
    if (velocityEngine == null) {
      velocityEngine = new VelocityEngine();
      try {
        Properties p = new Properties();
        p.setProperty(VelocityEngine.RESOURCE_LOADER, "classpath");
        p.setProperty(
            "classpath." + VelocityEngine.RESOURCE_LOADER + ".class",
            ClasspathResourceLoader.class.getName());
        p.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogSystem.class.getName());
        p.setProperty(VelocityEngine.VM_CONTEXT_LOCALSCOPE, Boolean.toString(true));
        velocityEngine.init(p);
      } catch (Exception e) {
        throw new MojoFailureException("Failed to initialize Velocity engine", e);
      }
    }

    // Initialize a Velocity context
    velocityContext = new VelocityContext();
  }

  /** {@inheritDoc} */
  @Override
  protected void run() throws MojoExecutionException, MojoFailureException {
    // Create the "Source view" directory
    File sourceViewDirectory =
        new File(project.getBuild().getDirectory() + File.separator + sourceViewDirectoryName);
    sourceViewDirectory.mkdirs();

    // Start processing the main source directory
    processDirectory(new File(project.getBuild().getSourceDirectory()), sourceViewDirectory);

    // Initialize contents of the Velocity context
    velocityContext.put("sourceViewDirectory", sourceViewDirectory);
    velocityContext.put("project", project);
    velocityContext.put("contentFrameSource", getContentFrameSource());

    // Generate the HTML pages from the templates
    processTemplate("index.html", sourceViewDirectory);
    processTemplate("navigation.html", sourceViewDirectory);
    processTemplate("style.css", sourceViewDirectory);
  }

  /** {@inheritDoc} */
  @Override
  protected void tearDown() throws MojoExecutionException, MojoFailureException {}

  /**
   * Loop through source files in the directory and syntax highlight and/or copy them to the target
   * directory.
   *
   * @param directory The source directory to process.
   * @param targetDirectory The directory where to store the output.
   */
  protected void processDirectory(File directory, File targetDirectory) {
    getLog().debug("Processing directory " + directory.getName());

    // Loop through files in the directory
    for (File file : directory.listFiles((FileFilter) filter)) {
      // Skip hidden files
      if (!file.isHidden() && !file.getName().startsWith(".")) {
        if (file.isDirectory()) {
          File newTargetDirectory = new File(targetDirectory, file.getName());
          newTargetDirectory.mkdir();
          processDirectory(file, newTargetDirectory);
        } else {
          try {
            processFile(file, targetDirectory);
          } catch (IOException e) {
            getLog().warn("Error while processing " + file.getName(), e);
          }
        }
      }
    }
  }

  /**
   * Syntax highlight and/or copy the source file to the target directory.
   *
   * @param file The file to process.
   * @param targetDirectory The directory where to store the output.
   * @throws IOException If there was a file read/write exception.
   */
  protected void processFile(File file, File targetDirectory) throws IOException {
    getLog().debug("Processing file " + file.getName());

    // Prepare to copy the file
    String destinationFilePath =
        targetDirectory.getCanonicalPath() + File.separator + file.getName();

    // Check if the file can be syntax highlighted
    String extension = file.getName().substring(file.getName().lastIndexOf('.') + 1);
    String highlightFilter = getHighlightFilter(extension);

    if (highlightFilter != null) {
      getLog().debug("Converting " + file.getName() + "to HTML.");
      destinationFilePath += ".html";
      XhtmlRendererFactory.getRenderer(highlightFilter)
          .highlight(
              file.getName(),
              new FileInputStream(file),
              new FileOutputStream(destinationFilePath),
              Charset.forName(outputEncoding).name(),
              false);
    } else {
      getLog().debug("Copying " + file.getName());
      FileUtils.copyFileToDirectory(file, targetDirectory);
    }
  }

  /**
   * Get the syntax highlighting filter to use for a file extension.
   *
   * @param fileExtension the file extension to test.
   * @return null if no filter available for this file type.
   * @see {@link XhtmlRendererFactory#getSupportedTypes()}
   */
  protected String getHighlightFilter(String fileExtension) {
    // FIXME Using file extensions are less trustable than getting the real
    // filetype...

    if (fileExtension != null && !"".equals(fileExtension)) {
      if ("as".equals(fileExtension)) {
        return XhtmlRendererFactory.JAVA;
      } else if (fileExtension.startsWith("xml") || fileExtension.endsWith("xml")) {
        return XhtmlRendererFactory.XML;
      } else if (fileExtension.startsWith("htm") || fileExtension.startsWith("xhtm")) {
        return XhtmlRendererFactory.XHTML;
      }
    }
    return null;
  }

  /**
   * Merge the given template with the {@link SourceViewMojo#velocityContext} and produce the file
   * in the output documentation.
   *
   * @param templateName The name of the template to process.
   * @param targetDirectory The directory where to store the output file.
   * @throws MojoFailureException If the template could not be loaded, parsed, or the output file
   *     could not be written.
   */
  protected void processTemplate(String templateName, File targetDirectory)
      throws MojoFailureException {
    FileWriterWithEncoding pageWriter = null;
    try {
      pageWriter =
          new FileWriterWithEncoding(
              new File(targetDirectory.getCanonicalPath() + File.separator + templateName),
              Charset.forName(outputEncoding));
      velocityEngine.mergeTemplate(
          "/templates/source-view/" + templateName + ".vm",
          Charset.forName("UTF-8").name(),
          velocityContext,
          pageWriter);
    } catch (ResourceNotFoundException e) {
      throw new MojoFailureException("The template '" + templateName + "' could not be found.", e);
    } catch (ParseErrorException e) {
      throw new MojoFailureException("Failed to parse the template '" + templateName + "' .", e);
    } catch (Exception e) {
      throw new MojoFailureException("Failed to load the template '" + templateName + "' .", e);
    } finally {
      try {
        pageWriter.close();
      } catch (IOException e) {
        throw new MojoFailureException("Failed to write the template '" + templateName + "' .", e);
      }
    }
  }

  /**
   * Resolve the file to assign as the source of the content frame in the generated documentation.
   *
   * <p>Tries to resolve the main source and defaults to a blank page if not found.
   *
   * @return The path to the page to use in the generated documentation.
   */
  @SuppressWarnings("unchecked")
  protected String getContentFrameSource() {
    File mainSourceFile =
        SourceFileResolver.resolveSourceFile(
            project.getCompileSourceRoots(), null, project.getGroupId(), project.getArtifactId());

    return (mainSourceFile == null) ? "about:blank" : mainSourceFile.getName() + ".html";
  }
}
Esempio n. 24
0
  @Test
  public void testImageReaderGOME2AncillaryFiles() throws Exception {
    final File file = TestData.file(this, "20130101.METOPA.GOME2.NO2.DUMMY.nc");
    final NetCDFImageReaderSpi unidataImageReaderSpi = new NetCDFImageReaderSpi();
    assertTrue(unidataImageReaderSpi.canDecodeInput(file));
    NetCDFImageReader reader = null;
    try {

      // checking low level
      reader = (NetCDFImageReader) unidataImageReaderSpi.createReaderInstance();
      reader.setInput(file);
      int numImages = reader.getNumImages(true);
      assertEquals(1, numImages);
      LOGGER.info("Found " + numImages + " images.");
      for (int i = 0; i < numImages; i++) {
        Slice2DIndex sliceIndex = reader.getSlice2DIndex(i);
        assertNotNull(sliceIndex);
        spitOutSliceInformation(i, sliceIndex);
      }

      // check coverage names
      final List<Name> names = reader.getCoveragesNames();
      assertNotNull(names);
      assertTrue(!names.isEmpty());
      assertTrue(1 == names.size());
      assertEquals("NO2", names.get(0).toString());

      // checking slice catalog
      final CoverageSlicesCatalog cs = reader.getCatalog();
      assertNotNull(cs);

      MessageDigest md = MessageDigest.getInstance("SHA-1");
      md.update(file.getCanonicalPath().getBytes());
      String hashCode = AncillaryFileManager.convertToHex(md.digest());

      // Check if the auxiliary files directory is present
      File parentDir = file.getParentFile();

      String auxiliaryDirPath =
          parentDir
              + File.separator
              + "."
              + FilenameUtils.getBaseName(file.getName())
              + "_"
              + hashCode;

      File auxiliaryDir = new File(auxiliaryDirPath);

      assertTrue(auxiliaryDir.exists());
      assertTrue(auxiliaryDir.isDirectory());

      // Check if the Auxiliary File Directory contains the origin.txt file
      FilenameFilter nameFileFilter = FileFilterUtils.nameFileFilter("origin.txt");
      File[] files = auxiliaryDir.listFiles(nameFileFilter);
      assertTrue(files != null);
      assertTrue(files[0].exists());
    } finally {
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }
    }
  }
 @Bean
 public IOFileFilter bomVersionPropertyValidatorFilter() {
   return FileFilterUtils.trueFileFilter();
 }
Esempio n. 26
0
  @Override
  public boolean publish(ProblemQuery problems) throws IOException {
    boolean ok;
    boolean subsetGoog = true;

    final String intermediateDirPath = outputFolder.getPath();
    final File intermediateDir = new File(intermediateDirPath);
    File srcDir = new File(configuration.getTargetFile());
    srcDir = srcDir.getParentFile();

    final String projectName = FilenameUtils.getBaseName(configuration.getTargetFile());
    final String outputFileName = projectName + "." + JSSharedData.OUTPUT_EXTENSION;

    File releaseDir = new File(outputParentFolder, FLEXJS_RELEASE_DIR_NAME);
    final String releaseDirPath = releaseDir.getPath();

    if (!isMarmotinniRun) {
      if (releaseDir.exists()) org.apache.commons.io.FileUtils.deleteQuietly(releaseDir);

      releaseDir.mkdirs();
    }

    // If the closure-lib parameter is empty we'll try to find the resources
    // in the classpath, dump its content to the output directory and use this
    // as closure-lib parameter.
    final String closureLibDirPath;
    if (((JSGoogConfiguration) configuration).isClosureLibSet()) {
      closureLibDirPath = ((JSGoogConfiguration) configuration).getClosureLib();
    } else {
      // Check if the "goog/deps.js" is available in the classpath.
      URL resource = Thread.currentThread().getContextClassLoader().getResource("goog/deps.js");
      if (resource != null) {
        File closureLibDir = new File(intermediateDir.getParent(), "closure");

        // Only create and dump the content, if the directory does not exists.
        if (!closureLibDir.exists()) {
          if (!closureLibDir.mkdirs()) {
            throw new IOException(
                "Unable to create directory for closure-lib at " + closureLibDir.getAbsolutePath());
          }

          // Strip the url of the parts we don't need.
          // Unless we are not using some insanely complex setup
          // the resource will always be on the same machine.
          String resourceJarPath = resource.getFile();
          if (resourceJarPath.contains(":")) {
            resourceJarPath = resourceJarPath.substring(resourceJarPath.lastIndexOf(":") + 1);
          }
          if (resourceJarPath.contains("!")) {
            resourceJarPath = resourceJarPath.substring(0, resourceJarPath.indexOf("!"));
          }
          File resourceJar = new File(resourceJarPath);

          // Dump the closure lib from classpath.
          dumpJar(resourceJar, closureLibDir);
        }
        // The compiler automatically adds a "closure" to the lib dir path,
        // so we omit this here.
        closureLibDirPath = intermediateDir.getParentFile().getPath();
      }
      // Fallback to the default.
      else {
        closureLibDirPath = ((JSGoogConfiguration) configuration).getClosureLib();
      }
    }

    final String closureGoogSrcLibDirPath = closureLibDirPath + "/closure/goog/";
    final String closureGoogTgtLibDirPath = intermediateDirPath + "/library/closure/goog";
    final String depsSrcFilePath = intermediateDirPath + "/library/closure/goog/deps.js";
    final String depsTgtFilePath = intermediateDirPath + "/deps.js";
    final String projectIntermediateJSFilePath =
        intermediateDirPath + File.separator + outputFileName;
    final String projectReleaseJSFilePath = releaseDirPath + File.separator + outputFileName;

    appendExportSymbol(projectIntermediateJSFilePath, projectName);
    appendEncodedCSS(projectIntermediateJSFilePath, projectName);

    if (!subsetGoog) {
      // (erikdebruin) We need to leave the 'goog' files and dependencies well
      //               enough alone. We copy the entire library over so the
      //               'goog' dependencies will resolve without our help.
      FileUtils.copyDirectory(
          new File(closureGoogSrcLibDirPath), new File(closureGoogTgtLibDirPath));
    }

    VF2JSClosureCompilerWrapper compilerWrapper = new VF2JSClosureCompilerWrapper();

    VF2JSDepsWriter gdw =
        new VF2JSDepsWriter(intermediateDir, projectName, (JSGoogConfiguration) configuration);
    try {
      StringBuilder depsFileData = new StringBuilder();
      ok = gdw.generateDeps(problems, depsFileData);
      if (!subsetGoog) {
        writeFile(depsTgtFilePath, depsFileData.toString(), false);
      } else {
        String s = depsFileData.toString();
        int c = s.indexOf("'goog.");
        ArrayList<String> googreqs = new ArrayList<String>();
        while (c != -1) {
          int c2 = s.indexOf("'", c + 1);
          String googreq = s.substring(c, c2 + 1);
          googreqs.add(googreq);
          c = s.indexOf("'goog.", c2);
        }
        HashMap<String, DependencyRecord> defmap = new HashMap<String, DependencyRecord>();
        // read in goog's deps.js
        FileInputStream fis = new FileInputStream(closureGoogSrcLibDirPath + "/deps.js");
        Scanner scanner = new Scanner(fis, "UTF-8");
        String addDependency = "goog.addDependency('";
        int currentLine = 0;
        while (scanner.hasNextLine()) {
          String googline = scanner.nextLine();
          if (googline.indexOf(addDependency) == 0) {
            int c1 = googline.indexOf("'", addDependency.length() + 1);
            String googpath = googline.substring(addDependency.length(), c1);
            String googdefs = googline.substring(googline.indexOf("[") + 1, googline.indexOf("]"));
            String googdeps =
                googline.substring(googline.lastIndexOf("[") + 1, googline.lastIndexOf("]"));
            String[] thedefs = googdefs.split(",");
            DependencyRecord deprec = new DependencyRecord();
            deprec.path = googpath;
            deprec.deps = googdeps;
            deprec.line = googline;
            deprec.lineNumber = currentLine;
            for (String def : thedefs) {
              def = def.trim();
              defmap.put(def, deprec);
            }
          }
          currentLine++;
        }
        // (erikdebruin) Prevent 'Resource leak' warning on line 212:
        scanner.close();
        ArrayList<DependencyRecord> subsetdeps = new ArrayList<DependencyRecord>();
        HashMap<String, String> gotgoog = new HashMap<String, String>();
        for (String req : googreqs) {
          DependencyRecord deprec = defmap.get(req);
          // if we've already processed this file, skip
          if (!gotgoog.containsKey(deprec.path)) {
            gotgoog.put(deprec.path, null);
            subsetdeps.add(deprec);
            addDeps(subsetdeps, gotgoog, defmap, deprec.deps);
          }
        }
        // now we should have the subset of files we need in the order needed
        StringBuilder sb = new StringBuilder();
        sb.append("goog.addDependency('base.js', ['goog'], []);\n");
        File file = new File(closureGoogSrcLibDirPath + "/base.js");
        FileUtils.copyFileToDirectory(file, new File(closureGoogTgtLibDirPath));
        compilerWrapper.addJSSourceFile(file.getCanonicalPath());
        Collections.sort(subsetdeps, new DependencyLineComparator());
        for (DependencyRecord subsetdeprec : subsetdeps) {
          sb.append(subsetdeprec.line).append("\n");
        }
        writeFile(depsTgtFilePath, sb.toString() + depsFileData.toString(), false);
        // copy the required files
        for (String googfn : gotgoog.keySet()) {
          file = new File(closureGoogSrcLibDirPath + File.separator + googfn);
          String dir = closureGoogTgtLibDirPath;
          if (googfn.contains("/")) {
            dir += File.separator + googfn.substring(0, googfn.lastIndexOf("/"));
          }
          FileUtils.copyFileToDirectory(file, new File(dir));
          compilerWrapper.addJSSourceFile(file.getCanonicalPath());
        }
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
      return false;
    }

    IOFileFilter pngSuffixFilter =
        FileFilterUtils.and(FileFileFilter.FILE, FileFilterUtils.suffixFileFilter(".png"));
    IOFileFilter gifSuffixFilter =
        FileFilterUtils.and(FileFileFilter.FILE, FileFilterUtils.suffixFileFilter(".gif"));
    IOFileFilter jpgSuffixFilter =
        FileFilterUtils.and(FileFileFilter.FILE, FileFilterUtils.suffixFileFilter(".jpg"));
    IOFileFilter assetFiles = FileFilterUtils.or(pngSuffixFilter, jpgSuffixFilter, gifSuffixFilter);

    FileUtils.copyDirectory(srcDir, intermediateDir, assetFiles);
    FileUtils.copyDirectory(srcDir, releaseDir, assetFiles);

    File srcDeps = new File(depsSrcFilePath);

    // ToDo (erikdebruin): yeah, right, hard coded the path, nice!
    File sdkDepsFile =
        new File("/Users/erik/Documents/ApacheFlex/git/flex-asjs/vf2js/frameworks/js/sdk-deps.js");
    if (sdkDepsFile.exists())
      FileUtils.copyFile(
          sdkDepsFile, new File(intermediateDirPath + File.separator + "sdk-deps.js"));

    writeHTML("intermediate", projectName, intermediateDirPath, gdw.additionalHTML);
    writeHTML("release", projectName, releaseDirPath, gdw.additionalHTML);
    writeCSS(projectName, intermediateDirPath);
    writeCSS(projectName, releaseDirPath);

    if (!subsetGoog) {
      // (erikdebruin) add 'goog' files
      Collection<File> files =
          org.apache.commons.io.FileUtils.listFiles(
              new File(closureGoogTgtLibDirPath),
              new RegexFileFilter("^.*(\\.js)"),
              DirectoryFileFilter.DIRECTORY);
      for (File file : files) {
        compilerWrapper.addJSSourceFile(file.getCanonicalPath());
      }
    }

    // (erikdebruin) add project files
    for (String filePath : gdw.filePathsInOrder) {
      compilerWrapper.addJSSourceFile(new File(filePath).getCanonicalPath());
    }

    compilerWrapper.setOptions(projectReleaseJSFilePath, useStrictPublishing);

    // (erikdebruin) Include the 'goog' deps to allow the compiler to resolve
    //               dependencies.
    compilerWrapper.addJSSourceFile(closureGoogSrcLibDirPath + File.separator + "deps.js");

    List<String> externs = ((JSGoogConfiguration) configuration).getExternalJSLib();
    for (String extern : externs) {
      compilerWrapper.addJSExternsFile(extern);
    }

    compilerWrapper.targetFilePath = projectReleaseJSFilePath;
    compilerWrapper.compile();

    appendSourceMapLocation(projectReleaseJSFilePath, projectName);

    if (!isMarmotinniRun) {
      String allDeps = "";
      if (!subsetGoog) allDeps += FileUtils.readFileToString(srcDeps);
      allDeps += FileUtils.readFileToString(new File(depsTgtFilePath));

      FileUtils.writeStringToFile(srcDeps, allDeps);

      org.apache.commons.io.FileUtils.deleteQuietly(new File(depsTgtFilePath));
    }

    if (ok)
      System.out.println(
          "The project '" + projectName + "' has been successfully compiled and optimized.");

    return true;
  }
Esempio n. 27
0
 public static List<File> listFiles(List<File> files, IOFileFilter filter) {
   return FileFilterUtils.filterList(filter, files);
 }
Esempio n. 28
0
 public static Iterable<File> listFiles(Iterable<File> files, IOFileFilter filter) {
   return FileFilterUtils.filterList(filter, files);
 }
 public GenerateWebCatalogCommand() {
   super(FileFilterUtils.and(FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE), -1);
 }
Esempio n. 30
0
 public HtmlTagSubstitutor(String suffix) {
   super(HiddenFileFilter.VISIBLE, FileFilterUtils.suffixFileFilter("." + suffix), 10);
   initCT();
 }