List of usage examples for org.apache.commons.lang3 StringEscapeUtils escapeHtml4
public static final String escapeHtml4(final String input)
Escapes the characters in a String using HTML entities.
For example:
"bread" & "butter"
"bread" & "butter"
.
From source file:podd.model.entity.impl.AbstractPoddEntityImpl.java
@Override public String escapeLabelForHTML() { return StringEscapeUtils.escapeHtml4(label).replace("\n", "<br />").replace("\r", ""); }
From source file:psiprobe.jsp.OutTag.java
/** * Prints the./* www. j a v a 2 s .c o m*/ * * @param displayValue the display value * @param out the out * @throws JspException the jsp exception */ private void print(String displayValue, JspWriter out) throws JspException { try { if (maxLength != -1 && displayValue.length() > maxLength) { String newValue; if (ellipsisRight) { newValue = displayValue.substring(0, maxLength - 3) + "..."; } else { newValue = "..." + displayValue.substring(displayValue.length() - maxLength + 3); } String title = StringEscapeUtils.escapeHtml4(displayValue); out.print("<span title=\"" + title + "\">" + newValue + "</span>"); } else { out.print(displayValue); } } catch (IOException e) { throw new JspException(e); } }
From source file:pt.ua.dicoogle.sdk.settings.types.CheckboxWithHint.java
public String toHTMLString(String htmlElementID) { String result = ""; result += "<label class=\"checkbox\" title=\"" + StringEscapeUtils.escapeHtml4(hint) + "\">"; result += "<input type=\"checkbox\" id=\"" + htmlElementID + "\" name=\"" + htmlElementID + "\" " + (checked ? "checked=\"checked\"" : "") + " /> " + StringEscapeUtils.escapeHtml4(text); result += "</label>"; return result; }
From source file:pt.ua.dicoogle.sdk.settings.types.ComboBox.java
public String toHTMLString(String htmlElementID) { String result = ""; result += "<select id=\"" + htmlElementID + "\" name=\"" + htmlElementID + "\">"; for (Map.Entry<String, String> elem : elements.entrySet()) { result += "<option value=\"" + StringEscapeUtils.escapeHtml4(elem.getValue()) + "\" " + ((current.equalsIgnoreCase(elem.getKey())) ? "selected=\"selected\"" : "") + ">"; result += StringEscapeUtils.escapeHtml4(elem.getKey()); result += "</option>"; }//from w w w.ja v a 2 s .c om result += "</select>"; return result; }
From source file:pt.ua.dicoogle.sdk.settings.types.DataTable.java
@Override public synchronized String toHTMLString(String htmlElementID) { String result = ""; result += "<div id=\"" + htmlElementID + "\" class=\"data-table\">"; // loop through all the rows and add their cell data and column name to the result for (int row = 0; row < data.length; row++) { result += "<div class=\"data-table-row\">"; for (int column = 0; column < columns.length; column++) { String columnID = Utils.getHTMLElementIDFromString(htmlElementID + " " + columns[column]); result += "<div style=\"margin-right: 6px; display: inline-block;\">"; result += "<span>"; result += StringEscapeUtils.escapeHtml4(columns[column]); result += "</span>"; result += "<span style=\"padding-left: 4px;\">"; result += Utils.getHTMLInputFromType(columnID, data[row][column], true); result += "</span>"; result += "</div>"; }//from ww w .j a va2s. com // add a button to remove each element, except the first one result += "<button type=\"button\" class=\"btn btn-small btn-danger removeButton\" onclick=\"removeDataTableRow(this.parentNode.parentNode, this.parentNode);\" " + ((row == 0) ? "hidden style=\"display: none !important;\"" : "") + ">Remove</button>"; result += "</div>"; } result += "</div>"; // add one button to add another element to the table result += "<button type=\"button\" class=\"btn btn-small btn-success\" onclick=\"addDataTableRow(document.getElementById('" + htmlElementID + "'));\">Add</button><br />"; // FIXME replace the getelementbyid with this.previoussibling ?!? http://www.w3schools.com/dom/prop_element_previoussibling.asp return result; }
From source file:pt.ua.dicoogle.sdk.settings.Utils.java
/** * Based on the "real" type/class of the plugin setting Object returns the appropriate form input for it. * * @param name the Form Name of this HTML input. * @param value a Object.//from w w w . ja v a2s.c o m * @param isArrayElement if this value Object is part of a another multiple value Object. * @return the appropriate form input for the supplied Object. */ public static String getHTMLInputFromType(String name, Object value, boolean isArrayElement) { String result = "<input name=\"" + name + (isArrayElement ? "[]" : "") + "\" "; if (value == null) { result += "type=\"text\" value=\"\" />"; } else if (value.getClass().equals(Integer.class)) { result += "type=\"number\" value=\"" + ((Integer) value).intValue() + "\" />"; } else if (value.getClass().equals(Float.class)) { result += "type=\"number\" value=\"" + ((Float) value).floatValue() + "\" />"; } else if (value.getClass().equals(Boolean.class)) { result += "type=\"checkbox\" " + (((Boolean) value).booleanValue() ? "checked=\"checked\"" : "") + " />"; } else if (value instanceof GenericSetting) { result = ((GenericSetting) value).toHTMLString(name + (isArrayElement ? "[]" : "")); } else // NOTE add extra data type classes here, if needed if (value.getClass().equals(String.class)) { result += "type=\"text\" value=\"" + StringEscapeUtils.escapeHtml4((String) value) + "\" />"; } else // unrecognized type/class { //throw new ClassCastException("Unsupported class \"" + value.getClass().getName() + "\""); //result += "type=\"text\" value=\"" + StringEscapeUtils.escapeHtml4( value.toString()) + "\" />"; result += StringEscapeUtils.escapeHtml4(value.toString()); } return result; }
From source file:rmteles.learning.mongodb.blog.BlogController.java
private void initializeRoutes() throws IOException { // this is the blog home page get("/", new FreemarkerBasedRoute("blog_template.ftl") { @Override/*from w ww. j av a2 s. c o m*/ public void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String username = sessionDAO.findUserNameBySessionId(getSessionCookie(request)); List<Document> posts = blogPostDAO.findByDateDescending(10); SimpleHash root = new SimpleHash(); root.put("myposts", posts); if (username != null) { root.put("username", username); } template.process(root, writer); } }); // used to display actual blog post detail page get("/post/:permalink", new FreemarkerBasedRoute("entry_template.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String permalink = request.params(":permalink"); System.out.println("/post: get " + permalink); Document post = blogPostDAO.findByPermalink(permalink); if (post == null) { response.redirect("/post_not_found"); } else { // empty comment to hold new comment in form at bottom of blog entry detail page SimpleHash newComment = new SimpleHash(); newComment.put("name", ""); newComment.put("email", ""); newComment.put("body", ""); SimpleHash root = new SimpleHash(); root.put("post", post); root.put("comments", newComment); template.process(root, writer); } } }); // handle the signup post post("/signup", new FreemarkerBasedRoute("signup.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String email = request.queryParams("email"); String username = request.queryParams("username"); String password = request.queryParams("password"); String verify = request.queryParams("verify"); HashMap<String, String> root = new HashMap<String, String>(); root.put("username", StringEscapeUtils.escapeHtml4(username)); root.put("email", StringEscapeUtils.escapeHtml4(email)); if (validateSignup(username, password, verify, email, root)) { // good user System.out.println("Signup: Creating user with: " + username + " " + password); if (!userDAO.addUser(username, password, email)) { // duplicate user root.put("username_error", "Username already in use, Please choose another"); template.process(root, writer); } else { // good user, let's start a session String sessionID = sessionDAO.startSession(username); System.out.println("Session ID is" + sessionID); response.raw().addCookie(new Cookie("session", sessionID)); response.redirect("/welcome"); } } else { // bad signup System.out.println("User Registration did not validate"); template.process(root, writer); } } }); // present signup form for blog get("/signup", new FreemarkerBasedRoute("signup.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { SimpleHash root = new SimpleHash(); // initialize values for the form. root.put("username", ""); root.put("password", ""); root.put("email", ""); root.put("password_error", ""); root.put("username_error", ""); root.put("email_error", ""); root.put("verify_error", ""); template.process(root, writer); } }); get("/welcome", new FreemarkerBasedRoute("welcome.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String cookie = getSessionCookie(request); String username = sessionDAO.findUserNameBySessionId(cookie); if (username == null) { System.out.println("welcome() can't identify the user, redirecting to signup"); response.redirect("/signup"); } else { SimpleHash root = new SimpleHash(); root.put("username", username); template.process(root, writer); } } }); // will present the form used to process new blog posts get("/newpost", new FreemarkerBasedRoute("newpost_template.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { // get cookie String username = sessionDAO.findUserNameBySessionId(getSessionCookie(request)); if (username == null) { // looks like a bad request. user is not logged in response.redirect("/login"); } else { SimpleHash root = new SimpleHash(); root.put("username", username); template.process(root, writer); } } }); // handle the new post submission post("/newpost", new FreemarkerBasedRoute("newpost_template.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String title = StringEscapeUtils.escapeHtml4(request.queryParams("subject")); String post = StringEscapeUtils.escapeHtml4(request.queryParams("body")); String tags = StringEscapeUtils.escapeHtml4(request.queryParams("tags")); String username = sessionDAO.findUserNameBySessionId(getSessionCookie(request)); if (username == null) { response.redirect("/login"); // only logged in users can post to blog } else if (title.equals("") || post.equals("")) { // redisplay page with errors HashMap<String, String> root = new HashMap<String, String>(); root.put("errors", "post must contain a title and blog entry."); root.put("subject", title); root.put("username", username); root.put("tags", tags); root.put("body", post); template.process(root, writer); } else { // extract tags ArrayList<String> tagsArray = extractTags(tags); // substitute some <p> for the paragraph breaks post = post.replaceAll("\\r?\\n", "<p>"); String permalink = blogPostDAO.addPost(title, post, tagsArray, username); // now redirect to the blog permalink response.redirect("/post/" + permalink); } } }); get("/welcome", new FreemarkerBasedRoute("welcome.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String cookie = getSessionCookie(request); String username = sessionDAO.findUserNameBySessionId(cookie); if (username == null) { System.out.println("welcome() can't identify the user, redirecting to signup"); response.redirect("/signup"); } else { SimpleHash root = new SimpleHash(); root.put("username", username); template.process(root, writer); } } }); // process a new comment post("/newcomment", new FreemarkerBasedRoute("entry_template.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String name = StringEscapeUtils.escapeHtml4(request.queryParams("commentName")); String email = StringEscapeUtils.escapeHtml4(request.queryParams("commentEmail")); String body = StringEscapeUtils.escapeHtml4(request.queryParams("commentBody")); String permalink = request.queryParams("permalink"); Document post = blogPostDAO.findByPermalink(permalink); if (post == null) { response.redirect("/post_not_found"); } // check that comment is good else if (name.equals("") || body.equals("")) { // bounce this back to the user for correction SimpleHash root = new SimpleHash(); SimpleHash comment = new SimpleHash(); comment.put("name", name); comment.put("email", email); comment.put("body", body); root.put("comments", comment); root.put("post", post); root.put("errors", "Post must contain your name and an actual comment"); template.process(root, writer); } else { blogPostDAO.addPostComment(name, email, body, permalink); response.redirect("/post/" + permalink); } } }); // present the login page get("/login", new FreemarkerBasedRoute("login.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { SimpleHash root = new SimpleHash(); root.put("username", ""); root.put("login_error", ""); template.process(root, writer); } }); // process output coming from login form. On success redirect folks to the welcome page // on failure, just return an error and let them try again. post("/login", new FreemarkerBasedRoute("login.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String username = request.queryParams("username"); String password = request.queryParams("password"); System.out.println("Login: User submitted: " + username + " " + password); Document user = userDAO.validateLogin(username, password); if (user != null) { // valid user, let's log them in String sessionID = sessionDAO.startSession(user.get("_id").toString()); if (sessionID == null) { response.redirect("/internal_error"); } else { // set the cookie for the user's browser response.raw().addCookie(new Cookie("session", sessionID)); response.redirect("/welcome"); } } else { SimpleHash root = new SimpleHash(); root.put("username", StringEscapeUtils.escapeHtml4(username)); root.put("password", ""); root.put("login_error", "Invalid Login"); template.process(root, writer); } } }); // tells the user that the URL is dead get("/post_not_found", new FreemarkerBasedRoute("post_not_found.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { SimpleHash root = new SimpleHash(); template.process(root, writer); } }); // allows the user to logout of the blog get("/logout", new FreemarkerBasedRoute("signup.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String sessionID = getSessionCookie(request); if (sessionID == null) { // no session to end response.redirect("/login"); } else { // deletes from session table sessionDAO.endSession(sessionID); // this should delete the cookie Cookie c = getSessionCookieActual(request); c.setMaxAge(0); response.raw().addCookie(c); response.redirect("/login"); } } }); // used to process internal errors get("/internal_error", new FreemarkerBasedRoute("error_template.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { SimpleHash root = new SimpleHash(); root.put("error", "System has encountered an error."); template.process(root, writer); } }); }
From source file:se.nawroth.asciidoc.browser.AsciidocBrowserApplication.java
private void showFile(final File file, final boolean addIt) { locationTextField.setText(file.getAbsolutePath()); refreshReplacements();/*from ww w . j ava2 s . c o m*/ try { StringBuilder sb = new StringBuilder(10 * 1024); sb.append("<html><head><title>" + file.getName() + "</title><style>body {font-size: 1em;}pre {margin: 0;}</style></head><body>"); String parent = file.getParent(); LineIterator lines = FileUtils.lineIterator(file, "UTF-8"); while (lines.hasNext()) { String line = StringEscapeUtils.escapeHtml4(lines.next()); sb.append("<pre>"); if (line.startsWith(LINK_LINE_START)) { String href = getFileLocation(parent, line); sb.append("<a href=\"").append(href).append("\">").append(line).append("</a>"); } else { sb.append(line); } sb.append("</pre>"); } sb.append("</body></html>"); lines.close(); sourceEditorPane.setText(sb.toString()); sourceEditorPane.setCaretPosition(0); if (addIt) { int listSize = pageList.size(); if (listSize > 0) { int pageIndex = pageList.indexOf(currentFile); if (pageIndex < listSize - 1) { for (int i = listSize - 1; i > pageIndex; i--) { pageList.remove(i); } } } pageList.add(file); } currentFile = file; updateButtons(); if (paths.containsKey(file)) { currentSelectionPath = new TreePath(paths.get(file)); documentTree.setSelectionPath(currentSelectionPath); } } catch (IOException e) { showError("Error in file handling: " + e); } }
From source file:secureemailclient.applet.ViewMailFrame.java
public String toHtml(String s) { return "<html>" + StringEscapeUtils.escapeHtml4(s) + "</html>"; }
From source file:ste.web.http.api.ApiHandler.java
/** * Note that we expect response to have a body entity set (@see HttpEntiry) * //from ww w.j a v a 2 s .co m * @param request * @param response * @param context * * @throws HttpException * @throws IOException */ @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { RRequest rr = null; File actionScript = null, applicationScript = null; try { rr = new RRequest(reduce(request.getRequestLine())); if (log.isLoggable(Level.FINE)) { log.fine(String.format("serving %s", rr.getPath())); } applicationScript = new File(apiroot, getApplicationScript(rr)); actionScript = new File(apiroot, getActionScript(rr)); if (log.isLoggable(Level.FINE)) { log.fine(String.format("application script path: %s", applicationScript.getAbsolutePath())); log.fine(String.format("action script path: %s", actionScript.getAbsolutePath())); } Interpreter bsh = new Interpreter(); BeanShellUtils.setup(bsh, request, response, (HttpSessionContext) context); bsh.set(VAR_SOURCE, actionScript.getAbsolutePath()); bsh.set(VAR_RREQUEST, rr); if (applicationScript.exists()) { bsh.eval(BeanShellUtils.getScript(applicationScript)); } bsh.eval(BeanShellUtils.getScript(actionScript)); Object body = bsh.get(rr.getHandler()); AbstractHttpEntity e = (AbstractHttpEntity) response.getEntity(); if (e.getContentType() == null) { e.setContentType("application/json"); } if (body != null) { if (body instanceof File) { File f = (File) body; e = new FileEntity(f); response.setEntity(e); String mimeType = MimeUtils.getInstance().getMimeType(f); e.setContentType( MimeUtils.MIME_UNKNOWN.equals(mimeType) ? "application/octet-stream" : mimeType); } else { String bodyString = String.valueOf(body); byte[] buf = bodyString.getBytes(); ByteArrayInputStream is = new ByteArrayInputStream(buf); BasicHttpEntity basicEntity = (BasicHttpEntity) e; basicEntity.setContent(is); basicEntity.setContentLength(buf.length); if (e.getContentType() == null) { e.setContentType("application/json"); } } } BeanShellUtils.cleanup(bsh, request); BeanShellUtils.setVariablesAttributes(bsh, context); } catch (FileNotFoundException e) { response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "Script " + actionScript + " not found."); } catch (EvalError x) { String msg = x.getMessage(); if (log.isLoggable(Level.SEVERE)) { log.severe(String.format("error evaluating: %s: %s", actionScript, msg)); log.throwing(getClass().getName(), "handleError", x); } // // We shall not expose to the client any details of a server error // throw new HttpException("server erorr processing the resource - see server log for details", x); } catch (URISyntaxException x) { response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, StringEscapeUtils.escapeHtml4(x.getMessage())); } catch (Exception x) { response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, StringEscapeUtils.escapeHtml4(x.getMessage())); } }