import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.*;
import java.util.AbstractCollection;
public class Wiki {
// private ConcurrentHashMap<String, SynchronizedStack<TXTFile>> repository;
private ConcurrentHashMap<String, Stack<TXTFile>> repository;
public Wiki() {
// repository = new ConcurrentHashMap<String,
// SynchronizedStack<TXTFile>>();
repository = new ConcurrentHashMap<String, Stack<TXTFile>>();
}
// HashMap to store files
private HashMap<String, TXTFile> files;
/**
* Method used to wait for input and invoke methods according to that input.
*/
public void Start()
{
System.out.println("Wiki started");
String input = "";
Scanner in = new Scanner(System.in);
// Create the txt_files folder if it does not already exist.
File dir = new File("txt_files");
if(! dir.exists())
{
dir.mkdir();
}
// Enter a loop to parse all input.
do{
System.out.println(">");
input = in.nextLine();
// Perform string matching
/*if(input.startsWith("commit"))
{
commit();
continue;
}*/
if(input.startsWith("add"))
{
/* "\\s+" this groups all whitespaces as a delimiter... so if i have the
string "Hello[space][tab]World", this should yield the strings
"Hello" and "World" and omit the empty space between the space and the tab*/
Add(input.substring(4).split("\\s+"));
continue;
}
if(input.equals("show"))
{
list();
continue;
}
// Fall through case
System.out.println("Illegal command: " + input);
}while(!input.equals("quit"));
// Cleanup.
System.out.println("Wiki is terminating...");
in.close();
}
/**
* Method used for listing all wiki files.
*/
public void list() {
File dir = new File("txt_files");
File[] wfiles = dir.listFiles();
// No files in directory.
if (wfiles == null) {
// return;
System.out.println("There no files !");
}
// Initialise streams
FileInputStream fis = null;
BufferedInputStream bis = null;
BufferedReader reader = null;
// Loop over all files in the directory.
for (File f : wfiles) {
// System.out.println(f.getName().endsWith(".txt"));
System.out.println(f.getName());
}
}
/**
* Method used for outputting the content of wiki files.
*/
/*
* private void list() { File dir = new File("txt_files"); File[] wfiles =
* dir.listFiles(); // No files in directory. if(wfiles == null) { return; }
* // Initialise streams FileInputStream fis = null; BufferedInputStream bis
* = null; BufferedReader reader = null;
*
* // Loop over all files in the directory. for(File f : wfiles) {
* if(f.getName().endsWith(".txt")) { try{ fis = new FileInputStream(f); bis
* = new BufferedInputStream(fis); reader = new BufferedReader(new
* InputStreamReader(bis)); String entry;
*
* while ((entry = reader.readLine()) != null) {
* System.out.println(f.getName().substring(0, f.getName().indexOf(".")) +
* "\t" + entry); }
*
* }catch(Exception e){ e.printStackTrace(); }finally{ try{ fis.close();
* bis.close(); reader.close(); }catch(IOException e) {
* System.out.println("Error closing streams!"); e.printStackTrace(); } } }
* } System.out.println(); }
*/
// ?? load factor = initial-capacity x .75 = number of items (round up) that
// HashMap supports
/*
* commit method is used before adding a file to the repository if accepted
* version < Wiki's newest version, an Exception is thrown HashMap contains
* all files which will be added to the repository
*/
/*
* public void commit(HashMap<String, TXTFile> files) throws Exception{
* Iterator<String> keySet = files.keySet().iterator(); while
* (keySet.hasNext()) { TXTFile file = files.get(keySet.next()); add(file);
* } }
*/
/**
* Adds a file to the repository (1).
*/
public void Add(String[] tokens) {
if (tokens.length < 2) {
System.out.println("Illegal arguments");
System.out.println("add [file_name] [content]");
System.out.println();
return;
}
// processing user input, creating a TXTFile
String fileName = tokens[0];
String content = tokens[1];
TXTFile file = new TXTFile(fileName, content);
// managing file versions
Stack<TXTFile> stack = repository.get(file.getFileName());
if (stack == null) // if there are no versions yet
{
stack = new Stack<TXTFile>();
file.setVersion(1);
TXTFile copy = file.clone();
stack.push(copy);
repository.put(file.getFileName(), stack);
}
else {
int subVersion = file.getVersion();
int latestVersion = stack.peek().getVersion();
/*
* if(subVersion == -1) throw new Exception(
* "Your file has no Version yet. You need to checkout or update before submitting files"
* );
*
* if(subVersion < latestVersion || subVersion > latestVersion)
* throw new Exception("Your file \"" +file.getFileName()
* +"\" has an outdated or not allowed version");
*/
file.setVersion(++latestVersion);
stack.push(file.clone());
}
}
/**
* Adds a file to the repository (2).
*/
/*
* public void add(TXTFile file) throws Exception { if
* (file.getFileName().length() < 1) throw new Exception("No FileName !");
*
* //SynchronizedStack<TXTFile> stack = repository.get(file.getFileName());
* Stack<TXTFile> stack = repository.get(file.getFileName());
*
* if(stack == null) { //stack = new SynchronizedStack<TXTFile>(); stack =
* new Stack<TXTFile>(); file.setVersion(0); TXTFile copy =
* file.clone(file); stack.push(file.clone(file));
* repository.put(file.getFileName(), stack); }
*
* else { int subVersion = file.getVersion(); int latestVersion =
* stack.peek().getVersion();
*
* if(subVersion == -1) throw new Exception(
* "Your file has no Version yet. You need to checkout or update before submitting files"
* );
*
* if(subVersion < latestVersion || subVersion > latestVersion) throw new
* Exception("Your file \"" +file.getFileName()
* +"\" has an outdated or not allowed version");
*
* file.setVersion(++latestVersion); stack.push(file.clone(file)); } }
*/
// removes a file from the repository
public void remove(String fileName) throws Exception {
if (!repository.containsKey(fileName))
throw new Exception("The file \"" + fileName
+ "\" doesnt exist in the Repository");
repository.remove(fileName);
}
// Update method to update a single file to the newest version available
public TXTFile update(String filename) throws Exception {
if (!repository.containsKey(filename))
throw new Exception("The file \"" + filename
+ "\" doesnt exist in the Repository");
// SynchronizedStack<TXTFile> stack = repository.get(filename);
Stack<TXTFile> stack = repository.get(filename);
TXTFile file = stack.peek();
//try {
return file.clone();
//} catch (CloneNotSupportedException ex) {
// return null;
//}
}
// Reverts a single file to the version given as parameter
public TXTFile revert(String filename, int version) throws Exception {
if (!repository.containsKey(filename))
throw new Exception("The file \"" + filename
+ "\" doesnt exist in the Repository");
// SynchronizedStack<JavaCode> stack = repository.get(filename);
Stack<TXTFile> stack = repository.get(filename);
if (version < 0 || version >= stack.size())
throw new Exception("Please check your version. Your Version: \""
+ version + "\"");
TXTFile file = stack.get(version);
Boolean found = false;
if (file.getVersion() != version) {
Iterator<TXTFile> iterator = stack.iterator();
while (iterator.hasNext())
if (iterator.next().getVersion() == version) {
found = true;
break;
}
if (!found)
throw new Exception("The version \"" + version
+ "\" of the file \"" + filename
+ "\" doesn't exist in the Repository");
}
return file.clone();
}
}