View Javadoc

1   package uk.ac.cam.spectra.spectrasub;
2   
3   import java.io.FileInputStream;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.util.ArrayList;
7   import java.util.Collections;
8   import java.util.List;
9   
10  import org.apache.commons.digester.Digester;
11  import org.apache.commons.io.IOUtils;
12  import org.apache.log4j.Logger;
13  import org.xml.sax.SAXException;
14  
15  /**
16   * Describes a packaging process, what datafiles are needed, which are to be
17   * converted to CML, where the main InChI comes from, what metadata components
18   * are required and so on.
19   * 
20   * @author ojd20
21   * @todo Turn XML parsing initialization into a static factory method. This will
22   *       make allow easier programmatic configuration in non-webapp contexts.
23   */
24  public class Profile {
25  
26      private static final Logger LOG = Logger.getLogger(Profile.class);
27  
28      private List<DataFile> dataFiles = new ArrayList<DataFile>();
29  
30      private String file = null;
31      
32      private String metadataPage = null;
33  
34      public String getMetadataPage() {
35          return metadataPage;
36      }
37  
38      public void setMetadataPage(String metadataPage) {
39          this.metadataPage = metadataPage;
40      }
41  
42      public String getFile() {
43          return file;
44      }
45  
46      public void setFile(String file) {
47          this.file = file;
48      }
49  
50      public void init() throws IOException {
51          Digester digester = new Digester();
52          digester.setValidating(false);
53          digester.addObjectCreate("profile", Profile.class);
54          digester.addObjectCreate("profile/dataFiles/dataFile", DataFile.class);
55          digester.addSetProperties("profile/dataFiles/dataFile");
56          digester.addSetNext("profile/dataFiles/dataFile", "addDataFile",
57                  "uk.ac.cam.spectra.spectrasub.DataFile");
58          digester.addCallMethod("profile", "setMetadataPage", 1);
59          digester.addCallParam("profile/metadata", 0, "page");
60  
61          InputStream in = null;
62          try {
63              in = new FileInputStream(getFile());
64              Profile p = (Profile) digester.parse(in);
65              copyValues(p);
66              LOG.info("Parsed Profile");
67          } catch (IOException e) {
68              LOG.error("Problem parsing profile.xml: " + e.getMessage(), e);
69              throw e;
70          } catch (SAXException e) {
71              LOG.error("Problem parsing profile.xml: " + e.getMessage(), e);
72          } finally {
73              IOUtils.closeQuietly(in);
74          }
75      }
76  
77      public void addDataFile(DataFile df) {
78          dataFiles.add(df);
79      }
80  
81      public List<DataFile> getDataFiles() {
82          return Collections.unmodifiableList(dataFiles);
83      }
84  
85      /**
86       * Copy the properties of p onto this object
87       * 
88       * @param p
89       */
90      public void copyValues(Profile p) {
91          dataFiles = p.dataFiles;
92          metadataPage = p.metadataPage;
93      }
94  
95      public DataPackage createPackage() {
96          LOG.info("Creating package with "+ getDataFiles());
97          DataPackage pp = new DataPackage();
98          pp.setRemainingDataFiles(getDataFiles());
99          return pp;
100     }
101 
102     public String toString() {
103         StringBuilder sb = new StringBuilder("Profile. DataFiles: <");
104         for (DataFile df : getDataFiles()) {
105             sb.append(df).append("|");
106         }
107         return sb.append(">").toString();
108     }
109 }