Skip to content

Package: ArchiveGenerator

ArchiveGenerator

nameinstructionbranchcomplexitylinemethod
ArchiveGenerator(RouterConfiguration)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
addToList(Path, String, OptEntry)
M: 33 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
addToOpt(AddToOptFacade)
M: 0 C: 50
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
checkValue(VariableAssignment, RegEx)
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%
copyPending(VariableAssignment)
M: 25 C: 28
53%
M: 5 C: 1
17%
M: 3 C: 1
25%
M: 5 C: 7
58%
M: 0 C: 1
100%
generatePathList()
M: 54 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getNewDefaultAss(AbstractVariableDefinition)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
handlePkgRule(PackageRule)
M: 139 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 31 C: 0
0%
M: 1 C: 0
0%
isActive(AbstractVariableDefinition)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isWeak(AbstractVariableDefinition)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
mkFli4l(File)
M: 43 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
parseOptions(String)
M: 0 C: 73
100%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 0 C: 16
100%
M: 0 C: 1
100%
rcGenerator(File)
M: 5 C: 67
93%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 10
83%
M: 0 C: 1
100%

Coverage

1: package generator;
2:
3: import java.io.BufferedWriter;
4: import java.io.File;
5: import java.io.FileNotFoundException;
6: import java.io.FileOutputStream;
7: import java.io.IOException;
8: import java.io.OutputStreamWriter;
9: import java.util.ArrayList;
10: import java.util.Collections;
11: import java.util.HashMap;
12: import java.util.List;
13: import java.util.Map;
14: import java.util.Map.Entry;
15: import java.util.Set;
16: import java.util.TreeMap;
17:
18: import generator.option.AbstractOption;
19: import generator.option.TypeOption;
20: import model.Package;
21: import model.Position;
22: import model.RouterConfiguration;
23: import model.assignment.VariableAssignment;
24: import model.definition.AbstractVariableDefinition;
25: import model.packages.Option;
26: import model.packages.PackageDescriptionFile;
27: import model.packages.PackageRule;
28: import model.packages.Path;
29: import model.type.ReferenceIsUnresolvedException;
30: import model.type.RegEx;
31: import pruefskript.parser.nodes.AddToOptFacade;
32:
33: /**
34: *
35: * @author Muri
36: *
37: */
38: public class ArchiveGenerator { // NOPMD ja die komplexit�t ist hoch.
39:
40:         // /**
41:         // * List of weak variables.
42:         // */
43:         // private final transient List<AbstractVariableDefinition> weakList =
44:         // new ArrayList<AbstractVariableDefinition>();
45:
46:         /**
47:          * equalsign.
48:          */
49:         private static final String EQUALS = "=";
50:
51:         /**
52:          * List of Files for the opt.img.
53:          */
54:         private final transient Map<String, OptEntry> optList = new HashMap<String, OptEntry>();
55:
56:         /**
57:          * List of Files fot the rootfs.img.
58:          */
59:         private final transient Map<String, OptEntry> rootfsList = new HashMap<String, OptEntry>();
60:
61:         /**
62:          * rc.cfg, lofgile.
63:          */
64:         private final transient Map<String, VariableAssignment> rcLog =
65:                         new TreeMap<String, VariableAssignment>();
66:
67:         /**
68:          * the list with the pathes of the files.
69:          */
70:         private final transient List<String> pathList = new ArrayList<String>();
71:         /**
72:          * The associated router configuration.
73:          */
74:         private final RouterConfiguration rc;
75:
76:         /**
77:          * Constructor.
78:          *
79:          * @param rc
80:          * The associated router configuration.
81:          */
82:         public ArchiveGenerator(final RouterConfiguration rc) {
83:                 this.rc = rc;
84:         }
85:
86:         /**
87:          * @param path
88:          * where the build files should go
89:          * @throws ReferenceIsUnresolvedException
90:          * ReferenceIsUnresolvedException
91:          * @throws FileNotFoundException
92:          * FileNotFoundException
93:          *
94:          */
95:         public void mkFli4l(final File path)
96:                         throws ReferenceIsUnresolvedException, FileNotFoundException {
97:                 final Map<String, Package> pkgs = this.rc.getRouterSoftware().getPackages();
98:
99:•                for (Entry<String, Package> pkg : pkgs.entrySet()) {
100:
101:                         final PackageDescriptionFile pdf = pkg.getValue().getPackageDescriptionFile();
102:•                        for (PackageRule pkgRule : pdf.getPackagerules()) {
103:                                 handlePkgRule(pkgRule);
104:                         }
105:                 }
106:                 generatePathList();
107:                 rcGenerator(path);
108:         }
109:
110:         /**
111:          * handles one package Rule.
112:          *
113:          * @param pkgRule
114:          * pkgRule
115:          * @throws ReferenceIsUnresolvedException
116:          * ReferenceIsUnresolvedException
117:          * @throws FileNotFoundException
118:          * FileNotFoundException
119:          */
120:         private void handlePkgRule(final PackageRule pkgRule) // NOPMD
121:                                                                                                                         // hohe
122:                         // komplexit�t
123:                         throws ReferenceIsUnresolvedException, FileNotFoundException {
124:                 final List<AbstractVariableDefinition> varDefs = pkgRule.getVariableDefinition();
125:•                for (AbstractVariableDefinition aVD : varDefs) {
126:                         final Map<String, VariableAssignment> asses = this.rc.findAssesByDefinition(aVD);
127:
128:                         final Path path = pkgRule.getPath();
129:                         final String pathString = path.getValue();
130:
131:                         // continue if it's a kernel module (no path and only a filename)
132:•                        if (pathString.startsWith("/")) { // NOPMD no, I want to check, not get a Symbol.
133:                                 continue;
134:                         }
135:
136:                         // If no assignment has been found, the default value of the definition is used as the
137:                         // assignment value
138:•                        if (asses.isEmpty()) {
139:                                 final VariableAssignment defaultAss = getNewDefaultAss(aVD);
140:
141:                                 asses.put(defaultAss.getNameOfAssignment(), defaultAss);
142:                         }
143:
144:•                        for (Entry<String, VariableAssignment> entry : asses.entrySet()) {
145:                                 final VariableAssignment ass = entry.getValue();
146:
147:•                                if (checkValue(ass, pkgRule.getValue())) {
148:
149:                                         final File newFile = new File(pathString); // NOPMD need for one Object per
150:                                                                                                                                 // iteration!
151:                                         final AbstractVariableDefinition def = ass.getDefinition();
152:
153:                                         // If neither the File exists, nor the definition is weak, there is something
154:                                         // extremely wrong.
155:•                                        if (!(newFile.exists() && isWeak(def))) {
156:                                                 throw new java.io.FileNotFoundException(pathString + " not found and " // NOPMD
157:                                                                                                                                                                                                 // bug
158:                                                                                                                                                                                                 // in
159:                                                                                                                                                                                                 // PMD
160:                                                                 + def.getName() + " is not weak.");
161:                                         }
162:
163:                                         final OptionHandler optHandler = OptionHandler.getTheInstance();
164:                                         final List<AbstractOption> optionList =
165:                                                         optHandler.convertOptions(pkgRule.getOptions());
166:                                         final OptEntry optEntry =
167:                                                         new OptEntry(ass.getNameOfAssignment(), ass.getValue(), newFile, // NOPMD
168:                                                                         optionList);
169:                                         // need object for every iteration
170:
171:•                                        for (AbstractOption abstractOption : optionList) {
172:•                                                if (abstractOption.isTypeOption()) {
173:•                                                        if (((TypeOption) abstractOption).getType().isFileType()) { // NOPMD
174:                                                                                                                                                                                 // cannot be
175:                                                                                                                                                                                 // nested
176:                                                                                                                                                                                 // (because
177:                                                                                                                                                                                 // of the
178:                                                                                                                                                                                 // casting)
179:                                                                 addToList(path, pathString, optEntry);
180:                                                         }
181:                                                 }
182:                                         }
183:                                 }
184:                         }
185:                 }
186:         }
187:
188:         /**
189:          * method for generating a temporary assignment.
190:          *
191:          * @param aVD
192:          * aVD
193:          * @return VariableAssignment
194:          */
195:         private VariableAssignment getNewDefaultAss(final AbstractVariableDefinition aVD) {
196:                 // FIXME: this obviously doesn't work
197:                 new VariableAssignment(aVD.getName(), aVD, aVD.getDefaultValue().toString(),
198:                                 new Position(0, 0, ""));
199:                 return null;
200:         }
201:
202:         /**
203:          *
204:          * @param path
205:          * path
206:          * @param pathString
207:          * pathString
208:          * @param optEntry
209:          * optEntry
210:          */
211:         private void addToList(final Path path, final String pathString, final OptEntry optEntry) {
212:•                if (this.rootfsList.get(pathString) != null) {
213:•                        if (path.isRootfs()) {
214:•                                if (this.optList.get(pathString) != null) {
215:                                         this.optList.remove(pathString);
216:                                 }
217:                                 this.rootfsList.put(pathString, optEntry);
218:                         } else {
219:                                 this.optList.put(pathString, optEntry);
220:                         }
221:                 }
222:         }
223:
224:         /**
225:          * Checks, if the value matches the RegEx.
226:          *
227:          * @param ass
228:          * ass
229:          * @param value
230:          * value
231:          * @return true if the RegEx matches
232:          * @throws ReferenceIsUnresolvedException
233:          * ReferenceIsUnresolvedException
234:          */
235:         private boolean checkValue(final VariableAssignment ass, final RegEx value)
236:                         throws ReferenceIsUnresolvedException {
237:                 return ass.getValue().matches(value.toStringResolved());
238:         }
239:
240:         /**
241:          * f�gt eine weitere Datei zu dem OPT-Archiv hinzu.
242:          *
243:          * @param aTO
244:          * AddToOptFacade
245:          * @throws FileNotFoundException
246:          * FileNotFoundException
247:          */
248:         public void addToOpt(final AddToOptFacade aTO) throws FileNotFoundException {
249:                 final String path = aTO.getPath();
250:                 final File newFile = new File(path);
251:                 final List<Option> options = parseOptions(aTO.getFlags());
252:                 final OptionHandler optHandler = OptionHandler.getTheInstance();
253:                 final List<AbstractOption> optionList = optHandler.convertOptions(options); // NOPMD bug in
254:                                                                                                                                                                         // PMD
255:                 final OptEntry optEntry = new OptEntry("", "", newFile, optionList);
256:•                if (!(newFile.exists())) {
257:                         throw new java.io.FileNotFoundException(path // NOPMD bug in PMD
258:                                         + " not found (while adding additional Files to OPT).");
259:                 }
260:                 this.optList.put(optEntry.getPath(), optEntry);
261:
262:         }
263:
264:         /**
265:          *
266:          * @param options
267:          * options
268:          * @return List of options
269:          */
270:         private List<Option> parseOptions(final String options) {
271:                 final String[] array = options.split("=|\\s");
272:                 final ArrayList<String> arrayList = new ArrayList<String>();
273:•                for (String string : array) {
274:•                        if (!("\\s".equals(string) || string.equals(EQUALS) || "".equals(string))) {
275:                                 arrayList.add(string);
276:                         }
277:                 }
278:                 final List<Option> opts = new ArrayList<Option>();
279:                 int pos = 0;
280:•                while (pos < arrayList.size()) {
281:                         final String name = array[pos];
282:                         pos++;
283:                         final String value = array[pos];
284:                         final Option opt = new Option(name, value); // NOPMD need object per iteration!
285:                         opts.add(opt);
286:                         pos++;
287:                 }
288:                 return opts;
289:         }
290:
291:         /**
292:          * Pr�ft unteranderem auf Aktivit�t (OPT) des Assignments (mit isActive). Gibt true zur�ck, wenn
293:          * keine Datei f�r das Assignment kopiert wurde.
294:          *
295:          * @param ass
296:          * Assignment to check
297:          * @return boolean
298:          */
299:         public boolean copyPending(final VariableAssignment ass) {
300:                 boolean copyPending = this.isActive(ass.getDefinition());
301:                 final Map<String, OptEntry> copyList = new HashMap<String, OptEntry>();
302:                 copyList.putAll(this.optList);
303:                 copyList.putAll(this.rootfsList);
304:                 final Set<Entry<String, OptEntry>> copySet = copyList.entrySet();
305:•                for (Entry<String, OptEntry> entry : copySet) {
306:•                        if (entry.getValue().getAssName().equals(ass.getNameOfAssignment())
307:•                                        && entry.getValue().getAssValue().equals(ass.getValue())) {
308:                                 copyPending = Boolean.FALSE;
309:                                 break;
310:                         }
311:                 }
312:
313:                 return copyPending;
314:
315:         }
316:
317:         /**
318:          * generates the rc.cfg file.
319:          *
320:          * @param path
321:          * the path where to store the configuration file
322:          */
323:         private void rcGenerator(final File path) {
324:                 try {
325:                         final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
326:                                         new FileOutputStream(path.getAbsolutePath() + File.separator + "rc.cfg"),
327:                                         "UTF-8"));
328:                         this.rcLog.putAll(rc.getAllAssignments());
329:•                        for (Entry<String, VariableAssignment> rcEntry : this.rcLog.entrySet()) {
330:                                 final VariableAssignment ass = rcEntry.getValue();
331:                                 out.write(ass.getNameOfAssignment() + EQUALS + "'" + ass.getValue() + "'\n");
332:                         }
333:                         out.close();
334:                 } catch (final IOException e) {
335:                         org.apache.log4j.Logger.getRootLogger().error(e.getMessage());
336:                 }
337:         }
338:
339:         /**
340:          * Wenn die Definition in der Liste der Weak Variablen auftaucht, gibt sie true zur�ck. Dann
341:          * sollte die Methode die diese aufruft keinen Fehler werfen.
342:          *
343:          * @param aVD
344:          * AbstractVariableDefinition
345:          * @return true if the identifier is a weak identifier
346:          */
347:         private boolean isWeak(final AbstractVariableDefinition aVD) {
348:                 return this.rc.getRouterSoftware().getWeakList().containsKey(aVD.getName());
349:         }
350:
351:         /**
352:          * generates the pathList.
353:          */
354:         private void generatePathList() {
355:•                for (Map.Entry<String, OptEntry> entry : this.optList.entrySet()) {
356:                         final OptEntry nextEntry = entry.getValue();
357:                         final String nextDirectory = nextEntry.getDirectory();
358:                         this.pathList.add(nextDirectory);
359:                 }
360:•                for (Map.Entry<String, OptEntry> entry : this.rootfsList.entrySet()) {
361:                         final OptEntry nextEntry = entry.getValue();
362:                         final String nextDirectory = nextEntry.getDirectory();
363:                         this.pathList.add(nextDirectory);
364:                 }
365:                 Collections.sort(this.pathList);
366:         }
367:
368:         /**
369:          *
370:          * @param aVD
371:          * AbstractVariableDefinition to check
372:          * @return something boolean
373:          */
374:         public boolean isActive(final AbstractVariableDefinition aVD) {
375:                 return aVD.isVariableActive(this.rc, aVD);
376:         }
377:
378: }