Commit 9a16fc29 authored by RobJBarr's avatar RobJBarr
Browse files

Implemented reading styling from html file

parent a2309693
Pipeline #172737 passed with stages
in 40 seconds
......@@ -2,6 +2,7 @@ package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownResultPage;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -27,8 +28,11 @@ public class WebServer {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String isMarkdown = req.getParameter("markdown");
if (query == null) {
new IndexPage().writeTo(resp);
} else if("md".equals(isMarkdown)) {
new MarkdownResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
}
......
package ic.doc.web;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
......@@ -17,25 +20,35 @@ public class HTMLResultPage implements Page {
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
File style = new File("style.html");
BufferedReader reader = new BufferedReader(new FileReader(style));
// Header
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
writer.println(
"<html>\n" + " <head>\n" + " <title>\n" + " Welcome\n" + " </title>\n");
String line = reader.readLine();
while (line != null){
writer.println(line);
line = reader.readLine();
}
writer.println("</head><body><div class=\"container\"><h1>");
if (answer == null || answer.isEmpty()) {
writer.println("Query Not Found");
} else {
writer.println(query.toUpperCase());
}
writer.println("</h1><hr>");
// Content
if (answer == null || answer.isEmpty()) {
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
} else {
writer.println("<h1>" + query + "</h1>");
writer.println("<p>" + answer.replace("\n", "<br>") + "</p>");
}
writer.println("<a href=\"/\"><button>Back to Search Page</button></a>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
// Footer
writer.println("</body>");
writer.println("</html>");
writer.println("</div></body></html>");
}
}
package ic.doc.web;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
......@@ -9,27 +12,46 @@ public class IndexPage implements Page {
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
File style = new File("style.html");
BufferedReader reader = new BufferedReader(new FileReader(style));
// Header
writer.println("<html>");
writer.println("<head><title>Welcome</title></head>");
writer.println("<body>");
writer.println(
"<html>\n" + " <head>\n" + " <title>\n" + " Welcome\n" + " </title>\n");
String line = reader.readLine();
while (line != null){
writer.println(line);
line = reader.readLine();
}
// Content
writer.println(
"<h1>Welcome!!</h1>" +
"<p>Enter your query in the box below: " +
"<form>" +
"<input type=\"text\" name=\"q\" />" +
"<input type=\"radio\" id=\"md\" name=\"markdown\" value=\"md\">" +
"<label for=\"markdown\">Download as Markdown file?</label><br>" +
"<br><br><input type=\"submit\">" +
"</form>" +
"</p>");
" </head>\n"
+ " <body>\n"
+ " <div class=\"container\">\n"
+ " <h1>\n"
+ " Search\n"
+ " </h1>\n"
+ " <hr>\n"
+ " <form>\n"
+ " <label>Enter your query in the box below.</label>\n"
+ " <input type=\"text\" name=\"q\" />\n"
+ " <label for=\"dl\">Choose a download type:</label>\n"
+ "\n"
+ " <select name=\"markdown\" id=\"dl\">\n"
+ " <option>No Download (Default)</option>\n"
+ " <option id=\"md\" name=\"markdown\" value=\"md\">Markdown File</option>\n"
+ " <option value=\"pdf\">PDF File</option>\n"
+ " </select>\n"
+ " <input type=\"submit\">\n"
+ " </form>\n"
+ " </div>\n"
+ " </body>\n"
+ "</html>");
// Footer
writer.println("</body>");
writer.println("</html>");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment