@Test public void mileage() { List<Map<String, String>> fields = getFields(); Map<String, String> fld = getField("hltCityMPG", "string", ".*(${e.city_mileage})", "rowdata", null); fld.put(REPLACE_WITH, "*** $1 ***"); fields.add(fld); fld = getField("t1", "string","duff", "rowdata", null); fields.add(fld); fld = getField("t2", "string","duff", "rowdata", null); fld.put(REPLACE_WITH, "60"); fields.add(fld); fld = getField("t3", "string","(Range)", "rowdata", null); fld.put(REPLACE_WITH, "range"); fld.put(GROUP_NAMES,"t4,t5"); fields.add(fld); Map<String, Object> row = new HashMap<String, Object>(); String s = "Fuel Economy Range: 26 mpg Hwy, 19 mpg City"; row.put("rowdata", s); VariableResolverImpl resolver = new VariableResolverImpl(); resolver.addNamespace("e", row); Map<String, String> eAttrs = AbstractDataImportHandlerTest.createMap("name", "e"); Context context = AbstractDataImportHandlerTest.getContext(null, resolver, null, Context.FULL_DUMP, fields, eAttrs); Map<String, Object> result = new RegexTransformer().transformRow(row, context); Assert.assertEquals(5, result.size()); Assert.assertEquals(s, result.get("rowdata")); Assert.assertEquals("26", result.get("highway_mileage")); Assert.assertEquals("19", result.get("city_mileage")); Assert.assertEquals("*** 19 *** mpg City", result.get("hltCityMPG")); Assert.assertEquals("Fuel Economy range: 26 mpg Hwy, 19 mpg City", result.get("t3")); }
@SuppressWarnings("unchecked") public Map<String, Object> transformRow(Map<String, Object> row, Context context) { VariableResolver vr = context.getVariableResolver(); List<Map<String, String>> fields = context.getAllEntityFields(); for (Map<String, String> field : fields) { String col = field.get(DataImporter.COLUMN); String reStr = field.get(REGEX); reStr = vr.replaceTokens(reStr); String splitBy = field.get(SPLIT_BY); splitBy = vr.replaceTokens(splitBy); String replaceWith = field.get(REPLACE_WITH); replaceWith = vr.replaceTokens(replaceWith); String groupNames = vr.replaceTokens(field.get(GROUP_NAMES)); if (reStr != null || splitBy != null) { String srcColName = field.get(SRC_COL_NAME); if (srcColName == null) { srcColName = col; } Object tmpVal = row.get(srcColName); if (tmpVal == null) continue; if (tmpVal instanceof List) { List<String> inputs = (List<String>) tmpVal; List results = new ArrayList(); Map<String,List> otherVars= null; for (String input : inputs) { Object o = process(col, reStr, splitBy, replaceWith, input, groupNames); if (o != null){ if (o instanceof Map) { Map map = (Map) o; for (Object e : map.entrySet()) { Map.Entry<String ,Object> entry = (Map.Entry<String, Object>) e; List l = results; if(!col.equals(entry.getKey())){ if(otherVars == null) otherVars = new HashMap<String, List>(); l = otherVars.get(entry.getKey()); if(l == null){ l = new ArrayList(); otherVars.put(entry.getKey(), l); } } if (entry.getValue() instanceof Collection) { l.addAll((Collection) entry.getValue()); } else { l.add(entry.getValue()); } } } else { if (o instanceof Collection) { results.addAll((Collection) o); } else { results.add(o); } } } } row.put(col, results); if(otherVars != null) row.putAll(otherVars); } else { String value = tmpVal.toString(); Object o = process(col, reStr, splitBy, replaceWith, value, groupNames); if (o != null){ if (o instanceof Map) { row.putAll((Map) o); } else{ row.put(col, o); } } } } } return row; }
RegexTransformer extends Transformer { @SuppressWarnings("unchecked") public Map<String, Object> transformRow(Map<String, Object> row, Context context) { VariableResolver vr = context.getVariableResolver(); List<Map<String, String>> fields = context.getAllEntityFields(); for (Map<String, String> field : fields) { String col = field.get(DataImporter.COLUMN); String reStr = field.get(REGEX); reStr = vr.replaceTokens(reStr); String splitBy = field.get(SPLIT_BY); splitBy = vr.replaceTokens(splitBy); String replaceWith = field.get(REPLACE_WITH); replaceWith = vr.replaceTokens(replaceWith); String groupNames = vr.replaceTokens(field.get(GROUP_NAMES)); if (reStr != null || splitBy != null) { String srcColName = field.get(SRC_COL_NAME); if (srcColName == null) { srcColName = col; } Object tmpVal = row.get(srcColName); if (tmpVal == null) continue; if (tmpVal instanceof List) { List<String> inputs = (List<String>) tmpVal; List results = new ArrayList(); Map<String,List> otherVars= null; for (String input : inputs) { Object o = process(col, reStr, splitBy, replaceWith, input, groupNames); if (o != null){ if (o instanceof Map) { Map map = (Map) o; for (Object e : map.entrySet()) { Map.Entry<String ,Object> entry = (Map.Entry<String, Object>) e; List l = results; if(!col.equals(entry.getKey())){ if(otherVars == null) otherVars = new HashMap<String, List>(); l = otherVars.get(entry.getKey()); if(l == null){ l = new ArrayList(); otherVars.put(entry.getKey(), l); } } if (entry.getValue() instanceof Collection) { l.addAll((Collection) entry.getValue()); } else { l.add(entry.getValue()); } } } else { if (o instanceof Collection) { results.addAll((Collection) o); } else { results.add(o); } } } } row.put(col, results); if(otherVars != null) row.putAll(otherVars); } else { String value = tmpVal.toString(); Object o = process(col, reStr, splitBy, replaceWith, value, groupNames); if (o != null){ if (o instanceof Map) { row.putAll((Map) o); } else{ row.put(col, o); } } } } } return row; } }
RegexTransformer extends Transformer { @SuppressWarnings("unchecked") public Map<String, Object> transformRow(Map<String, Object> row, Context context) { VariableResolver vr = context.getVariableResolver(); List<Map<String, String>> fields = context.getAllEntityFields(); for (Map<String, String> field : fields) { String col = field.get(DataImporter.COLUMN); String reStr = field.get(REGEX); reStr = vr.replaceTokens(reStr); String splitBy = field.get(SPLIT_BY); splitBy = vr.replaceTokens(splitBy); String replaceWith = field.get(REPLACE_WITH); replaceWith = vr.replaceTokens(replaceWith); String groupNames = vr.replaceTokens(field.get(GROUP_NAMES)); if (reStr != null || splitBy != null) { String srcColName = field.get(SRC_COL_NAME); if (srcColName == null) { srcColName = col; } Object tmpVal = row.get(srcColName); if (tmpVal == null) continue; if (tmpVal instanceof List) { List<String> inputs = (List<String>) tmpVal; List results = new ArrayList(); Map<String,List> otherVars= null; for (String input : inputs) { Object o = process(col, reStr, splitBy, replaceWith, input, groupNames); if (o != null){ if (o instanceof Map) { Map map = (Map) o; for (Object e : map.entrySet()) { Map.Entry<String ,Object> entry = (Map.Entry<String, Object>) e; List l = results; if(!col.equals(entry.getKey())){ if(otherVars == null) otherVars = new HashMap<String, List>(); l = otherVars.get(entry.getKey()); if(l == null){ l = new ArrayList(); otherVars.put(entry.getKey(), l); } } if (entry.getValue() instanceof Collection) { l.addAll((Collection) entry.getValue()); } else { l.add(entry.getValue()); } } } else { if (o instanceof Collection) { results.addAll((Collection) o); } else { results.add(o); } } } } row.put(col, results); if(otherVars != null) row.putAll(otherVars); } else { String value = tmpVal.toString(); Object o = process(col, reStr, splitBy, replaceWith, value, groupNames); if (o != null){ if (o instanceof Map) { row.putAll((Map) o); } else{ row.put(col, o); } } } } } return row; }  }
RegexTransformer extends Transformer { @SuppressWarnings("unchecked") public Map<String, Object> transformRow(Map<String, Object> row, Context context) { VariableResolver vr = context.getVariableResolver(); List<Map<String, String>> fields = context.getAllEntityFields(); for (Map<String, String> field : fields) { String col = field.get(DataImporter.COLUMN); String reStr = field.get(REGEX); reStr = vr.replaceTokens(reStr); String splitBy = field.get(SPLIT_BY); splitBy = vr.replaceTokens(splitBy); String replaceWith = field.get(REPLACE_WITH); replaceWith = vr.replaceTokens(replaceWith); String groupNames = vr.replaceTokens(field.get(GROUP_NAMES)); if (reStr != null || splitBy != null) { String srcColName = field.get(SRC_COL_NAME); if (srcColName == null) { srcColName = col; } Object tmpVal = row.get(srcColName); if (tmpVal == null) continue; if (tmpVal instanceof List) { List<String> inputs = (List<String>) tmpVal; List results = new ArrayList(); Map<String,List> otherVars= null; for (String input : inputs) { Object o = process(col, reStr, splitBy, replaceWith, input, groupNames); if (o != null){ if (o instanceof Map) { Map map = (Map) o; for (Object e : map.entrySet()) { Map.Entry<String ,Object> entry = (Map.Entry<String, Object>) e; List l = results; if(!col.equals(entry.getKey())){ if(otherVars == null) otherVars = new HashMap<String, List>(); l = otherVars.get(entry.getKey()); if(l == null){ l = new ArrayList(); otherVars.put(entry.getKey(), l); } } if (entry.getValue() instanceof Collection) { l.addAll((Collection) entry.getValue()); } else { l.add(entry.getValue()); } } } else { if (o instanceof Collection) { results.addAll((Collection) o); } else { results.add(o); } } } } row.put(col, results); if(otherVars != null) row.putAll(otherVars); } else { String value = tmpVal.toString(); Object o = process(col, reStr, splitBy, replaceWith, value, groupNames); if (o != null){ if (o instanceof Map) { row.putAll((Map) o); } else{ row.put(col, o); } } } } } return row; }  @SuppressWarnings("unchecked") Map<String, Object> transformRow(Map<String, Object> row,
                                          Context context); }
RegexTransformer extends Transformer { @SuppressWarnings("unchecked") public Map<String, Object> transformRow(Map<String, Object> row, Context context) { VariableResolver vr = context.getVariableResolver(); List<Map<String, String>> fields = context.getAllEntityFields(); for (Map<String, String> field : fields) { String col = field.get(DataImporter.COLUMN); String reStr = field.get(REGEX); reStr = vr.replaceTokens(reStr); String splitBy = field.get(SPLIT_BY); splitBy = vr.replaceTokens(splitBy); String replaceWith = field.get(REPLACE_WITH); replaceWith = vr.replaceTokens(replaceWith); String groupNames = vr.replaceTokens(field.get(GROUP_NAMES)); if (reStr != null || splitBy != null) { String srcColName = field.get(SRC_COL_NAME); if (srcColName == null) { srcColName = col; } Object tmpVal = row.get(srcColName); if (tmpVal == null) continue; if (tmpVal instanceof List) { List<String> inputs = (List<String>) tmpVal; List results = new ArrayList(); Map<String,List> otherVars= null; for (String input : inputs) { Object o = process(col, reStr, splitBy, replaceWith, input, groupNames); if (o != null){ if (o instanceof Map) { Map map = (Map) o; for (Object e : map.entrySet()) { Map.Entry<String ,Object> entry = (Map.Entry<String, Object>) e; List l = results; if(!col.equals(entry.getKey())){ if(otherVars == null) otherVars = new HashMap<String, List>(); l = otherVars.get(entry.getKey()); if(l == null){ l = new ArrayList(); otherVars.put(entry.getKey(), l); } } if (entry.getValue() instanceof Collection) { l.addAll((Collection) entry.getValue()); } else { l.add(entry.getValue()); } } } else { if (o instanceof Collection) { results.addAll((Collection) o); } else { results.add(o); } } } } row.put(col, results); if(otherVars != null) row.putAll(otherVars); } else { String value = tmpVal.toString(); Object o = process(col, reStr, splitBy, replaceWith, value, groupNames); if (o != null){ if (o instanceof Map) { row.putAll((Map) o); } else{ row.put(col, o); } } } } } return row; }  @SuppressWarnings("unchecked") Map<String, Object> transformRow(Map<String, Object> row,
                                          Context context); static final String REGEX; static final String REPLACE_WITH; static final String SPLIT_BY; static final String SRC_COL_NAME; static final String GROUP_NAMES; }