Skip to Content
0
Jul 20, 2010 at 09:34 PM

Replace string in java mapping

164 Views

Hello all,

In a java mapping, i have this condition :

String inData = this.convertStreamToString(arg0.getInputPayload().getInputStream());
      String interfaceID = "";
      String outData = "";
      Pattern p = Pattern.compile("<schemaID>(.*)</schemaID>");

      for(Matcher m = p.matcher(inData); m.find(); interfaceID = m.group(1)) {
         ;
      }
      
     this.getTrace().addInfo("BEFORE OK");
      if (interfaceID != null && interfaceID.length()> 0 && !interfaceID.equals("MAREVA")){
    	  String tagWithNamespace = "<TEST xmlns=\"http://namespace/mfr/mfr" + interfaceID.substring(3, 6) + "\" xmlns:ns2=\"http://namespace/types\">";
    	  this.getTrace().addInfo(tagWithNamespace);
    	  outData = inData.replaceFirst("<TEST xmlns:ns2=\"http://namespace/types\">", tagWithNamespace);

      }else if (interfaceID.equals("")){
    	 outData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <TEST></TEST>";
    	 this.getTrace().addInfo("ELSE OK");   	 
      }else{
         outData = inData;
      }

The fonction replacefirst will be useful only if it find the all the characters.

But in my case,sometime there is a namespace in the tag <TEST> but other time there isn't.

In my case, i always want to replace the tag <TEST> by the string Outdata,in anycase.

Does someone now how to do it please?

Thank you,

Jean-Philippe

Edited by: Jean-Philippe PAIN on Jul 20, 2010 11:34 PM

Edited by: Jean-Philippe PAIN on Jul 20, 2010 11:34 PM

Edited by: Jean-Philippe PAIN on Jul 20, 2010 11:35 PM