View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.atom.transformers;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.api.transport.OutputHandler;
12  import org.mule.transformer.AbstractDiscoverableTransformer;
13  import org.mule.transformer.types.DataTypeFactory;
14  
15  import java.io.IOException;
16  import java.io.OutputStream;
17  
18  import org.apache.abdera.model.Base;
19  import org.apache.abdera.parser.stax.FOMWriterOptions;
20  
21  /**
22   * Converts Abdera model elements which extend {@link Base} to OutputHandlers.
23   */
24  public class BaseToOutputHandler extends AbstractDiscoverableTransformer
25  {
26      public BaseToOutputHandler()
27      {
28          this.registerSourceType(DataTypeFactory.create(Base.class));
29          setReturnDataType(DataTypeFactory.create(OutputHandler.class));
30      }
31  
32      @Override
33      public Object doTransform(Object src, String outputEncoding) throws TransformerException
34      {
35          try
36          {
37              final Base e = (Base) src;
38  
39              return new OutputHandler()
40              {
41                  public void write(MuleEvent event, OutputStream out) throws IOException
42                  {
43                      FOMWriterOptions opts = new FOMWriterOptions();
44                      opts.setCharset(event.getEncoding());
45                      e.writeTo(out, opts);
46                  }
47              };
48          }
49          catch (Exception e)
50          {
51              throw new TransformerException(this, e);
52          }
53      }
54  }