All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.st.p2012.mind.ResourceCopier Maven / Gradle / Ivy

The newest version!
/**
 \file com/st/p2012/mind/Launcher.java
 \brief A class to copy documentation resources.


 
 \n
 This file is part of the Platform 2012 program,
 a cooperation between STMicroelectronics and CEA.\n
 Redistribution of this file to outside parties is
 strictly prohibited without the written consent
 of the module owner indicated below.\n
 

 \par  Module owner: [email protected]


 \par  Copyright (C) 2009 STMicroelectronics

 \par  Authors: [email protected]

 \par  Id: $Id$
 \par  Date: $Date$
 \par  Revision: $Rev$

 */
package com.st.p2012.mind;

import static com.st.p2012.mind.Launcher.DOC_FILES_DIRECTORY;
import static com.st.p2012.mind.Launcher.HTML_RESOURCES_DIR;
import static com.st.p2012.mind.Launcher.getMindocHome;
import static com.st.p2012.mind.Launcher.logger;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.FileUtils;

public class ResourceCopier {

  private static class DocFilesCopier extends DirectoryWalker {
    private final String sourceDirectory;
    private final File targetDirectory;

    public DocFilesCopier(final File sourceDirectory, final File targetDirectory) throws IOException {
      this.sourceDirectory = sourceDirectory.getCanonicalPath();
      this.targetDirectory = targetDirectory;
    }

    public static List copy(final File sourceDirectory, final File targetDirectory) throws IOException {
      final DocFilesCopier finder = new DocFilesCopier(sourceDirectory, targetDirectory);
      final List results = new LinkedList();
      finder.walk(sourceDirectory, results);
      return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected boolean handleDirectory(final File directory, final int depth,
        final Collection results) throws IOException {
      if(directory.getName().equals(DOC_FILES_DIRECTORY)) {
        final String docFilesDirName = directory.getCanonicalPath();
        final String localName = docFilesDirName.substring(sourceDirectory.length());
        final File destDir = new File(targetDirectory, localName);
        FileUtils.copyDirectory(directory, destDir);
        return false; //don't walk in subdirectories
      } else {
        return true;
      }
    }
  }

  public static void copyResources(final File sourceDirectories[], final File targetDirectory) {
    //copy css and html resources
    final File htmlResourceDirectory = new File(getMindocHome(), HTML_RESOURCES_DIR);

    if(!htmlResourceDirectory.canRead()) {
      logger.severe("Cannot read resource directory: " + htmlResourceDirectory.getPath());
      System.exit(1);
    }

    final FileFilter ff = new FileFilter() {
      public boolean accept(final File file) {
        return file.getName().endsWith(".css") || file.getName().endsWith(".html");
      }
    };
    try {
      FileUtils.copyDirectory(htmlResourceDirectory, targetDirectory, ff);
    } catch (final IOException e) {
      logger.severe("Error while copying resources: " + e.getLocalizedMessage());
      System.exit(1);
    }



    //copy **/doc-files/*
    try {
      for (final File sourceDirectory : sourceDirectories) {
        DocFilesCopier.copy(sourceDirectory, targetDirectory);
      }
    } catch (final IOException e) {
      logger.severe("Error while copying resources: " + e.getLocalizedMessage());
      System.exit(1);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy