Skip to content

Package: PackageDescriptionFileParser

PackageDescriptionFileParser

nameinstructionbranchcomplexitylinemethod
PackageDescriptionFileParser(RouterSoftware, Buffer)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
addPackageRule(PackageRule)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handleWeakVariable()
M: 100 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 23 C: 0
0%
M: 1 C: 0
0%
parse()
M: 10 C: 48
83%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 4 C: 11
73%
M: 0 C: 1
100%
parse(RouterSoftware, Buffer)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
peekSymbol()
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setFormatVersion(Integer)
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%
skip()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: *
3: */
4: package parser;
5:
6: import java.util.ArrayList;
7: import java.util.List;
8:
9: import basic.Buffer;
10: import model.Comment;
11: import model.Position;
12: import model.RouterSoftware;
13: import model.packages.PackageDescriptionFile;
14: import model.packages.PackageRule;
15: import parser.states.packagestates.AbstractPackageParserState;
16: import parser.states.packagestates.PackageDescriptionInitialState;
17: import symbols.AbstractSymbol;
18: import symbols.PackageDescriptionFileSymbol;
19: import symbols.RowEndSymbol;
20: import symbols.ValueSymbol;
21: import symbols.VariableSymbol;
22:
23: /**
24: * Parser for the PackageDescription File.
25: *
26: * @author HFW410 .
27: */
28: public final class PackageDescriptionFileParser extends AbstractFileParser {
29:         /**
30:          * The associated RouterSoftware.
31:          */
32:         private final RouterSoftware rs;
33:         /**
34:          * line.
35:          */
36:         private static final String LINE = " line ";
37:         /**
38:          * is the List of PackageRules of the Parser.
39:          */
40:         private final transient List<PackageRule> packageRules;
41:         /**
42:          * The formatVersion of the package description file.
43:          */
44:         private transient Integer formatVersion;
45:
46:         /**
47:          * constructor.
48:          *
49:          * @param rs
50:          * The {@link RouterSoftware} to use.
51:          * @param symbolBuffer
52:          * Buffer of Symbols.
53:          */
54:         private PackageDescriptionFileParser(final RouterSoftware rs,
55:                         final Buffer<AbstractSymbol> symbolBuffer) {
56:                 super(symbolBuffer);
57:                 this.rs = rs;
58:                 this.setComments(new ArrayList<Comment>());
59:                 this.packageRules = new ArrayList<PackageRule>();
60:                 this.setFinished(false);
61:                 this.formatVersion = 0;
62:         }
63:
64:         /**
65:          * creates a PackageDescriptionFileParser and starts the parse-Method.
66:          *
67:          * @param rs
68:          * The {@link RouterSoftware} to use.
69:          * @param buffer
70:          * is the buffer with the symbols.
71:          * @return a PackageDescriptionFile
72:          * @throws InterruptedException
73:          * if there is any problem with the buffer.
74:          * @throws ParserException
75:          * if there is any problem during the parse-methode.
76:          */
77:         public static PackageDescriptionFile parse(final RouterSoftware rs,
78:                         final Buffer<AbstractSymbol> buffer) throws InterruptedException, ParserException {
79:                 return new PackageDescriptionFileParser(rs, buffer).parse();
80:         }
81:
82:         /**
83:          * parses the symbols from the scanner and create a PackageDescriptionFile.
84:          *
85:          * @return a PackageDescriptionFile
86:          * @throws ParserException
87:          * if there is any fail in the parser.
88:          * @throws InterruptedException
89:          * from the buffer.
90:          *
91:          */
92:         private PackageDescriptionFile parse() throws InterruptedException, ParserException {
93:•                if (this.getBuffer() == null) {
94:                         throw new ParserException(ParserConstants.INITIAL_FEHLER);
95:                 } else {
96:                         AbstractPackageParserState state = new PackageDescriptionInitialState(this);
97:•                        while (!this.isFinished()) {
98:                                 final PackageDescriptionFileSymbol nextSymbol = this.peekSymbol();
99:
100:                                 // First check if Variable is named "weak". This is a declaration of a weak variable
101:                                 // and is handled differently.
102:•                                if (nextSymbol instanceof VariableSymbol) {
103:                                         final String content = ((VariableSymbol) nextSymbol).getContent();
104:•                                        if ("weak".equalsIgnoreCase(content)) {
105:                                                 this.skip(); // we don't need the "weak" symbol anymore.
106:                                                 handleWeakVariable();
107:                                                 state = new PackageDescriptionInitialState(this);
108:                                                 continue;
109:                                         }
110:                                 }
111:                                 // end of "weak" check and handling
112:
113:                                 state = state.action(this.rs, nextSymbol);
114:                         }
115:                         return new PackageDescriptionFile(this.getComments(), this.packageRules,
116:                                         this.formatVersion);
117:                 }
118:         }
119:
120:         /**
121:          * Uses the next two symbols of the buffer to add a weak declaration.
122:          *
123:          * @throws InterruptedException
124:          * InterruptedException
125:          * @throws ParserException
126:          * ParserException
127:          */
128:         private void handleWeakVariable() throws InterruptedException, ParserException {
129:                 final PackageDescriptionFileSymbol pdfs1 = this.peekSymbol();
130:                 final Position position = pdfs1.getPosition();
131:•                if (pdfs1 instanceof VariableSymbol) {
132:                         final String defName = ((VariableSymbol) pdfs1).getContent().toUpperCase();
133:                         this.skip();
134:                         final PackageDescriptionFileSymbol pdfs2 = this.peekSymbol();
135:•                        if (pdfs2 instanceof ValueSymbol) {
136:                                 final String valName = ((ValueSymbol) pdfs2).getContent();
137:                                 this.rs.getWeakList().put(defName, valName); // adding an entry to the weakList
138:                                 this.skip();
139:                         } else {
140:                                 throw new ParserException(
141:                                                 "Expecting ValueSymbol after Weak declaration for Definition: '" + defName
142:                                                                 + "'. Please add a default Value. In " + position.getPath() + LINE
143:                                                                 + position.getRow());
144:                         }
145:                 } else {
146:                         throw new ParserException(
147:                                         "Expecting VariableSymbol with definition name after 'weak' declaration in line"
148:                                                         + position.getRow());
149:                 }
150:                 final PackageDescriptionFileSymbol end = this.peekSymbol();
151:•                if (end instanceof RowEndSymbol) {
152:                         this.skip();
153:                 } else {
154:                         throw new ParserException("Expecting end of line after weak declaration in "
155:                                         + position.getPath() + LINE + position.getRow());
156:                 }
157:
158:         }
159:
160:         /**
161:          * adds a new Packagerule to the List of the PackageRules.
162:          *
163:          * @param packageRule
164:          * new one
165:          */
166:         public void addPackageRule(final PackageRule packageRule) {
167:                 this.packageRules.add(packageRule);
168:         }
169:
170:         /**
171:          * sets the format version.
172:          *
173:          * @param version
174:          * is the format version of the package description.
175:          */
176:         public void setFormatVersion(final Integer version) {
177:                 this.formatVersion = version;
178:         }
179:
180:         /**
181:          * removes the first symbol of the buffer.
182:          *
183:          * @throws InterruptedException
184:          * from the buffer.
185:          */
186:         public void skip() throws InterruptedException {
187:                 this.getBuffer().remove();
188:         }
189:
190:         /**
191:          *
192:          * @return a copy of the next symbol in the buffer.
193:          * @throws InterruptedException
194:          * from the buffer.
195:          * @throws ParserException
196:          * if the next Symbol is no package description file symbol.
197:          */
198:         public PackageDescriptionFileSymbol peekSymbol()
199:                         throws InterruptedException, ParserException {
200:                 final AbstractSymbol symbol = this.getBuffer().peek();
201:•                if (symbol.isPackageDescriptionFileSymbol()) {
202:                         return symbol.toPackageDescriptionFileSymbol();
203:                 } else {
204:                         throw new ParserException(ParserConstants.NO_PACKAGE_SYMBOL);
205:                 }
206:
207:         }
208:
209: }