Java 类org.springframework.web.util.JavaScriptUtils 实例源码

项目:openmrs-module-legacyui    文件:OpenmrsMessageTag.java   
/**
 * @see MessageTag#doStartTagInternal()
 * @should evaluate specified message resolvable
 * @should resolve message by code
 * @should resolve message in locale that different from default
 * @should return code if no message resolved
 * @should use body content as fallback if no message resolved
 * @should use text attribute as fallback if no message resolved
 * @should use body content in prior to text attribute as fallback if no message resolved
 * @should ignore fallbacks if tag locale differs from context locale
 */
@Override
protected int doEndTagInternal() throws JspException, IOException {
    try {
        // Resolve the unescaped message.
        String msg = resolveMessage();

        // HTML and/or JavaScript escape, if demanded.
        msg = isHtmlEscape() ? HtmlUtils.htmlEscape(msg) : msg;
        msg = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(msg) : msg;

        // Expose as variable, if demanded, else write to the page.
        String resolvedVar = this.var;
        if (resolvedVar != null) {
            pageContext.setAttribute(resolvedVar, msg, TagUtils.getScope(this.scope));
        } else {
            writeMessage(msg);
        }

        return EVAL_PAGE;
    }
    catch (NoSuchMessageException ex) {
        throw new JspTagException(getNoSuchMessageExceptionDescription(ex));
    }
}
项目:GeneralUtils    文件:StringEscapeEditor.java   
/**
 * override setAsText method , then register
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {

     if (!StringUtils.hasText(text)) {  
            return;  
        }else {
        String value = text;
        if (escapeHTML) {
            value = HtmlUtils.htmlEscape(value);
        }
        if (escapeJavaScript) {
            value = JavaScriptUtils.javaScriptEscape(value);
        }
        super.setValue(value);
    }
}
项目:sinavi-jfw    文件:TagUtils.java   
/**
 * リンクとして出力するURLを生成します。
 * @param url パス
 * @param params パスに付与するパラメータ
 * @param pageContext ページコンテキスト
 * @param isHtmlEscape HTMLの特殊文字をエスケープするかどうか
 * @param isJavaScriptEscape JavaScriptの特殊文字をエスケープするかどうか
 * @return パス
 * @throws JspException 予期しない例外
 */
public static String createUrl(String url, Map<String, String[]> params, PageContext pageContext, boolean isHtmlEscape, boolean isJavaScriptEscape) throws JspException {
    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
    HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();

    StringBuilder buffer = new StringBuilder();
    UrlType urlType = getUrlType(url);
    if (urlType == UrlType.CONTEXT_RELATIVE) {
        buffer.append(request.getContextPath());
        if (!url.startsWith("/")) {
            buffer.append("/");
        }
    }
    buffer.append(replaceUriTemplateParams(url, params, pageContext));
    buffer.append(createQueryString(params, (url.indexOf("?") == -1), pageContext));

    String urlStr = buffer.toString();
    if (urlType != UrlType.ABSOLUTE) {
        urlStr = response.encodeURL(urlStr);
    }

    urlStr = isHtmlEscape ? HtmlUtils.htmlEscape(urlStr) : urlStr;
    urlStr = isJavaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;

    return urlStr;
}
项目:spring4-understanding    文件:UrlTag.java   
/**
 * Build the URL for the tag from the tag attributes and parameters.
 * @return the URL value as a String
 * @throws JspException
 */
private String createUrl() throws JspException {
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
    StringBuilder url = new StringBuilder();
    if (this.type == UrlType.CONTEXT_RELATIVE) {
        // add application context to url
        if (this.context == null) {
            url.append(request.getContextPath());
        }
        else {
            if (this.context.endsWith("/")) {
                url.append(this.context.substring(0, this.context.length() - 1));
            }
            else {
                url.append(this.context);
            }
        }
    }
    if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {
        url.append("/");
    }
    url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
    url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));

    String urlStr = url.toString();
    if (this.type != UrlType.ABSOLUTE) {
        // Add the session identifier if needed
        // (Do not embed the session identifier in a remote link!)
        urlStr = response.encodeURL(urlStr);
    }

    // HTML and/or JavaScript escape, if demanded.
    urlStr = htmlEscape(urlStr);
    urlStr = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;

    return urlStr;
}
项目:spring4-understanding    文件:EscapeBodyTag.java   
@Override
public int doAfterBody() throws JspException {
    try {
        String content = readBodyContent();
        // HTML and/or JavaScript escape, if demanded
        content = htmlEscape(content);
        content = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content;
        writeBodyContent(content);
    }
    catch (IOException ex) {
        throw new JspException("Could not write escaped body", ex);
    }
    return (SKIP_BODY);
}
项目:spring4-understanding    文件:HtmlFileTransportHandler.java   
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
    return new DefaultSockJsFrameFormat("<script>\np(\"%s\");\n</script>\r\n") {
        @Override
        protected String preProcessContent(String content) {
            return JavaScriptUtils.javaScriptEscape(content);
        }
    };
}
项目:spring4-understanding    文件:JsonpPollingTransportHandler.java   
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
    // We already validated the parameter above...
    String callback = getCallbackParam(request);

    return new DefaultSockJsFrameFormat("/**/" + callback + "(\"%s\");\r\n") {
        @Override
        protected String preProcessContent(String content) {
            return JavaScriptUtils.javaScriptEscape(content);
        }
    };
}
项目:leopard    文件:JstlFunctions.java   
/**
 * 对javascript变量进行转义.
 * 
 * @param str
 *            js变量
 * @return 转义后的字符串
 */
public static String escapeJavascriptParam(String str) {
    if (str == null) {
        return null;
    }
    if (str.indexOf('"') != -1) {
        Exception e = new Exception("invalid js param:" + str);
        logger.error(e.getMessage(), e);
        str = str.replace("\"", "");
    }
    return JavaScriptUtils.javaScriptEscape(str);
}
项目:fengduo    文件:StringEscapeEditor.java   
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (text == null) {
        setValue(null);
    } else {
        String value = text;
        if (escapeHTML) {
            value = HtmlUtils.htmlEscape(value);
        }
        if (escapeJavaScript) {
            value = JavaScriptUtils.javaScriptEscape(value);
        }
        setValue(value);
    }
}
项目:class-guard    文件:MessageTag.java   
/**
 * Resolves the message, escapes it if demanded,
 * and writes it to the page (or exposes it as variable).
 * @see #resolveMessage()
 * @see org.springframework.web.util.HtmlUtils#htmlEscape(String)
 * @see org.springframework.web.util.JavaScriptUtils#javaScriptEscape(String)
 * @see #writeMessage(String)
 */
@Override
protected final int doStartTagInternal() throws JspException, IOException {
    try {
        // Resolve the unescaped message.
        String msg = resolveMessage();

        // HTML and/or JavaScript escape, if demanded.
        msg = isHtmlEscape() ? HtmlUtils.htmlEscape(msg) : msg;
        msg = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(msg) : msg;

        // Expose as variable, if demanded, else write to the page.
        String resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext);
        if (resolvedVar != null) {
            String resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext);
            pageContext.setAttribute(resolvedVar, msg, TagUtils.getScope(resolvedScope));
        }
        else {
            writeMessage(msg);
        }

        return EVAL_BODY_INCLUDE;
    }
    catch (NoSuchMessageException ex) {
        throw new JspTagException(getNoSuchMessageExceptionDescription(ex));
    }
}
项目:class-guard    文件:UrlTag.java   
/**
 * Build the URL for the tag from the tag attributes and parameters.
 * @return the URL value as a String
 * @throws JspException
 */
private String createUrl() throws JspException {
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
    StringBuilder url = new StringBuilder();
    if (this.type == UrlType.CONTEXT_RELATIVE) {
        // add application context to url
        if (this.context == null) {
            url.append(request.getContextPath());
        }
        else {
            url.append(this.context);
        }
    }
    if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {
        url.append("/");
    }
    url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
    url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));

    String urlStr = url.toString();
    if (this.type != UrlType.ABSOLUTE) {
        // Add the session identifier if needed
        // (Do not embed the session identifier in a remote link!)
        urlStr = response.encodeURL(urlStr);
    }

    // HTML and/or JavaScript escape, if demanded.
    urlStr = isHtmlEscape() ? HtmlUtils.htmlEscape(urlStr) : urlStr;
    urlStr = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;

    return urlStr;
}
项目:class-guard    文件:EscapeBodyTag.java   
@Override
public int doAfterBody() throws JspException {
    try {
        String content = readBodyContent();
        // HTML and/or JavaScript escape, if demanded
        content = isHtmlEscape() ? HtmlUtils.htmlEscape(content) : content;
        content = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content;
        writeBodyContent(content);
    }
    catch (IOException ex) {
        throw new JspException("Could not write escaped body", ex);
    }
    return (SKIP_BODY);
}
项目:alfresco-mvc    文件:DispatcherWebscript.java   
private void convertExceptionToJson(Throwable ex, WebScriptServletResponse wsr, final HttpServletResponse sr, LocalHttpServletResponse mockHttpServletResponse) throws IOException {
  ObjectMapper objectMapper = new ObjectMapper();
  ResponseMapBuilder builder = ResponseMapBuilder.createFailResponseMap().withEntry("event", "exception").withEntry("exception", ex.getClass()).withEntry("message",
      JavaScriptUtils.javaScriptEscape(ex.getMessage()));

  int status = mockHttpServletResponse.getStatus();
  if (HttpServletResponse.SC_OK == status) {
    status = HttpServletResponse.SC_BAD_REQUEST;
  }

  // String errorMessage = ex.getLocalizedMessage();
  if (ex instanceof NestedServletException) {
    NestedServletException nestedServletException = (NestedServletException) ex;
    if (nestedServletException.getCause() != null) {
      builder.withEntry("cause", nestedServletException.getCause().getClass());
      builder.withEntry("causeMessage", nestedServletException.getCause().getMessage());
      if (nestedServletException.getCause() instanceof DataAccessException) {
        if (HttpServletResponse.SC_OK == mockHttpServletResponse.getStatus()) {
          status = HttpServletResponse.SC_NOT_ACCEPTABLE;
        }
      }
    }
  }

  // mockHttpServletResponse.sendError(status, errorMessage);
  mockHttpServletResponse.setStatus(status);
  mockHttpServletResponse.setContentType("application/json");
  objectMapper.writeValue(mockHttpServletResponse.getOutputStream(), builder.build());
  writeResponseToWebscript(wsr, mockHttpServletResponse);
}
项目:cerberus-source    文件:ReadTestCaseExecution.java   
private JSONObject testCaseExecutionToJSONObject(
        TestCaseExecution testCaseExecution) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("ID", String.valueOf(testCaseExecution.getId()));
    result.put("Test", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTest()));
    result.put("TestCase", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCase()));
    result.put("Environment", JavaScriptUtils.javaScriptEscape(testCaseExecution.getEnvironment()));
    result.put("Start", testCaseExecution.getStart());
    result.put("End", testCaseExecution.getEnd());
    result.put("Country", JavaScriptUtils.javaScriptEscape(testCaseExecution.getCountry()));
    result.put("Browser", JavaScriptUtils.javaScriptEscape(testCaseExecution.getBrowser()));
    result.put("ControlStatus", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlStatus()));
    result.put("ControlMessage", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlMessage()));
    result.put("Status", JavaScriptUtils.javaScriptEscape(testCaseExecution.getStatus()));

    String bugId;
    if (testCaseExecution.getApplicationObj() != null && testCaseExecution.getApplicationObj().getBugTrackerUrl() != null
            && !"".equals(testCaseExecution.getApplicationObj().getBugTrackerUrl()) && testCaseExecution.getTestCaseObj().getBugID() != null) {
        bugId = testCaseExecution.getApplicationObj().getBugTrackerUrl().replace("%BUGID%", testCaseExecution.getTestCaseObj().getBugID());
        bugId = new StringBuffer("<a href='")
                .append(bugId)
                .append("' target='reportBugID'>")
                .append(testCaseExecution.getTestCaseObj().getBugID())
                .append("</a>")
                .toString();
    } else {
        bugId = testCaseExecution.getTestCaseObj().getBugID();
    }
    result.put("BugID", bugId);

    result.put("Comment", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCaseObj().getComment()));
    result.put("Priority", JavaScriptUtils.javaScriptEscape(String.valueOf(testCaseExecution.getTestCaseObj().getPriority())));
    result.put("Function", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCaseObj().getFunction()));
    result.put("Application", JavaScriptUtils.javaScriptEscape(testCaseExecution.getApplication()));
    result.put("ShortDescription", testCaseExecution.getTestCaseObj().getDescription());

    return result;
}
项目:onetwo    文件:XssUtils.java   
public static String escape(String content){
    return HtmlUtils.htmlEscape(JavaScriptUtils.javaScriptEscape(content));
}
项目:cerberus-source    文件:ReadTestCaseExecutionByTag.java   
private JSONObject testCaseExecutionToJSONObject(TestCaseExecution testCaseExecution) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("ID", String.valueOf(testCaseExecution.getId()));
    result.put("QueueID", String.valueOf(testCaseExecution.getQueueID()));
    result.put("Test", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTest()));
    result.put("TestCase", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCase()));
    result.put("Environment", JavaScriptUtils.javaScriptEscape(testCaseExecution.getEnvironment()));
    result.put("Start", testCaseExecution.getStart());
    result.put("End", testCaseExecution.getEnd());
    result.put("Country", JavaScriptUtils.javaScriptEscape(testCaseExecution.getCountry()));
    result.put("Browser", JavaScriptUtils.javaScriptEscape(testCaseExecution.getBrowser()));
    result.put("ControlStatus", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlStatus()));
    result.put("ControlMessage", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlMessage()));
    result.put("Status", JavaScriptUtils.javaScriptEscape(testCaseExecution.getStatus()));
    result.put("NbExecutions", String.valueOf(testCaseExecution.getNbExecutions()));
    if (testCaseExecution.getQueueState() != null) {
        result.put("QueueState", JavaScriptUtils.javaScriptEscape(testCaseExecution.getQueueState()));
    }

    String bugId;
    String comment;
    String function;
    String shortDesc;
    if ((testCaseExecution.getTestCaseObj() != null) && (testCaseExecution.getTestCaseObj().getTest() != null)) {
        if (testCaseExecution.getApplicationObj() != null && testCaseExecution.getApplicationObj().getBugTrackerUrl() != null
                && !"".equals(testCaseExecution.getApplicationObj().getBugTrackerUrl()) && testCaseExecution.getTestCaseObj().getBugID() != null) {
            bugId = testCaseExecution.getApplicationObj().getBugTrackerUrl().replace("%BUGID%", testCaseExecution.getTestCaseObj().getBugID());
            bugId = new StringBuffer("<a href='")
                    .append(bugId)
                    .append("' target='reportBugID'>")
                    .append(testCaseExecution.getTestCaseObj().getBugID())
                    .append("</a>")
                    .toString();
        } else {
            bugId = testCaseExecution.getTestCaseObj().getBugID();
        }
        comment = JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCaseObj().getComment());
        function = JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCaseObj().getFunction());
        shortDesc = testCaseExecution.getTestCaseObj().getDescription();
    } else {
        bugId = "";
        comment = "";
        function = "";
        shortDesc = "";
    }
    result.put("BugID", bugId);

    result.put("Priority", JavaScriptUtils.javaScriptEscape(String.valueOf(testCaseExecution.getTestCaseObj().getPriority())));
    result.put("Comment", comment);
    result.put("Function", function);
    result.put("ShortDescription", shortDesc);

    result.put("Application", JavaScriptUtils.javaScriptEscape(testCaseExecution.getApplication()));

    return result;
}
项目:leopard    文件:JstlFunctions.java   
/**
 * 对javascript特殊字符进行转义.
 * 
 * @param str
 *            javascript文本
 * @return 转义后的字符串
 */
public static String escapeJavascript(String str) {
    return JavaScriptUtils.javaScriptEscape(str);
}
项目:beetl2.0    文件:UtilsFunctionPackage.java   
/**
 * 对值进行JavaScript转义
 *
 * @param input
 *            输入文本
 * @return 转义文本
 */
public String javaScript(String input) {
    return JavaScriptUtils.javaScriptEscape(input);
}