Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 13

Transforming XML fields

When transforming XML to JSON you may run into a couple of difficulties:

XML only allows one root element, while JSON allows multiple.
There is no array concept in XML (only repeated elements), however the JSON
data structure is based on arrays and objects.
To overcome these issues, DataWeave provides the following solutions:

To transform XML repeated elements into a JSON array, use the asterisk “*”
DataWeave selector to create it.
As JSON could have multiple root elements, you don’t have to do anything
specific to generate the output.

Only HTTP Listner


<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener


config" doc:id="aeed9810-1548-4b29-a123-13ecfaece654" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>

<flow name="skb-training4-american-wsFlow" doc:id="c0e5f40d-e84a-47eb-8422-


27abe99be3ac" >
<http:listener doc:name="GET /flights" doc:id="128b9fe7-42a5-4c06-9534-
b9840e1e5859" config-ref="HTTP_Listener_config" path="/flights"
allowedMethods="GET"/>
</flow>

</mule>

After adding payload from mule palette

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http=http://www.mulesoft.org/schema/mule/http
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener


config" doc:id="aeed9810-1548-4b29-a123-13ecfaece654" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>

<flow name="skb-training4-american-wsFlow" doc:id="c0e5f40d-e84a-47eb-8422-


27abe99be3ac" >
<http:listener doc:name="GET /flights" doc:id="128b9fe7-42a5-4c06-9534-
b9840e1e5859" config-ref="HTTP_Listener_config" path="/flights"
allowedMethods="GET"/>
<set-payload value="Flight Info" doc:name="Set Payload"
doc:id="233835b6-7355-45f7-b44d-0d826cff470f" />
</flow>

</mule>

*******************************************

XML for HTTP, db connection using config porperties file


<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db
http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">

<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener


config" doc:id="bd1b8a06-fdf9-490e-ae5c-7f073671877b" >
<http:listener-connection host="0.0.0.0" port="${http.port}" />
</http:listener-config>

<configuration-properties doc:name="Configuration properties"


doc:id="9dabe3a6-b55b-419b-bb65-9118e9e741b8" file="config.yaml" />

<db:config name="MySQL_Database_Config" doc:name="Database Config"


doc:id="d5cc32a9-b76a-4fcc-9bc6-cd46b19d78fd" >
<db:my-sql-connection host="${mysqldb.host}" port="${mysqldb.port}"
user="${mysqldb.user}" password="${mysqldb.password}" database="$
{mysqldb.database}"/>
</db:config>

</mule>

*******************************************
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db
http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

<flow name="skb-training4-american-wsFlow" doc:id="c0e5f40d-e84a-47eb-8422-


27abe99be3ac" >

<http:listener doc:name="GET /flights" doc:id="128b9fe7-42a5-4c06-9534-


b9840e1e5859" path="/flights" allowedMethods="GET" config-
ref="HTTP_Listener_config"/>

<db:select doc:name="Select" doc:id="d1682d0a-b2cf-41fb-b4c6-


14ba43156cdd" config-ref="MySQL_Database_Config">
<db:sql ><![CDATA[select * from american]]></db:sql>
</db:select>

<ee:transform doc:name="sql to json" doc:id="db16d41f-711e-43e0-961a-


3020405de7ca" >

<ee:message >

<ee:set-payload ><![CDATA[%dw 2.0


output application/json
---
payload map ( payload01 , indexOfPayload01 ) -> {
ID: payload01.ID,
code: (payload01.code1
default "") ++ (payload01.code2 default ""),
price: payload01.price default 0,
departureDate: payload01.takeOffDate as String default "",
origin: payload01.fromAirport default "",
destination: payload01.toAirport default "",
emptySeats: payload01.seatsAvailable default 0,
plane: {
"type": payload01.planeType default "",
totalSeats: payload01.totalSeats default 0
}
}
]]></ee:set-payload>

</ee:message>

</ee:transform>

</flow>

</mule>

*******************************************************
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/mule-apikit
http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd ">
<apikit:config name="american-flights-api-config" api="resource::a5bbf80e-03e5-
4f32-9968-0b69770d1024:american-flights-api:1.0.1:raml:zip:american-flights-
api.raml" outboundHeadersMapName="outboundHeaders"
httpStatusVarName="httpStatus" />
<flow name="american-flights-api-main">
<http:listener config-ref="HTTP_Listener_config" path="/api/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:router config-ref="american-flights-api-config" />
<error-handler>
<on-error-propagate type="APIKIT:BAD_REQUEST">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Bad request"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">400</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:METHOD_NOT_ALLOWED">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Method not allowed"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">405</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_ACCEPTABLE">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not acceptable"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">406</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:UNSUPPORTED_MEDIA_TYPE">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Unsupported media type"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">415</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_IMPLEMENTED">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not Implemented"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">501</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="american-flights-api-console">
<http:listener config-ref="HTTP_Listener_config" path="/console/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:console config-ref="american-flights-api-config" />
<error-handler>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-
variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="put:\flights\(ID):application\json:american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable
variableName="ID">attributes.uriParams.'ID'</ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
message: "Flight updated (but not really)"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="delete:\flights\(ID):american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable
variableName="ID">attributes.uriParams.'ID'</ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
message: "Flight deleted (but not really)"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="get:\flights:american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
[
{
ID: 1,
code: "ER38sd",
price: 400,
departureDate: "2017/07/26",
origin: "CLE",
destination: "SFO",
emptySeats: 0,
plane: {
"type": "Boeing 737",
totalSeats: 150
}
},
{
ID: 2,
code: "ER45if",
price: 540.99,
departureDate: "2017/07/27",
origin: "SFO",
destination: "ORD",
emptySeats: 54,
plane: {
"type": "Boeing 777",
totalSeats: 300
}
}
]]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>

<flow name="get:\flights\(ID):american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable
variableName="ID">attributes.uriParams.'ID'</ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
ID: 1,
code: "ER38sd",
price: 400,
departureDate: "2017/07/26",
origin: "CLE",
destination: "SFO",
emptySeats: 0,
plane: {
"type": "Boeing 737",
totalSeats: 150
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>

<flow name="post:\flights:application\json:american-flights-api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
message: "Flight added {but not really}"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>

</mule>
********************************************************************

Transformation XML with output variable


<ee:transform doc:name="Transform Message" doc:id="409552bf-0e35-421a-
912a-95c4a4cb96d0" >
<ee:message >
<ee:set-payload >
<![CDATA[%dw 2.0 output application/java ---
payload]]>
</ee:set-payload>
</ee:message>
<ee:variables >
<ee:set-variable variableName="DWoutput" >
<![CDATA[%dw 2.0 output application/json --- {}]]>
</ee:set-variable>
</ee:variables>
</ee:transform>
after setting dataweave code to external file
<ee:transform doc:name="Transform Message" doc:id="409552bf-0e35-421a-
912a-95c4a4cb96d0" >
<ee:message >
<ee:set-payload resource="json_flight_playground.dwl" />
</ee:message>
<ee:variables >
<ee:set-variable variableName="DWoutput" >
<![CDATA[%dw 2.0 output application/json --- { }]]>
</ee:set-variable>
</ee:variables>
</ee:transform>

*********************************************
Mule configuration file XML with no object it it
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

</mule>

*********************************************
Global Configuration Properties XML
<configuration-properties doc:name="Configuration properties"
doc:id="808cb43d-ea15-4bca-9977-b66d6909cafe" file="config.yaml" />

*********************************************
HTTP Listner configuration connection-
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener
config" doc:id="888e991b-a5ac-4d62-858e-79abcff9fb7a" >
<http:listener-connection host="0.0.0.0" port="${http.port}" />
</http:listener-config>
*********************************************
Adding Mule meta data type
After you add first metadata type application-type.xml file will be created.

<?xml version='1.0' encoding='UTF-8'?>


<types:mule xmlns:types="http://www.mulesoft.org/schema/mule/types">
<types:catalog>
<types:type name="flights_json" format="json">
<types:shape format="weave" example="examples/flights-example.json"><!
[CDATA[%dw 2.0

<!-- Added flights_json.json-->

type flights_json = Array<{| airline: String, flightCode: String,


fromAirportCode: String, toAirportCode: String, departureDate: String,
emptySeats: Number {"typeId": "int"}, totalSeats: Number {"typeId": "int"},
price: Number {"typeId": "int"}, planeType: String|}> {"example":
"[{\\\"airline\\\":\\\"United\\\",\\\"flightCode\\\":\\\"ER38sd\\\",\\\"fromAirport
Code\\\":\\\"LAX\\\",\\\"toAirportCode\\\":\\\"SFO\\\",\\\"departureDate\\\":\\\"Ma
y 21,
2016\\\",\\\"emptySeats\\\":0,\\\"totalSeats\\\":200,\\\"price\\\":199,\\\"planeTyp
e\\\":\\\"Boeing 737\\\"},
{\\\"airline\\\":\\\"Delta\\\",\\\"flightCode\\\":\\\"ER0945\\\",\\\"fromAirportCod
e\\\":\\\"PDX\\\",\\\"toAirportCode\\\":\\\"CLE\\\",\\\"departureDate\\\":\\\"June
1,
2016\\\",\\\"emptySeats\\\":24,\\\"totalSeats\\\":350,\\\"price\\\":450,\\\"planeTy
pe\\\":\\\"Boeing 747\\\"}]"}

]]></types:shape>
</types:type>

<!-- Added flight_json.json-->

<types:type name="flight_json" format="json">


<types:shape format="weave" example="examples/flight-example-1.json"><!
[CDATA[%dw 2.0

type flight_json = {|
airline: String,
flightCode: String,
fromAirportCode: String,
toAirportCode: String,
departureDate: String,
emptySeats: Number {"typeId": "int"},
totalSeats: Number {"typeId": "int"},
price: Number {"typeId": "int"},
planeType: String
|} {"example":
"{\\\"airline\\\":\\\"United\\\",\\\"flightCode\\\":\\\"ER38sd\\\",\\\"fromAirportC
ode\\\":\\\"LAX\\\",\\\"toAirportCode\\\":\\\"SFO\\\",\\\"departureDate\\\":\\\"May
21,
2016\\\",\\\"emptySeats\\\":0,\\\"totalSeats\\\":200,\\\"price\\\":199,\\\"planeTyp
e\\\":\\\"Boeing 737\\\"}"}

]]></types:shape>
</types:type>

<!-- Added flight_json.xml-->

<types:type name="flights_xml" format="xml">


<types:example format="xml"
element="{http://soap.training.mulesoft.com/}listAllFlightsResponse"
location="examples/flights-example.xml"/>
</types:type>

<!-- Added flight.java-->


<types:type name="Flight_pojo" format="java">
<types:shape format="java" element="com.mulesoft.training.Flight"/>
</types:type>

</types:catalog>
</types:mule>

*********************************************
validation is true xml
<validation:is-true doc:name="Is true" doc:id="c57d5d35-daef-4bc6-b9c2-
a78e2334328a" expression="#[['SFO','LAX','CLE','PDX','PDF'] contains vars.code]"
message="#['Invalid destination ' ++ (vars.code default ' ')]"/>

*********************************************
second transformation with the same component variable name to DWoutput
<ee:transform doc:name="Transform Message" doc:id="409552bf-0e35-421a-
912a-95c4a4cb96d0" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0 output application/java
---
payload
]]></ee:set-payload>
</ee:message>
<ee:variables >
<ee:set-variable variableName="DWoutput" ><![CDATA[%dw 2.0
output application/java
---
payload]]>
</ee:set-variable>
</ee:variables>
</ee:transform>
*****DataWeave script to an external file DataWeave json_flight_playground*****
a new file json_flight_playground.dwl will be created under src/main/resources.

<ee:transform doc:name="Transform Message" doc:id="409552bf-0e35-421a-912a-


95c4a4cb96d0" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0 output application/java
---
payload
]]></ee:set-payload>
<ee:set-attributes resource="json_flight_playground.dwl" />
</ee:message>
<ee:variables >
<ee:set-variable variableName="DWoutput" ><![CDATA[%dw 2.0
output application/java
---
payload]]>
</ee:set-variable>
</ee:variables>
</ee:transform>
*********************************************

when you post josn message, and if tranform message has output application/java
then payload will have linkedhashmap

*********************************************

*********************************************

*********************************************
*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************
*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************
*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************
*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************
*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

*********************************************

You might also like