Commit 8cc101b1 authored by Joshua Priestley's avatar Joshua Priestley
Browse files

Merge remote-tracking branch 'origin/java_extensions'

parents c33b029c 9c8221b1
Pipeline #172901 passed with stages
in 35 seconds
......@@ -2,21 +2,50 @@ package ic.doc;
public class QueryProcessor {
public String process(String query) {
StringBuilder results = new StringBuilder();
if (query.toLowerCase().contains("shakespeare")) {
results.append("William Shakespeare (26 April 1564 - 23 April 1616) was an\n" +
"English poet, playwright, and actor, widely regarded as the greatest\n" +
"writer in the English language and the world's pre-eminent dramatist. \n");
results.append(System.lineSeparator());
}
public String process(String query) {
StringBuilder results = new StringBuilder();
if (query.toLowerCase().contains("shakespeare")) {
results.append("William Shakespeare (26 April 1564 - 23 April 1616) was an\n" +
"English poet, playwright, and actor, widely regarded as the greatest\n" +
"writer in the English language and the world's pre-eminent dramatist. \n");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("asimov")) {
results.append("Isaac Asimov (2 January 1920 - 6 April 1992) was an\n" +
"American writer and professor of Biochemistry, famous for\n" +
"his works of hard science fiction and popular science. \n");
results.append(System.lineSeparator());
}
return results.toString();
if (query.toLowerCase().contains("asimov")) {
results.append("Isaac Asimov (2 January 1920 - 6 April 1992) was an\n" +
"American writer and professor of Biochemistry, famous for\n" +
"his works of hard science fiction and popular science. \n");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("muhammad")) {
results.append("Muhammad (570 - 632 AD)" +
"Founder and the central human figure of Islam, regarded by Muslims as a prophet of God and the last messenger\n"
+
"Also active as a social reformer, diplomat, merchant, philosopher, orator, legislator, military leader.");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("newton")) {
results.append("Isaac Newton (1643 - 1727)" +
"English physicist, mathematician, astronomer, natural philosopher, alchemist, and theologian.\n" +
"His law of universal gravitation and three laws of motion laid the groundwork for classical mechanics.");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("jesus")) {
results.append("Jesus of Nazareth (7-2 BC - 26-36 AD)\n" +
"The central figure of Christianity, revered by Christians as the Son of God and the incarnation of God.\n" +
"Also regarded as a major prophet in Islam.");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("buddha")) {
results.append("Buddha (563 - 483 BC)\n" +
"Spiritual teacher and philosopher from ancient India. Founder of Buddhism.");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("einstein")) {
results.append("Albert Einstein (14 March 1879 - 18 April 1955\n" +
"German-born theoretical physicist, best known for his theory of relativity and specifically mass–energy equivalence, expressed by the equation E = mc2.");
results.append(System.lineSeparator());
}
return results.toString();
}
}
......@@ -2,6 +2,8 @@ package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownResultPage;
import ic.doc.web.PDFResultPage;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -27,8 +29,13 @@ public class WebServer {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String downloadType = req.getParameter("markdown");
if (query == null) {
new IndexPage().writeTo(resp);
} else if ("pdf".equals(downloadType)){
new PDFResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else if("md".equals(downloadType)) {
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;
......@@ -18,24 +21,24 @@ public class HTMLResultPage implements Page {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
// Header
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
writer.println(
"<html>\n" + " <head><link rel = \"stylesheet\" href=\"https://drive.google.com/uc?export=view&id=1b-2_6zWL_6F360SBlZrdIGrpsuWgNkKz\" />\n" + " <title>\n" + " Welcome\n" + " </title>\n");
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;
......@@ -11,23 +14,36 @@ public class IndexPage implements Page {
PrintWriter writer = resp.getWriter();
// Header
writer.println("<html>");
writer.println("<head><title>Welcome</title></head>");
writer.println("<body>");
writer.println(
"<html>\n" + " <head><link rel = \"stylesheet\" href=\"https://drive.google.com/uc?export=view&id=1b-2_6zWL_6F360SBlZrdIGrpsuWgNkKz\" />\n" + " <title>\n" + " Welcome\n" + " </title>\n");
// Content
writer.println(
"<h1>Welcome!!</h1>" +
"<p>Enter your query in the box below: " +
"<form>" +
"<input type=\"text\" name=\"q\" />" +
"<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>");
}
}
package ic.doc.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
public class MarkdownResultPage implements Page {
private final String query;
private final String answer;
public MarkdownResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
File markdown = getFile();
resp.setContentType("file/markdown");
resp.setHeader("Content-Disposition", "Inline; filename=" + query + ".md");
outputStreams(markdown, resp);
// Remove temporary file
markdown.delete();
}
void outputStreams(File markdown, HttpServletResponse resp) throws IOException {
// Byte streams
InputStream inputStream = new FileInputStream(markdown);
inputStream.transferTo(resp.getOutputStream());
}
File getFile() throws IOException {
File markdown = File.createTempFile("result", ".md");
// Header
FileWriter writer = new FileWriter(markdown);
writer.write("<h1>" + query + "<h1>");
writer.write("-------------------------------------------------<br>");
// Content
if (answer == null || answer.isEmpty()) {
writer.write("Sorry, we did not understand your query.\n");
} else {
writer.write("<p>" + answer + "</p>");
}
writer.flush();
return markdown;
}
}
package ic.doc.web;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
public class PDFResultPage extends MarkdownResultPage {
public PDFResultPage(String query, String answer) {
super(query, answer);
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
File markdown = getFile();
String currentDir = System.getProperty("user.dir");
ProcessBuilder pb = new ProcessBuilder("pandoc", "-s", "-o",
currentDir + "/result.pdf", markdown.getAbsolutePath());
Process p = pb.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(System.getProperty("user.dir"));
File pdf = new File(currentDir + "/result.pdf");
outputStreams(pdf, resp);
// Remove temporary file
markdown.delete();
pdf.delete();
}
}
<style>body {
font-family: Helvetica, sans-serif;
}
.container {
width: 500px;
height: 350px;
background-color: rgb(238, 238, 238);
position: fixed;
border: 2px solid rgb(190, 190, 190);
top: 50%;
left: 50%;
transform: translate(-50%, -90%);
border-radius: 20px;
padding:40px;
}
h1 {
font-family: Helvetica, sans-serif;
margin-top: 30px;
}
hr {
border: 3px solid rgb(190, 190, 190);
border-radius: 5px;
}
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit], button {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
form {
margin-top: 30px;
}</style>
\ No newline at end of file
......@@ -25,6 +25,30 @@ public class QueryProcessorTest {
assertThat(queryProcessor.process("Asimov"), containsString("science fiction"));
}
@Test
public void knowsAboutJesus() throws Exception {
assertThat(queryProcessor.process("Jesus"), containsString("Christianity"));
}
@Test
public void knowsAboutMuhammad() throws Exception {
assertThat(queryProcessor.process("Muhammad"), containsString("Islam"));
}
@Test
public void knowsAboutNewton() throws Exception {
assertThat(queryProcessor.process("Newton"), containsString("laws of motion"));
}
@Test
public void knowsAboutBuddha() throws Exception {
assertThat(queryProcessor.process("Buddha"), containsString("Buddhism"));
}
@Test
public void knowsAboutEinstein() throws Exception {
assertThat(queryProcessor.process("Einstein"), containsString("relativity"));
}
@Test
public void isNotCaseSensitive() throws Exception {
assertThat(queryProcessor.process("shakespeare"), containsString("playwright"));
......
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