import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
public class TextResource {
private String source = null;
/**
* Manages string resource file structure.
*
* @param source
*/
public TextResource(String source) {
this.source = source;
}
/**
* Appends data text to specified category.
*
* @param name
* Category name.
* @param text
* Data text.
* @return
*/
public boolean add(String name, String text) {
if (!createNameDirectory(name))
return false;
Vector<String> data = toCategoryStringVector(name);
data.add(text.trim());
return saveStringVector(name, data);
}
/**
* Checks if name category directory and data file exist. If not they will
* be created .
*
* @param name
* Category name.
* @return True if successful.
*/
private boolean createNameDirectory(String name) {
boolean ok = true;
String msg = "Can't create data category " + name;
if (name == null || name.length() == 0)
return error(msg);
File file = new File(getLocationDirectoryString(name));
if (!file.exists()) {
file.mkdirs();
}
File data = new File(getLocationFileString(name));
if (!data.exists()) {
try {
ok = data.createNewFile();
} catch (IOException e) {
return error(msg);
}
}
if (!file.exists() || !ok)
return error(msg);
return true;
}
private boolean error(String msg) {
System.err.println(msg);
return false;
}
private boolean existsCategoryDirectory(String name) {
if (name == null || name.length() == 0)
return false;
File dir = new File(getLocationDirectoryString(name));
return dir.exists();
}
/**
* @param name
* Category name.
* @return String of categories directory location.
*/
private String getLocationDirectoryString(String name) {
return source + "/" + name + "/";
}
/**
* @param name
* Category name.
* @return String of categories data location.
*/
private String getLocationFileString(String name) {
return getLocationDirectoryString(name) + "data.txt";
}
/**
* Gets data at position of specified category.
*
* @param name
* Category name.
* @param pos
* Position's id.
* @return String or null.
*/
public String get(String name, int pos) {
boolean ok = existsCategoryDirectory(name);
if (!ok)
return null;
Vector<String> data = toCategoryStringVector(name);
if (pos < 0 || pos >= data.size())
return null;
return data.get(pos);
}
/**
* @return String[] containing available categories.
*/
public String[] getAllCategories() {
File tmp = new File(source);
if (!tmp.exists())
return null;
return tmp.list();
}
public boolean insert(String name, String text, int pos) {
if (!createNameDirectory(name))
return false;
Vector<String> data = toCategoryStringVector(name);
if (pos >= data.size())
return add(name, text);
if (pos < 0)
pos = 0;
data.insertElementAt(text, pos);
return saveStringVector(name, data);
}
/**
* Removes data at position of specified category.
*
* @param name
* Category name.
* @param pos
* Positions id.
* @return Removed data if successful. Otherwise null.
*/
public String remove(String name, int pos) {
if (!createNameDirectory(name))
return null;
Vector<String> data = toCategoryStringVector(name);
if (pos < 0 || pos >= data.size())
return null;
String text = data.remove(pos);
return saveStringVector(name, data) ? text : null;
}
/**
* Removes specified category.
*
* @param name
* Category name.
* @return True if successful.
*/
public boolean removeCategory(String name) {
boolean ok = existsCategoryDirectory(name);
if (!ok)
return false;
String dir = getLocationDirectoryString(name);
FileOperation.deleteDirecoryContent(dir);
return FileOperation.deleteFile(dir);
}
private boolean saveStringVector(String name, Vector<String> data) {
if (data == null || name == null || name.length() == 0)
return false;
FileStreamOut out = new FileStreamOut(getLocationFileString(name), "write " + name + " error!");
Iterator<String> it = data.iterator();
while (it.hasNext())
out.writeLine(it.next().trim());
out.close();
return true;
}
public int sizeOfCategory(String name) {
boolean ok = existsCategoryDirectory(name);
if (!ok)
return 0;
Vector<String> data = toCategoryStringVector(name);
return data == null ? 0 : data.size();
}
/**
* @param name
* Category name.
* @return Vector<String> Containing categories data or null.
*/
public Vector<String> toCategoryStringVector(String name) {
String fileName = getLocationFileString(name);
File f = new File(fileName);
if (!f.exists())
return null;
FileStreamIn in = new FileStreamIn(fileName, "read " + name + " error!");
String line = null;
Vector<String> data = new Vector<String>();
while ((line = in.readLine()) != null)
data.add(line.trim());
in.close();
return data;
}
}