-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleController.java
More file actions
195 lines (141 loc) · 4.65 KB
/
RuleController.java
File metadata and controls
195 lines (141 loc) · 4.65 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package org.uva.controller;
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.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import javax.xml.parsers.ParserConfigurationException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import org.implicit.Implicit;
import org.implicit.random.Condition;
import org.json.JSONException;
import org.json.JSONObject;
import org.uva.dao.oracle.TaskDAO;
import java.io.IOException;
/*
import org.jsoup.*;
import org.jsoup.helper.*;
import org.jsoup.nodes.*;
import org.jsoup.select.*;
*/
public class RuleController extends HttpServlet{
private static Category cat = Logger.getLogger(TaskDAO.class);
String msg =new String("1");
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
processRequest(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException{
msg="1";
doGet(request,response);
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ParserConfigurationException{
try {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
String str;
String key = null;
String val= null;
Object o = null;
StringBuilder sb = new StringBuilder();
BufferedReader br = request.getReader();
cat.debug("starting save file");
while( (str = br.readLine()) != null ){
sb.append(str);
}
try {
JSONObject jObj = new JSONObject(sb.toString());
Iterator it = jObj.keys(); //gets all the keys
HashMap map = new HashMap();
while(it.hasNext())
{
key = (String)it.next();
val = (String) jObj.get(key).toString();
map.put(key,val);
}
writeXML(map,request);
} catch (JSONException e1) {
// TODO Auto-generated catch block
msg = e1.getMessage();
e1.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
msg =e.getMessage();
if (cat.isDebugEnabled())
cat.debug(e.getMessage());
e.printStackTrace();
}catch (IOException e){
msg =e.getMessage();
if (cat.isDebugEnabled())
cat.debug(e.getMessage());
e.printStackTrace();
}
PrintWriter out = response.getWriter();
out.println(msg);
out.close();
}
public void processHTML(String filepath,String tr) throws IOException{
FileWriter out;
out = new FileWriter(filepath, true);
out.write(tr);
out.close();
//"http://localhost/implicit/forms/checklist.html"
//Document doc = null;
//System.out.println("before calling method");
//String html = "<html><head><title>First parse</title></head><body><p>Parsed HTML into a doc.</p></body></html>";
//System.out.println("after calling method");
//doc = Jsoup.connect("http://en.wikipedia.org/").get();
//Document doc = Jsoup.parse(html);
}
protected void writeXML(HashMap dataMap,HttpServletRequest request) throws ParserConfigurationException, IOException{
String path = (String) dataMap.get("path");
String fileName = (String) dataMap.get("FileName");
String xml = (String) dataMap.get("xml");
String submit = (String) dataMap.get("submit");
String real = (String) dataMap.get("realPath");
String ending = new String(".xml");
String pathlocale = org.uva.Implicit.REALPATH;
String filePath = new String("");
if (submit.equals("true")){
ending = ".html";
filePath = (pathlocale+"/"+path);
processHTML(filePath,xml);
}else{
filePath = (pathlocale+"/"+path+"/"+fileName+ending);
if (!real.equals("")){
filePath=real+"/"+fileName+ending;
}
File file = new File(filePath);
//File file = new File(path);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(xml);
bw.close();
}else{
msg = "21";//code for file exist
}
// System.out.println("Done");
}
}
}