XMLRepo repo = XMLRepo.instance(pageContext);
User user = (User) session.getAttribute("user");
if (user == null) {
return;
}
{
boolean saveProps = false;
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String root = session.getServletContext().getContextPath();
String libURL = "/users/" + user.getID() + "/library";
String libPath = session.getServletContext()
.getRealPath(libURL);
File file = new File(libPath);
if (!file.exists()) {
file.mkdir();
new File(libPath, "modules").mkdir();
new File(libPath, "mods.d").mkdir();
}
file = new File(libPath + "/mods.d", "globals.conf");
if (!file.exists()) {
file.createNewFile();
FileOutputStream fout = new FileOutputStream(file, false);
fout.write(("[globals]\nowner=" + user.getID()).getBytes());
fout.close();
}
boolean reload = false;
String action = request.getParameter("action");
if ("newMod".equals(action)) {
String newName = request.getParameter("newName");
if (newName != null) {
String fileName = newName.replace(' ', '_');
file = new File(libPath + "/mods.d", fileName+".conf");
if (!file.exists()) {
FileOutputStream fout = new FileOutputStream(file, false);
fout.write(("["+fileName+"]\nModDrv=RawText\nDataPath=modules/"+fileName+"\nDescription="+newName+"\nAbout="+newName+"\n").getBytes());
fout.close();
new File(libPath + "/modules", fileName).mkdir();
new File(libPath + "/modules/"+fileName, "cache").mkdir();
repo.save(new Document("scribe", fileName));
reload = true;
}
}
}
else if ("selectMod".equals(action)) {
String selName = request.getParameter("selName");
if (selName != null) {
session.setAttribute("modName", selName);
}
}
String modName = (String)session.getAttribute("modName");
if ("delete".equals(action)) {
new File(libPath + "/mods.d", modName+".conf").delete();
new File(libPath + "/modules/"+modName, "module.properties").delete();
modName = null;
reload = true;
}
SWMgr mgr = SWMgr.getInstance(libPath);
if (reload) {
mgr.config = null;
mgr.Load();
}
SWConfig swconfig = mgr.config;
HashMap modSection = null;
Document modDoc = null;
if (modName != null) {
modSection = (HashMap)swconfig.sections.get(modName);
modDoc = (Document)repo.get(new Document(user.getID(), modName), false);
}
if ("saveDoc".equals(action)) {
String val = request.getParameter("baseURL");
if (val != null) {
val = val.replace(" ", "%20");
modDoc.setBaseURL(val);
}
val = request.getParameter("ganame");
if (val != null) {
modDoc.setGAName(val);
}
val = request.getParameter("desc");
if (val != null) {
modDoc.setDescription(val);
}
val = request.getParameter("abbr");
if (val != null) {
modDoc.setAbbrevPref(val);
}
val = request.getParameter("otherAbbr");
if (val != null) {
modDoc.setAbbrevOther(val);
}
val = request.getParameter("dateMean");
if (val != null) {
modDoc.setDateMean(val);
}
val = request.getParameter("dateRange");
if (val != null) {
modDoc.setDateRange(val);
}
saveProps = true;
}
if ("loadAllImages".equals(action)) {
String d="";
URL url = new URL(modDoc.getBaseURL());
try {
InputStream responseStream = url.openStream();
byte line[] = new byte[254];
int len = 0;
d = "";
while ((len = responseStream.read(line)) != -1) {
d += new String(line, 0, len, "UTF-8");
}
responseStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int i = 0;
int baseCount = modDoc.getPages().length;
while (true) {
int last = i;
i = d.indexOf("href", last);
if (i < 0) i = d.indexOf("HREF", last);
if (i > -1) {
int s = d.indexOf('"', i);
if (s < 0) s = d.indexOf('\'', i);
if (s > -1) {
s++;
int e = d.indexOf('"', s);
if (e < 0) e = d.indexOf('\'', s);
if (e > -1) {
boolean valid = false;
String ref = d.substring(s, e);
if ( ref.toLowerCase().endsWith(".jpg")
|| ref.toLowerCase().endsWith(".bmp")
|| ref.toLowerCase().endsWith(".tif")
|| ref.toLowerCase().endsWith(".tiff")
|| ref.toLowerCase().endsWith(".jpeg")
|| ref.toLowerCase().endsWith(".gif")
) {
if (ref.startsWith(url.toString())) {
ref = ref.substring(url.toString().length());
baseCount++;
valid = true;
}
else if (ref.startsWith(url.getPath())) {
ref = ref.substring(url.getPath().length());
baseCount++;
valid = true;
}
// find new baseURL if we haven't yet added any images at out current base
else if (baseCount < 1) {
int us = ref.lastIndexOf('/');
String newBase = ref.substring(0, us+1);
ref = ref.substring(us+1);
newBase = newBase.replace(" ", "%20");
modDoc.setBaseURL(newBase);
url = new URL(newBase);
baseCount = 1;
saveProps = true;
valid = true;
}
if (valid) {
boolean exists = false;
for (Document.Page p : modDoc.getPages()) {
if (p.getURL().equals(ref)) {
exists = true;
break;
}
}
if (!exists) {
Document.Page p = modDoc.setPage(new Document.Page(Integer.toString(modDoc.getHighestPageID()+1)));
p.setURL(ref);
p.setDescription("");
p.setBibleCoverage("");
saveProps = true;
}
}
}
i = e;
}
}
}
else break;
last = ++i;
}
}
if ("saveImage".equals(action)) {
String imgNum = request.getParameter("imgNum");
if (imgNum != null) {
Document.Page p = modDoc.getPage(imgNum);
if (p != null) {
p.setDescription(request.getParameter("desc"));
p.setBibleCoverage(request.getParameter("vrange"));
saveProps = true;
}
}
}
if (saveProps) {
repo.save(modDoc);
}
}
success