Skip to content

Package: TypeFile

TypeFile

nameinstructionbranchcomplexitylinemethod
TypeFile()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
TypeFile(Map, List)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
TypeFile(Map, List, Map)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
convertExtension()
M: 71 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 23 C: 0
0%
M: 1 C: 0
0%
createPosCommentMap()
M: 0 C: 24
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
createPosTypeMap()
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
cycleDetector(NamedVariableType, List)
M: 0 C: 51
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
cycleListdetect()
M: 10 C: 8
44%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 2
50%
M: 0 C: 1
100%
equals(Object)
M: 0 C: 21
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 3
100%
M: 0 C: 1
100%
getComments()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getExtensions()
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%
getTypeFileManager()
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%
getTypes()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
iterateOverTypes(Iterator)
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
prepareStringToPrint()
M: 0 C: 108
100%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 27
100%
M: 0 C: 1
100%
print(String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
printTheLinesToFile(String, StringBuffer)
M: 7 C: 17
71%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 5
71%
M: 0 C: 1
100%
resolveRegEx(RegEx)
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
resolveTypes()
M: 12 C: 15
56%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 4
57%
M: 0 C: 1
100%
setTypeFileManager(TypeFileManager)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
sortPositions()
M: 0 C: 39
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: package model.type;
2:
3: import java.io.FileOutputStream;
4: import java.io.IOException;
5: import java.io.OutputStreamWriter;
6: import java.util.ArrayList;
7: import java.util.HashMap;
8: import java.util.Iterator;
9: import java.util.List;
10: import java.util.Map;
11:
12: import model.Comment;
13: import model.Position;
14: import parser.TypeExtensionProcessor;
15: import parser.tempparserobjects.TempTypeExtension;
16: import printer.PrinterException;
17: import printer.PrinterUtility;
18:
19: /**
20: * TypeFile represents the "\check\<package>.exp" files.
21: *
22: * @author HFW410RA - Philipp Rammos
23: *
24: */
25: public class TypeFile {
26:
27:         /**
28:          * TypeFileManager.
29:          */
30:         private TypeFileManager typeFileManager;
31:
32:         /**
33:          * HashMap of all comments from the AssignmentFile.
34:          */
35:         private final java.util.List<Comment> comments;
36:         /**
37:          * HashMap of all types from the TypeFile.
38:          */
39:         private final java.util.Map<String, NamedVariableType> types;
40:         /**
41:          * Hashmap for the typeextensions.
42:          */
43:         private final java.util.Map<String, ArrayList<TypeExtension>> extensions;
44:
45:         /**
46:          * Hashmap for the typeextensions.
47:          */
48:         private final java.util.Map<String, ArrayList<TempTypeExtension>> tempTypExtension;
49:
50:         /**
51:          * Default constructor.
52:          */
53:         public TypeFile() {
54:                 this.types = new HashMap<String, NamedVariableType>();
55:                 this.comments = new ArrayList<Comment>();
56:                 this.extensions = new HashMap<String, ArrayList<TypeExtension>>();
57:                 this.tempTypExtension = new HashMap<String, ArrayList<TempTypeExtension>>();
58:         }
59:
60:         /**
61:          * Constructor, just sets fields. extensions are NULL.
62:          *
63:          *
64:          * @param types2
65:          * the types to set.
66:          * @param comments
67:          * The comments to set.
68:          */
69:         public TypeFile(final Map<String, NamedVariableType> types2, final List<Comment> comments) {
70:                 this.types = types2;
71:                 this.comments = comments;
72:                 this.extensions = new HashMap<String, ArrayList<TypeExtension>>();
73:                 this.tempTypExtension = new HashMap<String, ArrayList<TempTypeExtension>>();
74:         }
75:
76:         /**
77:          * Constructor, just sets fields. No side effects.
78:          *
79:          * @param types2
80:          * the types to set.
81:          * @param comments
82:          * The comments to set.
83:          * @param tempextensionsMap
84:          * The Typfileextensions.
85:          */
86:         public TypeFile(final Map<String, NamedVariableType> types2, final List<Comment> comments,
87:                         final Map<String, ArrayList<TempTypeExtension>> tempextensionsMap) {
88:                 this.types = types2;
89:                 this.comments = comments;
90:                 this.tempTypExtension = tempextensionsMap;
91:                 this.extensions = new HashMap<String, ArrayList<TypeExtension>>();
92:
93:         }
94:
95:         /**
96:          * Convert the TempTypExtension to TypExtension.
97:          *
98:          */
99:         public void convertExtension() {
100:                 final Iterator<ArrayList<TempTypeExtension>> tempextensionsMapIterator =
101:                                 this.tempTypExtension.values().iterator();
102:•                while (tempextensionsMapIterator.hasNext()) {
103:                         try {
104:                                 final ArrayList<TempTypeExtension> listTempTypExtension =
105:                                                 tempextensionsMapIterator.next();
106:                                 final Iterator<TempTypeExtension> iterator = listTempTypExtension.iterator();
107:•                                while (iterator.hasNext()) {
108:                                         final TempTypeExtension next = iterator.next();
109:                                         final TypeExtension typeExtenion = (new TypeExtensionProcessor(next,
110:                                                         this.getTypeFileManager().getAlltypes()))
111:                                                                         .createTypeExtension(getTypeFileManager());
112:•                                        if (extensions.containsKey(next.getName())) {
113:                                                 final ArrayList<TypeExtension> typeExtensionsList =
114:                                                                 extensions.get(next.getName());
115:                                                 typeExtensionsList.add(typeExtenion);
116:                                         } else {
117:                                                 final ArrayList<TypeExtension> newTypeExtensionsList =
118:                                                                 new ArrayList<TypeExtension>();
119:                                                 newTypeExtensionsList.add(typeExtenion);
120:                                                 extensions.put(next.getName(), newTypeExtensionsList);
121:                                         }
122:                                 }
123:                         } catch (final TypeDoesNotExistException e) {
124:                                 // TODO Fail with OPT variables (OPT_XXX)
125:                         }
126:                 }
127:         }
128:
129:         @Override
130:         public boolean equals(final Object obj) {
131:•                return obj instanceof TypeFile
132:•                                && ((TypeFile) obj).getComments().equals(this.getComments())
133:•                                && ((TypeFile) obj).getTypes().equals(this.getTypes());
134:         }
135:
136:         @Override
137:         public int hashCode() {
138:                 return this.getComments().hashCode() + this.getTypes().hashCode();
139:         }
140:
141:         /**
142:          * Resolves the unresolved RegExparts of the types. Uses the allTypes HashMap which is set by
143:          * the TypeFileManager before calling this.
144:          *
145:          * @throws TypeDoesNotExistException
146:          * if a part is unresolvable cause the type to resolve doesnt exit.
147:          * @throws TypeExceptions
148:          * if a cycle is detected or the reference is unresolved
149:          */
150:         public void resolveTypes() throws TypeDoesNotExistException, TypeExceptions {
151:                 iterateOverTypes(this.types.values().iterator());
152:                 final Iterator<ArrayList<TypeExtension>> iterator = this.extensions.values().iterator();
153:
154:•                while (iterator.hasNext()) {
155:                         iterateOverTypes(iterator.next().iterator());
156:                         final TypeExtensionVisitor visitor = new TypeExtensionVisitor() {
157:
158:                                 @Override
159:                                 public void visit(final TypeExtensionWithCondition extension)
160:                                                 throws TypeDoesNotExistException, TypeExceptions {
161:                                         resolveRegEx(extension.getConditionExpression());
162:                                 }
163:
164:                                 @Override
165:                                 public void visit(final TypeExtensionWithoutCondition extension) {
166:                                         // Do nothing
167:
168:                                 }
169:                         };
170:                 }
171:         }
172:
173:         /**
174:          * Iterates over AbstractType and resolves the references.
175:          *
176:          * @param iterator
177:          * Iterator over which should be iterated
178:          * @throws TypeDoesNotExistException
179:          * if a part is unresolvable cause the type to resolve doesnt exit.
180:          * @throws TypeExceptions
181:          * if a cycle is detected or the reference is unresolved
182:          */
183:         private void iterateOverTypes(final Iterator<? extends AbstractType> iterator)
184:                         throws TypeDoesNotExistException, TypeExceptions {
185:•                while (iterator.hasNext()) {
186:                         final RegEx expression = iterator.next().getExpression();
187:                         resolveRegEx(expression);
188:                 }
189:         }
190:
191:         /**
192:          * Iterates over Parts and resolves the references.
193:          *
194:          * @param expression
195:          * The RegEx to resolve
196:          * @throws TypeDoesNotExistException
197:          * if a part is unresolvable cause the type to resolve doesnt exit.
198:          * @throws TypeExceptions
199:          * if a cycle is detected or the reference is unresolved
200:          */
201:         private void resolveRegEx(final RegEx expression)
202:                         throws TypeDoesNotExistException, TypeExceptions {
203:                 final Map<String, NamedVariableType> globalTypes = this.typeFileManager.getAlltypes();
204:                 final Iterator<RegExPart> partIterator = expression.getParts().iterator();
205:•                while (partIterator.hasNext()) {
206:                         partIterator.next().accept(new RegExPartVisitor() {
207:                                 @Override
208:                                 public void visit(final RegExRefPart regexrefpart)
209:                                                 throws TypeDoesNotExistException, TypeExceptions {
210:                                         regexrefpart.getState().accept(new RefStateVisitor() {
211:                                                 @Override
212:                                                 public void visit(final RefUnResState state)
213:                                                                 throws TypeExceptions, TypeDoesNotExistException {
214:                                                         if (globalTypes.get(state.getValue()) == null) {
215:                                                                 throw new TypeDoesNotExistException(
216:                                                                                 basic.PrinterConstants.TYPENOTEXISTEXC
217:                                                                                                 + state.getValue());
218:                                                         } else {
219:                                                                 regexrefpart.setState(
220:                                                                                 new RefResState(globalTypes.get(state.getValue())));
221:                                                         }
222:                                                 }
223:
224:                                                 @Override
225:                                                 public void visit(final RefResState state) {
226:                                                 }
227:                                         });
228:                                 }
229:
230:                                 @Override
231:                                 public void visit(final RegExStringPart regexstringpart) {
232:                                 }
233:                         });
234:                 }
235:         }
236:
237:         /**
238:          * detects cycles in typeList.
239:          *
240:          * @throws TypeExceptions
241:          * TypeExceptions
242:          * @throws TypeDoesNotExistException
243:          * TypeDoesNotExistException
244:          */
245:         public void cycleListdetect() throws TypeExceptions, TypeDoesNotExistException {
246:
247:                 final Iterator<NamedVariableType> typeIterator = this.types.values().iterator();
248:
249:•                while (typeIterator.hasNext()) {
250:                         cycleDetector(typeIterator.next(), new ArrayList<String>());
251:                 }
252:
253:         }
254:
255:         /**
256:          * Detectes cycles.
257:          *
258:          * @param type
259:          * string to test
260:          * @param cycleTypeList
261:          * List with NamedVariableTypes
262:          *
263:          * @throws TypeExceptions
264:          * typeexceptions
265:          * @throws TypeDoesNotExistException
266:          * TypeDoesNotExistException
267:          */
268:
269:         public void cycleDetector(final NamedVariableType type, final List<String> cycleTypeList)
270:                         throws TypeExceptions, TypeDoesNotExistException {
271:•                if (cycleTypeList.contains(type.getName())) {
272:                         final CycleException e =
273:                                         new CycleException(basic.PrinterConstants.CYCLEEXCEPTION + type.getName());
274:                         throw e;
275:                 } else {
276:                         cycleTypeList.add(type.getName());
277:                         final Iterator<RegExPart> partIterator = type.getExpression().getParts().iterator();
278:•                        while (partIterator.hasNext()) {
279:                                 final RegExPart regExPart = partIterator.next();
280:                                 regExPart.accept(new RegExPartVisitor() {
281:
282:                                         @Override
283:                                         public void visit(final RegExRefPart regexrefpart)
284:                                                         throws TypeExceptions, TypeDoesNotExistException {
285:
286:                                                 try {
287:                                                         TypeFile.this.cycleDetector(regexrefpart.getState().getType(),
288:                                                                         cycleTypeList);
289:                                                 } catch (final ReferenceIsUnresolvedException e) {
290:                                                         e.printStackTrace();
291:                                                 }
292:
293:                                         }
294:
295:                                         @Override
296:                                         public void visit(final RegExStringPart regexstringpart) {
297:                                                 // nothing to do
298:
299:                                         }
300:                                 });
301:                         }
302:                         cycleTypeList.remove(type.getName());
303:                 }
304:
305:         }
306:
307:         /**
308:          * Returns the field type. No side effects.
309:          *
310:          * @return this.types
311:          */
312:         public java.util.Map<String, NamedVariableType> getTypes() {
313:                 return this.types;
314:         }
315:
316:         /**
317:          * Prints an TypeFile containing the VariableTypes and Comments.
318:          *
319:          * @param pathToPrint
320:          * The path where to print the file.
321:          * @throws printer.PrinterException
322:          * If a IOException is thrown.
323:          * @throws printer.PrinterException
324:          * @return Returns a string representing the printed lines (for testing).
325:          */
326:         public String print(final String pathToPrint) throws printer.PrinterException {
327:
328:                 final StringBuffer stringBuffer = prepareStringToPrint();
329:
330:                 printTheLinesToFile(pathToPrint, stringBuffer);
331:
332:                 return stringBuffer.toString();
333:         }
334:
335:         /**
336:          * Prepares the StringBuffer for printing. After this method, the buffer is ready for printing
337:          * and it will contain the needed lines.
338:          *
339:          * @return The print-ready Buffer.
340:          */
341:         private StringBuffer prepareStringToPrint() {
342:                 final PrinterUtility printUt = new PrinterUtility();
343:                 final StringBuffer stringBuffer = new StringBuffer();
344:                 final List<Position> posSorted = sortPositions();
345:                 final Map<Position, Comment> commentMap = createPosCommentMap();
346:                 final Map<Position, NamedVariableType> typeMap = createPosTypeMap();
347:
348:                 final Iterator<Position> posIt = posSorted.iterator();
349:                 int row = 0;
350:                 int col = 0;
351:                 int size = 0;
352:•                while (posIt.hasNext()) {
353:                         final Position current = posIt.next();
354:•                        if (row < current.getRow()) {
355:                                 row += printUt.writeLineBreaks(stringBuffer, row, current.getRow());
356:                                 col = 0;
357:                         }
358:•                        if (row == current.getRow() && col < current.getColumn()) {
359:                                 col += printUt.writeSpaces(stringBuffer, col, current.getColumn());
360:                         }
361:•                        if (typeMap.containsKey(current)) {
362:                                 final NamedVariableType typeToPrint = typeMap.get(current);
363:                                 stringBuffer.append(typeToPrint.print());
364:                                 size = typeToPrint.print().length();
365:                         } else {
366:                                 final Comment commentToPrint = commentMap.get(current);
367:                                 stringBuffer.append(commentToPrint.print());
368:                                 size = commentToPrint.print().length();
369:                         }
370:                         col += size;
371:
372:                 }
373:                 return stringBuffer;
374:         }
375:
376:         /**
377:          * Creates a Map containing assignment.getPosition() as Key and assignment as Value. No side
378:          * effects to fields.
379:          *
380:          * @return The created map.
381:          */
382:         private Map<Position, NamedVariableType> createPosTypeMap() {
383:                 final Map<Position, NamedVariableType> typeMap =
384:                                 new HashMap<Position, NamedVariableType>();
385:                 final Iterator<NamedVariableType> typeIt = this.types.values().iterator();
386:•                while (typeIt.hasNext()) {
387:                         final NamedVariableType current = typeIt.next();
388:                         typeMap.put(current.getPosition(), current);
389:                 }
390:                 return typeMap;
391:         }
392:
393:         /**
394:          * Creates a Map containing comment.getPosition() as Key and comment as Value.No side effects to
395:          * fields.
396:          *
397:          * @return The created map.
398:          */
399:         private Map<Position, Comment> createPosCommentMap() {
400:                 final Map<Position, Comment> commentMap = new HashMap<Position, Comment>();
401:                 final Iterator<Comment> commentIt = this.comments.iterator();
402:•                while (commentIt.hasNext()) {
403:                         final Comment current = commentIt.next();
404:                         commentMap.put(current.getPosition(), current);
405:                 }
406:                 return commentMap;
407:         }
408:
409:         /**
410:          * The only one method, that real prints lines into the file.
411:          *
412:          * @param pathToPrint
413:          * The path where to print the file-
414:          * @param stringBuffer
415:          * The buffer with the lines to print.
416:          * @throws PrinterException
417:          * If an IOException is caused by the printer.
418:          */
419:         private void printTheLinesToFile(final String pathToPrint, final StringBuffer stringBuffer)
420:                         throws PrinterException {
421:                 try {
422:                         final OutputStreamWriter writer = new OutputStreamWriter(
423:                                         new FileOutputStream(pathToPrint), basic.PrinterConstants.ENCODING);
424:                         try {
425:                                 writer.write(stringBuffer.toString());
426:                         } finally {
427:                                 writer.close();
428:                         }
429:                 } catch (final IOException ioEx) {
430:                         throw new PrinterException(basic.PrinterConstants.PRINTEXCEPTION, ioEx);
431:
432:                 }
433:         }
434:
435:         /**
436:          * Sorts the Positions of VariableAssignments and Comments in one List.
437:          *
438:          * @return The list of sorted Positions.
439:          */
440:         private List<Position> sortPositions() {
441:                 final List<Position> sortedPositions = new ArrayList<Position>();
442:                 final Iterator<Comment> commantIt = this.comments.iterator();
443:•                while (commantIt.hasNext()) {
444:                         sortedPositions.add(commantIt.next().getPosition());
445:                 }
446:                 final Iterator<NamedVariableType> typeIt = this.types.values().iterator();
447:•                while (typeIt.hasNext()) {
448:                         sortedPositions.add(typeIt.next().getPosition());
449:                 }
450:                 java.util.Collections.sort(sortedPositions);
451:                 return sortedPositions;
452:         }
453:
454:         /**
455:          * @return the comments
456:          */
457:         public java.util.List<Comment> getComments() {
458:                 return this.comments;
459:         }
460:
461:         /**
462:          * @return the extensions
463:          */
464:         public java.util.Map<String, ArrayList<TypeExtension>> getExtensions() {
465:                 return this.extensions;
466:         }
467:
468:         /**
469:          * Getter for the TypeFileManager.
470:          *
471:          * @return TypeFileManager
472:          */
473:         public TypeFileManager getTypeFileManager() {
474:                 return typeFileManager;
475:         }
476:
477:         /**
478:          * Setter for the TypeFileManager. Is called by the FileManager itself.
479:          *
480:          * @param typeFileManager
481:          * TypeFileManager for this TypeFile
482:          */
483:         public void setTypeFileManager(final TypeFileManager typeFileManager) {
484:                 this.typeFileManager = typeFileManager;
485:         }
486: }