View Javadoc

1   package uk.ac.ebi.intenz.tools.export;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.io.OutputStreamWriter;
6   import java.util.Collection;
7   import java.util.Map;
8   import java.util.Properties;
9   
10  import org.apache.log4j.Logger;
11  import org.apache.velocity.Template;
12  import org.apache.velocity.VelocityContext;
13  import org.apache.velocity.app.Velocity;
14  import org.apache.velocity.exception.ParseErrorException;
15  import org.apache.velocity.exception.ResourceNotFoundException;
16  
17  import uk.ac.ebi.intenz.domain.constants.View;
18  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
19  import uk.ac.ebi.xchars.SpecialCharacters;
20  import uk.ac.ebi.xchars.domain.EncodingType;
21  
22  public class KeggExporter {
23  
24  	private static final Logger LOGGER = Logger.getLogger(KeggExporter.class);
25  
26  	public KeggExporter() throws Exception {
27  		Properties velocityProps = new Properties();
28  		velocityProps.load(KeggExporter.class.getClassLoader()
29  				.getResourceAsStream("velocity.properties"));
30          Velocity.init(velocityProps);
31  	}
32  	
33  	public void export(Collection<EnzymeEntry> enzymes, OutputStream os)
34  	throws IOException{
35          VelocityContext context = new VelocityContext();
36          context.put("nl", "\n");
37          context.put("enzymes", enzymes);
38          context.put("specialCharacters", SpecialCharacters.getInstance(null));
39          context.put("spEncoding", EncodingType.SWISSPROT_CODE);
40          context.put("intenzView", View.INTENZ);
41          
42          Template template = null;
43          try {
44  			template = Velocity.getTemplate("templates/keggEnzyme.vm");
45  		} catch (ResourceNotFoundException e) {
46  			LOGGER.error("Template not found", e);
47  		} catch (ParseErrorException e) {
48  			LOGGER.error("Error parsing the template", e);
49  		} catch (Exception e) {
50  			LOGGER.error("Error getting velocity template", e);
51  		}
52  		if (template == null) return;
53          OutputStreamWriter osw = null;
54          try {
55              osw = new OutputStreamWriter(os);
56              template.merge(context, osw);
57              osw.flush();
58          } catch (IOException ex) {
59              LOGGER.error("Unable to write keggEnzyme", ex);
60          } finally {
61              if (osw != null) osw.close();
62          }
63  	}
64  }