Changing the data type of the mapping input/output (C#, Java)

www.altova.com このトピックを印刷 前のページ 1つ上のレベル 次のページ

ホーム >  Code Generator > Integrating MapForce-Generated Code >

Changing the data type of the mapping input/output (C#, Java)

This topic provides details on the object types you can use programmatically, if you intend to run MapForce mappings from a custom Java or C# application.

 

You can use several input and output objects (such as files, strings, DOM documents, and others) as parameters to the run method. The run method is the most important function of generated mapping classes. It has one parameter for each static source or input component in the mapping, and a final parameter for the output component. Components that process multiple files do not appear as parameters to the run method, because in this case the file names are processed dynamically inside the mapping.

 

The objects that you can provide as parameters to the run method are available in the com.altova.io package (Java) and Altova.IO namespace (C#). The base classes of the generated input and output objects are as follows:

 

C#

Altova.IO.Input

Altova.IO.Output

 

Java

com.altova.io.Input

com.altova.io.Output

 

The object types supported as input/output parameters to the run method, including their applicable input/output file formats, are listed in the following table.

 

Object Type

XML

Microsoft Excel*

EDI* **

FlexText*

CSV

Fixed-length files

Files

Y

Y

Y

Y

Y

Y

Binary stream objects

Y

Y

Y

Y

Y

Y

Strings

Y

Y

Y

Y

Y

I/O Reader/Writer (character stream objects)

Y

Y

Y

Y

Y

DOM documents

Y

 

* Formats supported only in MapForce Enterprise Edition

** Includes X12 and HL7

 

Files

File objects (identified in the code file names) have the following definition:

 

C#

Altova.IO.FileInput(string filename)

Altova.IO.FileOutput(string filename)

 

Java

com.altova.io.FileInput(String filename)

com.altova.io.FileOutput(String filename)

 

 

Binary stream objects

Binary stream objects in the generated code represent an alternative way to working with physical files; there are no advantages as far as memory use is concerned. Binary stream objects have the following definition:

 

C#

Altova.IO.StreamInput(System.IO.Stream stream)

Altova.IO.StreamOutput(System.IO.Stream stream)

 

Java

com.altova.io.StreamInput(java.io.InputStream stream)

com.altova.io.StreamOutput(java.io.OutputStream stream)

 

Notes:

 

Binary stream objects are expected to be opened and ready-to-use before calling the run method.
By default, the run method closes the stream when finished. To prevent this behaviour, insert the following code before calling the run method:

 

Java

MappingMapToSomething.setCloseObjectsAfterRun(false); // Java

 

C#

MappingMapToSomething.CloseObjectsAfterRun = false;   // C#

 

Strings

String objects have the following definition:

 

C#

Altova.IO.StringInput(string content)

Altova.IO.StringOutput(StringBuilder content)

 

Java

com.altova.io.StringInput(String xmlcontent)

com.altova.io.StringOutput()

 

In Java, StringOutput does not take an argument. Content can be accessed with:

 

// mapping from String to (another) String
String MyText = "<here>is some XML text</here>";
 
Input input = new StringInput(MyText);
Output output = new StringOutput();
 
MappingMapToMyText.run(input, output);
 
String myTargetData = output.getString().toString();

 

 

The getString() method returns a StringBuffer, hence the need for toString().

 

In C#, StringOutput takes an argument (StringBuilder) which you need to provide beforehand. The StringBuilder may already contain data, so the mapping output is appended to it.

 

Excel sources/targets cannot map to or from strings.

 

I/O Reader/Writer (character stream objects)

Character stream objects have the following definition:

 

C#

Altova.IO.ReaderInput(System.IO.TextReader reader)

Altova.IO.WriterOutput(System.IO.TextWriter writer)

 

Java

com.altova.io.ReaderInput(java.io.Reader reader)

com.altova.io.WriterOutput(java.io.Writer writer)

 

Notes:

 

Character stream objects are expected to be opened and ready-to-use before calling the run method.
Excel sources/targets cannot be read from, or written to, character streams.
By default, the run method closes the stream when finished. To prevent this behaviour, insert the following code before calling the run method:

 

Java

MappingMapToSomething.setCloseObjectsAfterRun(false); // Java

 

C#

MappingMapToSomething.CloseObjectsAfterRun = false;   // C#

 

DOM documents

DOM documents have the following definition:

 

C#

Altova.IO.DocumentInput(System.Xml.XmlDocument document)

Altova.IO.DocumentOutput(System.Xml.XmlDocument document)

 

Java

com.altova.io.DocumentInput(org.w3c.dom.Document document)

com.altova.io.DocumentOutput(org.w3c.dom.Document document)

 

Notes:

 

The document passed to the DocumentOutput constructor as target must be empty.
After calling run, the DOM Document generated by the constructor of DocumentOutput already contains mapped data so "save to document" is not necessary. After mapping, you can manipulate the document as necessary.
Only XML content can be mapped to DOM documents.

 

Example

Let's assume you want to integrate the code generated by MapForce into your Java application. Your MapForce mapping consists of two source XML files and a target text file. When you generate the MapForce code, the run function looks as follows:

 

void run(Input in1, Input in2, Output out1);

 

Let's also assume that your application requires that you map data from a local file and binary stream into a character stream. Since data is supplied from other sources, your application must declare the sources and targets as:

 

String filename; // Declare the source of the first input
Java.io.InputStream stream; // Declare the source of the second input
Java.io.Writer writer; // Declare the output as character stream

 

 

The following wrappers must be constructed for the MapForce-generated run function:

 

// com.altova.io is considered imported here:
Input input1 = new FileInput(filename);
Input input2 = new StreamInput(stream);
Output output1 = new WriterOutput(writer);

 

Now you can call the MapForce generated run function:

 

MappingMapToSomething.run(input1, input2, output1);

 

The C# behavior is almost identical, except that run is called Run, and the .NET stream and reader/writer classes are named differently.

 

Using the same technique, you can also use other input and output types, such as strings or DOM documents.


(C) 2019 Altova GmbH