Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Replace string in java mapping

jean-philippe_pain2
Participant
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Jean-Philippe,

Regular expression would help in your case.

http://www.regular-expressions.info/java.html

Regards,

Min

6 REPLIES 6

Former Member
0 Kudos

Hi Jean-Philippe,

Regular expression would help in your case.

http://www.regular-expressions.info/java.html

Regards,

Min

0 Kudos

HEllo Min,

I've read your link but i don't understand how i can apply it to my code. (i am not a java expert).

Can you explain a little more plz.

Thank you.

0 Kudos

Hi Jean-Philippe,

If you want to replace just all the TEST tags but NOT the contents in between, you can use the following:

s.replaceAll("<(?i)[TEST|/TEST](.*?)>", "NEW STRING");

If you want to replace all the TEST tags AND the contents in between, you can use the following:

s.replaceAll("<(?i)TEST(.?)>(.?)</TEST", "NEW STRING");

(?i) makes the match case insensitve. (.*?) matches any strings such as namespaces.

There are always other ways to write regular expressions. Please try and see if it helps.

Regards,

Min

0 Kudos

Thanks Min.

I've began to use regular expression as you've subjected but i've got an issue.

Indeed, it's going always in my KO loop. Maybe my Pattern a is wrong, what do you think about it ?


public class JavaMapping extends AbstractTransformation {

   public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {
      this.getTrace().addInfo("JAVA Mapping Called");
      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://namespacer/mfr/mfr" + interfaceID.substring(3, 6) + "\" xmlns:ns2=\"http://namespace/types\">";
    	     	     	  
    	  this.getTrace().addInfo(tagWithNamespace);
    	  String Tag = "";
          Pattern a = Pattern.compile("<TEST xmlns.*>");
          
          for (Matcher c = a.matcher(inData); c.find(); Tag = c.group()){
        	  
          }
          
          if (Tag != null && Tag.length()> 0){
        	  this.getTrace().addInfo("ELSE PATTERN OK");
        	  outData = inData.replaceFirst("<TEST xmlns:ns2=\"http://namespace/types\">", tagWithNamespace);
        	  
          }else {
        		  this.getTrace().addInfo("ELSE PATTERN KO");
        		  this.getTrace().addInfo(Tag);
        		  outData = inData.replaceFirst("<TEST>", tagWithNamespace);  
        	  }
         // }
    	  this.getTrace().addInfo(outData);
    	  this.getTrace().addInfo("IF OK");
    	  
      }else if (interfaceID.equals("")){
    	 outData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <TEST></TEST>";
    	 this.getTrace().addInfo("ELSE OK");   	 
      }else{
         outData = inData;
      }
    	  
      
    this.getTrace().addInfo("END IF");
      try {
         arg1.getOutputPayload().getOutputStream().write(outData.getBytes("UTF-8"));
      } catch (Exception var10) {
         ;
      }

   }

   public String convertStreamToString(InputStream in) {
      StringBuffer sb = new StringBuffer();

      try {
         InputStreamReader isr = new InputStreamReader(in);
         BufferedReader reader = new BufferedReader(isr);

         int ch;
         while((ch = in.read()) > -1) {
            sb.append((char)ch);
         }

         reader.close();
      } catch (Exception var6) {
         ;
      }

      return sb.toString();
   }
}

Edited by: Jean-Philippe PAIN on Jul 21, 2010 1:07 AM

0 Kudos

Hi Jean-Philippe,

The pattern looks ok. But two of the FOR loops are questionable. You can use something like this:

for(Matcher m = p.matcher(inData); m.find(); ) {

interfaceID = m.group()

}

or simply this since you expect only one match:

Matcher m = p.matcher(inData);

m.find();

interfaceID = m.group();

You may want to talk to Java developers in your company for further help.

Regards,

Min

0 Kudos

Hello,

As you said, it works now.

Thank you