Package: LibraryUtil

LibraryUtil

nameinstructionbranchcomplexitylinemethod
LibraryUtil()
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%
copyFile(File, String, String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
createSelectedLibraries(IVdmProject, LibrarySelection)
M: 47 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
createSelectedLibraries(IVdmProject, Set)
M: 121 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 26 C: 0
0%
M: 1 C: 0
0%
setSelections(IVdmProject, LibrarySelection)
M: 114 C: 0
0%
M: 6 C: 0
0%
M: 5 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
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.ui.wizard.pages;
23:
24: import java.io.File;
25: import java.io.IOException;
26: import java.util.HashSet;
27: import java.util.Set;
28:
29: import org.eclipse.core.resources.IContainer;
30: import org.eclipse.core.resources.IFolder;
31: import org.eclipse.core.resources.IProject;
32: import org.eclipse.core.resources.IResource;
33: import org.eclipse.core.runtime.Assert;
34: import org.eclipse.core.runtime.CoreException;
35: import org.eclipse.core.runtime.Path;
36: import org.overture.ast.lex.Dialect;
37: import org.overture.ide.core.resources.IVdmProject;
38: import org.overture.ide.ui.IVdmUiConstants;
39: import org.overture.ide.ui.utility.PluginFolderInclude;
40:
41: public class LibraryUtil
42: {
43:         public static final String LIB_VDM_UNIT = "VDMUnit";
44:         public static final String LIB_CSV = "CSV";
45:         public static final String LIB_VDM_UTIL = "VDMUtil";
46:         public static final String LIB_MATH = "MATH";
47:         public static final String LIB_IO = "IO";
48:
49:         public static void setSelections(IVdmProject prj, LibrarySelection selection)
50:                         throws CoreException
51:         {
52:•                if (prj == null)
53:                 {
54:                         return;
55:                 }
56:                 String extension = "vdmpp";
57:•                switch (prj.getDialect())
58:                 {
59:                         case VDM_PP:
60:                                 extension = "vdmpp";
61:                                 break;
62:                         case VDM_RT:
63:                                 extension = "vdmrt";
64:                                 break;
65:                         case VDM_SL:
66:                                 extension = "vdmsl";
67:                                 break;
68:                         case CML:
69:                                 break;
70:
71:                 }
72:
73:                 extension = "." + extension;
74:
75:                 selection.setIoChecked(prj.getModelBuildPath().getLibrary().getFile(new Path(LIB_IO
76:                                 + extension)).exists());
77:                 selection.setMathChecked(prj.getModelBuildPath().getLibrary().getFile(new Path(LIB_MATH
78:                                 + extension)).exists());
79:                 selection.setVdmUtilChecked(prj.getModelBuildPath().getLibrary().getFile(new Path(LIB_VDM_UTIL
80:                                 + extension)).exists());
81:                 selection.setCsvChecked(prj.getModelBuildPath().getLibrary().getFile(new Path(LIB_CSV
82:                                 + extension)).exists());
83:                 selection.setVdmUnitChecked(prj.getModelBuildPath().getLibrary().getFile(new Path(LIB_VDM_UNIT
84:                                 + extension)).exists());
85:         }
86:
87:         public static void createSelectedLibraries(IVdmProject prj,
88:                         LibrarySelection selection) throws CoreException
89:         {
90:                 Set<String> importLibraries = new HashSet<String>();
91:
92:•                if (selection.isMathSelected())
93:                 {
94:                         importLibraries.add(LIB_MATH);
95:                 }
96:
97:•                if (selection.isIoSelected())
98:                 {
99:                         importLibraries.add(LIB_IO);
100:                 }
101:•                if (selection.isUtilSelected())
102:                 {
103:                         importLibraries.add(LIB_VDM_UTIL);
104:                 }
105:•                if (selection.isCsvSelected())
106:                 {
107:                         importLibraries.add(LIB_IO);
108:                         importLibraries.add(LIB_CSV);
109:                 }
110:•                if (selection.isVdmUnitSelected())
111:                 {
112:                         importLibraries.add(LIB_VDM_UNIT);
113:                 }
114:
115:                 createSelectedLibraries(prj, importLibraries);
116:         }
117:
118:         public static void createSelectedLibraries(IVdmProject prj,
119:                         Set<String> importLibraries) throws CoreException
120:         {
121:
122:•                if (!importLibraries.isEmpty())
123:                 {
124:                         IProject project = (IProject) prj.getAdapter(IProject.class);
125:                         Assert.isNotNull(project, "Project could not be adapted");
126:
127:                         File libFolder = null;
128:
129:                         IContainer tmp = prj.getModelBuildPath().getLibrary();
130:•                        if (tmp != null && tmp instanceof IFolder)
131:                         {
132:                                 libFolder = ((IFolder) tmp).getLocation().toFile();
133:                         } else
134:                         {
135:                                 File projectRoot = project.getLocation().toFile();
136:                                 libFolder = new File(projectRoot, "lib");
137:                         }
138:
139:•                        if (!libFolder.exists())
140:                                 libFolder.mkdirs();
141:
142:                         String extension = "pp";
143:
144:                         Dialect dialect = prj.getDialect();
145:
146:                         extension = dialect.name().replace("_", "").toLowerCase();
147:                         try
148:                         {
149:
150:•                                for (String lib : importLibraries)
151:                                 {
152:                                         String path = "includes/lib/";
153:•                                        if (dialect == Dialect.VDM_SL)
154:                                         {
155:                                                 path += "SL/" + lib + ".vdmsl";
156:                                         } else
157:                                         {
158:                                                 path += "PP/" + lib + ".vdmpp";
159:                                         }
160:                                         copyFile(libFolder, path, lib + "." + extension);
161:                                 }
162:
163:                         } catch (IOException e)
164:                         {
165:                                 e.printStackTrace();
166:                         }
167:
168:                         project.refreshLocal(IResource.DEPTH_INFINITE, null);
169:                 }
170:
171:         }
172:
173:         private static void copyFile(File libFolder, String sourceLocation,
174:                         String newName) throws IOException
175:         {
176:                 String io = PluginFolderInclude.readFile(IVdmUiConstants.PLUGIN_ID, sourceLocation);
177:                 PluginFolderInclude.writeFile(libFolder, newName, io);
178:
179:         }
180: }