1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.javadpkg.field.impl;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import net.sourceforge.javadpkg.Context;
30 import net.sourceforge.javadpkg.GlobalConstants;
31 import net.sourceforge.javadpkg.ParseException;
32 import net.sourceforge.javadpkg.field.Field;
33 import net.sourceforge.javadpkg.field.FieldParser;
34 import net.sourceforge.javadpkg.io.DataSource;
35
36
37
38
39
40
41
42
43
44
45 public class FieldParserImpl implements FieldParser, GlobalConstants {
46
47
48
49 private boolean allowEmptyLines;
50
51
52
53
54
55
56 private boolean returnEmptyLines;
57
58
59
60 private boolean allowNamelessFields;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public FieldParserImpl(boolean allowEmptyLines, boolean returnEmptyLines, boolean allowNamelessFields) {
79 super();
80
81 this.allowEmptyLines = allowEmptyLines;
82 this.returnEmptyLines = returnEmptyLines;
83 this.allowNamelessFields = allowNamelessFields;
84 }
85
86
87 @Override
88 public Map<String, Field> parseFieldsAsMap(DataSource source, Context context) throws IOException, ParseException {
89 Map<String, Field> map;
90 List<Field> list;
91 String name;
92
93
94 if (source == null)
95 throw new IllegalArgumentException("Argument source is null.");
96 if (context == null)
97 throw new IllegalArgumentException("Argument context is null.");
98
99
100 list = this.parseFieldsAsList(source, context);
101
102
103 map = new LinkedHashMap<>();
104 for (Field field : list) {
105
106 if (field.isEmpty() || field.isNameless()) {
107 continue;
108 }
109
110 name = field.getName().toLowerCase();
111 if (map.containsKey(name))
112 throw new ParseException("The field |" + field.getName() + "| exists more than one time in the source |"
113 + source.getName() + "|.");
114 map.put(name, field);
115 }
116 return map;
117 }
118
119
120 @Override
121 public List<Field> parseFieldsAsList(DataSource source, Context context) throws IOException, ParseException {
122 List<Field> fields;
123 String line, name, value;
124 FieldImpl field = null;
125 int index;
126 boolean lastLine = false;
127
128
129 if (source == null)
130 throw new IllegalArgumentException("Argument source is null.");
131 if (context == null)
132 throw new IllegalArgumentException("Argument context is null.");
133
134
135 fields = new ArrayList<>();
136 try {
137 try (BufferedReader reader = new BufferedReader(new InputStreamReader(source.getInputStream(), UTF_8_CHARSET))) {
138 while ((line = reader.readLine()) != null) {
139
140 if (lastLine)
141 throw new ParseException(
142 "Found a line |" + line + "| in source |" + source.getName() + "| which is not a field.");
143
144
145 if (line.startsWith("#")) {
146 continue;
147 }
148
149 else if (line.startsWith(" ") || line.startsWith("\t")) {
150 if (field == null)
151 if (this.allowNamelessFields) {
152 fields.add(new NamelessField(line));
153 } else
154 throw new ParseException("Found a continuation line |" + line + "| in source |"
155 + source.getName() + "|, but the initial line of the field is missing.");
156 else {
157 field.addValue("\n");
158 line = line.substring(1);
159 field.addValue(line);
160 }
161 }
162
163 else {
164
165 if (line.isEmpty()) {
166
167 if (!this.allowEmptyLines) {
168 lastLine = true;
169 }
170
171 else if (this.returnEmptyLines) {
172 fields.add(EmptyField.EMPTY_FIELD);
173 }
174 }
175
176 else {
177 index = line.indexOf(':');
178 if (index == -1) {
179 if (this.allowNamelessFields) {
180 fields.add(new NamelessField(line));
181 } else
182 throw new ParseException("Found a line |" + line + "| in source |" + source.getName()
183 + "| which is not a field.");
184 } else {
185 name = line.substring(0, index).trim();
186 value = line.substring(index + 1).trim();
187 field = new FieldImpl(name, value);
188 fields.add(field);
189 }
190 }
191 }
192 }
193 }
194 } catch (IOException e) {
195 throw new IOException(
196 "Couldn't read control for Debian binary package from source |" + source.getName() + "|: " + e.getMessage(),
197 e);
198 }
199 return fields;
200 }
201
202
203 }