/**
* Relational pipes
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarFile;
public class JarInfo extends Streamlet {
public static final String ATTRIBUTE_COMMENT = "comment";
public static final String ATTRIBUTE_ENTRIES = "entries";
public static final String ATTRIBUTE_MAIN_CLASS = "main_class";
private List<String> jarAttributes = new LinkedList<>();
public static void main(String[] args) throws IOException {
JarInfo s = new JarInfo();
int status = s.run();
System.exit(status);
// TODO: return real values:
JarFile jar = new JarFile(new File(args[0]));
String mainClass = jar.getManifest() == null ? null : jar.getManifest().getMainAttributes().getValue("Main-Class");
System.out.println("Name: " + jar.getName());
System.out.println("Comment: " + jar.getComment());
System.out.println("Entries: " + jar.stream().count());
System.out.println("Main class: " + mainClass);
}
protected List<Streamlet.AttributeMetadata> getOutputAttributesMetadata() {
List<Option> attributeOptions = getOptions("attribute");
if (attributeOptions.isEmpty()) {
attributeOptions.add(new Option("attribute", ATTRIBUTE_MAIN_CLASS));
attributeOptions.add(new Option("attribute", ATTRIBUTE_ENTRIES));
}
List<Streamlet.AttributeMetadata> result = new LinkedList<>();
for (int i = 0; i < attributeOptions.size(); i++) {
Streamlet.Option o = attributeOptions.get(i);
Streamlet.Type type;
switch (o.value) {
case ATTRIBUTE_COMMENT:
case ATTRIBUTE_MAIN_CLASS:
type = Streamlet.Type.STRING;
break;
case ATTRIBUTE_ENTRIES:
type = Streamlet.Type.INTEGER;
break;
default:
throw new IllegalArgumentException("Unsupported attribute: " + o.value);
}
result.add(new Streamlet.AttributeMetadata(getAlias(i, o.value), type));
jarAttributes.add(o.value);
}
return result;
}
@Override
protected List<Object> getOutputAttributes() {
List<Object> result = new LinkedList<>();
try {
JarFile jar = new JarFile(new File(currentFile));
for (String attributeName : jarAttributes) {
switch (attributeName) {
case ATTRIBUTE_COMMENT:
result.add(jar.getComment());
break;
case ATTRIBUTE_MAIN_CLASS:
String mainClass = jar.getManifest() == null ? null : jar.getManifest().getMainAttributes().getValue("Main-Class");
result.add(mainClass);
break;
case ATTRIBUTE_ENTRIES:
result.add(jar.size());
break;
}
}
} catch (Exception e) {
for (String attributeName : jarAttributes) {
result.add(null);
}
}
return result;
}
}