2

Sure I do something wrong, but can't realize what :) I populate a key/value pair collection Map keyValuePairs with data, after I try to check collection for specific key existence in a common way keyValuePairs.containsKey("Name") and get back "false". But keyValuePairs.keySet() gives back set of keys where "Name" exists.

public static void parseBusinessObject(String input, Object output) {

Class parsingObject = output.getClass();
Field[] fields = parsingObject.getDeclaredFields();

Pattern pattern = Pattern.compile("([^{=;]*)=([^;}]*);");
Matcher matcher = pattern.matcher(/*input*/"anyType{Id=1; Name=Til afskrivning; LocationId=1; Editable=true; Default=true; Transcribed=false; }");
Map<String, String> keyValuePairs = new HashMap<String, String>();
while (matcher.find()) {
    if(!keyValuePairs.containsKey(matcher.group(1))){
    keyValuePairs.put(matcher.group(1).trim(), matcher.group(2).trim());
}
}

for (Field field : fields) {
    if(keyValuePairs.containsKey(field.getName())){
        //TODO: add values to fields    
    }
}
}

output result after matching:

 Id=1;
 Name=Til afskrivning;
 LocationId=1;
 Editable=true;
 Default=true;
 Transcribed=false;

"keyValuePairs"= HashMap (id=830062742672) { LocationId=1, Default=true, Editable=true, Name=Til afskrivning, Id=1, Transcribed=false}

"keyValuePairs.keySet()"= HashMap$1 (id=830062763448)
[ LocationId, Default, Editable, Name, Id, Transcribed]

"keyValuePairs.containsKey("Name")"= false

Could anybody please explain me what's wrong with it? Thank you.

Maxim
  • 4,152
  • 8
  • 50
  • 77
  • 2
    Are you sure your key is "Name", and not "Name " or " Name" or even "\tName" ? – Riduidel Jul 08 '10 at 13:32
  • 1
    Might want to include the regex you're using and the source data, which would help answer @Riduidel's question – andersoj Jul 08 '10 at 13:39
  • 1
    Can we get a peak at your matcher regex? It could easily be that you're mis-matching something. It's usually a good idea to normalize String keys before doing a .put(...) anyway though. – j flemm Jul 08 '10 at 13:42

1 Answers1

3

Quoting the information you gave in the question:

"keyValuePairs.keySet()"= HashMap$1  (id=830062763448)  
[ LocationId,  Default,  Editable,  Name, Id,  Transcribed]

The extra spaces in front of some of the key names suggest that the key inserted was " Name" (note the preceding space). If you give more information about the regex, we may be able to figure out why this happened.

You can also debug this yourself by logging/printing what group(1) and group(2) matches; I'm sure you'll find that it matches the extra whitespaces.

A quick fix is to put group(1).trim() and group(2).trim() into the map instead, but the better option is to fix the regex.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 1
    I'm going to guess the regex is something like `([^=]*)=(...)`. The solution may just be to add `\s*` in front of group 1. – polygenelubricants Jul 08 '10 at 13:46
  • @Maxim: if you give enough information, I may be able to convince you to use `Scanner` instead. Or perhaps some other specialized libraries to handle key/value pairs, `java.util.Properties` et.al. – polygenelubricants Jul 08 '10 at 14:03
  • 1
    Question is edited, seems added everything, it's quite simple method to parse each property of soap response. – Maxim Jul 08 '10 at 14:20
  • @Maxim: I have no experience with SOAP, but check out http://stackoverflow.com/questions/1468428/how-to-extract-data-from-a-soap-response-in-java – polygenelubricants Jul 08 '10 at 14:30