package werti.util; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.io.UnsupportedEncodingException; import org.apache.uima.jcas.JCas; import org.apache.log4j.Logger; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import werti.server.ActivityConfiguration; /** * Produces an HTML document with enhancements from a CAS containing * Enhancements. * * @author Adriane Boyd * */ public class HTMLEnhancer { private JCas cas; private static final Logger log = Logger.getLogger(HTMLEnhancer.class); /** * @param cCas CAS with annotations for the topic */ public HTMLEnhancer(final JCas cCas) { cas = cCas; } /** * Converts an HTML CAS document with Enhancements to an HTML string. * * @return an HTML string containing enhancements */ public String enhance(final String activity, final String baseurl, HttpServletRequest req, ActivityConfiguration config, String servletContextName) throws UnsupportedEncodingException{ HashMap dict = new HashMap(); // translations of topics and activities to North Sámi dict.put("NounsAll", "Nouns (all)"); dict.put("SubstantiveSingular", "Substantive singular"); dict.put("SubstantivePlural", "Substantive plural"); dict.put("NounsNom", "Nominative nouns"); dict.put("NounsAcc", "Accusative nouns"); dict.put("NounsGen", "Genitive nouns"); dict.put("NounsLoc", "Locative nouns"); dict.put("NounsDat", "Dative nouns"); dict.put("NounsIns", "Instrumental nouns"); dict.put("PresFutInd", "Verb conjugation"); dict.put("Conjunctions", "Conjunctions"); dict.put("colorize", "Higlight the words"); dict.put("click", "Click on the words!"); dict.put("mc", "Select the correct word form!"); dict.put("cloze", "Fill in the correct word form!"); String enhancement = req.getParameter("client.enhancement"); String activityCat = activity.toLowerCase(); // get the translations of the topic and the exercise type from the small dictionary String activity_eng = dict.get(activity); String enhancement_eng = dict.get(enhancement); String htmlString = EnhancerUtils.casToEnhanced(cas, enhancement); // replace tags with wertiview spans // (this should probably be done with a real tree traversal, but it was causing me headaches and // a search and replace is probably sufficient and quicker) htmlString = htmlString.replace("", ""); htmlString = htmlString.replace("", ""); Document htmlDoc = Jsoup.parse(htmlString); // add base url Element base = htmlDoc.createElement("base"); base.attr("href", baseurl); htmlDoc.head().appendChild(base); // Write the chosen topic and activity into the page title. So the user has a short reminder about the exercise. String topic_activity = activity_eng + ": " + enhancement_eng; String customised_title = new String(topic_activity.getBytes(), "UTF-8"); // encode the title string as utf8 Element title = htmlDoc.select("title").first(); title.text(customised_title); // add js libraries String thisUrl = req.getRequestURL().toString(); log.info("requestURL:"+thisUrl); thisUrl = thisUrl.replace("/WERTiServlet",""); log.info("removed WERTiServlet:"+thisUrl); //thisUrl = thisUrl.replaceFirst("(?<=" + servletContextName + ").*", ""); if (activity.matches("Arts") || activity.matches("Dets")) { activityCat = "pos"; } final String jqueryJS = ""; final String wertiviewJS = ""; final String blurJS = ""; final String notificationJS = ""; final String wertiviewCSS = ""; final String libJS = ""; final String activityJS = ""; final String topicJS = ""; final String loadJS = "\n"; htmlDoc.head().append(jqueryJS); htmlDoc.head().append(wertiviewJS); htmlDoc.head().append(blurJS); htmlDoc.head().append(notificationJS); htmlDoc.head().append(wertiviewCSS); htmlDoc.head().append(libJS); htmlDoc.head().append(activityJS); htmlDoc.head().append(topicJS); htmlDoc.head().append(loadJS); htmlDoc.select("span.wertiview").select("span").attr("style", EnhancerUtils.addedSpanStyle); return htmlDoc.html(); } }