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.Collections; 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 import uk.ac.ebi.intenz.domain.constants.View; 17 import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry; 18 import uk.ac.ebi.xchars.SpecialCharacters; 19 import uk.ac.ebi.xchars.domain.EncodingType; 20 21 public class KeggExporter implements IntenzExporter { 22 23 private static final Logger LOGGER = Logger.getLogger(KeggExporter.class); 24 25 public KeggExporter() throws Exception { 26 Properties velocityProps = new Properties(); 27 velocityProps.load(KeggExporter.class.getClassLoader() 28 .getResourceAsStream("velocity.properties")); 29 Velocity.init(velocityProps); 30 } 31 32 public void export(Collection<EnzymeEntry> enzymes, OutputStream os) 33 throws IOException{ 34 VelocityContext context = new VelocityContext(); 35 context.put("nl", "\n"); 36 context.put("enzymes", enzymes); 37 context.put("specialCharacters", SpecialCharacters.getInstance(null)); 38 context.put("spEncoding", EncodingType.SWISSPROT_CODE); 39 context.put("intenzView", View.INTENZ); 40 41 Template template = null; 42 try { 43 template = Velocity.getTemplate("templates/keggEnzyme.vm"); 44 } catch (ResourceNotFoundException e) { 45 LOGGER.error("Template not found", e); 46 } catch (ParseErrorException e) { 47 LOGGER.error("Error parsing the template", e); 48 } catch (Exception e) { 49 LOGGER.error("Error getting velocity template", e); 50 } 51 if (template == null) return; 52 OutputStreamWriter osw = null; 53 try { 54 osw = new OutputStreamWriter(os); 55 template.merge(context, osw); 56 osw.flush(); 57 } catch (IOException ex) { 58 LOGGER.error("Unable to write keggEnzyme", ex); 59 } finally { 60 if (osw != null) osw.close(); 61 } 62 } 63 64 public void export(EnzymeEntry enzyme, OutputStream os) throws IOException { 65 export(Collections.singleton(enzyme), os); 66 } 67 }