Package: SourceParserManager

SourceParserManager

nameinstructionbranchcomplexitylinemethod
SourceParserManager()
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%
getInstance()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getParserWithHeighestPriority(String, IConfigurationElement[])
M: 58 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
getSourceParser(IVdmProject)
M: 29 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
parseFile(IVdmSourceUnit)
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
parseMissingFiles(IVdmProject, IVdmModel, IProgressMonitor)
M: 47 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
static {...}
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%

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.parser;
23:
24: import java.io.IOException;
25: import java.util.List;
26:
27: import org.eclipse.core.resources.IProject;
28: import org.eclipse.core.resources.IResource;
29: import org.eclipse.core.runtime.Assert;
30: import org.eclipse.core.runtime.CoreException;
31: import org.eclipse.core.runtime.IConfigurationElement;
32: import org.eclipse.core.runtime.IProgressMonitor;
33: import org.eclipse.core.runtime.Platform;
34: import org.overture.ide.core.ICoreConstants;
35: import org.overture.ide.core.IVdmModel;
36:
37: import org.overture.ide.core.VdmCore;
38: import org.overture.ide.core.resources.IVdmProject;
39: import org.overture.ide.core.resources.IVdmSourceUnit;
40:
41: public class SourceParserManager
42: {
43:         /**
44:          * A handle to the unique Singleton instance.
45:          */
46:         static private SourceParserManager _instance = null;
47:
48:         /**
49:          * @return The unique instance of this class.
50:          */
51:         static public SourceParserManager getInstance()
52:         {
53:•                if (null == _instance)
54:                 {
55:                         _instance = new SourceParserManager();
56:                 }
57:                 return _instance;
58:         }
59:
60:         /**
61:          * Loads a source parser for the given project
62:          *
63:          * @param project
64:          * the project to load the source parser for
65:          * @return a valid source parser for the current project based on the
66:          * highest priority parser available from the nature, or null if no
67:          * parser could be found
68:          * @throws CoreException
69:          */
70:         public ISourceParser getSourceParser(IVdmProject project)
71:                         throws CoreException
72:         {
73:                 IConfigurationElement[] config = Platform
74:                                 .getExtensionRegistry()
75:                                 .getConfigurationElementsFor(ICoreConstants.EXTENSION_PARSER_ID);
76:
77:                 IConfigurationElement selectedParser = getParserWithHeighestPriority(
78:                                 project.getVdmNature(), config);
79:
80:•                if (selectedParser != null)
81:                 {
82:                         final Object o = selectedParser.createExecutableExtension("class");
83:
84:•                        if (o instanceof AbstractParserParticipant)
85:                         {
86:                                 AbstractParserParticipant parser = (AbstractParserParticipant) o;
87:
88:                                 parser.setProject(project);
89:
90:                                 return parser;
91:                         }
92:
93:                 }
94:                 return null;
95:
96:         }
97:
98:         /**
99:          * Gets the parser available for the given nature having the highest
100:          * priority
101:          *
102:          * @param natureId
103:          * the nature to lookup a parser for
104:          * @param config
105:          * the configuration of the extension point
106:          * @return a valid source parser or null
107:          */
108:         private IConfigurationElement getParserWithHeighestPriority(
109:                         String natureId, IConfigurationElement[] config)
110:         {
111:                 IConfigurationElement selectedParser = null;
112:                 int selectedParserPriority = 0;
113:•                for (IConfigurationElement e : config)
114:                 {
115:•                        if (e.getAttribute("nature").equals(natureId))
116:                         {
117:•                                if (selectedParser == null)
118:                                 {
119:                                         selectedParser = e;
120:                                         String selectedParserPriorityString = selectedParser
121:                                                         .getAttribute("priority");
122:•                                        if (selectedParserPriorityString != null)
123:                                                 selectedParserPriority = Integer
124:                                                                 .parseInt(selectedParserPriorityString);
125:                                 } else
126:                                 {
127:                                         String parserPriorityString = selectedParser
128:                                                         .getAttribute("priority");
129:•                                        if (parserPriorityString != null)
130:                                         {
131:                                                 int parserPriority = Integer
132:                                                                 .parseInt(parserPriorityString);
133:•                                                if (parserPriority > selectedParserPriority)
134:                                                 {
135:                                                         selectedParser = e;
136:                                                         selectedParserPriority = parserPriority;
137:                                                 }
138:                                         }
139:                                 }
140:                         }
141:                 }
142:                 return selectedParser;
143:         }
144:
145:         /***
146:          * Parses files in a project which has a content type and should be parsed
147:          * before a build could be performed
148:          *
149:          * @param vdmProject
150:          * the project
151:          * @param model
152:          * the model with ast
153:          * @param monitor
154:          * an optional monitor, can be null
155:          * @throws CoreException
156:          * @throws IOException
157:          */
158:         public static void parseMissingFiles(IVdmProject vdmProject,
159:                         IVdmModel model, IProgressMonitor monitor) throws CoreException,
160:                         IOException
161:         {
162:
163:                 IProject project = (IProject) vdmProject.getAdapter(IProject.class);
164:
165:•                if (monitor != null)
166:                 {
167:                         monitor.subTask("Parsing files for project: " + project.getName());
168:                 }
169:
170:                 Assert.isNotNull(project, "Project could not be adapted");
171:•                if (project != null)
172:                 {
173:•                        if (!project.isSynchronized(IResource.DEPTH_INFINITE))
174:                         {
175:                                 project.refreshLocal(IResource.DEPTH_INFINITE, null);
176:                         }
177:                         List<IVdmSourceUnit> files = model.getSourceUnits();
178:•                        for (IVdmSourceUnit source : files)
179:                         {
180:                                 // IVdmModel model = file.getProject().getModel();
181:                                 // if (model != null && model.hasFile(file) && file.)
182:                                 // return;
183:                                 // if(!file.hasParseTree())
184:                                 // file.getParseList().clear();
185: //                                System.out.println("Parsing file (build): "+ source);
186:                                 parseFile(source);
187:                         }
188:                 }
189:         }
190:
191:         /**
192:          * Parse a single file from a project
193:          *
194:          * @param source
195:          * the file to be parsed
196:          * @throws CoreException
197:          * @throws IOException
198:          */
199:         public static void parseFile(final IVdmSourceUnit source)
200:                         throws CoreException, IOException
201:         {
202:                 try
203:                 {
204:                         ISourceParser parser = SourceParserManager.getInstance()
205:                                         .getSourceParser(source.getProject());
206:                         Assert.isNotNull(parser, "No parser for file : "
207:                                         + source.toString() + " in project "
208:                                         + source.getProject().toString());
209:                         parser.parse(source);
210:                 } catch (Exception e)
211:                 {
212:•                        if (VdmCore.DEBUG)
213:                         {
214: //                                System.err.println("Error in parseFile");
215:                                 VdmCore.log("SourceParseManager-Error in parseFile", e);
216:                         }
217:                 }
218:         }
219: }