/** * @param uic the current user context * @return the xml declaration with the theme processing instruction */ public static String getXMLDeclarationWithThemeXslt(final UIContext uic) { String theme = WebUtilities.encode(ThemeUtil.getThemeXslt(uic)); String dec = XML_DECLARATION + "\n<?xml-stylesheet type=\"text/xsl\" href=\"" + theme + "\"?>"; return dec; } }
/** * Sets the source code to be displayed in the panel. * * @param sourceText the source code to display. */ public void setSource(final String sourceText) { String formattedSource; if (sourceText == null) { formattedSource = ""; } else { formattedSource = WebUtilities.encode(sourceText); // XML escape content } source.setText(formattedSource); } }
/** * Override in order to paint the component. Real applications should not emit HTML directly. * * @param renderContext the renderContext to send output to. */ @Override protected void afterPaint(final RenderContext renderContext) { if (renderContext instanceof WebXmlRenderContext) { PrintWriter writer = ((WebXmlRenderContext) renderContext).getWriter(); writer.println(WebUtilities.encode(message)); if (error != null) { writer.println("\n<br/>\n<pre>\n"); StringWriter buf = new StringWriter(); error.printStackTrace(new PrintWriter(buf)); writer.println(WebUtilities.encode(buf.toString())); writer.println("\n</pre>"); } } } }
/** * {@inheritDoc} */ @Override public void renderInline(final String templateInline, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) { LOG.debug("Rendering inline plain text template."); boolean xmlEncode = options.containsKey(XML_ENCODE); try { String output = templateInline; if (xmlEncode) { output = WebUtilities.encode(output); } writer.write(output); } catch (Exception e) { throw new SystemException("Problems with inline plain text template. " + e.getMessage(), e); } }
/** * {@inheritDoc} */ @Override public void openDoc(final PrintWriter writer) { UIContext uic = UIContextHolder.getCurrent(); String title = getApplicationTitle(uic); writer.write(XMLUtil.getXMLDeclarationWithThemeXslt(uic)); StringBuilder rootElement = new StringBuilder(); rootElement.append("\n<ui:root title=\""); rootElement.append(WebUtilities.encode(title)); rootElement.append("\" "); Locale locale = I18nUtilities.getEffectiveLocale(); if (locale != null) { String lang = locale.toLanguageTag(); if (lang != null) { rootElement.append("lang=\""); rootElement.append(WebUtilities.encode(locale.toLanguageTag())); rootElement.append("\" "); } } rootElement.append(XMLUtil.STANDARD_NAMESPACES); rootElement.append(">"); writer.write(rootElement.toString()); }
/** * Encode URL for XML. * * @param urlStr the URL to escape * @return the URL percent encoded */ public static String encodeUrl(final String urlStr) { if (Util.empty(urlStr)) { return urlStr; } // Percent Encode String percentEncode = percentEncodeUrl(urlStr); // XML Enocde return encode(percentEncode); }
@Test public void testEncode() { Assert.assertEquals("Incorrectly encoded null string", null, WebUtilities.encode(null)); Assert.assertEquals("Incorrectly encoded empty string", "", WebUtilities.encode("")); Assert.assertEquals("Incorrectly encoded 1 char string", "x", WebUtilities.encode("x")); Assert.assertEquals("Incorrectly encoded 1 special char string", "&", WebUtilities. encode("&")); Assert.assertEquals("Incorrectly encoded open bracket", "{", WebUtilities.encode("{")); Assert.assertEquals("Incorrectly encoded close bracket", "}", WebUtilities.encode("}")); String in = "Hello world greater> less< amper& quote\"\t\r\n"; String expected = "Hello world greater> less< amper& quote"\t\r\n"; Assert.assertEquals("Incorrectly encoded value", expected, WebUtilities.encode(in)); in = characterRange(0, 32); Assert.assertEquals("Encode should blat special characters", "\t\n\r ", WebUtilities.encode(in)); }
/** * Retrieves the list of messages. * * @return the messages for the current context. */ public List<String> getMessages() { MessageModel model = getComponentModel(); List<String> messages = new ArrayList<>(model.messages.size()); for (Duplet<Serializable, Boolean> message : model.messages) { String text = I18nUtilities.format(null, message.getFirst()); // If we are outputting unencoded content it must be XML valid. boolean encode = message.getSecond(); messages.add(encode ? WebUtilities.encode(text) : HtmlToXMLUtil.unescapeToXML(text)); } return Collections.unmodifiableList(messages); }
primitiveString = WebUtilities.encode(primitiveString); xml.append(" value=\"").append(primitiveString).append('"');
/** * Appends the string to this XmlStringBuilder. XML values are not escaped. * * @param string the String to append. * @param encode true to encode the string before output, false to output as is */ public void append(final String string, final boolean encode) { if (encode) { appendOptional(WebUtilities.encode(string)); } else { // unescaped content still has to be XML compliant. write(HtmlToXMLUtil.unescapeToXML(string)); } }
output = WebUtilities.encode(output);
@Test public void testEncodeText() throws IOException, SAXException, XpathException { String value = "T1<b>T2</b>T3"; String encoded = WebUtilities.encode(value); WText text = new WText(); text.setText(value); // Encoded (default) String xml = toXHtml(text); Assert.assertTrue("XML should have encoded text", xml.contains(encoded)); // Not encoded text.setEncodeText(false); xml = toXHtml(text); Assert.assertTrue("XML should have not encoded text", xml.contains(value)); }
return value.equals(((WRadioButton) component).getValue()) ? current : null; } else { return current.findElement(By.xpath(".//*[@value='" + WebUtilities.encode(String.valueOf(value)) + "']"));
@Test public void testDecode() { Assert.assertEquals("Incorrectly decoded null string", null, WebUtilities.decode(null)); Assert.assertEquals("Incorrectly decoded empty string", "", WebUtilities.decode("")); Assert.assertEquals("Incorrectly decoded 1 char string", "x", WebUtilities.decode("x")); Assert.assertEquals("Incorrectly decoded 1 special char string", "&", WebUtilities.decode( "&")); Assert.assertEquals("Incorrectly decoded open bracket", "{", WebUtilities.decode("{")); Assert.assertEquals("Incorrectly decoded close bracket", "}", WebUtilities.decode("}")); String in = "Hello world greater> less< amper& quote""; String expected = "Hello world greater> less< amper& quote\""; Assert.assertEquals("Incorrectly decoded value", expected, WebUtilities.decode(in)); // Finally, check a encode/decode pair String encoded = WebUtilities.encode(expected); Assert.assertEquals("Incorrectly encoded/decoded value", expected, WebUtilities.decode( encoded)); }
/** * Paints the given WText. * * @param component the WText to paint. * @param renderContext the RenderContext to paint to. */ @Override public void doRender(final WComponent component, final WebXmlRenderContext renderContext) { WText text = (WText) component; XmlStringBuilder xml = renderContext.getWriter(); String textString = text.getText(); if (textString != null) { if (text.isEncodeText()) { xml.print(WebUtilities.encode(textString)); } else { // If we are outputting unencoded content it must be XML valid. xml.print(HtmlToXMLUtil.unescapeToXML(textString)); } } } }
writer.println(WebUtilities.encode(buf.toString())); writer.println("\n</pre>");
@Test public void testEncodeText() throws IOException, SAXException, XpathException { String text = "T1<b>T2</b>T3"; String encoded = WebUtilities.encode(text); WMessageBox messageBox = new WMessageBox(WMessageBox.INFO); messageBox.addMessage(text); // Encoded (default) assertSchemaMatch(messageBox); String xml = toXHtml(messageBox); Assert.assertTrue("XML should have encoded message", xml.contains(encoded)); // Not encoded messageBox.reset(); messageBox.addMessage(false, text); assertSchemaMatch(messageBox); xml = toXHtml(messageBox); Assert.assertTrue("XML should have not encoded message", xml.contains(text)); }
@Test public void testExample() { // Launch the web browser to the LDE SeleniumWComponentsWebDriver driver = getDriver(); String divText = "Hello world&"; String html = "<div id='HtmlInjector_Test.id'>" + WebUtilities.encode(divText) + "</div>"; driver.findWTextArea(byWComponentPath("WTextArea")).sendKeys(html); driver.findElement(byWComponentPath("WButton")).click(); Assert.assertEquals("Incorrect div text", divText, driver.findElement(By.id("HtmlInjector_Test.id")).getText()); } }
xml.appendOptionalAttribute("isNull", isNull, "true"); xml.appendClose(); xml.append(WebUtilities.encode(LOOKUP_TABLE.getDescription(table, item))); xml.appendEndTag("ui:option");
xml.appendOptionalAttribute("hidden", button.isHidden(), "true"); xml.appendAttribute("groupName", button.getGroupName()); xml.appendAttribute("value", WebUtilities.encode(value)); if (readOnly) { xml.appendAttribute("readOnly", "true");