List of usage examples for java.util Map keySet
Set<K> keySet();
From source file:com.mnt.base.mail.MailHelper.java
private static String buildMailContent(String source, Map<String, Object> infoMap, boolean isContent) { for (String key : infoMap.keySet()) { if (key.startsWith(BLOCK_PREFIX)) { if (isContent) { // String regex = "\\$\\{" + key.substring(BLOCK_PREFIX_LEN) + "\\}\\[#[^#]+#\\]"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(source); if (matcher.find()) { String subSourceBase = matcher.group(); String newSubSourceBase = subSourceBase.substring( ("${" + key.substring(BLOCK_PREFIX_LEN) + "}[#").length(), subSourceBase.length() - 2); List<Map<String, Object>> subInfoList = CommonUtil.uncheckedListCast(infoMap.get(key)); StringBuilder sb = new StringBuilder(); if (subInfoList != null) { for (Map<String, Object> subInfoMap : subInfoList) { sb.append(buildMailContent(newSubSourceBase, subInfoMap, true));// do not support next level block }/*from w w w . j av a 2 s . co m*/ } else { sb.append(" none<br />"); } source = source.replace(subSourceBase, sb); } /* else { // skip it }*/ } } else { Object value = infoMap.get(key); source = source.replace("${" + key + "}", value == null ? "-" : value.toString()); } } source = source.replace("${now}", dateFormatter.format(new Date())); return source; }
From source file:de.vandermeer.skb.base.utils.Skb_TextUtils.java
/** * Returns a transformer that takes a map and returns a string representation of its contents. * @return map to string transformer/* w ww.j av a 2 s . c o m*/ */ public static final Skb_Transformer<Map<String, ?>, String> MAP_TO_TEXT() { return new Skb_Transformer<Map<String, ?>, String>() { @Override public String transform(Map<String, ?> map) { StrBuilder ret = new StrBuilder(map.size() * 20); TreeSet<String> keys = new TreeSet<String>(map.keySet()); for (String leaf : keys) { // int level=StringUtils.countMatches(leaf, builder.getSeparatorAsString()); // ret.append('-'); // for(int i=0;i<=level;i++){ // ret.append("-"); // } // ret.append("> ").append(leaf); ret.append("--> ").append(leaf); ret.append(" ::= "); ret.append(map.get(leaf)); ret.append('\n'); } return ret.toString(); } }; }
From source file:crow.util.JsonUtil.java
private static JSONObject toJSONObject(Map<String, Object> map) throws JSONException { JSONObject jsonOb = new JSONObject(); if ((map == null) || (map.isEmpty())) return jsonOb; Iterator<String> iter = map.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); Object value = map.get(key); if ((value instanceof String) || (value instanceof Integer) || (value instanceof Double) || (value instanceof Long) || (value instanceof Float)) { jsonOb.put(key, value);/*from ww w .j av a 2 s.c om*/ continue; } if ((value instanceof Map<?, ?>)) { try { @SuppressWarnings("unchecked") Map<String, Object> valueMap = (Map<String, Object>) value; jsonOb.put(key, toJSONObject(valueMap)); } catch (ClassCastException e) { Log.w(TAG, "Unknown map type in json serialization: ", e); } continue; } if ((value instanceof Set<?>)) { try { @SuppressWarnings("unchecked") Set<Object> valueSet = (Set<Object>) value; jsonOb.put(key, toJSONObject(valueSet)); } catch (ClassCastException e) { Log.w(TAG, "Unknown map type in json serialization: ", e); } continue; } Log.e(TAG, "Unknown value in json serialization: " + value.toString() + " : " + value.getClass().getCanonicalName().toString()); } return jsonOb; }
From source file:net.openkoncept.vroom.VroomUtilities.java
public static JSONObject convertObjectToJSONObject(Object object) throws JSONException { JSONObject jo = new JSONObject(); if (object instanceof Character || object instanceof String || object instanceof Boolean || object instanceof Short || object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double || object instanceof java.util.Date || object instanceof java.math.BigDecimal || object instanceof java.math.BigInteger) { jo.put("value", getValueForJSONObject(object)); } else if (object instanceof java.util.Map) { Map m = (Map) object; Iterator iter = m.keySet().iterator(); while (iter.hasNext()) { String key = (String) iter.next(); jo.put(key, getValueForJSONObject(m.get(key))); }// w w w . j a v a2s. c o m } else if (object instanceof Collection) { Collection c = (Collection) object; Iterator iter = c.iterator(); JSONArray ja = new JSONArray(); while (iter.hasNext()) { ja.put(getValueForJSONObject(iter.next())); } jo.put("array", ja); } else if (object != null && object.getClass().isArray()) { Object[] oa = (Object[]) object; JSONArray ja = new JSONArray(); for (int i = 0; i < oa.length; i++) { ja.put(getValueForJSONObject(oa[i])); } jo.put("array", ja); } else if (object instanceof ResourceBundle) { ResourceBundle rb = (ResourceBundle) object; Enumeration e = rb.getKeys(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); Object value = getValueForJSONObject(rb.getObject(key)); jo.put(key, value); } } else if (object != null) { Class clazz = object.getClass(); PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(object); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; String name = pd.getName(); if (!"class".equals(name) && PropertyUtils.isReadable(object, pd.getName())) { try { Object value = PropertyUtils.getProperty(object, name); jo.put(name, getValueForJSONObject(value)); } catch (Exception e) { // Useless... } } } } else { jo.put("value", ""); } return jo; }
From source file:com.google.cloud.genomics.dockerflow.util.StringUtils.java
/** * Substitute all global variables of the form $(KEY) in a string. * * @param globals/*from w w w . ja va 2s . c om*/ * @param value * @return */ public static String replaceAll(Map<String, String> globals, String value) { String retval = value; if (value != null && globals != null) { for (String key : globals.keySet()) { String var = "${" + key + "}"; if (value.contains(var) && globals.get(key) != null) { retval = retval.replace(var, globals.get(key)); } } } return retval; }
From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java
/** * Is period value empty./*from w w w. jav a 2 s . c om*/ * * @return */ protected static boolean isEmpty(Map<String, Value> periodValue) { Iterator<String> it = periodValue.keySet().iterator(); while (it.hasNext()) { String id = it.next(); Value value = periodValue.get(id); if (!value.isEmpty()) { return false; } } return true; }
From source file:com.networknt.mask.Mask.java
/** * Mask the input string with a list of patterns indexed by key in string section in mask.json * This is usually used to mask header values, query parameters and uri parameters * * @param input String The source of the string that needs to be masked * @param key String The key that maps to a list of patterns for masking in config file * @return Masked result/*from www . j ava 2s . c om*/ */ public static String maskString(String input, String key) { String output = input; Map<String, Object> stringConfig = (Map<String, Object>) config.get(MASK_TYPE_STRING); if (stringConfig != null) { Map<String, Object> keyConfig = (Map<String, Object>) stringConfig.get(key); if (keyConfig != null) { Set<String> patterns = keyConfig.keySet(); for (String pattern : patterns) { output = output.replaceAll(pattern, (String) keyConfig.get(pattern)); } } } return output; }
From source file:org.wso2.dss.integration.test.odata.ODataTestUtils.java
public static int sendPUT(String endpoint, String content, Map<String, String> headers) throws IOException { HttpClient httpClient = new DefaultHttpClient(); HttpPut httpPut = new HttpPut(endpoint); for (String headerType : headers.keySet()) { httpPut.setHeader(headerType, headers.get(headerType)); }/* w ww .ja va 2s. c om*/ if (null != content) { HttpEntity httpEntity = new ByteArrayEntity(content.getBytes("UTF-8")); if (headers.get("Content-Type") == null) { httpPut.setHeader("Content-Type", "application/json"); } httpPut.setEntity(httpEntity); } HttpResponse httpResponse = httpClient.execute(httpPut); return httpResponse.getStatusLine().getStatusCode(); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <E> Map<String, E> union(Map<String, E> mapA, Map<String, E> mapB) { Map<String, E> mapC = null; try {// ww w .j av a 2 s . c om mapC = mapA.getClass().newInstance(); } catch (Exception e) { e.printStackTrace(); return mapC; } Iterator<String> akeys = mapA.keySet().iterator(); Iterator<String> bkeys = mapB.keySet().iterator(); while (akeys.hasNext() || bkeys.hasNext()) { if (akeys.hasNext()) { String ak = akeys.next(); mapC.put(ak, mapA.get(ak)); } if (bkeys.hasNext()) { String bk = bkeys.next(); E e = mapB.get(bk); if (!mapA.containsKey(bk) || !mapA.get(bk).equals(e)) { mapC.put(bk, mapA.get(e)); } } } return mapC; }
From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java
/** * Is period value empty./*from w w w.j av a 2s . com*/ * * @return */ protected static boolean isEmptyBudget(Map<String, Double> periodBudget) { Iterator<String> it = periodBudget.keySet().iterator(); while (it.hasNext()) { String id = it.next(); Double value = periodBudget.get(id); if (value.doubleValue() != 0.0) { return false; } } return true; }