/** * @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; } }
/** * Indicates whether this is a top-level menu (ie. attached to a menu bar). * * @return {@code true} if this is a top-level menu. */ public boolean isTopLevelMenu() { MenuContainer container = WebUtilities.getAncestorOfClass(MenuContainer.class, this); if (container instanceof WMenuItemGroup) { container = WebUtilities.getAncestorOfClass(MenuContainer.class, container); } return container instanceof WMenu; }
/** * This is a slightly different version of appendGetParam that doesn't encode the ampersand seperator. It is * intended to be used in urls that are generated for javascript functions. * * @param key the key to append * @param value the value to append * @param vars the existing query string * @param existingVars true if there are already existing query string key/value pairs */ public static void appendGetParamForJavascript(final String key, final String value, final StringBuffer vars, final boolean existingVars) { vars.append(existingVars ? '&' : '?'); vars.append(key).append('=').append(WebUtilities.escapeForUrl(value)); }
/** * Get this component's parent naming context. * * @param component the component to process * @return true the parent naming context or null */ public static NamingContextable getParentNamingContext(final WComponent component) { if (component == null) { return null; } WComponent child = component; NamingContextable parent = null; while (true) { NamingContextable naming = WebUtilities.getAncestorOfClass(NamingContextable.class, child); if (naming == null) { break; } if (WebUtilities.isActiveNamingContext(naming)) { parent = naming; break; } child = naming; } return parent; }
/** * 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); }
/** * Unescape HTML entities to safe XML. * * <p> * Example</p> * <pre> * {@code StringEscapeHTMLToXMLUtil.unescapeToXML("•&Dagger}"); // returns "•‡"} * {@code StringEscapeHTMLToXMLUtil.unescapeToXML("<p>"); // returns "<p>" not "<p>"} * </pre> * * @param input The String to unescape. * @return the input with all HTML4 character entities unescaped except those which are also XML entities. */ public static String unescapeToXML(final String input) { if (Util.empty(input)) { return input; } // Check if input has encoded brackets String encoded = WebUtilities.doubleEncodeBrackets(input); String unescaped = UNESCAPE_HTML_TO_XML.translate(encoded); String decoded = WebUtilities.doubleDecodeBrackets(unescaped); return decoded; } }
String random = WebUtilities.generateRandom(); parameters.put(Environment.UNIQUE_RANDOM_PARAM, random); } else { return WebUtilities.getPath(url, parameters, true);
@Override public void execute(final ActionEvent event) { WebUtilities.updateBeanValue(layout); } });
@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)); }
resp.setContentType(WebUtilities.getContentType(fileName)); resp.setHeader("Cache-Control", CacheType.THEME_CACHE.getSettings()); resp.setHeader("ETag", "\"" + WebUtilities.getProjectVersion() + "\"");
/** * Override preparePaint in order to set up the resources on first access by a user. * * @param request the request being responded to. */ @Override protected void preparePaintComponent(final Request request) { super.preparePaintComponent(request); if (!isInitialised()) { // Check project versions for Wcomponents-examples and WComponents match String egVersion = Config.getInstance().getString("wcomponents-examples.version"); String wcVersion = WebUtilities.getProjectVersion(); if (egVersion != null && !egVersion.equals(wcVersion)) { String msg = "WComponents-Examples version (" + egVersion + ") does not match WComponents version (" + wcVersion + ")."; LOG.error(msg); messages.addMessage(new Message(Message.ERROR_MESSAGE, msg)); } setInitialised(true); } } }
@Test public void testDoubleEncodeBracketsWithMultipleMatches() { String in = "{}<> {}}"; String out = "&#123;&#125;<> &#123;&#125;&#125;"; Assert.assertEquals("Double encode brackets not correct", out, WebUtilities.doubleEncodeBrackets(in)); }
@Test public void testEncodeBrackets() { String in = "{}<{}>"; String out = "{}<{}>"; Assert.assertEquals("Encode brackets not correct", out, WebUtilities.encodeBrackets(in)); }
@Test public void testDoubleDecodeBrackets() { String in = "&#123;&#125;<>"; String out = "{}<>"; Assert.assertEquals("Double decode brackets not correct", out, WebUtilities.doubleDecodeBrackets(in)); }
@Test public void testGenerateRandom() { String random1 = WebUtilities.generateRandom(); String random2 = WebUtilities.generateRandom(); Assert.assertNotSame("Generated strings should be different", random1, random2); }
/** * Returns the closest WApplication instance (ancestor component) from the given base component. * * @param base the component from which we start scanning up the tree for a WApplication instance * @return the closest WApplication instance from the given base component */ public static WApplication instance(final WComponent base) { WApplication appl = WebUtilities.getClosestOfClass(WApplication.class, base); return appl; }
String random = WebUtilities.generateRandom(); parameters.put(Environment.UNIQUE_RANDOM_PARAM, random); } else { urls[i] = WebUtilities.getPath(url, parameters, true);
/** * Updates the bean value with the current value of the component and all its bean-bound children. By default this * method will only process visible components. * * @param component the component whose contents need to be copied to the bean. */ public static void updateBeanValue(final WComponent component) { updateBeanValue(component, true); }
xml = WebUtilities.doubleEncodeBrackets(xml); tempResp = WebUtilities.doubleDecodeBrackets(tempResp);
@Test public void testGetProjectVersion() { String testVersion = "TEST VERSION"; Config.getInstance().setProperty(ConfigurationProperties.PROJECT_VERSION, testVersion); Assert.assertEquals("Wrong project version returned", testVersion, WebUtilities. getProjectVersion()); try { Config.getInstance().clearProperty(ConfigurationProperties.PROJECT_VERSION); WebUtilities.getProjectVersion(); Assert.fail("An exception should have been thrown for a null project version"); } catch (SystemException e) { Assert.assertNotNull("No error message included for null project version", e. getMessage()); } }