Package ca.carleton.gcrc.couch.app

Source Code of ca.carleton.gcrc.couch.app.JSONDirectoryBuilder

package ca.carleton.gcrc.couch.app;

import java.io.File;
import java.io.FilenameFilter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.json.util.JSONBuilder;

public class JSONDirectoryBuilder {

  private File top;
  private FilenameFilter filter = null;

  public JSONDirectoryBuilder(File directory) throws Exception {
    // Check that directory exists and is a directory
    if( false == directory.exists() ) {
      throw new Exception("Can not build JSON from a directory that does not exist: "+directory.getAbsolutePath());
    }
    if( false == directory.isDirectory() ) {
      throw new Exception("Build JSON specifying a non-directory path: "+directory.getAbsolutePath());
    }
   
    this.top = directory;
    this.filter = new FilenameFilter() {
      @Override
      public boolean accept(File dir, String name) {
        if( name.startsWith(".") ) {
          return false;
        }
        return true;
      }
    };
  }
 
  public FilenameFilter getFilter() {
    return filter;
  }

  public void setFilter(FilenameFilter filter) {
    this.filter = filter;
  }
 
  public void write(OutputStream stream, String charSet) throws Exception {
    OutputStreamWriter osw = new OutputStreamWriter(stream, charSet);
    write(osw);
    osw.flush();
  }
 
  public void write(Writer writer) throws Exception {
    JSONBuilder builder = new JSONBuilder(writer);
    builder.object();
    writeDirectory(builder, top);
    builder.endObject();
  }

  private void writeDirectory(JSONBuilder builder, File dir) throws Exception {
    // List all elements in directory, in sorted order
    List<String> children = null;
    {
      String[] childrenArr = dir.list(filter);
      children = new ArrayList<String>(childrenArr.length);
      for(String child : childrenArr) {
        children.add(child);
      }
      Collections.sort(children);
    }
   
    // Process each child
    for(String child : children) {
      File file = new File(dir,child);
     
      // Compute name without extension, which is the key
      String key = child.split("\\.")[0];
      builder.key(key);
     
      if( file.isDirectory() ) {
        builder.object();
        writeDirectory(builder, file);
        builder.endObject();
      } else if( file.isFile() ) {
        String content = fileToString(file);
        builder.value(content);
      } else {
        // Ignore?
      }
    }
  }

  private String fileToString(File file) throws Exception {
    StringWriter sw = new StringWriter();
    FileInputStream fis = null;
    char[] buffer = new char[100];
    try {
      fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
     
      int size = isr.read(buffer);
      while( size >= 0 ) {
        sw.write(buffer, 0, size);
        size = isr.read(buffer);
      }
     
      sw.flush();
     
    } catch (Exception e) {
      throw new Exception("Error while reading file: "+file.getAbsolutePath(), e);
     
    } finally {
      if( null != fis ) {
        try {
          fis.close();
        } catch (Exception e) {
          // Ignore
        }
      }
    }
   
    return sw.toString();
  }
}
TOP

Related Classes of ca.carleton.gcrc.couch.app.JSONDirectoryBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.