RequestedModuleNames implements IRequestedModuleNames { protected JSONObject decodeModules(String encstr) throws IOException, JSONException { final String sourceMethod = "decodeModules"; JSONObject result = null; if (isTraceLogging) { log.entering(RequestedModuleNames.class.getName(), sourceMethod, new Object[]{encstr}); } if (encstr == null || encstr.length() == 0) { result = new JSONObject("{}"); } else { StringBuffer json = new StringBuffer(encstr.length() * 2); Matcher m = DECODE_JSON.matcher(encstr); while (m.find()) { String match = m.group(1); if (match.equals("!")) m.appendReplacement(json, ":"); else if (match.equals("(")) m.appendReplacement(json, "{"); else if (match.equals(")")) m.appendReplacement(json, "}"); else if (match.equals("|")) m.appendReplacement(json, "!"); else if (match.equals("*")) m.appendReplacement(json, ","); else if (match.equals("<")) m.appendReplacement(json, "("); else if (match.equals(">")) m.appendReplacement(json, ")"); } m.appendTail(json); String jsonstr = json.toString(); jsonstr = REQUOTE_JSON.matcher(jsonstr).replaceAll("$1\"$2\"$3"); jsonstr = REQUOTE_JSON.matcher(jsonstr).replaceAll("$1\"$2\"$3"); result = new JSONObject(jsonstr); } if (isTraceLogging) { log.exiting(RequestedModuleNames.class.getName(), sourceMethod, result); } return result; } RequestedModuleNames(HttpServletRequest request, List<String> idList, byte[] idListHash); @Override List<String> getDeps(); @Override List<String> getModules(); @Override List<String> getPreloads(); @Override List<String> getScripts(); @Override List<String> getExcludes(); @Override String toString(); boolean isVersionError(); String toString(boolean decode); static public Pattern NON_PATH_PROP_PATTERN; static public String PLUGIN_PREFIXES_PROP_NAME; }
@Test public void testDecodeMopdules() throws Exception { IAggregator mockAggregator = TestUtils.createMockAggregator(); HttpServletRequest request = TestUtils.createMockRequest(mockAggregator); EasyMock.replay(request, mockAggregator); RequestedModuleNames requestedNames = new RequestedModuleNames(request, null, null); JSONObject decoded = requestedNames.decodeModules("(foo!(bar!0*baz!(<|xxx>!2*yyy!1))*dir!3)"); Assert.assertEquals(new JSONObject("{foo:{bar:'0',baz:{'(!xxx)':'2',yyy:'1'}},dir:'3'}"), decoded); decoded = requestedNames.decodeModules("("+RequestedModuleNames.PLUGIN_PREFIXES_PROP_NAME+"!(combo/text!0*abc!1)*foo!(bar!0*baz!(xxx.txt!1-0*yyy.txt!2-1)))"); Assert.assertEquals(new JSONObject("{'"+RequestedModuleNames.PLUGIN_PREFIXES_PROP_NAME+"':{'combo/text':'0', abc:'1'},foo:{bar:'0', baz:{xxx.txt:'1-0', yyy.txt:'2-1'}}}"), decoded); }