Package: ModelBuildPath

ModelBuildPath

nameinstructionbranchcomplexitylinemethod
ModelBuildPath(IVdmProject)
M: 44 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
add(IContainer)
M: 56 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
contains(IContainer)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getDefaultModelSrcPath()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getLibrary()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getModelSrcPaths()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getOutput()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasModelPath()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parse()
M: 136 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 35 C: 0
0%
M: 1 C: 0
0%
reload()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
remove(IContainer)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
save()
M: 131 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 28 C: 0
0%
M: 1 C: 0
0%
setLibrary(IContainer)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setOutput(IContainer)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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.util.List;
29: import java.util.Vector;
30:
31: import javax.xml.parsers.DocumentBuilder;
32: import javax.xml.parsers.DocumentBuilderFactory;
33:
34: import org.eclipse.core.resources.IContainer;
35: import org.eclipse.core.resources.IFolder;
36: import org.eclipse.core.resources.IProject;
37: import org.eclipse.core.runtime.CoreException;
38: import org.eclipse.core.runtime.IPath;
39: import org.overture.ide.core.VdmCore;
40: import org.overture.ide.internal.core.ResourceManager;
41: import org.w3c.dom.Document;
42: import org.w3c.dom.Node;
43: import org.w3c.dom.NodeList;
44:
45: public class ModelBuildPath
46: {
47:         final IVdmProject vdmProject;
48:         final IProject project;
49:         final File modelPathFile;
50:
51:         List<IContainer> srcPaths = new Vector<IContainer>();
52:         IContainer output;
53:         IContainer library;
54:
55:         public ModelBuildPath(IVdmProject project)
56:         {
57:                 this.vdmProject = project;
58:
59:                 this.project = (IProject) this.vdmProject.getAdapter(IProject.class);
60:                 IPath base = this.project.getLocation();
61:                 base = base.append(".modelpath");
62:                 this.modelPathFile = base.toFile();
63:                 this.output = this.project.getFolder("generated");
64:                 this.library = this.project.getFolder("lib");
65:                 parse();
66:         }
67:
68:         private boolean hasModelPath()
69:         {
70:                 return this.modelPathFile.exists();
71:         }
72:
73:         private IContainer getDefaultModelSrcPath()
74:         {
75:                 return this.project;
76:         }
77:
78:         public List<IContainer> getModelSrcPaths()
79:         {
80:                 List<IContainer> tmp = new Vector<IContainer>(srcPaths.size());
81:                 tmp.addAll(srcPaths);
82:                 return tmp;
83:         }
84:
85:         public synchronized IContainer getOutput()
86:         {
87:                 return this.output;
88:         }
89:
90:         public synchronized IContainer getLibrary()
91:         {
92:                 return this.library;
93:         }
94:
95:         private synchronized void parse()
96:         {
97:•                if (!hasModelPath())
98:                 {
99:                         srcPaths.add(getDefaultModelSrcPath());
100:                         return;
101:                 }
102:                 try
103:                 {
104:                         File file = this.modelPathFile;
105:                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
106:                         DocumentBuilder db = dbf.newDocumentBuilder();
107:                         Document doc = db.parse(file);
108:                         doc.getDocumentElement().normalize();
109:                         NodeList nodeLst = doc.getElementsByTagName("modelpathentry");
110:•                        for (int s = 0; s < nodeLst.getLength(); s++)
111:                         {
112:                                 Node fstNode = nodeLst.item(s);
113:•                                if (fstNode.getNodeType() == Node.ELEMENT_NODE)
114:                                 {
115:                                         Node kindAttribute = fstNode.getAttributes().getNamedItem("kind");
116:                                         String kindValue = kindAttribute.getNodeValue();
117:•                                        if (kindValue != null)
118:                                         {
119:•                                                if (kindValue.equals("src"))
120:                                                 {
121:                                                         Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
122:                                                         String pathValue = pathAttribute.getNodeValue();
123:•                                                        if(pathValue.equals("."))
124:                                                         {
125:                                                                 add(getDefaultModelSrcPath());
126:                                                         }else
127:                                                         {
128:                                                                 add(this.project.getFolder(pathValue));
129:                                                         }
130:•                                                } else if (kindValue.equals("output"))
131:                                                 {
132:                                                         Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
133:                                                         String pathValue = pathAttribute.getNodeValue();
134:                                                         output = this.project.getFolder(pathValue);
135:•                                                } else if (kindValue.equals("library"))
136:                                                 {
137:                                                         Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
138:                                                         String pathValue = pathAttribute.getNodeValue();
139:                                                         library = this.project.getFolder(pathValue);
140:                                                 }
141:                                         }
142:                                 }
143:
144:                         }
145:                         
146:•                        if(srcPaths.isEmpty())
147:                         {
148:                                 srcPaths.add(getDefaultModelSrcPath());
149:                         }
150:                         
151:                 } catch (Exception e)
152:                 {
153:                         VdmCore.log("Faild to parse .modelpath file", e);
154:                 }
155:         }
156:
157:         public synchronized void setOutput(IContainer container)
158:         {
159:                 this.output = container;
160:         }
161:
162:         public synchronized void setLibrary(IContainer container)
163:         {
164:                 this.library = container;
165:         }
166:
167:         public synchronized void add(IContainer container)
168:         {
169:•                if(container instanceof IProject)
170:                 {
171:                         srcPaths.clear();
172:                 }
173:•                else if(container instanceof IFolder)
174:                 {
175:                         String fullPath = container.getProjectRelativePath().toString();
176:                         
177:                         
178:                         
179:                         boolean flag = true;
180:•                        for (IContainer s : srcPaths)
181:                         {
182:•                                flag = flag && s.getProjectRelativePath().toString().startsWith(fullPath);
183:                         }
184:                         
185:•                        if(flag)
186:                                 srcPaths.clear();
187:                 }
188:                 
189:•                if (!srcPaths.contains(container))
190:                 {
191:                         srcPaths.add(container);
192:                 }
193:         }
194:
195:         public synchronized void remove(IContainer container)
196:         {
197:•                if (srcPaths.contains(container))
198:                 {
199:                         srcPaths.remove(container);
200:                 }
201:         }
202:
203:         public synchronized boolean contains(IContainer container)
204:         {
205:                 return srcPaths.contains(container);
206:         }
207:
208:         public synchronized void save() throws CoreException
209:         {
210:                 StringBuffer sb = new StringBuffer();
211:
212:                 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
213:                 sb.append("<modelpath>\n");
214:
215:•                for (IContainer src : srcPaths)
216:                 {
217:•                        if (src.getProjectRelativePath().toString().length() > 0)
218:                         {
219:                                 sb.append("\t<modelpathentry kind=\"src\" path=\""
220:                                                 + src.getProjectRelativePath() + "\"/>\n");
221:•                        }else if (src instanceof IProject)
222:                         {
223:                                 sb.append("\t<modelpathentry kind=\"src\" path=\".\"/>\n");
224:                         }
225:
226:                 }
227:
228:•                if (output != null
229:•                                && output.getProjectRelativePath().toString().length() > 0)
230:                 {
231:                         sb.append("\t<modelpathentry kind=\"output\" path=\""
232:                                         + output.getProjectRelativePath() + "\"/>\n");
233:                 }
234:
235:•                if (library != null
236:•                                && library.getProjectRelativePath().toString().length() > 0)
237:                 {
238:                         sb.append("\t<modelpathentry kind=\"library\" path=\""
239:                                         + library.getProjectRelativePath() + "\"/>\n");
240:                 }
241:                 sb.append("</modelpath>");
242:
243:                 PrintWriter out = null;
244:                 try
245:                 {
246:                         FileWriter outFile = new FileWriter(this.modelPathFile);
247:                         out = new PrintWriter(outFile);
248:                         out.println(sb.toString());
249:
250:                 } catch (IOException e)
251:                 {
252:                         VdmCore.log("Faild to save .modelpath file", e);
253:                 } finally
254:                 {
255:•                        if (out != null)
256:                         {
257:                                 out.close();
258:                         }
259:                 }
260:                 ResourceManager.getInstance().syncBuildPath(vdmProject);
261:         }
262:
263:         /**
264:          * Reload the build path and discard any un-saved changes
265:          */
266:         public void reload()
267:         {
268:                 parse();
269:         }
270:
271: }