<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Replace string in java mapping in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126688#M1511892</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jean-Philippe, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regular expression would help in your case. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.regular-expressions.info/java.html" target="test_blank"&gt;http://www.regular-expressions.info/java.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Min&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 20 Jul 2010 21:43:07 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2010-07-20T21:43:07Z</dc:date>
    <item>
      <title>Replace string in java mapping</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126687#M1511891</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In a java mapping, i have this condition :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
String inData = this.convertStreamToString(arg0.getInputPayload().getInputStream());
      String interfaceID = "";
      String outData = "";
      Pattern p = Pattern.compile("&amp;lt;schemaID&amp;gt;(.*)&amp;lt;/schemaID&amp;gt;");

      for(Matcher m = p.matcher(inData); m.find(); interfaceID = m.group(1)) {
         ;
      }
      
     this.getTrace().addInfo("BEFORE OK");
      if (interfaceID != null &amp;amp;&amp;amp; interfaceID.length()&amp;gt; 0 &amp;amp;&amp;amp; !interfaceID.equals("MAREVA")){
    	  String tagWithNamespace = "&amp;lt;TEST xmlns=\"http://namespace/mfr/mfr" + interfaceID.substring(3, 6) + "\" xmlns:ns2=\"http://namespace/types\"&amp;gt;";
    	  this.getTrace().addInfo(tagWithNamespace);
    	  outData = inData.replaceFirst("&amp;lt;TEST xmlns:ns2=\"http://namespace/types\"&amp;gt;", tagWithNamespace);

      }else if (interfaceID.equals("")){
    	 outData = "&amp;lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&amp;gt; &amp;lt;TEST&amp;gt;&amp;lt;/TEST&amp;gt;";
    	 this.getTrace().addInfo("ELSE OK");   	 
      }else{
         outData = inData;
      }
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The fonction replacefirst will be useful only if it find the all the characters.&lt;/P&gt;&lt;P&gt;But in my case,sometime there is a namespace in the tag &amp;lt;TEST&amp;gt; but other time there isn't.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my case, i always want to replace the tag &amp;lt;TEST&amp;gt; by the string Outdata,in anycase.&lt;/P&gt;&lt;P&gt;Does someone now how to do it please? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Jean-Philippe&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Jean-Philippe PAIN on Jul 20, 2010 11:34 PM&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Jean-Philippe PAIN on Jul 20, 2010 11:34 PM&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Jean-Philippe PAIN on Jul 20, 2010 11:35 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jul 2010 21:34:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126687#M1511891</guid>
      <dc:creator>jean-philippe_pain2</dc:creator>
      <dc:date>2010-07-20T21:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in java mapping</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126688#M1511892</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jean-Philippe, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regular expression would help in your case. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.regular-expressions.info/java.html" target="test_blank"&gt;http://www.regular-expressions.info/java.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Min&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jul 2010 21:43:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126688#M1511892</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-07-20T21:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in java mapping</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126689#M1511893</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HEllo Min,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've read your link but i don't understand how i can apply it to my code. (i am not a java expert).&lt;/P&gt;&lt;P&gt;Can you explain a little more plz.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jul 2010 22:21:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126689#M1511893</guid>
      <dc:creator>jean-philippe_pain2</dc:creator>
      <dc:date>2010-07-20T22:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in java mapping</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126690#M1511894</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jean-Philippe, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to replace just all the TEST tags but NOT the contents in between, you can use the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;s.replaceAll("&amp;lt;(?i)[TEST|/TEST](.*?)&amp;gt;", "NEW STRING");&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to replace all the TEST tags AND the contents in between, you can use the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;s.replaceAll("&amp;lt;(?i)TEST(.&lt;STRONG&gt;?)&amp;gt;(.&lt;/STRONG&gt;?)&amp;lt;/TEST", "NEW STRING");&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;(?i) makes the match case insensitve. (.*?) matches any strings such as namespaces. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are always other ways to write regular expressions. Please try and see if it helps. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Min&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jul 2010 23:00:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126690#M1511894</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-07-20T23:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in java mapping</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126691#M1511895</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Min.&lt;/P&gt;&lt;P&gt;I've began to use regular expression as you've subjected but i've got an issue.&lt;/P&gt;&lt;P&gt;Indeed, it's going always in my KO loop. Maybe my Pattern a is wrong, what do you think about it ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
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("&amp;lt;schemaID&amp;gt;(.*)&amp;lt;/schemaID&amp;gt;");

      for(Matcher m = p.matcher(inData); m.find(); interfaceID = m.group(1)) {
         ;
      }
      
      
     this.getTrace().addInfo("BEFORE OK");
      if (interfaceID != null &amp;amp;&amp;amp; interfaceID.length()&amp;gt; 0 &amp;amp;&amp;amp; !interfaceID.equals("MAREVA")){
    	  String tagWithNamespace = "&amp;lt;TEST xmlns=\"http://namespacer/mfr/mfr" + interfaceID.substring(3, 6) + "\" xmlns:ns2=\"http://namespace/types\"&amp;gt;";
    	     	     	  
    	  this.getTrace().addInfo(tagWithNamespace);
    	  String Tag = "";
          Pattern a = Pattern.compile("&amp;lt;TEST xmlns.*&amp;gt;");
          
          for (Matcher c = a.matcher(inData); c.find(); Tag = c.group()){
        	  
          }
          
          if (Tag != null &amp;amp;&amp;amp; Tag.length()&amp;gt; 0){
        	  this.getTrace().addInfo("ELSE PATTERN OK");
        	  outData = inData.replaceFirst("&amp;lt;TEST xmlns:ns2=\"http://namespace/types\"&amp;gt;", tagWithNamespace);
        	  
          }else {
        		  this.getTrace().addInfo("ELSE PATTERN KO");
        		  this.getTrace().addInfo(Tag);
        		  outData = inData.replaceFirst("&amp;lt;TEST&amp;gt;", tagWithNamespace);  
        	  }
         // }
    	  this.getTrace().addInfo(outData);
    	  this.getTrace().addInfo("IF OK");
    	  
      }else if (interfaceID.equals("")){
    	 outData = "&amp;lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&amp;gt; &amp;lt;TEST&amp;gt;&amp;lt;/TEST&amp;gt;";
    	 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()) &amp;gt; -1) {
            sb.append((char)ch);
         }

         reader.close();
      } catch (Exception var6) {
         ;
      }

      return sb.toString();
   }
}
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Jean-Philippe PAIN on Jul 21, 2010 1:07 AM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jul 2010 23:06:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126691#M1511895</guid>
      <dc:creator>jean-philippe_pain2</dc:creator>
      <dc:date>2010-07-20T23:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in java mapping</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126692#M1511896</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jean-Philippe, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The pattern looks ok. But two of the FOR loops are questionable. You can use something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for(Matcher m = p.matcher(inData); m.find(); ) {&lt;/P&gt;&lt;P&gt;         interfaceID = m.group()&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or simply this since you expect only one match:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Matcher m = p.matcher(inData);&lt;/P&gt;&lt;P&gt;m.find();&lt;/P&gt;&lt;P&gt;interfaceID = m.group();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You may want to talk to Java developers in your company for further help. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Min&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Jul 2010 16:13:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126692#M1511896</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-07-21T16:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in java mapping</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126693#M1511897</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As you said, it works now.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jul 2010 12:23:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/replace-string-in-java-mapping/m-p/7126693#M1511897</guid>
      <dc:creator>jean-philippe_pain2</dc:creator>
      <dc:date>2010-07-23T12:23:01Z</dc:date>
    </item>
  </channel>
</rss>

