View Javadoc

1   /*
2    * $Id: CreditProfileXmlToCreditProfile.java 10789 2008-02-12 20:04:43Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.example.loanbroker.transformers;
12  
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.example.loanbroker.messages.CreditProfile;
15  import org.mule.transformer.AbstractTransformer;
16  
17  import org.dom4j.Document;
18  import org.dom4j.DocumentException;
19  import org.dom4j.DocumentHelper;
20  
21  public class CreditProfileXmlToCreditProfile extends AbstractTransformer
22  {
23  
24      public CreditProfileXmlToCreditProfile()
25      {
26          registerSourceType(String.class);
27          registerSourceType(Document.class);
28          setReturnClass(CreditProfile.class);
29      }
30  
31      public Object doTransform(Object src, String encoding) throws TransformerException
32      {
33          Document doc = null;
34  
35          if (src instanceof Document)
36          {
37              doc = (Document)src;
38          }
39          else
40          {
41              try
42              {
43                  doc = DocumentHelper.parseText(src.toString());
44              }
45              catch (DocumentException e)
46              {
47                  throw new TransformerException(this, e);
48              }
49          }
50  
51          String history = doc.valueOf("/credit-profile/customer-history");
52          String score = doc.valueOf("/credit-profile/credit-score");
53          CreditProfile cp = new CreditProfile();
54          cp.setCreditHistory(Integer.valueOf(history).intValue());
55          cp.setCreditScore(Integer.valueOf(score).intValue());
56          return cp;
57      }
58  
59  }