Package: Options

Options

nameinstructionbranchcomplexitylinemethod
Options()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
createGroup(String)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getGroup(String)
M: 25 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getGroup(String, boolean)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
init(IVdmProject)
M: 38 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
internalCreateGroup(String)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
load(IVdmProject)
M: 33 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
readXml(File)
M: 124 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%
save()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
writeXml()
M: 129 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 28 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.core
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU 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 Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.core.resources;
23:
24: import java.io.File;
25: import java.io.FileWriter;
26: import java.io.IOException;
27: import java.io.PrintWriter;
28: import java.io.Serializable;
29: import java.util.List;
30: import java.util.Map;
31: import java.util.Vector;
32: import java.util.Map.Entry;
33:
34: import javax.xml.parsers.DocumentBuilder;
35: import javax.xml.parsers.DocumentBuilderFactory;
36:
37: import org.eclipse.core.resources.IProject;
38: import org.eclipse.core.runtime.Assert;
39: import org.eclipse.core.runtime.IPath;
40: import org.overture.ide.core.VdmCore;
41: import org.overture.ide.internal.core.resources.OptionGroup;
42: import org.w3c.dom.Document;
43: import org.w3c.dom.Element;
44: import org.w3c.dom.Node;
45: import org.w3c.dom.NodeList;
46:
47: public class Options implements Serializable
48: {
49:         private static final String BOOLEAN_ATTRIBUTE = "booleanAttribute";
50:
51:         private static final String STRING_ATTRIBUTE = "stringAttribute";
52:
53:         /**
54:          *
55:          */
56:         private static final long serialVersionUID = -7776063704790917820L;
57:
58:         transient private IProject project;
59:         transient private File optionPathFile;
60:         transient private IVdmProject vdmProject;
61:         final List<OptionGroup> groups = new Vector<OptionGroup>();
62:
63:         public Options()
64:         {
65:
66:         }
67:
68:         public static Options load(IVdmProject vdmProject)
69:         {
70:                 IProject project = (IProject) vdmProject.getAdapter(IProject.class);
71:                 IPath base = project.getLocation();
72:                 base = base.append(".overture");
73:                 File optionPathFile = base.toFile();
74:
75:                 Options opt = null;
76:
77:•                if (!optionPathFile.exists())
78:                 {
79:                         opt = new Options();
80:                 } else
81:                 {
82:                         // FileInputStream fis = null;
83:                         // ObjectInputStream in = null;
84:                         // try
85:                         // {
86:                         // fis = new FileInputStream(optionPathFile);
87:                         // in = new ObjectInputStream(fis);
88:                         // opt = (Options) in.readObject();
89:                         // in.close();
90:                         // } catch (IOException ex)
91:                         // {
92:                         // ex.printStackTrace();
93:                         // } catch (ClassNotFoundException ex)
94:                         // {
95:                         // ex.printStackTrace();
96:                         // }
97:                         // XMLDecoder decoder = null;
98:                         // try
99:                         // {
100:                         // decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(optionPathFile)));
101:                         // opt = (Options) decoder.readObject();
102:                         //
103:                         // } catch (FileNotFoundException e)
104:                         // {
105:                         // // TODO Auto-generated catch block
106:                         // e.printStackTrace();
107:                         // } finally
108:                         // {
109:                         // decoder.close();
110:                         // }
111:                         opt = readXml(optionPathFile);
112:
113:                 }
114:
115:                 opt.init(vdmProject);
116:
117:                 return opt;
118:         }
119:
120:         private synchronized void init(IVdmProject project)
121:         {
122:                 this.vdmProject = project;
123:                 this.project = (IProject) this.vdmProject.getAdapter(IProject.class);
124:                 IPath base = this.project.getLocation();
125:                 base = base.append(".overture");
126:                 this.optionPathFile = base.toFile();
127:•                for (OptionGroup g : this.groups)
128:                 {
129:                         g.setParent(this);
130:                 }
131:         }
132:
133:         public synchronized void save()
134:         {
135:                 Assert.isNotNull(this.project, "Options not initlialized, project null");
136:                 // FileOutputStream fos = null;
137:                 // ObjectOutputStream out = null;
138:                 // try
139:                 // {
140:                 // fos = new FileOutputStream(this.optionPathFile);
141:                 // out = new ObjectOutputStream(fos);
142:                 // out.writeObject(this);
143:                 // out.close();
144:                 // } catch (IOException ex)
145:                 // {
146:                 // ex.printStackTrace();
147:                 // }
148:                 // XMLEncoder encoder = null;
149:                 // try
150:                 // {
151:                 // encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(optionPathFile)));
152:                 // encoder.setExceptionListener(new ExceptionListener() {
153:                 // public void exceptionThrown(Exception exception) {
154:                 // exception.printStackTrace();
155:                 // }
156:                 // });
157:                 //                        
158:                 // encoder.writeObject(new A());
159:                 // } catch (FileNotFoundException e)
160:                 // {
161:                 // // TODO Auto-generated catch block
162:                 // e.printStackTrace();
163:                 // } finally
164:                 // {
165:                 // encoder.close();
166:                 // }
167:                 writeXml();
168:         }
169:
170:         private void writeXml()
171:         {
172:                 StringBuffer sb = new StringBuffer();
173:
174:                 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
175:                 sb.append("<overture>\n");
176:
177:•                for (OptionGroup g : groups)
178:                 {
179:                         sb.append("\t<group key=\"" + g.id + "\">\n");
180:
181:                         Map<String, Object> attributes = g.getAttributes();
182:•                        for (Entry<String, Object> entry : attributes.entrySet())
183:                         {
184:                                 String type = null;
185:
186:                                 Object value = entry.getValue();
187:
188:•                                if (value instanceof String)
189:                                 {
190:                                         type = STRING_ATTRIBUTE;
191:•                                } else if (value instanceof Boolean)
192:                                 {
193:                                         type = BOOLEAN_ATTRIBUTE;
194:                                 }
195:
196:•                                if (type != null)
197:                                 {
198:                                         sb.append("\t\t<" + type + " key=\"" + entry.getKey()
199:                                                         + "\" value=\"" + entry.getValue().toString()
200:                                                         + "\"/>\n");
201:                                 }
202:                         }
203:
204:                         sb.append("\t</group>\n");
205:                 }
206:
207:                 sb.append("</overture>");
208:
209:                 PrintWriter out = null;
210:                 try
211:                 {
212:                         FileWriter outFile = new FileWriter(this.optionPathFile);
213:                         out = new PrintWriter(outFile);
214:                         out.println(sb.toString());
215:
216:                 } catch (IOException e)
217:                 {
218:                         VdmCore.log("Faild to save .modelpath file", e);
219:                 } finally
220:                 {
221:•                        if(out != null)
222:                         {
223:                                 out.close();
224:                         }
225:                 }
226:         }
227:
228:         private static Options readXml(File file)
229:         {
230:                 try
231:                 {
232:                         Options opt = new Options();
233:
234:                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
235:                         DocumentBuilder db = dbf.newDocumentBuilder();
236:                         Document doc = db.parse(file);
237:                         doc.getDocumentElement().normalize();
238:                         NodeList nodeLst = doc.getElementsByTagName("group");
239:•                        for (int s = 0; s < nodeLst.getLength(); s++)
240:                         {
241:                                 Node fstNode = nodeLst.item(s);
242:•                                if (fstNode.getNodeType() == Node.ELEMENT_NODE)
243:                                 {
244:                                         Node keyAttribute = fstNode.getAttributes().getNamedItem("key");
245:                                         String keyValue = keyAttribute.getNodeValue();
246:•                                        if (keyValue != null)
247:                                         {
248:                                                 OptionGroup g = opt.internalCreateGroup(keyValue);
249:
250:                                                 Element groupElement = (Element) fstNode;
251:                                                 {
252:                                                         NodeList nodeLstStringAtt = groupElement.getElementsByTagName(STRING_ATTRIBUTE);
253:•                                                        for (int j = 0; j < nodeLstStringAtt.getLength(); j++)
254:                                                         {
255:                                                                 Node fstNodeStrAtt = nodeLstStringAtt.item(j);
256:•                                                                if (fstNodeStrAtt.getNodeType() == Node.ELEMENT_NODE)
257:                                                                 {
258:                                                                         Element eStrAtt = (Element) fstNodeStrAtt;
259:
260:                                                                         g.setAttribute(eStrAtt.getAttribute("key"), eStrAtt.getAttribute("value"));
261:
262:                                                                 }
263:
264:                                                         }
265:                                                 }
266:                                                 {
267:                                                         NodeList nodeLstStringAtt = groupElement.getElementsByTagName(BOOLEAN_ATTRIBUTE);
268:•                                                        for (int j = 0; j < nodeLstStringAtt.getLength(); j++)
269:                                                         {
270:                                                                 Node fstNodeStrAtt = nodeLstStringAtt.item(j);
271:•                                                                if (fstNodeStrAtt.getNodeType() == Node.ELEMENT_NODE)
272:                                                                 {
273:                                                                         Element eStrAtt = (Element) fstNodeStrAtt;
274:
275:                                                                         g.setAttribute(eStrAtt.getAttribute("key"), Boolean.valueOf(eStrAtt.getAttribute("value")));
276:
277:                                                                 }
278:
279:                                                         }
280:                                                 }
281:
282:                                         }
283:                                         // if (keyValue.equals("src"))
284:                                         // {
285:                                         // Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
286:                                         // String pathValue = pathAttribute.getNodeValue();
287:                                         // srcPaths.add(this.project.getFolder(pathValue));
288:                                         // } else if (keyValue.equals("output"))
289:                                         // {
290:                                         // Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
291:                                         // String pathValue = pathAttribute.getNodeValue();
292:                                         // output = this.project.getFolder(pathValue);
293:                                         // }
294:                                 }
295:                         }
296:                         return opt;
297:                 } catch (Exception e)
298:                 {
299:                         VdmCore.log("Faild to parse .modelpath file", e);
300:                 }
301:                 return null;
302:         }
303:
304:         public synchronized IOptionGroup getGroup(String id)
305:         {
306:                 Assert.isNotNull(this.project, "Options not initlialized, project null");
307:
308:•                for (OptionGroup g : groups)
309:                 {
310:•                        if (g.id.equals(id))
311:                         {
312:                                 return g;
313:                         }
314:                 }
315:
316:                 return null;
317:         }
318:
319:         public synchronized IOptionGroup getGroup(String id,
320:                         boolean createIfNotFound)
321:         {
322:•                if (createIfNotFound)
323:                 {
324:                         return createGroup(id);
325:                 } else
326:                 {
327:                         return getGroup(id);
328:                 }
329:         }
330:
331:         public IOptionGroup createGroup(String id)
332:         {
333:                 Assert.isNotNull(this.project, "Options not initlialized, project null");
334:                 IOptionGroup group = getGroup(id);
335:•                if (group != null)
336:                 {
337:                         return group;
338:                 } else
339:                 {
340:                         return internalCreateGroup(id);
341:                 }
342:         }
343:
344:         private synchronized OptionGroup internalCreateGroup(String id)
345:         {
346:                 OptionGroup g = new OptionGroup(id, this);
347:                 groups.add(g);
348:                 return g;
349:         }
350: }