package org.mcraig.cs445.refentry;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
/**
* Transforms sets of RefEntry documents from XML to XHTML for
* consumption by the client browser.
* @author <a href="mailto:mark@mcraig.org">Mark Craig</a>
*/
class RefEntryTransformer implements Serializable {
/**
* Default constructor.
* @param path Servlet URL.
*/
RefEntryTransformer(String path) {
Server = path;
}
/** Servlet URL as a string. */
private String Server;
/**
* Transform a single RefEntry resulting from a search.
* @param source RefEntry to transform.
* @param search Literal search string from client request.
*/
String transform(RefEntry source, String search) {
return (
getHead(
source.getRefName() +
"(" +
source.getManVolNum() +
") " +
"- " +
source.getRefPurpose()
) +
getSearchBar(search) +
source.getXHTML() +
getTail()
);
}
/**
* Transform a set of RefEntry results from a search.
* @param refentries Set of documents to transform.
* @param search Literal search string from client request.
*/
String transform(HashSet refentries, String search) {
// Dispatch to other methods unless search has
// returned more than one entry.
if (refentries.isEmpty()) return transform(search);
else if (refentries.size() == 1) {
Iterator iter = refentries.iterator();
RefEntry entry = (RefEntry)iter.next();
return transform(entry, search);
}
return (
getHead("Search Results") +
getSearchBar(search) +
getList(refentries) +
getTail()
);
}
/** Serve up XHTML for a search that returned nothing.
* @param search Literal search string from client request.
*/
String transform(String search) {
return (
getHead("Search Results") +
getSearchBar(search) +
"<h1 align=\"center\">SEARCH RESULTS</h1>\n" +
"<p>No results were found at " +
Server +
" \n" +
" for search string <tt>" +
search +
"</tt>.</p>\n" +
getTail()
);
}
/** Generate the head of the page.
* @param title Title element content for the page.
*/
private String getHead(String title) {
return (
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
"<head>\n" +
"<title>" +
title +
"</title>\n" +
"</head>\n" +
"<body bgcolor=\"white\">\n"
);
}
/** Generate the search bar form.
* @param search Literal search string from client request.
*/
private String getSearchBar(String search) {
return (
"<form>\n" +
"<input name=\"SrchString\" size=\"60\" value=\"" +
search +
"\" />\n" +
"<input type=\"submit\" value=\"Search\" />\n" +
"</form>\n" +
"<hr />\n"
);
}
/** Generate a list of search results.
* @param refentries Set of documents to transform.
*/
private String getList(HashSet refentries) {
RefEntry[] refs = new RefEntry[refentries.size()];
Iterator iter = refentries.iterator();
for (int i = 0; iter.hasNext(); ++i) {
refs[i] = (RefEntry)iter.next();
}
Arrays.sort(refs);
String list = new String("<ol>");
for (int i = 0; i < refentries.size(); ++i) {
list = list.concat(
"<li><a href=\"" +
Server +
"?RefEntry=" +
refs[i].getRefName() +
"-" +
refs[i].getManVolNum() +
"\">" +
refs[i].getRefName() +
"(" +
refs[i].getManVolNum() +
")</a>" +
"-" +
refs[i].getRefPurpose() +
"</li>\n"
);
}
list = list.concat("</ol>\n");
return list;
}
/** Generate the tail of the page. */
private String getTail() {
Date date = new Date();
return (
"<table border=\"0\" width=\"100%\">\n" +
"<tr>\n" +
"<td align=\"left\">Generated [" +
date.toString() +
"]</td>\n" +
"<td align=\"right\">Server [" +
Server +
"]</td>\n" +
"</tr>\n" +
"</table>\n" +
"</body>\n" +
"</html>"
);
}
} |