Skip to content

Package: RouterSoftware$2

RouterSoftware$2

nameinstructionbranchcomplexitylinemethod
add(Controller, Package, File)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 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: package model;
2:
3: import java.io.File;
4: import java.io.IOException;
5: import java.nio.charset.Charset;
6: import java.nio.file.Files;
7: import java.nio.file.Paths;
8: import java.util.HashMap;
9: import java.util.Iterator;
10: import java.util.List;
11: import java.util.Map;
12: import java.util.Map.Entry;
13:
14: import controller.Controller;
15: import controller.buildingphase.ScanAndParseDefinitionFile;
16: import controller.buildingphase.ScanAndParsePackageDescriptionFile;
17: import controller.buildingphase.ScanAndParseTypeFile;
18: import controller.extendingphase.ExtendTypes;
19: import controller.resolvingphase.ResolveTypes;
20: import model.definition.AbstractVariableDefinition;
21: import model.definition.ConfigurationContext;
22: import model.definition.VariableContext;
23: import model.type.NamedVariableType;
24: import model.type.TypeDoesNotExistException;
25: import model.type.TypeExceptions;
26: import model.type.TypeFileManager;
27: import parser.ParserException;
28: import reader.PackageReader;
29: import reader.ReaderException;
30: import scanner.ScannerException;
31:
32: /**
33: * Represents some router software.
34: */
35: public final class RouterSoftware {
36:         /**
37:          * String für "check".
38:          */
39:         private static final String CHECK_STRING = "check";
40:         /**
41:          * String für ".txt".
42:          */
43:         private static final String TXT_STRING = ".txt";
44:         /**
45:          * Directory containing type definitions.
46:          */
47:         private static final String DIRECTORY_CONTAINING_TYPE_DEFINITIONS = CHECK_STRING;
48:         /**
49:          * Directory containing variable definitions.
50:          */
51:         private static final String DIRECTORY_CONTAINING_VARIABLE_DEFINITIONS = CHECK_STRING;
52:         /**
53:          * Directory containing files describing package contents.
54:          */
55:         private static final String DIRECTORY_CONTAINING_PACKAGE_DESCRIPTIONS = "opt";
56:
57:         /**
58:          * Ending of files with type definitions.
59:          */
60:         private static final String FILE_ENDING_TYPE_DEFINITIONS = ".exp";
61:         /**
62:          * Ending of files with variable definitions.
63:          */
64:         private static final String FILE_ENDING_VARIABLE_DEFINITIONS = TXT_STRING;
65:         /**
66:          * Ending of files describing package contents.
67:          */
68:         private static final String FILE_ENDING_PACKAGE_DESCRIPTIONS = TXT_STRING;
69:
70:         /**
71:          * The path to the software directory.
72:          */
73:         private final String path;
74:         /**
75:          * the list of Packages. identifiable by the name (key).
76:          */
77:         private final transient Map<String, Package> packageList;
78:         /**
79:          * represents the list of weak variable definitions.
80:          */
81:         private final transient Map<String, String> weakList;
82:
83:         /**
84:          * TypeFileManager to store all TypeFiles.
85:          */
86:         private final TypeFileManager typeFileManager;
87:
88:         /**
89:          * Getter for the TypeFileManager.
90:          *
91:          * @return TypeFileManager
92:          */
93:         public TypeFileManager getTypeFileManager() {
94:                 return typeFileManager;
95:         }
96:
97:         /**
98:          * Builds building phases.
99:          * <p>
100:          * TODO: merge with {@link RouterConfiguration#BuildingPhaseBuilder}.
101:          */
102:         interface BuildingPhaseBuilder {
103:                 /**
104:                  * Adds a building phase to passed controller.
105:                  *
106:                  * @param controller
107:                  * The {@link Controller} to use.
108:                  * @param pkg
109:                  * The package the file belongs to.
110:                  * @param file
111:                  * The file.
112:                  */
113:                 void add(Controller controller, Package pkg, File file);
114:         }
115:
116:         /**
117:          * Default constructor.
118:          */
119:         public RouterSoftware() {
120:                 super();
121:                 this.path = null;
122:                 packageList = new HashMap<String, Package>();
123:                 weakList = new HashMap<String, String>();
124:                 this.typeFileManager = new TypeFileManager();
125:                 generateTypesLists();
126:         }
127:
128:         /**
129:          * Getter for this Object.
130:          *
131:          * @return RouterSoftware
132:          */
133:         private RouterSoftware getThis() {
134:                 return this;
135:         }
136:
137:         /**
138:          * Constructor.
139:          *
140:          * @param path
141:          * the path to the software.
142:          * @throws TypeDoesNotExistException
143:          * if some referenced type is not found.
144:          * @throws ScannerException
145:          * if scanning fails.
146:          * @throws InterruptedException
147:          * if asynchronous scanning/parsing has been interrupted.
148:          * @throws IOException
149:          * if an I/O error occurs while reading files.
150:          * @throws ReaderException
151:          * if some necessary package file is missing.
152:          * @throws ParserException
153:          * if parsing files.
154:          * @throws TypeExceptions
155:          * if a cycle is detected or the reference is unresolved.
156:          */
157:         public RouterSoftware(final String path)
158:                         throws TypeDoesNotExistException, ScannerException, InterruptedException, IOException,
159:                         ReaderException, ParserException, TypeExceptions {
160:                 super();
161:                 this.path = path;
162:                 packageList = new HashMap<String, Package>();
163:                 weakList = new HashMap<String, String>();
164:                 this.typeFileManager = new TypeFileManager();
165:
166:                 final Controller controller = new Controller();
167:                 this.addBuildingPhases(controller,
168:                                 PackageReader.readFiles(
169:                                                 Paths.get(this.path, DIRECTORY_CONTAINING_TYPE_DEFINITIONS).toString(),
170:                                                 FILE_ENDING_TYPE_DEFINITIONS),
171:                                 new BuildingPhaseBuilder() {
172:                                         @Override
173:                                         public void add(final Controller controller, final Package pkg,
174:                                                         final File file) {
175:                                                 controller.addToBulidingPhase(
176:                                                                 new ScanAndParseTypeFile(pkg, file, getThis()));
177:                                         }
178:                                 });
179:                 this.addBuildingPhases(controller,
180:                                 PackageReader.readFiles(Paths
181:                                                 .get(this.path, DIRECTORY_CONTAINING_VARIABLE_DEFINITIONS).toString(),
182:                                                 FILE_ENDING_VARIABLE_DEFINITIONS),
183:                                 new BuildingPhaseBuilder() {
184:                                         @Override
185:                                         public void add(final Controller controller, final Package pkg,
186:                                                         final File file) {
187:                                                 controller.addToBulidingPhase(new ScanAndParseDefinitionFile(pkg, file));
188:                                         }
189:                                 });
190:                 this.addBuildingPhases(controller,
191:                                 PackageReader.readFiles(Paths
192:                                                 .get(this.path, DIRECTORY_CONTAINING_PACKAGE_DESCRIPTIONS).toString(),
193:                                                 FILE_ENDING_PACKAGE_DESCRIPTIONS),
194:                                 new BuildingPhaseBuilder() {
195:                                         @Override
196:                                         public void add(final Controller controller, final Package pkg,
197:                                                         final File file) {
198:                                                 controller.addToBulidingPhase(
199:                                                                 new ScanAndParsePackageDescriptionFile(pkg, file));
200:                                         }
201:                                 });
202:                 generateTypesLists();
203:                 controller.addToDissolvingPhase(new ResolveTypes(this.typeFileManager));
204:                 controller.addToExtendingPhase(new ExtendTypes(this.typeFileManager));
205:                 controller.start();
206:         }
207:
208:         /**
209:          * Adds building phases to passed controller.
210:          *
211:          * @param controller
212:          * The {@link Controller} to use.
213:          * @param files
214:          * The files to iterate over.
215:          * @param builder
216:          * Builds the building phases for the controller according to the files passed.
217:          */
218:         private void addBuildingPhases(final Controller controller, final Map<String, File> files,
219:                         final BuildingPhaseBuilder builder) {
220:                 for (Map.Entry<String, File> entry : files.entrySet()) {
221:                         final String name = entry.getKey();
222:                         final Package pkg = this.hasThisPackage(name)
223:                                         ? this.getThisPackage(name)
224:                                         : this.add(new Package(this, name));
225:                         builder.add(controller, pkg, entry.getValue());
226:                 }
227:         }
228:
229:         @Override
230:         public boolean equals(final Object o) {
231:                 if (!(o instanceof RouterSoftware)) {
232:                         return false;
233:                 }
234:                 final RouterSoftware other = (RouterSoftware) o;
235:                 return other.getPackages().equals(this.getPackages())
236:                                 && other.getWeakList().equals(this.getWeakList());
237:         }
238:
239:         @Override
240:         public int hashCode() {
241:                 return this.packageList.hashCode() + this.weakList.hashCode();
242:         }
243:
244:         /**
245:          *
246:          * @param pkg
247:          * a package
248:          * @return pkg
249:          */
250:         public Package add(final Package pkg) {
251:                 this.packageList.put(pkg.getName(), pkg);
252:                 return pkg;
253:         }
254:
255:         /**
256:          * returns true, if the package with this Name exists.
257:          *
258:          * @param pkgName
259:          * this Package
260:          * @return true if the package manager has this package
261:          */
262:         public boolean hasThisPackage(final String pkgName) {
263:                 return this.packageList.containsKey(pkgName);
264:         }
265:
266:         /**
267:          * returns the package with this package name.
268:          *
269:          * @param pkgName
270:          * name of the desired Package
271:          * @return the package
272:          */
273:         public Package getThisPackage(final String pkgName) {
274:                 return this.packageList.get(pkgName);
275:         }
276:
277:         /**
278:          * @return the packageList
279:          */
280:         public Map<String, Package> getPackages() {
281:                 return this.packageList;
282:         }
283:
284:         /**
285:          * @return The path to the software directory.
286:          */
287:         public String getPath() {
288:                 return this.path;
289:         }
290:
291:         /**
292:          * @return The software version.
293:          */
294:         public String getVersion() {
295:                 try {
296:                         final List<String> lines = Files.readAllLines(Paths.get(this.path, "version.txt"),
297:                                         Charset.forName("UTF-8"));
298:                         return lines.isEmpty()
299:                                         ? ""
300:                                         : lines.get(0);
301:                 } catch (final IOException e) {
302:                         return "";
303:                 }
304:         }
305:
306:         /**
307:          * generating a list of all Types.
308:          *
309:          * @return a lsit of Assignemnts
310:          */
311:         private Map<String, NamedVariableType> generateTypesLists() {
312:
313:                 final Iterator<Entry<String, Package>> iter = this.getPackages().entrySet().iterator();
314:
315:                 final Map<String, NamedVariableType> types = new HashMap<String, NamedVariableType>();
316:
317:                 while (iter.hasNext()) {
318:                         final Package currentPackage = iter.next().getValue();
319:                         types.putAll(currentPackage.getTypeFile().getTypes());
320:                 }
321:                 return types;
322:         }
323:
324:         /**
325:          * generating a list of all Definitions.
326:          *
327:          * @param context
328:          * .
329:          * @return a list of Definitions
330:          */
331:         private Map<String, AbstractVariableDefinition>
332:                         generateDefList(final VariableContext context) {
333:
334:                 final Iterator<Entry<String, Package>> iter = this.getPackages().entrySet().iterator();
335:
336:                 final Map<String, AbstractVariableDefinition> definitions =
337:                                 new HashMap<String, AbstractVariableDefinition>();
338:
339:                 while (iter.hasNext()) {
340:                         final Package currentPackage = iter.next().getValue();
341:                         final Iterator<AbstractVariableDefinition> definitionsToCurPack =
342:                                         currentPackage.getDefFile().getDefinitions().values().iterator();
343:                         while (definitionsToCurPack.hasNext()) {
344:                                 final AbstractVariableDefinition currentDef = definitionsToCurPack.next();
345:                                 if (currentDef.getContext().equals(context)) {
346:                                         definitions.put(currentDef.getName(), currentDef);
347:                                 }
348:                         }
349:                 }
350:
351:                 return definitions;
352:         }
353:
354:         /**
355:          *
356:          * @param def
357:          * def
358:          *
359:          * @return AbstractVariableDefinition
360:          */
361:         public AbstractVariableDefinition findDefinition(final String def) {
362:                 return this.findDefinition(def, new ConfigurationContext());
363:         }
364:
365:         /**
366:          * @param context
367:          * .
368:          * @param def
369:          * def
370:          *
371:          * @return AbstractVariableDefinition
372:          */
373:         public AbstractVariableDefinition findDefinition(final String def,
374:                         final VariableContext context) {
375:                 final Map<String, AbstractVariableDefinition> defs = this.generateDefList(context);
376:                 return defs.get(def);
377:
378:         }
379:
380:         /**
381:          * @return the weakList
382:          */
383:         public Map<String, String> getWeakList() {
384:                 return this.weakList;
385:         }
386:
387:         /**
388:          * Returniert einen regulaeren Ausdruck.
389:          *
390:          * @param name
391:          * Der Name der zum Namen eines Typen passen muss.
392:          * @return Der zum Typ passende regulaere ausdruck.
393:          */
394:         public NamedVariableType findTypeByName(final String name) {
395:                 return this.generateTypesLists().get(name);
396:         }
397:
398:         /**
399:          * @return the default configuration
400:          * @throws TypeDoesNotExistException
401:          * if some referenced type is not found.
402:          * @throws ScannerException
403:          * if scanning fails.
404:          * @throws InterruptedException
405:          * if asynchronous scanning/parsing has been interrupted.
406:          * @throws IOException
407:          * if an I/O error occurs while reading files.
408:          * @throws ReaderException
409:          * if some necessary package file is missing.
410:          * @throws ParserException
411:          * if parsing files.
412:          * @throws TypeExceptions
413:          * if a cycle is detected or the reference is unresolved.
414:          *
415:          */
416:         public RouterConfiguration getDefaultConfiguration()
417:                         throws ParserException, TypeDoesNotExistException, ScannerException,
418:                         InterruptedException, IOException, ReaderException, TypeExceptions {
419:                 return new RouterConfiguration(this, Paths.get(this.path, "config").toString());
420:         }
421:
422:         /**
423:          * @param configuration
424:          * The configuration location.
425:          * @return the default configuration
426:          * @throws TypeDoesNotExistException
427:          * if some referenced type is not found.
428:          * @throws ScannerException
429:          * if scanning fails.
430:          * @throws InterruptedException
431:          * if asynchronous scanning/parsing has been interrupted.
432:          * @throws IOException
433:          * if an I/O error occurs while reading files.
434:          * @throws ReaderException
435:          * if some necessary package file is missing.
436:          * @throws ParserException
437:          * if parsing files.
438:          * @throws TypeExceptions
439:          * if a cycle is detected or the reference is unresolved.
440:          *
441:          */
442:         public RouterConfiguration getConfiguration(final String configuration)
443:                         throws ParserException, TypeDoesNotExistException, ScannerException,
444:                         InterruptedException, IOException, ReaderException, TypeExceptions {
445:                 return new RouterConfiguration(this, Paths.get(this.path, configuration).toString());
446:         }
447:
448: }