cancel
Showing results for 
Search instead for 
Did you mean: 

How to FCC a long source file in a single line?

former_member203764
Participant
0 Kudos

Hi Gurus,

After long research I cannot find a suitable sulotion.

Envirement: PI 7.50 SP03 single-Java.

Reqiurement: File --> PI --> SAP

I'm using FCC to convert the source file.

Recordset,*

Recordset.fieldNames: Comany, sub, customer, firstName, familyName, street...

Recordset.fieldFixedLengths: 3,3,40,20,20,60,...

Recordset.endSeparator: 'nl'

The 1 Recordset length is 250.

The proble is:

The source file is a long ascii file just in one single line with thousand recordsets one after another. The total length is nX250 (n is the number of recordset).

With the above FCC i got just the first Recordset into PI.

Who can tell me, how to get them all into PI?

Very urgent!

Thanks a lot!

Regards

Sara

former_member190293
Active Contributor
0 Kudos

Yes, I see that it doesn't work this way.

Accepted Solutions (1)

Accepted Solutions (1)

weberpat
Contributor
0 Kudos

Hi Sara,
I don't think this is possible in FCC and you'll either need to handle the content conversion in an adapter module or execute a shell script that will insert line breaks after 250 characters as a pre-process in your file communication channel.

Assuming your system runs on Linux or some form of Unix, the fold command is your friend.

Regards,
Patrick

Answers (6)

Answers (6)

former_member190293
Active Contributor
0 Kudos

Hi Sara!

As per your request: here is a sample code for reading single line file and convert its content into XML structure:

package com.xpi.train.globus;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class SingleLinePlainToXMLSplitter {

public static ArrayList<String> getLinesArray(String inputString, int lineLength, boolean truncLastLine) {
ArrayList<String> lines = new ArrayList<String>();

if (inputString == null || inputString.isEmpty() || lineLength <= 0)
return null;
if (!truncLastLine && (inputString.length() % lineLength != 0))
return null;

while(inputString.length() > 0) {
    if (inputString.length() < lineLength) {
    lines.add(inputString);
    break;
    }
    else {
    String chunk = inputString.substring(0,lineLength);
    lines.add(chunk);
    inputString = inputString.substring(lineLength);
    }
} 

return lines;
}

public static Map<String, String> splitLine(String inputLine, Map<String, Integer> lineStruct) {
Map<String, String> struct = new LinkedHashMap<String, String>();

if (inputLine == null || lineStruct.isEmpty())
return null;
for (Entry<String, Integer> lineItem : lineStruct.entrySet()) {
int currLength = lineItem.getValue();
if (inputLine.length() <= currLength) {
struct.put(lineItem.getKey().trim(), inputLine);
break;
}
else {
String chunk = inputLine.substring(0,currLength);
struct.put(lineItem.getKey().trim(), chunk);
inputLine = inputLine.substring(currLength);
}
}

return struct;
}

public static ArrayList<Map<String, String>> splitString(String inputString, int lineLength, boolean acceptTruncatedLines, Map<String, Integer> lineStruct) {
ArrayList<Map<String, String>> stringContents = new ArrayList<Map<String, String>>();
if (inputString == null || inputString.isEmpty() || lineLength <= 0 || lineStruct.isEmpty())
return null;

ArrayList<String> stringLines = getLinesArray(inputString, lineLength, acceptTruncatedLines); 
if (stringLines == null || stringLines.isEmpty())
return null;
for (String stringLine : stringLines) {
Map<String, String> lineContents = splitLine(stringLine, lineStruct);
if (lineContents == null || lineContents.isEmpty())
return null;
else
stringContents.add(lineContents);
}

return stringContents;
}

public static ArrayList<Map<String, String>> splitString(String inputString, Integer lineLength, boolean acceptTruncatedLines, String fieldNames, String fieldLengths) {
String[] fields = fieldNames.trim().split(",|;");
String[] lengths = fieldLengths.trim().split(",|;");

if (fields.length == 0 || lengths.length == 0 || fields.length != lengths.length)
return null;
Map<String, Integer> fieldsMap = new LinkedHashMap<String, Integer>();
int lengthsSum = 0;
for (int i = 0; i < fields.length; i++) {
fieldsMap.put(fields[i].trim(), Integer.parseInt(lengths[i].trim()));
lengthsSum = lengthsSum + Integer.parseInt(lengths[i].trim());
}
if (lineLength == null || lineLength <= 0)
lineLength = lengthsSum;

return splitString(inputString, lineLength, acceptTruncatedLines, fieldsMap);
}

public static ArrayList<Map<String, String>> splitString(String inputString, boolean acceptTruncatedLines, String fieldNames, String fieldLengths) {

return splitString(inputString, null, acceptTruncatedLines, fieldNames, fieldLengths);
}
public static void main(String[] args) {
ArrayList<Map<String, String>> fileContents = new ArrayList<Map<String, String>>();

try {
InputStream fileIn = new FileInputStream("C:\\Tests\\SingleLinePlainIn.txt");
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = fileIn.read(buffer)) != -1) {
    result.write(buffer, 0, length);
}
String fileString = result.toString("UTF-8");
fileIn.close();

fileContents = splitString(fileString, true, "EmployeeID,LastName,FirstName,MiddleName,Position", "6,11,9,15,15");
if (fileContents != null) {
String outXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:MT_SingleRowMessage xmlns:ns0=\"http://tempuri.org\">";
for (Map<String, String> row : fileContents) {
outXML += "<Row>";
for (Entry<String, String> elem : row.entrySet())
outXML += "<" + elem.getKey().trim() + ">" + elem.getValue().trim() + "</" + elem.getKey().trim() + ">";
outXML += "</Row>";
}
outXML += "</ns0:MT_SingleRowMessage>";
OutputStream outFile = new FileOutputStream("C:\\Tests\\SingleLineXMLOut.xml");

outFile.write(outXML.getBytes("UTF-8"));
outFile.flush();
outFile.close();

System.out.println("File is written successfully!");
}
else {
System.out.println("File content is undefined");
}

}
catch (Exception e) {
e.printStackTrace();
}

}
}

Regards, Evgeniy.

manoj_khavatkopp
Active Contributor
0 Kudos

Alternatively if your third party doesn't agree to add line breaks then you can do it splitting it as 2 scenario:

1 . First Scenario this would be a pass through scenario with ESR.

in Sender channel use FCC for just 1 filed with fixed length as 250 chars and in receiver make it as plain file back with line break. and drop the file in local PI folder.

2.In Second scenario do ur actual FCC

Br,

Manoj

former_member190293
Active Contributor
0 Kudos

Hi Manoj!

The point is that FCC (and even MessageTransformBean) reqires each line to be separated. So even if you try to split the line by length of 250 symbols (use one field per record) - you'll get only the first one.

Actually, java code I wrote for similar requirement is rather small and simple. And it could be used as first mapping program to get correct XML structure.

Regards, Evgeniy.

former_member203764
Participant
0 Kudos

Hi Evgeniy,

As Manoj said, the third-party cannot add line-break in their file.

You have a java code to transform the long line into record,* structure? Great!

Could u paste your code here and share with us?

Thanks and nice weekend!

Sara

former_member203764
Participant
0 Kudos

Thank you all for the suggestion!

OK. I'll contact provider to add line separator in the file.

apu_das2
Active Contributor
0 Kudos

As Evgeniy and Patrick said, this will always give you first recordset data unless your recordset will be separated by new line.

To deal with this ascii you need to use either java mapping or custom module.

If you are getting data from NFS and not from FTP, then you can use shell script as well.

Thanks,

Apu

former_member190293
Active Contributor
0 Kudos

Hi Sara!

You should follow Patrick's suggestion.

Another option is to read file without FCC and use java mapping to build desired XML structure.

Regards, Evgeniy.

former_member203764
Participant
0 Kudos

anyone could help me out?