Altova MapForce 2024 Professional Edition

Change Data Type of Input/Output

Home Prev Top Next

MapForce-generated code can be integrated, or adapted to your specific application, even though the result of code generation is a complete and fully-functioning application. After generating Java or C# code with MapForce, you can optionally change the data type of the mapping input or output by editing the generated code. More specifically, you can use as mapping parameters objects of types other than those generated by default. For example, instead of having the mapping read the input from a file on the disk, you can provide a string or a stream object as input. Note that this feature is specific to code generated in C# or Java only.

 

The object types supported as input or output are listed in the first column of the table below. Each subsequent column specifies data formats where that specific type is supported. For a more precise definition of each type, see the "Type definitions" section below.

 


XML

JSON*

Microsoft Excel*

EDI (includes X12, HL7)*

FlexText*

CSV/Text

Files

Yes

Yes

Yes

Yes

Yes

Yes

Streams

Yes

Yes

Yes

Yes

Yes

Yes

Strings

Yes

Yes

Yes

Yes

Yes

Reader/Writer

Yes

Yes

Yes

Yes

Yes

DOM documents

Yes

 

* Formats supported only in MapForce Enterprise Edition

To change the data type of the mapping input or output:

1.Generate C# or Java code from a mapping.

2.In the generated code, find the call to the run method (in Java) or Run method (in C#), as follows:

 

a.If using C#, open the MappingConsole.cs file.

b.If using Java, open the MappingConsole.java (the console program) or the MappingFrame.java file (the GUI program).

 

Note:The name of the file may be different if you have changed the application name in the mapping settings. For example, if you changed it to "MyApp", then name of the generated file becomes MyAppConsole.js and MyAppConsole.java, and MyAppFrame.java, respectively.

 

3.Create an instance of the required type (see the "Type definitions" section).

4.Supply the declared objects as parameters to the run method (in Java) or Run method (in C#), as shown in the examples below.

 

The run method is the most important method 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. If your mapping contains components that process multiple files dynamically, the respective parameters do not appear in generated code, because in this case the file names are processed dynamically inside the mapping.

 

Type definitions

In C#, the types that you can provide as parameters to the Run method are classes defined in the Altova.IO namespace. The base classes are Altova.IO.Input and Altova.IO.Output, respectively.

C# types

Files

Altova.IO.FileInput(string filename)

Altova.IO.FileOutput(string filename)

Streams

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

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

Strings

Altova.IO.StringInput(string content)

Altova.IO.StringOutput(System.Text.StringBuilder sbuilder)

Reader/Writer

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

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

DOM documents

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

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

 

In Java, the types that you can provide as parameters to the run method are classes defined in the com.altova.io package. The base classes are com.altova.io.Input and com.altova.io.Output, respectively.

Java types

Files

com.altova.io.FileInput(String filename)
com.altova.io.FileOutput(String filename)

Streams

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

com.altova.io.StreamOutput(String filename)

Strings

com.altova.io.StringInput(String content)

com.altova.io.StringOutput()

Reader/Writer

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

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

DOM documents

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

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

 

Example

To illustrate changing the input and output programmatically, we will use the ConvertProducts.mfd mapping as a model. After installing MapForce and running it at least once, you can find this mapping in the following directory: C:\Users\<username>\Documents\Altova\MapForce2024\MapForceExamples\Tutorials.

cg_input_output_01

ConvertProducts.mfd

As illustrated above, the mapping converts data from a source XML document to another XML document. Our goals are as follows:

 

1.Generate Java and C# program code from this mapping.

2.Change the data type of the source component to a string type.

3.Change the data type of the target component to a string writer type.

 

To generate the program code, open the ConvertProducts.mfd mapping and run the File | Generate code in | C#  (or Java) command. For the scope of this example, we will assume that the mapping settings of ConvertProducts.mfd are the default ones.

cg_input_output_02

This example uses the following target directories for the generated code (feel free to change the path if necessary):

 

C:\codegen\cs\ConvertProducts, for C#

C:\codegen\java\ConvertProducts, for Java

 

Having generated the program code, open the MappingConsole.cs (in C#) or MappingConsole.java (in Java) and find the following lines:

C#

Altova.IO.Input Products2Source = Altova.IO.StreamInput.createInput("Products.xml");
Altova.IO.Output ProductValuePairs2Target = new Altova.IO.FileOutput("ProductValuePairs.xml");

Java

com.altova.io.Input Products2Source = com.altova.io.StreamInput.createInput("Products.xml");
com.altova.io.Output ProductValuePairs2Target = new com.altova.io.FileOutput("ProductValuePairs.xml");

 

Comment out the lines above and change the code as follows:

C#

//Altova.IO.Input Products2Source = Altova.IO.StreamInput.createInput("Products.xml");
//Altova.IO.Output ProductValuePairs2Target = new Altova.IO.FileOutput("ProductValuePairs.xml");
 
Altova.IO.Input Products2Source = new Altova.IO.StringInput("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\" +
                                      "<products xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"products.xsd\">\" +
                                      "   <product>\" +
                                      "      <id>100</id>\" +
                                      "      <color>blue</color>\" +
                                      "      <size>XXL</size>\" +
                                      "   </product>\" +
                                      "</products>\");
                   
System.IO.StringWriter writer = new System.IO.StringWriter(new System.Text.StringBuilder());
Altova.IO.Output ProductValuePairs2Target = new Altova.IO.WriterOutput(writer);
 
try
{
  MappingMapToProductValuePairsObject.Run(Products2Source, ProductValuePairs2Target);
 
  // Print out the writer object
  Console.Write(writer.ToString());
}
finally
{
  Products2Source.Close();
  ProductValuePairs2Target.Close();

}

Java

//com.altova.io.Input Products2Source = com.altova.io.StreamInput.createInput("Products.xml");
//com.altova.io.Output ProductValuePairs2Target = new com.altova.io.FileOutput("ProductValuePairs.xml");
       
com.altova.io.Input Products2Source = new com.altova.io.StringInput("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\" +
              "<products xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"products.xsd\">\" +
              "   <product>\" +
              "      <id>100</id>\" +
              "      <color>blue</color>\" +
              "      <size>XXL</size>\" +
              "   </product>\" +
              "</products>\");
       
java.io.StringWriter writer = new java.io.StringWriter();
com.altova.io.Output ProductValuePairs2Target = new com.altova.io.WriterOutput(writer);
 
try {
  MappingMapToProductValuePairsObject.run(Products2Source, ProductValuePairs2Target);

 
  // Print out the writer object
  System.out.print(writer.toString());
 
  } finally {
    (Products2Source).close();
    ProductValuePairs2Target.close();
}

 

In the C# and Java code listings above, the following happens:

 

The two original lines that provide the input and output to the run method were commented out. Consequently, the mapping application no longer reads data from Products.xml. In fact, we did not even need to copy this file to the program's working directory.

The type Products2Source has been declared as a StringInput that provides the content of the input XML file to be processed.

The type ProductValuePairs2Target has been declared as a WriterOutput type that takes a string writer as argument.

After the mapping completes running, the contents of the string writer is printed out to the console.

 

Usage guidelines for streams and Reader/Writer objects

When using binary streams or Reader/Writer objects as input or output to the mapping, note the following:

 

Binary stream objects and Reader/Writer 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 behavior, insert (or uncomment) the following line before calling the run method:

C#

MappingMapToSomething.CloseObjectsAfterRun = false;

Java

MappingMapToSomething.setCloseObjectsAfterRun(false);

 

Note:Make sure to change MappingMapToSomething to the name of the mapping object as applicable to your generated code.

 

Usage guidelines for strings

In Java, the constructor of StringOutput does not take an argument. The string content produced by the mapping can be accessed by calling the getString() method, for example:

Java

com.altova.io.Input Products2Source = com.altova.io.StreamInput.createInput("Products.xml");
com.altova.io.StringOutput ProductValuePairs2Target = new com.altova.io.StringOutput();
       
try {
  // Run the mapping
  MappingMapToProductValuePairsObject.run(Products2Source, ProductValuePairs2Target);
  // Get the string object
  String str = ProductValuePairs2Target.getString().toString();
}

 

In C#, the constructor of StringOutput takes a parameter of type StringBuilder which you need to declare beforehand. If the StringBuilder object already contains data, the mapping output will be appended to it.

C#

Altova.IO.Input Products2Source = Altova.IO.StreamInput.createInput("Products.xml");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Altova.IO.Output ProductValuePairs2Target = new Altova.IO.StringOutput(sb);
 
try
{
  // Run the mapping
  MappingMapToProductValuePairsObject.Run(Products2Source, ProductValuePairs2Target);
  // Get the string output
  String str = sb.ToString();
}

 

To run these code listings, you can use the same generated project as in the previous example. Make sure, however, to copy the file Products.xml from C:\Users\<username>\Documents\Altova\MapForce2024\MapForceExamples\Tutorials\ to your program's working directory, since the mapping code reads data from this file.

 

Usage guidelines for DOM documents

When using DOM documents as mapping input or output, note the following:

 

The document instance supplied as parameter to the DocumentOutput constructor must be empty.

After calling run, the DOM Document generated by the constructor of DocumentOutput already contains the mapping output, and you can manipulate the document as necessary.

 

© 2018-2024 Altova GmbH