1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46
47
48
49
50 public class XMLLoader extends AbstractLoader {
51
52 private IProject project;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
77
78
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
89
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
124
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 }