Skip to content

Instantly share code, notes, and snippets.

@cjmamo
Last active August 29, 2015 14:05
Show Gist options
  • Save cjmamo/e80c8add45b776e0a0a5 to your computer and use it in GitHub Desktop.
Save cjmamo/e80c8add45b776e0a0a5 to your computer and use it in GitHub Desktop.
<#ftl ns_prefixes={"ossandme":"http://ossandme.org"}>
${record['ossandme:First_Name']}~${record['ossandme:Last_Name']}~${record['ossandme:ShippingStreet']}~${record['ossandme:ShippingCity']}~${record['ossandme:ShippingState']}~${record['ossandme:ShippingPostalCode']}~${record['ossandme:Member_Tier__c']}
<queryResult xmlns="http://ossandme.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<type>Account</type>
<First_Name>Carlos</First_Name>
<Last_Name>Di Sarli</Last_Name>
<ShippingStreet>San Telmo</ShippingStreet>
<ShippingCity>Buenos Aires</ShippingCity>
<ShippingState>N/A</ShippingState>
<ShippingPostalCode></ShippingPostalCode>
<Member_Tier__c>Gold</Member_Tier__c>
</record>
<record>
<type>Account</type>
<First_Name>Osvaldo</First_Name>
<Last_Name>Fresedo</Last_Name>
<ShippingStreet></ShippingStreet>
<ShippingCity>Rome</ShippingCity>
<ShippingState>N/A</ShippingState>
<ShippingPostalCode></ShippingPostalCode>
<Member_Tier__c>Silver</Member_Tier__c>
</record>
<record>
<type>Account</type>
<First_Name>Roberto</First_Name>
<Last_Name>Canelo</Last_Name>
<ShippingStreet>Venezuela</ShippingStreet>
<ShippingCity>Buenos Aires</ShippingCity>
<ShippingState>N/A</ShippingState>
<ShippingPostalCode></ShippingPostalCode>
<Member_Tier__c>Silver</Member_Tier__c>
</record>
<record>
<type>Account</type>
<First_Name>Juan</First_Name>
<Last_Name>D'Arienzo</Last_Name>
<ShippingStreet></ShippingStreet>
<ShippingCity></ShippingCity>
<ShippingState></ShippingState>
<ShippingPostalCode></ShippingPostalCode>
<Member_Tier__c>Gold</Member_Tier__c>
</record>
...
</queryResult>
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
000000Card Extract 20140921
Carlos~San Telmo~Buenos Aires~N/A~~Gold
Osvaldo~Fresedo~~Rome~N/A~~Silver
Roberto~Canelo~Venezuela~Buenos Aires~N/A~~Silver
Juan~D'Arienzo~~~~~Gold
...
999999002213
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd">
<csv:reader separator="~" fields="recordClass,code,itemId,itemDesc">
<csv:singleBinding beanId="product" class="org.ossandme.Product" />
</csv:reader>
</smooks-resource-list>
package org.ossandme;
import org.milyn.Smooks;
import javax.xml.transform.stream.StreamSource;
public class CsvToDbTransformer {
public void transform() throws Exception {
// create a Smooks instance for loading the CSV records to the database
Smooks smooks = new Smooks(CsvToDbTransformer.class.getResourceAsStream("/transactions-to-db.xml"));
// load the records
smooks.filterSource(new StreamSource(CsvToDbTransformer.class.getResourceAsStream("/transactions.csv")));
smooks.close();
}
}
package org.ossandme;
import org.milyn.Smooks;
import org.milyn.container.ExecutionContext;
import org.milyn.javabean.lifecycle.BeanContextLifecycleEvent;
import org.milyn.javabean.lifecycle.BeanContextLifecycleObserver;
import org.milyn.javabean.lifecycle.BeanLifecycle;
import javax.xml.transform.stream.StreamSource;
public class CsvToPojosTransformer {
public void transform() throws Exception {
// create a Smooks instance for transforming CSV to Products
Smooks smooks = new Smooks(CsvToPojosTransformer.class.getResourceAsStream("/csv-to-pojos.xml"));
ExecutionContext executionContext = smooks.createExecutionContext();
// set an event listener on Smooks
executionContext.getBeanContext().addObserver(new BeanContextLifecycleObserver() {
@Override
public void onBeanLifecycleEvent(BeanContextLifecycleEvent event) {
// apply logic only when Smooks has made a 'org.ossandme.Product' and set its properties
if (event.getLifecycle().equals(BeanLifecycle.END_FRAGMENT) && event.getBeanId().toString().equals("product")) {
Product product = (Product) event.getBean();
System.out.println(product.getItemDesc());
// DO STUFF
// ...
}
}
});
// transform CSV to Products
smooks.filterSource(executionContext, new StreamSource(CsvToPojosTransformer.class.getResourceAsStream("/products.csv")));
smooks.close();
}
}
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<reader class="org.ossandme.MapIteratorSourceReader"/>
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<ftl:freemarker applyOnElement="record">
<ftl:template>annual-census.ftl</ftl:template>
</ftl:freemarker>
</smooks-resource-list>
package org.ossandme;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.XMLConstants;
import org.apache.commons.lang.StringUtils;
import org.milyn.cdr.SmooksConfigurationException;
import org.milyn.container.ExecutionContext;
import org.milyn.delivery.java.JavaXMLReader;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.helpers.AttributesImpl;
public class MapIteratorSourceReader implements JavaXMLReader {
// the stream writer
private ContentHandler contentHandler;
// holds the iterator of maps
private List<Object> sourceObjects;
@Override
public ContentHandler getContentHandler() {
return contentHandler;
}
@Override
public DTDHandler getDTDHandler() {
return null;
}
@Override
public EntityResolver getEntityResolver() {
return null;
}
@Override
public ErrorHandler getErrorHandler() {
return null;
}
@Override
public boolean getFeature(String arg0) throws SAXNotRecognizedException, SAXNotSupportedException {
return false;
}
@Override
public Object getProperty(String arg0) throws SAXNotRecognizedException, SAXNotSupportedException {
return null;
}
// called by Smooks to perform transformation
@Override
public void parse(InputSource inputSource) throws IOException, SAXException {
// retrieve Iterator instance from sourceObjects; not the InputSource parameter
Iterator<Map<String, String>> iterator = (Iterator<Map<String, String>>) sourceObjects.get(0);
// write the start of the document
contentHandler.startDocument();
contentHandler.startElement(XMLConstants.NULL_NS_URI, "records", StringUtils.EMPTY, new AttributesImpl());
// iterate through the maps
while (iterator.hasNext()) {
// write a 'record' start tag to the stream for each map
contentHandler.startElement(XMLConstants.NULL_NS_URI, "record", StringUtils.EMPTY, new AttributesImpl());
// get a map from the iterator
Map<String, String> record = iterator.next();
// iterate through the map entries
for (Map.Entry<String, String> map : record.entrySet()) {
// write a start tag that is named after the entry key
contentHandler.startElement(XMLConstants.NULL_NS_URI, map.getKey(), StringUtils.EMPTY, new AttributesImpl());
if (map.getValue() != null) {
// set the element's text content to the entry value
contentHandler.characters(map.getValue().toCharArray(), 0, map.getValue().length());
}
// close the element that is mapped to an entry
contentHandler.endElement(XMLConstants.NULL_NS_URI, map.getKey(), StringUtils.EMPTY);
}
// close the 'record' element
contentHandler.endElement(XMLConstants.NULL_NS_URI, "record", StringUtils.EMPTY);
}
// close the document
contentHandler.endElement(XMLConstants.NULL_NS_URI, "records", StringUtils.EMPTY);
contentHandler.endDocument();
}
@Override
public void parse(String arg0) throws IOException, SAXException {
}
@Override
public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler;
}
@Override
public void setDTDHandler(DTDHandler arg0) {
}
@Override
public void setEntityResolver(EntityResolver arg0) {
}
@Override
public void setErrorHandler(ErrorHandler arg0) {
}
@Override
public void setFeature(String arg0, boolean arg1) throws SAXNotRecognizedException, SAXNotSupportedException {
}
@Override
public void setProperty(String arg0, Object arg1) throws SAXNotRecognizedException, SAXNotSupportedException {
}
@Override
public void setExecutionContext(ExecutionContext executionContext) {
}
@Override
public void setSourceObjects(List<Object> sourceObjects) throws SmooksConfigurationException {
this.sourceObjects = sourceObjects;
}
}
package org.ossandme;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Iterator;
import java.util.Map;
import javax.xml.transform.stream.StreamResult;
import org.milyn.Smooks;
import org.milyn.payload.JavaSource;
public class MapIteratorToCsvTransformer {
public InputStream transform(final Iterator<Map<String, String>> mapIterator) throws Exception {
PipedInputStream pipedInputStream = new PipedInputStream();
final Smooks smooks = new Smooks(getClass().getResourceAsStream("/map-iterator-to-csv.xml"));
final PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
new Thread(new Runnable() {
@Override
public void run() {
smooks.filterSource(new JavaSource(mapIterator), new StreamResult(pipedOutputStream));
smooks.close();
}
});
return pipedInputStream;
}
}
<csv-set>
<csv-record number="1">
<recordClass>3</recordClass>
<code>098</code>
<itemId>032</itemId>
<itemDesc>Shampoo</itemDesc>
</csv-record>
<csv-record number="2">
<recordClass>3</recordClass>
<code>075</code>
<itemId>392</itemId>
<itemDesc>Laptop</itemDesc>
</csv-record>
<csv-record number="3">
<recordClass>1</recordClass>
<code>032</code>
<itemId>478</itemId>
<itemDesc>Spade</itemDesc>
</csv-record>
<csv-record number="4">
<recordClass>3</recordClass>
<code>321</code>
<itemId>021</itemId>
<itemDesc>Blades</itemDesc>
</csv-record>
<csv-record number="5">
<recordClass>2</recordClass>
<code>045</code>
<itemId>432</itemId>
<itemDesc>Mobile</itemDesc>
</csv-record>
...
</csv-set>
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
0DARIENZO 20140408
3~098~032~Shampoo
3~075~392~Laptop
1~032~478~Spade
3~321~021~Blades
2~045~432~Mobile
...
...
...
9000000003
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.4.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd"
xmlns:db="http://www.milyn.org/xsd/smooks/db-routing-1.1.xsd"
xmlns:ds="http://www.milyn.org/xsd/smooks/datasource-1.3.xsd">
<csv:reader separator="~" fields="TH[seqNo,startDate,finishDate,status,type,code] | TB[seqNo,type,status,item,voucherNo,dept,amount] | TF[seqNo,expireDate,cardType]"/>
...
</smooks-resource-list>
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.4.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd"
xmlns:db="http://www.milyn.org/xsd/smooks/db-routing-1.1.xsd"
xmlns:ds="http://www.milyn.org/xsd/smooks/datasource-1.3.xsd">
<csv:reader separator="~" fields="TH[seqNo,startDate,finishDate,status,type,code] | TB[seqNo,type,status,item,voucherNo,dept,amount] | TF[seqNo,expireDate,cardType]"/>
<jb:bean beanId="transactionHeader" class="java.util.HashMap" createOnElement="TH">
<jb:value property="seqNo" data="TH/seqNo" />
<jb:value property="startDate" data="TH/startDate" />
<jb:value property="finishDate" data="TH/finishDate" />
<jb:value property="status" data="TH/status" />
<jb:value property="type" data="TH/type" />
<jb:value property="code" data="TH/code" />
</jb:bean>
<jb:bean beanId="transactionBody" class="java.util.HashMap" createOnElement="TB">
<jb:value property="seqNo" data="TB/seqNo" />
<jb:value property="type" data="TB/type" />
<jb:value property="status" data="TB/status" />
<jb:value property="item" data="TB/item" />
<jb:value property="voucherNo" data="TB/voucherNo" />
<jb:value property="dept" data="TB/dept" />
<jb:value property="amount" data="TB/amount" />
</jb:bean>
<jb:bean beanId="transactionFooter" class="java.util.HashMap" createOnElement="TF">
<jb:value property="seqNo" data="TF/seqNo" />
<jb:value property="expireDate" data="TF/expireDate" />
<jb:value property="cardType" data="TF/cardType" />
</jb:bean>
...
</smooks-resource-list>
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.4.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd"
xmlns:db="http://www.milyn.org/xsd/smooks/db-routing-1.1.xsd"
xmlns:ds="http://www.milyn.org/xsd/smooks/datasource-1.3.xsd">
<csv:reader separator="~" fields="TH[seqNo,startDate,finishDate,status,type,code] | TB[seqNo,type,status,item,voucherNo,dept,amount] | TF[seqNo,expireDate,cardType]"/>
<jb:bean beanId="transactionHeader" class="java.util.HashMap" createOnElement="TH">
<jb:value property="seqNo" data="TH/seqNo" />
<jb:value property="startDate" data="TH/startDate" />
<jb:value property="finishDate" data="TH/finishDate" />
<jb:value property="status" data="TH/status" />
<jb:value property="type" data="TH/type" />
<jb:value property="code" data="TH/code" />
</jb:bean>
<jb:bean beanId="transactionBody" class="java.util.HashMap" createOnElement="TB">
<jb:value property="seqNo" data="TB/seqNo" />
<jb:value property="type" data="TB/type" />
<jb:value property="status" data="TB/status" />
<jb:value property="item" data="TB/item" />
<jb:value property="voucherNo" data="TB/voucherNo" />
<jb:value property="dept" data="TB/dept" />
<jb:value property="amount" data="TB/amount" />
</jb:bean>
<jb:bean beanId="transactionFooter" class="java.util.HashMap" createOnElement="TF">
<jb:value property="seqNo" data="TF/seqNo" />
<jb:value property="expireDate" data="TF/expireDate" />
<jb:value property="cardType" data="TF/cardType" />
</jb:bean>
<db:executor executeOnElement="TH" datasource="StagingArea">
<db:statement>INSERT INTO TransactionHeaders (seqNo, startDate, finishDate, status, type, code) VALUES (${transactionHeader.seqNo}, ${transactionHeader.startDate}, ${transactionHeader.finishDate}, ${transactionHeader.status}, ${transactionHeader.type}, ${transactionHeader.code})</db:statement>
</db:executor>
<db:executor executeOnElement="TB" datasource="StagingArea">
<db:statement>INSERT INTO TransactionBody (seqNo, type, status, item, voucherNo, dept, amount) VALUES (${transactionBody.seqNo}, ${transactionBody.type}, ${transactionBody.status}, ${transactionBody.item}, ${transactionBody.voucherNo}, ${transactionBody.dept}, ${transactionBody.amount})</db:statement>
</db:executor>
<db:executor executeOnElement="TF" datasource="StagingArea">
<db:statement>INSERT INTO TransactionFooters (seqNo, expireDate, cardType) VALUES (${transactionFooter.seqNo}, ${transactionFooter.expireDate}, ${transactionFooter.cardType})</db:statement>
</db:executor>
...
</smooks-resource-list>
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.4.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd"
xmlns:db="http://www.milyn.org/xsd/smooks/db-routing-1.1.xsd"
xmlns:ds="http://www.milyn.org/xsd/smooks/datasource-1.3.xsd">
<csv:reader separator="~" fields="TH[seqNo,startDate,finishDate,status,type,code] | TB[seqNo,type,status,item,voucherNo,dept,amount] | TF[seqNo,expireDate,cardType]"/>
<jb:bean beanId="transactionHeader" class="java.util.HashMap" createOnElement="TH">
<jb:value property="seqNo" data="TH/seqNo" />
<jb:value property="startDate" data="TH/startDate" />
<jb:value property="finishDate" data="TH/finishDate" />
<jb:value property="status" data="TH/status" />
<jb:value property="type" data="TH/type" />
<jb:value property="code" data="TH/code" />
</jb:bean>
<jb:bean beanId="transactionBody" class="java.util.HashMap" createOnElement="TB">
<jb:value property="seqNo" data="TB/seqNo" />
<jb:value property="type" data="TB/type" />
<jb:value property="status" data="TB/status" />
<jb:value property="item" data="TB/item" />
<jb:value property="voucherNo" data="TB/voucherNo" />
<jb:value property="dept" data="TB/dept" />
<jb:value property="amount" data="TB/amount" />
</jb:bean>
<jb:bean beanId="transactionFooter" class="java.util.HashMap" createOnElement="TF">
<jb:value property="seqNo" data="TF/seqNo" />
<jb:value property="expireDate" data="TF/expireDate" />
<jb:value property="cardType" data="TF/cardType" />
</jb:bean>
<db:executor executeOnElement="TH" datasource="StagingArea">
<db:statement>INSERT INTO TransactionHeaders (seqNo, startDate, finishDate, status, type, code) VALUES (${transactionHeader.seqNo}, ${transactionHeader.startDate}, ${transactionHeader.finishDate}, ${transactionHeader.status}, ${transactionHeader.type}, ${transactionHeader.code})</db:statement>
</db:executor>
<db:executor executeOnElement="TB" datasource="StagingArea">
<db:statement>INSERT INTO TransactionBody (seqNo, type, status, item, voucherNo, dept, amount) VALUES (${transactionBody.seqNo}, ${transactionBody.type}, ${transactionBody.status}, ${transactionBody.item}, ${transactionBody.voucherNo}, ${transactionBody.dept}, ${transactionBody.amount})</db:statement>
</db:executor>
<db:executor executeOnElement="TF" datasource="StagingArea">
<db:statement>INSERT INTO TransactionFooters (seqNo, expireDate, cardType) VALUES (${transactionFooter.seqNo}, ${transactionFooter.expireDate}, ${transactionFooter.cardType})</db:statement>
</db:executor>
<ds:direct bindOnElement="$document" datasource="StagingArea"
driver="org.apache.derby.jdbc.EmbeddedDriver" url="jdbc:derby:memory:staging"
autoCommit="true" username="" password="" />
</smooks-resource-list>
<csv-set>
<UNMATCHED number="1">
<value>FH</value>
</UNMATCHED>
<TH number="2">
<seqNo>1</seqNo>
<startDate>2014-04-06</startDate>
<finishDate>2014-04-06 15:19:59</finishDate>
<status>APPROVED</status>
<type>SALE</type>
<code>109</code>
</TH>
<TB number="3">
<seqNo>1</seqNo>
<type>3</type>
<status>APPROVED</status>
<item>Shampoo</item>
<voucherNo>29012</voucherNo>
<dept>2</dept>
<amount>4.30</amount>
</TB>
<TB number="4">
<seqNo>1</seqNo>
<type>3</type>
<status>APPROVED</status>
<item>Soap</item>
<voucherNo>29012</voucherNo>
<dept>2</dept>
<amount>1.00</amount>
</TB>
<TB number="5">
<seqNo>1</seqNo>
<type>3</type>
<status>APPROVED</status>
<item>Gel</item>
<voucherNo>29012</voucherNo>
<dept>2</dept>
<amount>2.90</amount>
</TB>
<TB number="6">
<seqNo>1</seqNo>
<type>3</type>
<status>DECLINED</status>
<item>Soap</item>
<voucherNo>29012</voucherNo>
<dept>2</dept>
<amount>1.00</amount>
</TB>
<TF number="7">
<seqNo>1</seqNo>
<expireDate>2014-12-01 00:00:00</expireDate>
<cardType>VISA</cardType>
</TF>
<TF number="8">
<seqNo>1</seqNo>
<expireDate>2014-12-01 00:00:00</expireDate>
<cardType>VISA</cardType>
</TF>
...
<UNMATCHED number="9">
<value>FT</value>
</UNMATCHED>
</csv-set>
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
FH~20140407224630~1235~Calo Data
TH~1~2014-04-06~2014-04-06 15:19:59~APPROVED~SALE~109
TB~1~3~APPROVED~Shampoo~29012~2~4.30
TB~1~3~APPROVED~Soap~29012~2~1.00
TB~1~3~APPROVED~Gel~29012~2~2.90
TB~1~3~DECLINED~Soap~29012~2~1.00
TF~1~2014-12-01 00:00:00~VISA
TF~1~2014-12-01 00:00:00~VISA
...
...
...
FT~265449~4412826.67~4410413.48~4248007.43
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd">
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
</smooks-resource-list>
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<ftl:freemarker applyOnElement="record">
<ftl:template>account.ftl</ftl:template>
</ftl:freemarker>
</smooks-resource-list>
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<ftl:freemarker applyOnElement="#document">
<ftl:template><!--000000Card Extract ${now?string('yyyyMMdd')}
<?TEMPLATE-SPLIT-PI?>--></ftl:template>
</ftl:freemarker>
<ftl:freemarker applyOnElement="record">
<ftl:template>account.ftl</ftl:template>
</ftl:freemarker>
</smooks-resource-list>
<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
xmlns:calc="http://www.milyn.org/xsd/smooks/calc-1.1.xsd">
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<calc:counter countOnElement="#document" beanId="totalRecordCount" start="0"/>
<calc:counter countOnElement="record" beanId="totalRecordCount" start="1"/>
<ftl:freemarker applyOnElement="#document">
<ftl:template><!--000000Card Extract ${now?string('yyyyMMdd')}
<?TEMPLATE-SPLIT-PI?>999999${totalRecordCount?string?left_pad(6, '0')}--></ftl:template>
</ftl:freemarker>
<ftl:freemarker applyOnElement="record">
<ftl:template>account.ftl</ftl:template>
</ftl:freemarker>
</smooks-resource-list>
package org.ossandme;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.milyn.Smooks;
public class XmlToCsvTransformer {
public InputStream transform(final InputStream inputStream) throws Exception {
// create a Smooks instance for transforming XML to CSV
final Smooks smooks = new Smooks(getClass().getResourceAsStream("/xml-to-csv.xml"));
// create an InputStream to be read by the FTP client library
PipedInputStream pipedInputStream = new PipedInputStream();
// create an OutputStream for Smooks to write the CSV to
final PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
// smooks.filterSource(...) blocks so we carry out the transformation on a new thread
new Thread(new Runnable() {
@Override
public void run() {
// transform XML read from the InputStream to CSV
smooks.filterSource(new StreamSource(inputStream), new StreamResult(pipedOutputStream));
smooks.close();
}
});
// return the PipedInputStream to be read by the FTP client library
return pipedInputStream;
}
}
package org.ossandme;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Date;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.milyn.Smooks;
import org.milyn.container.ExecutionContext;
public class XmlToCsvTransformer {
public InputStream transform(final InputStream inputStream) throws Exception {
// create a Smooks instance for transforming XML to CSV
final Smooks smooks = new Smooks(getClass().getResourceAsStream("/xml-to-csv.xml"));
// create an InputStream to be read by the FTP client library
PipedInputStream pipedInputStream = new PipedInputStream();
// create an OutputStream for Smooks to write the CSV to
final PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
final ExecutionContext executionContext = smooks.createExecutionContext();
// bind the current date to Smook's bean context
executionContext.getBeanContext().addBean("now", new Date());
// smooks.filterSource(...) blocks so we carry out the transformation on a new thread
new Thread(new Runnable() {
@Override
public void run() {
// transform XML read from the InputStream to CSV
smooks.filterSource(executionContext, new StreamSource(inputStream), new StreamResult(pipedOutputStream));
smooks.close();
}
});
// return the PipedInputStream to be read by the FTP client library
return pipedInputStream;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment