View Javadoc
1   /*
2    * #%L
3    * EUGene :: EUGene
4    * %%
5    * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric
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.digester3.Digester;
26  import org.codehaus.plexus.component.annotations.Component;
27  import org.nuiton.eugene.ModelHelper;
28  import org.nuiton.eugene.ModelReader;
29  import org.nuiton.eugene.Template;
30  import org.nuiton.eugene.models.object.ObjectModel;
31  import org.nuiton.eugene.models.object.xml.DigesterObjectModelRuleSet;
32  import org.xml.sax.SAXException;
33  
34  import java.io.File;
35  import java.io.IOException;
36  
37  /**
38   * To read object xml model 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 = "xmlobjectmodel")
44  public class XmlObjectModelReader extends AbstractObjectModelReader {
45  
46      protected Digester digester;
47  
48      @Override
49      public String getInputType() {
50          return ModelHelper.ModelInputType.XML.getAlias();
51      }
52  
53      @Override
54      protected void beforeReadFile(File... files) {
55          super.beforeReadFile(files);
56          digester = new Digester();
57          digester.addRuleSet(new DigesterObjectModelRuleSet());
58      }
59  
60      @Override
61      protected void readFileToModel(File file, ObjectModel model) throws IOException {
62          try {
63              digester.push(model);
64              digester.parse(file);
65          } catch (SAXException e) {
66              throw new IOException("Unable to parse ObjectModel input file : " + file, e);
67          }
68      }
69  
70  }