/
ManifestAnalyzer.java
200 lines (193 loc) · 6 KB
/
ManifestAnalyzer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
* Copyright (c) 2000, 2013 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings({"rawtypes", "unchecked"})
public class ManifestAnalyzer {
private static final int
START = 0,
IN_CLASSPATH_HEADER = 1, // multistate
PAST_CLASSPATH_HEADER = 2,
SKIPPING_WHITESPACE = 3,
READING_JAR = 4,
CONTINUING = 5,
SKIP_LINE = 6;
private static final char[] CLASSPATH_HEADER_TOKEN =
"Class-Path:".toCharArray(); //$NON-NLS-1$
private int classpathSectionsCount;
private ArrayList calledFilesNames;
/**
* Analyzes the manifest contents. The given input stream is read using a UTF-8 encoded reader.
* If the contents of the input stream is not encoded using a UTF-8 encoding, the analysis will fail.
*
* @param inputStream the given input stream.
*
* @return <code>true</code> if the analysis is successful, <code>false</code> otherwise.
* @throws IOException if an exception occurs while analyzing the file
*/
public boolean analyzeManifestContents(InputStream inputStream) throws IOException {
char[] chars = Util.getInputStreamAsCharArray(inputStream, -1, Util.UTF_8);
return analyzeManifestContents(chars);
}
/**
* Analyzes the manifest contents.
*
* @param chars the content of the manifest
*
* @return <code>true</code> if the analysis is successful, <code>false</code> otherwise.
*/
public boolean analyzeManifestContents(char[] chars) {
int state = START, substate = 0;
StringBuffer currentJarToken = new StringBuffer();
int currentChar;
this.classpathSectionsCount = 0;
this.calledFilesNames = null;
for (int i = 0, max = chars.length; i < max;) {
currentChar = chars[i++];
if (currentChar == '\r') {
// skip \r, will consider \n later (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=251079 )
if (i < max) {
currentChar = chars[i++];
}
}
switch (state) {
case START:
if (currentChar == CLASSPATH_HEADER_TOKEN[0]) {
state = IN_CLASSPATH_HEADER;
substate = 1;
} else {
state = SKIP_LINE;
}
break;
case IN_CLASSPATH_HEADER:
if (currentChar == '\n') {
state = START;
} else if (currentChar != CLASSPATH_HEADER_TOKEN[substate++]) {
state = SKIP_LINE;
} else if (substate == CLASSPATH_HEADER_TOKEN.length) {
state = PAST_CLASSPATH_HEADER;
}
break;
case PAST_CLASSPATH_HEADER:
if (currentChar == ' ') {
state = SKIPPING_WHITESPACE;
this.classpathSectionsCount++;
} else {
return false;
}
break;
case SKIPPING_WHITESPACE:
if (currentChar == '\n') {
state = CONTINUING;
} else if (currentChar != ' ') {
currentJarToken.append((char) currentChar);
state = READING_JAR;
} else {
// >>>>>>>>>>>>>>>>>> Add the latest jar read
addCurrentTokenJarWhenNecessary(currentJarToken);
}
break;
case CONTINUING:
if (currentChar == '\n') {
addCurrentTokenJarWhenNecessary(currentJarToken);
state = START;
} else if (currentChar == ' ') {
state = SKIPPING_WHITESPACE;
} else if (currentChar == CLASSPATH_HEADER_TOKEN[0]) {
addCurrentTokenJarWhenNecessary(currentJarToken);
state = IN_CLASSPATH_HEADER;
substate = 1;
} else if (this.calledFilesNames == null) {
// >>>>>>>>>>>>>>>>>> Add the latest jar read
addCurrentTokenJarWhenNecessary(currentJarToken);
state = START;
} else {
// >>>>>>>>>>>>>>>>>> Add the latest jar read
addCurrentTokenJarWhenNecessary(currentJarToken);
state = SKIP_LINE;
}
break;
case SKIP_LINE:
if (currentChar == '\n') {
state = START;
}
break;
case READING_JAR:
if (currentChar == '\n') {
// appends token below
state = CONTINUING;
// >>>>>>>>>>> Add a break to not add the jar yet as it can continue on the next line
break;
} else if (currentChar == ' ') {
// appends token below
state = SKIPPING_WHITESPACE;
} else {
currentJarToken.append((char) currentChar);
break;
}
addCurrentTokenJarWhenNecessary(currentJarToken);
break;
}
}
switch (state) {
case START:
return true;
case IN_CLASSPATH_HEADER:
return true;
case PAST_CLASSPATH_HEADER:
return false;
case SKIPPING_WHITESPACE:
// >>>>>>>>>>>>>>>>>> Add the latest jar read
addCurrentTokenJarWhenNecessary(currentJarToken);
return true;
case CONTINUING:
// >>>>>>>>>>>>>>>>>> Add the latest jar read
addCurrentTokenJarWhenNecessary(currentJarToken);
return true;
case SKIP_LINE:
if (this.classpathSectionsCount != 0) {
if (this.calledFilesNames == null) {
return false;
}
}
return true;
case READING_JAR:
// >>>>>>>>>>>>>>>>>> Add the latest jar read
return false;
}
return true;
}
// >>>>>>>>>>>>>>>> Method Extracted from analyzeManifestContents in the READING_JAR Block
private boolean addCurrentTokenJarWhenNecessary(StringBuffer currentJarToken) {
if (currentJarToken != null && currentJarToken.length() > 0) {
if (this.calledFilesNames == null) {
this.calledFilesNames = new ArrayList();
}
this.calledFilesNames.add(currentJarToken.toString());
currentJarToken.setLength(0);
return true;
}
return false;
}
// <<<<<<<<<<<<<<<<<<<<<<
public int getClasspathSectionsCount() {
return this.classpathSectionsCount;
}
public List getCalledFileNames() {
return this.calledFilesNames;
}
}