/**
* 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;
/**
* <p>
* This streamlet provides metadata from JAR (or ZIP) files.</p>
*
* <p>
* With no options it returns the main class (if any) and number of entries (files and directories) in the archive.</p>
*
* <p>
* Specific attributes can be selected using options – e.g. --option 'attribute' '…' Supported attributes are:</p>
*
* <ul>
* <li>comment</li>
* <li>entries</li>
* <li>main_class</li>
* </ul>
*/
public class JarInfo extends Streamlet {
// TODO: total size
// TODO: OSGi metadata etc.
// TODO: more OOP, move to separate repository, proper Maven project, clean-up, stabilize API
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);
}
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;
}
}