-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathAddConstituent.jsp
147 lines (137 loc) · 5.85 KB
/
AddConstituent.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<%@ include file="Prelude.jsp" %>
<%
/** This code is copyright Teknowledge (c) 2003, Articulate Software (c) 2003-2017,
Infosys (c) 2017-present.
This software is released under the GNU Public License
<http://www.gnu.org/copyleft/gpl.html>.
Please cite the following article in any publication with references:
Pease A., and Benzmüller C. (2013). Sigma: An Integrated Development Environment
for Logical Theories. AI Communications 26, pp79-97. See also
http://github.com/ontologyportal
*/
if (!role.equalsIgnoreCase("admin"))
response.sendRedirect("KBs.jsp");
else {
String kbDir = mgr.getPref("kbDir");
File kbDirFile = new File(kbDir);
MultipartParser mpp = null;
int postSize = Integer.MAX_VALUE;
Part requestPart = null;
String fileName = "";
String baseName = "";
String overwrite = mgr.getPref("overwrite");
boolean overwriteP = (StringUtil.isNonEmptyString(overwrite)
&& overwrite.equalsIgnoreCase("yes"));
String extension = "";
File existingFile = null;
File outfile = null;
long writeCount = -1L;
try {
boolean isError = false;
mpp = new MultipartParser(request, postSize, true, true);
while ((requestPart = mpp.readNextPart()) != null) {
String paramName = requestPart.getName();
if (paramName == null)
paramName = "";
if (requestPart.isParam()) {
ParamPart pp = (ParamPart) requestPart;
if (paramName.equalsIgnoreCase("kb"))
kbName = pp.getStringValue();
}
else if (requestPart.isFile()) {
FilePart fp = (FilePart) requestPart;
fileName = fp.getFileName();
int lidx = fileName.lastIndexOf(".");
baseName = ((lidx != -1)
? fileName.substring(0, lidx)
: fileName);
extension = ((lidx != -1)
? fileName.substring(lidx,fileName.length())
: ".kif");
existingFile = new File(kbDirFile, (baseName + extension));
System.out.println("INFO in AddConstituent.jsp: filename: " + fileName);
outfile = StringUtil.renameFileIfExists(existingFile);
FileOutputStream fos = new FileOutputStream(outfile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
writeCount = -1L;
try {
writeCount = fp.writeTo(bos);
bos.flush();
bos.close();
fos.close();
}
catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
String errStr = "";
if (overwriteP && !existingFile.getCanonicalPath().equalsIgnoreCase(outfile.getCanonicalPath())) {
boolean overwriteSucceeded = false;
try {
if (existingFile.delete() && outfile.renameTo(existingFile)) {
outfile = existingFile;
overwriteSucceeded = outfile.canRead();
}
}
catch (Exception owex) {
owex.printStackTrace();
}
if (!overwriteSucceeded)
errStr = "Error: Could not overwrite existing consituent file";
}
if (StringUtil.emptyString(errStr)) {
if (StringUtil.emptyString(kbName))
errStr = "Error in AddConstituent.jsp: No knowledge base name specified";
else if ((outfile == null) || !outfile.canRead())
errStr = "Error in AddConstituent.jsp: The constituent file could not be saved or cannot be read";
}
if (StringUtil.isNonEmptyString(errStr)) {
mgr.setError(mgr.getError() + "\n<br/>" + errStr + "\n<br/>");
System.out.println(errStr);
isError = true;
response.sendRedirect("KBs.jsp");
}
else {
boolean newKB = false;
if (!mgr.existsKB(kbName))
newKB = true;
else
kb = mgr.getKB(kbName);
if (newKB) {
ArrayList<String> list = new ArrayList<String>();
list.add(outfile.getCanonicalPath());
mgr.loadKB(kbName, list);
}
else { // Remove the constituent, if it is already present.
//ListIterator<String> lit = kb.constituents.listIterator();
//while (lit.hasNext()) {
// String constituent = lit.next();
// if (StringUtil.isNonEmptyString(baseName) && constituent.contains(baseName))
// lit.remove();
//}
//kb.addNewConstituent(outfile.getCanonicalPath());
kb.addConstituent(outfile.getCanonicalPath());
kb.checkArity();
if (mgr.getPref("cache").equalsIgnoreCase("yes")) {
kb.kbCache.buildCaches();
kb.kbCache.writeCacheFile();
}
kb.loadEProver();
KBmanager.getMgr().writeConfiguration();
}
}
if (!isError)
response.sendRedirect("Manifest.jsp?kb=" + kbName);
}
catch (Exception e) {
String errStr = "ERROR in AddConstituent.jsp: " + e.getMessage();
mgr.setError(mgr.getError() + "\n<br/>" + errStr + "\n<br/>");
System.out.println(errStr);
System.out.println(" kbName == " + kbName);
System.out.println(" fileName == " + fileName);
e.printStackTrace();
response.sendRedirect("KBs.jsp");
}
}
%>