/*
  Copyright (C) 2007 Helge Hess

  This file is part of JOPE.

  JOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/
package org.opengroupware.jope.ofs.fs;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opengroupware.jope.foundation.NSObject;

abstract public class OFSFileManager extends NSObject
  implements IOFSFileManager
{
  protected static final Log log = LogFactory.getLog("OFSFileManager");
  protected static String[]   emptyStringArray = new String[0];
  protected static String[][] emptyStringStringArray = new String[0][];
  
  /* hierarchy */
  
  public IOFSFileInfo fileInfoForPath(String[] _path, String _childName) {
    if (_childName == null)
      return null;
    
    if (_path == null || _path.length == 0)
      return this.fileInfoForPath(new String[] { _childName });
    
    String[] childPath = new String[_path.length + 1];
    System.arraycopy(_path, 0, childPath, 0, _path.length);
    childPath[_path.length] = _childName;
    return this.fileInfoForPath(childPath);
  }
  
  public IOFSFileInfo[] childInfosAtPath(String[] _path) {
    String[][] pathes = this.childPathesAtPath(_path);
    if (pathes == null) return null;
    
    int len = pathes.length;
    if (len == 0) return new IOFSFileInfo[0];
    
    IOFSFileInfo[] infos = new IOFSFileInfo[len];
    for (int i = 0; i < infos.length; i++)
      infos[i] = this.fileInfoForPath(pathes[i]);
    return infos;
  }

  public String[][] childPathesAtPath(String[] _path) {
    String[] names = this.childNamesAtPath(_path);
    if (names == null) return null;
    
    int len = names.length;
    if (len == 0) return emptyStringStringArray;
    
    String[][] pathes = new String[0][];
    int pathLen = _path != null ? _path.length : 0;
    
    for (int i = 0; i < len; i++) {
      String[] childPath;
      if (pathLen <= 0)
        childPath = new String[] { names[i] };
      else {
        childPath = new String[pathLen + 1];
        System.arraycopy(_path, 0, childPath, 0, pathLen);
        childPath[pathLen] = names[i];
      }
      
      pathes[i] = childPath;
    }
    
    return pathes;
  }
  
  /* names */
  
  public boolean isValidFilename(String _name) {
    return !(_name == null || _name.length() == 0);
  }
  
  /* log */
  
  public Log log() {
    return log;
  }
  
  /* utilities */
  
  public static String[] pathForChild(String[] _dirname, String _filename) {
    if (_filename == null)
      return null;
    
    if (_dirname == null || _dirname.length == 0)
      return new String[] { _filename };
    
    int pathLen = _dirname.length;
    String[] childPath = new String[pathLen + 1];
    System.arraycopy(_dirname, 0, childPath, 0, pathLen);
    childPath[pathLen] = _filename;
    return childPath;
  }
}
