Skip to content
Snippets Groups Projects
Commit d0e5a75f authored by felmer's avatar felmer
Browse files

refactor package structure

SVN: 2079
parent fc0f799d
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.container;
import java.io.File;
import ch.systemsx.cisd.bds.AbstractDataStructure;
import ch.systemsx.cisd.bds.DataStructureFactory;
import ch.systemsx.cisd.bds.IStorage;
import ch.systemsx.cisd.bds.Version;
import ch.systemsx.cisd.bds.fs.FileStorage;
import ch.systemsx.cisd.bds.hdf5.HDF5Storage;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
/**
*
*
* @author Franz-Josef Elmer
*/
public class Container
{
private final File baseDir;
public Container(File baseDir)
{
assert baseDir != null : "Unspecified base directory.";
assert baseDir.isDirectory() : "Is not a directory : " + baseDir.getAbsolutePath();
this.baseDir = baseDir;
}
public AbstractDataStructure load(String name)
{
IStorage storage = createStorage(name);
storage.load();
Version version = Version.loadFrom(storage.getRoot());
return DataStructureFactory.createDataStructure(storage, version);
}
private IStorage createStorage(String name)
{
File file = new File(baseDir, name);
if (file.exists() == false)
{
throw new UserFailureException("No container name '" + name + "' exists in " + baseDir.getAbsolutePath());
}
if (file.isDirectory())
{
return new FileStorage(file);
}
File hdf5File = new File(baseDir, name + ".hdf5");
if (hdf5File.exists())
{
return new HDF5Storage(hdf5File);
}
throw new UserFailureException("Couldn't found appropriate container named '" + name + "' in "
+ baseDir.getAbsolutePath());
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.fs;
import java.io.File;
import ch.systemsx.cisd.bds.IDirectory;
import ch.systemsx.cisd.bds.INode;
/**
*
*
* @author Franz-Josef Elmer
*/
abstract class AbstractNode implements INode
{
protected final File fileNode;
AbstractNode(File file)
{
assert file != null : "Unspecified file";
this.fileNode = file;
}
public String getName()
{
return fileNode.getName();
}
public IDirectory tryToGetParent()
{
File dir = fileNode.getParentFile();
return dir == null ? null : new Directory(dir);
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.fs;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import ch.systemsx.cisd.bds.IDirectory;
import ch.systemsx.cisd.bds.INode;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
/**
*
*
* @author Franz-Josef Elmer
*/
class Directory extends AbstractNode implements IDirectory
{
public Directory(File directory)
{
super(directory);
assert directory.isDirectory() : "Not a directory: " + directory.getAbsolutePath();
}
public INode getNode(String name)
{
// TODO Auto-generated method stub
return null;
}
public IDirectory appendDirectory(String name)
{
// TODO Auto-generated method stub
return null;
}
public void appendKeyValuePair(String key, String value)
{
File file = new File(fileNode, key);
try
{
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(value);
fileWriter.close();
} catch (IOException ex)
{
file.delete();
throw new EnvironmentFailureException("Can not create " + file.getAbsolutePath() + ": " + ex);
}
}
public void appendNode(INode node)
{
// TODO Auto-generated method stub
}
public void appendRealFile(File file)
{
File newFile = new File(fileNode, file.getName());
if (file.renameTo(newFile) == false)
{
throw new EnvironmentFailureException("Couldn't move file " + file.getAbsolutePath() + " to "
+ fileNode.getAbsolutePath());
}
}
public void appendLink(String name, INode node)
{
// TODO Auto-generated method stub
}
public Iterator<INode> iterator()
{
// TODO Auto-generated method stub
return null;
}
public void extractTo(File directory) throws UserFailureException, EnvironmentFailureException
{
// TODO Auto-generated method stub
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.fs;
import java.io.File;
import ch.systemsx.cisd.bds.IDirectory;
import ch.systemsx.cisd.bds.IStorage;
/**
*
*
* @author Franz-Josef Elmer
*/
public class FileStorage implements IStorage
{
private Directory root;
public FileStorage(File folder)
{
root = new Directory(folder);
}
public IDirectory getRoot()
{
return root;
}
public void load()
{
}
public void save()
{
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.fs;
import java.io.File;
import ch.systemsx.cisd.bds.IDirectory;
import ch.systemsx.cisd.bds.ILink;
import ch.systemsx.cisd.bds.INode;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
/**
*
*
* @author Franz-Josef Elmer
*/
class Link implements ILink
{
private final String name;
private IDirectory parent;
private final INode reference;
Link(String name, INode reference)
{
this.name = name;
this.reference = reference;
}
public String getName()
{
return name;
}
void setParent(IDirectory parentOrNull)
{
parent = parentOrNull;
}
public IDirectory tryToGetParent()
{
return parent;
}
public INode getReference()
{
return reference;
}
public void extractTo(File directory) throws UserFailureException, EnvironmentFailureException
{
// TODO Auto-generated method stub
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.fs;
import java.io.File;
import java.io.IOException;
import ch.systemsx.cisd.bds.INode;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
/**
*
*
* @author Franz-Josef Elmer
*/
class NodeFactory
{
static INode createNode(File file)
{
assert file != null : "Unspecified node";
String absolutePath = file.getAbsolutePath();
try
{
String canonicalPath = file.getCanonicalPath();
if (absolutePath.equals(canonicalPath) == false)
{
return new Link(file.getName(), createNode(new File(canonicalPath)));
}
if (file.isDirectory())
{
return new Directory(file);
}
return new StringFile(file);
} catch (IOException ex)
{
throw new EnvironmentFailureException("Couldn't get canonical path of file " + absolutePath);
}
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.fs;
import java.io.File;
import ch.systemsx.cisd.bds.IFile;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
import ch.systemsx.cisd.common.utilities.FileUtilities;
/**
*
*
* @author Franz-Josef Elmer
*/
class StringFile extends AbstractNode implements IFile<String>
{
StringFile(File file)
{
super(file);
assert file.isFile() : "Not a file " + file.getAbsolutePath();
}
public String getValue()
{
return FileUtilities.loadToString(fileNode);
}
public void extractTo(File directory) throws UserFailureException, EnvironmentFailureException
{
// TODO Auto-generated method stub
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.bds.hdf5;
import java.io.File;
import ch.systemsx.cisd.bds.IDirectory;
import ch.systemsx.cisd.bds.IStorage;
/**
*
*
* @author Franz-Josef Elmer
*/
public class HDF5Storage implements IStorage
{
public HDF5Storage(File hdf5File)
{
assert hdf5File != null : "Unspecified HDF5 file.";
}
public IDirectory getRoot()
{
// TODO Auto-generated method stub
return null;
}
public void load()
{
// TODO Auto-generated method stub
}
public void save()
{
// TODO Auto-generated method stub
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment