View Javadoc
1   package org.nuiton.eugene.models.object.reader.yaml;
2   
3   /*
4    * #%L
5    * EUGene :: EUGene
6    * %%
7    * Copyright (C) 2004 - 2013 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import org.nuiton.eugene.models.object.ObjectModel;
26  import org.nuiton.eugene.models.object.xml.ObjectModelImpl;
27  import org.yaml.snakeyaml.Yaml;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * User: agiraudet
38   * Date: 10/06/13
39   * Time: 16:11
40   */
41  public class LoadYamlFile implements KeyWords {
42  
43      Yaml yaml;
44  
45      public LoadYamlFile() {
46          yaml = new Yaml();
47      }
48  
49      public void loadFile(File file, ObjectModel model) throws IOException {
50          //parse le fichier YAML
51          InputStream inputModel = new FileInputStream(file);
52          Object modelYAML = yaml.load(inputModel);
53          inputModel.close();
54  
55          ObjectModelImpl modelOM = (ObjectModelImpl) model;
56          YamlObject modelYAMLO = new YamlObject();
57  
58          //recherche la version
59          Object version = null;
60          String syntaxeVersion = "0";
61          String defaultVersion = "0";
62  
63          if (modelYAML instanceof List) {
64              version = YamlUtil.collectElementList((List) modelYAML, SYNTAXE);
65              syntaxeVersion = "1";
66              defaultVersion = "0";
67          } else if (modelYAML instanceof Map) {
68              version = YamlUtil.collectElementMap((Map) modelYAML, SYNTAXE);
69              syntaxeVersion = "2";
70              defaultVersion = "0";
71          }
72  
73          if (version != null) {
74              syntaxeVersion = YamlUtil.beforeChar(String.valueOf(version), '.');
75              defaultVersion = YamlUtil.afterChar(String.valueOf(version), '.');
76          }
77  
78          //charge le modele en fonction de la version
79          if (syntaxeVersion.equals("1")) {
80              SyntaxePureYaml.loadYamlObject(modelYAML, modelYAMLO);
81          } else if (syntaxeVersion.equals("2")) {
82              SyntaxeUserFriendly.loadYamlObject(modelYAML, modelYAMLO);
83          } else {
84              ;//impossible de charger le modele
85          }
86  
87          LoadObjectModel loader = new LoadObjectModel(modelYAMLO, modelOM, DefaultValues.getDefaultValues(defaultVersion));//sortir getDefaultValues ?
88          loader.loadModel();
89      }
90  }