View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: XMLLoader.java
13   * Created: 2008-07-12
14   * Author: apohllo
15   * $Id: XMLLoader.java 2232 2009-01-04 22:59:53Z apohllo $
16   */
17  
18  package pl.edu.agh.cast.model.mapper.internal;
19  
20  import java.io.File;
21  import java.io.FileReader;
22  import java.io.FilenameFilter;
23  import java.io.LineNumberReader;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.LinkedList;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.SortedMap;
30  
31  import javax.xml.parsers.SAXParser;
32  import javax.xml.parsers.SAXParserFactory;
33  
34  import org.eclipse.core.resources.IProject;
35  
36  import pl.edu.agh.cast.model.base.DataSet;
37  import pl.edu.agh.cast.model.base.IModel;
38  import pl.edu.agh.cast.model.base.Model;
39  import pl.edu.agh.cast.model.mapper.Mappable;
40  import pl.edu.agh.cast.model.mapper.Node;
41  import pl.edu.agh.cast.resources.BaseProjectUtil;
42  
43  /**
44   * Limited version of model loader - allows only loading of DataSets and Models. If the data set is to be loaded, the
45   * conditions map must contain the id of the data set. If the model is to be loaded, conditions are discarded.
46   *
47   * @author AGH CAST Team
48   *
49   */
50  public class XMLLoader extends AbstractLoader {
51  
52  	private IProject project;
53  
54  	/**
55  	 * Parameterized constructor.
56  	 *
57  	 * @param cache
58  	 *            The object cache.
59  	 * @param klass
60  	 *            The class of the objects to load.
61  	 * @param typeMap
62  	 *            The type map.
63  	 * @param conditions
64  	 *            The conditions the objects have to satisfy to be loaded.
65  	 * @param project
66  	 *            The project which the objects belongs to.
67  	 */
68  	public XMLLoader(Map<Node, Mappable> cache, Class klass, Map<String, Class> typeMap,
69  	        SortedMap<String, Object> conditions, IProject project) {
70  		super(cache, klass, typeMap, conditions);
71  		this.project = project;
72  	}
73  
74  	/**
75  	 *
76  	 * {@inheritDoc}
77  	 *
78  	 * @see pl.edu.agh.cast.model.mapper.Loader#find()
79  	 */
80  	public List<Mappable> find() {
81  		if (typeMap == null) {
82  			typeMap = new HashMap<String, Class>();
83  		}
84  		String typeName = Helper.mappedTypeName(klass);
85  		if (typeMap.get(typeName) == null) {
86  			typeMap.put(typeName, klass);
87  		}
88  		// String dataSetId = _conditions.get(key);
89  		// Reader file = new FileReader(.fileFor(_path, dataSetId));
90  
91  		try {
92  			if (klass.equals(Model.class)) {
93  				return Collections.singletonList((Mappable)loadModel());
94  			} else if (klass.equals(DataSet.class)) {
95  				IModel model = loadModel();
96  				List<Mappable> result = new LinkedList<Mappable>();
97  				result.addAll(model.getDataSets());
98  				return result;
99  			}
100 		} catch (Exception ex) {
101 			ex.printStackTrace();
102 		}
103 		return Collections.emptyList();
104 	}
105 
106 	private IModel loadModel() throws Exception {
107 		File dir = new File(XMLSaver.dirFor(project));
108 		File[] files = dir.listFiles(new FilenameFilter() {
109 			public boolean accept(File dir, String name) {
110 				if (name.indexOf(XMLSaver.EXTENSION) != -1) {
111 					return true;
112 				}
113 				return false;
114 			}
115 		});
116 		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
117 		final IModel model = new Model();
118 
119 		String modelName = null;
120 		if (project != null) {
121 			modelName = BaseProjectUtil.getProperty(project, BaseProjectUtil.PROJECT_ID_PROPERTY);
122 		} else {
123 			// this solution is used only in tests, when there is
124 			// no instance of Eclipse environment
125 			LineNumberReader projectProps = new LineNumberReader(new FileReader(XMLSaver.dirFor(project)
126 			        + XMLSaver.MODEL_PROPS));
127 			modelName = projectProps.readLine();
128 			projectProps.close();
129 		}
130 		model.setName(modelName);
131 		if (files != null) {
132 			for (File file : files) {
133 				parser.parse(file, new ModelParser(model, typeMap));
134 			}
135 		}
136 		return model;
137 	}
138 }