View Javadoc
1   /*
2    * #%L
3    * EUGene :: EUGene
4    * %%
5    * Copyright (C) 2004 - 2013 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.eugene.models.object.reader;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.codehaus.plexus.component.annotations.Component;
28  import org.nuiton.eugene.ModelHelper;
29  import org.nuiton.eugene.ModelReader;
30  import org.nuiton.eugene.models.object.ObjectModel;
31  import org.nuiton.eugene.models.object.reader.yaml.LoadYamlFile;
32  import org.yaml.snakeyaml.error.YAMLException;
33  
34  import java.io.File;
35  import java.io.IOException;
36  
37  /**
38   * To read object model from yaml files into an memory object model.
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 2.6.3
42   */
43  @Component(role = ModelReader.class, hint = "yamlobjectmodel")
44  public class YamlObjectModelReader extends AbstractObjectModelReader {
45  
46      private static final Log log = LogFactory.getLog(YamlObjectModelReader.class);
47  
48      protected LoadYamlFile loaderYAML;
49  
50      @Override
51      public String getInputType() {
52          return ModelHelper.ModelInputType.YAML.getAlias();
53      }
54  
55      @Override
56      protected void beforeReadFile(File... files) {
57          super.beforeReadFile(files);
58          loaderYAML = new LoadYamlFile();
59      }
60  
61      @Override
62      protected void readFileToModel(File file, ObjectModel model) throws IOException {
63          try {
64              loaderYAML.loadFile(file, model);
65          } catch (YAMLException e) {
66              throw new IOException("Unable to parse ObjectModel input file : " + file, e);
67          }
68      }
69  
70  }