cancel
Showing results for 
Search instead for 
Did you mean: 

Handling space in java class

jean-philippe_pain2
Participant
0 Kudos

Hello all,

My scenario is Mail -> PI - > SQL Server.

In order to bypass a problem of data format in my incoming mail, i use the java class Message Unwrapper.

Everything works fine except that it stuck all my words together !!

For example :

Input :

<Address>Test test test Test test test

test test test

Test test test Test test test

</adress>

Output :

<adress>TesttesttestTesttesttestTesttesttestTesttesttest</adress>

I want in output : <adress> test test test test test test test test test test </adress>

Do you know how i can handle that ?

My code is :


public class MessageUnwrapper implements StreamTransformation { 
// Outbound structure 
        public static int bufferSize = 128; 
private static final String XML_START_CONTENT = "<Content>";      
        /** 
         * @see com.sap.aii.mapping.api.StreamTransformation#setParameter(Map) 
         */ 
        public void setParameter(Map arg0) { 
public void execute(InputStream in, OutputStream out) 
                        throws StreamTransformationException { 
                try { 
                        String xml = getStreamAsString(in); 
                        writeOutputStream(xml, out); 
                } catch (Exception e) { 
                        e.printStackTrace(); 
                        throw new StreamTransformationException( 
                                        "Exception during KeyMapping: (" + e.getClass().getName() 
                                                        + ") " + e.getMessage(), e); 
                } 
    } 
        private BufferedReader readInputStream(InputStream in) { 
                InputStreamReader isr = new InputStreamReader(in); 
                return new BufferedReader(isr); 
        } 
        /** 
         * @param output 
         * @return 
         */ 
        private void writeOutputStream(String xml, OutputStream out) { 
                try { 
                        out.write(xml.getBytes()); 
                } catch (Exception e) { 
                        System.out.println(xml); 
                } 
        } 
private String getStreamAsString(InputStream in) { 
                try { 
                        StringBuffer output = new StringBuffer(); 
                        String xml = ""; 
                        BufferedReader br = readInputStream(in); 
                        String line1; 
                        
                        
                        while ((line1 = br.readLine()) != null) {                                                                                
output.append(line1); 
                        } 

                        br.close();                        
                                   // < and > 
                                            xml += output.toString(); 
                        int deb; 
                        String xmldeb = ""; 
                        String xmlcontent = ""; 
                        
                        if ( ((deb = xml.indexOf(XML_START_CONTENT)) != -1) ) { 
                        xmldeb = xml.substring(0, deb); 
                        xmlcontent = xml.substring(deb); 
                                } 
                        
                        xmlcontent = xmlcontent.replaceAll("\\&gt\\;", ">"); 
                        xmlcontent = xmlcontent.replaceAll("\\&lt\\;", "<"); 
                        xmlcontent = xmlcontent.replaceAll("\\<\\;", "<"); 
                        xmlcontent = xmlcontent.replaceAll("\\>\\;", ">");                  
// & 
                        xmlcontent = xmlcontent.replaceAll("\\&amp\\;", "&"); 
                        xmlcontent = xmlcontent.replaceAll("\\&\\;", "&"); 
                        
                     xmlcontent = xmlcontent.replaceAll("&", "&amp;"); 
    xml = xmldeb + xmlcontent;
 return xml; 
                } catch (Exception e) { 
                        e.printStackTrace(); 
                        return null; 
                } 
        } 
public static void main(String[] args) { 
                if (args.length < 1) { 
                        System.out.println("Input file not found!"); 
                        System.exit(0); 
                } 
                try { 
                        InputStream in = new FileInputStream(args[0]); 
                        System.out.println(args[0]); 
                        OutputStream out = new FileOutputStream(args[0] + ".out.xml"); 
                        new MessageUnwrapper().execute(in, out); 
                } catch (Exception e) { 
                        e.printStackTrace(); 
                        System.exit(0); 
                } 
        } 
} 

Edited by: Jean-Philippe PAIN on Apr 20, 2009 11:34 AM

Edited by: Jean-Philippe PAIN on Apr 20, 2009 11:35 AM

Accepted Solutions (0)

Answers (3)

Answers (3)

prasannakrishna_mynam
Contributor
0 Kudos

Hello,

Modify statement in your method private String getStreamAsString(InputStream in) , the following statement to modify:

case-1:

while ((line1 = br.readLine()) != null) {

xml += line1;

}

if case:1 doesn't work try case 2:

case-2:

output.append(line1); instead use output.writeln(line1) check with this, think you shall get that..

Regards..

P.K

jean-philippe_pain2
Participant
0 Kudos

I still have my problem when i look the output in the Test tab of the Interface mapping.

I even tried to do :

output.append(line1+ "\n");

but it doesn't works either...

I recall my problem :

<text>

NAME : Brian

LASTNAME : BREAD

</text>

output:

<text>NAME : BrianLASTNAME : BREAD </text>

I don't understand why it doesn't work...I must miss something...

prasannakrishna_mynam
Contributor
0 Kudos

Hello, have you tested with the cases i have mentioned in my previouse reply, still you face the problem try with the Following...

FileWriter fw = new FileWriter(filename);

PrintWriter pr = new PrintWriter(fw);

while ((line_read = br.readLine()) != null) {

pr.println(line_read);

}

jean-philippe_pain2
Participant
0 Kudos

With the code, it's better !

I hope someone got an idea !

Thanks.

jean-philippe_pain2
Participant
0 Kudos

Thanks for the answer but i am not trying to handle the CAPS but the space..

Former Member
0 Kudos

Hi

Write UDF to add space if needed in between test (if test is always contant length)

Can u give u r Element input and output format clearly seeing u r output format required we can suggest

srini

jean-philippe_pain2
Participant
0 Kudos

TEST was an example. Normally, it's some commentary so the length is not fixed !!

My input date is in XML format (string), my output also.

I don't understand why it stucks all my words !!

e.g :

input :

<comment> Brian is in the kitchen.

Where is Brian ?

</comment>

Bad output :

<comment>Brianisinthekitchen.WhereisBrian?</comment>

Good output(what i want) :

<comment>Brian is in the kitchen. where is Brian ?</comment>

Former Member
0 Kudos

Hi

Hope u r using JAVA Mapping for u r Mapping requirement , am not sure why this is happening,

But try this solution

Mapping Program (MM1) --- JAVA Mapping

Create a Message Mapping (MM2) --- Do one to one --- Corresponding to the comment do one to one mapping

Now in Interface mapping use both Mapping program in sequence

IM_OUT -


MM1 -


IM_IN

MM2

Try this if it works lemme know

Srini

jean-philippe_pain2
Participant
0 Kudos

I am not using UDF but a java class before my mapping...

Former Member
0 Kudos

I mean the same u r using as mapping program as JAVA Class (Java Mapping)

MM1 - U r Java Mapping

MM2 - Create Message mapping and map only "Comment"

Use both the mapping program in Interface mapping

rgds

srini

Former Member
0 Kudos

Hi

Give Address ELEMENT ---> ReplaceAll ("Test" , "test") --> Target Element

Srini