streamlet-examples/JarInfo.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 13 May 2022 21:35:30 +0200
branchv_0
changeset 96 c34106244a54
parent 75 ecbf6504915c
permissions -rw-r--r--
portable order of (i++) parameters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
/**
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Relational pipes
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 *
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * This program is free software: you can redistribute it and/or modify
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * the Free Software Foundation, version 3 of the License.
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
import java.io.File;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
import java.io.IOException;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
import java.util.LinkedList;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import java.util.List;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import java.util.jar.JarFile;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
74
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    24
/**
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    25
 * <p>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    26
 * This streamlet provides metadata from JAR (or ZIP) files.</p>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    27
 *
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    28
 * <p>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    29
 * With no options it returns the main class (if any) and number of entries (files and directories) in the archive.</p>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    30
 *
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    31
 * <p>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    32
 * Specific attributes can be selected using options – e.g. --option 'attribute' '…' Supported attributes are:</p>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    33
 *
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    34
 * <ul>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    35
 * <li>comment</li>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    36
 * <li>entries</li>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    37
 * <li>main_class</li>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    38
 * </ul>
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    39
 */
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
public class JarInfo extends Streamlet {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
74
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    42
	// TODO: total size 
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    43
	// TODO: OSGi metadata etc.
a2aa84f310a5 streamlet examples: documentation
František Kučera <franta-hg@frantovo.cz>
parents: 73
diff changeset
    44
	// TODO: more OOP, move to separate repository, proper Maven project, clean-up, stabilize API
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    45
	public static final String ATTRIBUTE_COMMENT = "comment";
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    46
	public static final String ATTRIBUTE_ENTRIES = "entries";
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    47
	public static final String ATTRIBUTE_MAIN_CLASS = "main_class";
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    48
75
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 74
diff changeset
    49
	private final List<String> jarAttributes = new LinkedList<>();
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    50
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	public static void main(String[] args) throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		JarInfo s = new JarInfo();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		int status = s.run();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		System.exit(status);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	protected List<Streamlet.AttributeMetadata> getOutputAttributesMetadata() {
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    58
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    59
		List<Option> attributeOptions = getOptions("attribute");
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    60
		if (attributeOptions.isEmpty()) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    61
			attributeOptions.add(new Option("attribute", ATTRIBUTE_MAIN_CLASS));
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    62
			attributeOptions.add(new Option("attribute", ATTRIBUTE_ENTRIES));
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    63
		}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    64
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		List<Streamlet.AttributeMetadata> result = new LinkedList<>();
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    66
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    67
		for (int i = 0; i < attributeOptions.size(); i++) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    68
			Streamlet.Option o = attributeOptions.get(i);
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    69
			Streamlet.Type type;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    70
			switch (o.value) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    71
				case ATTRIBUTE_COMMENT:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    72
				case ATTRIBUTE_MAIN_CLASS:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    73
					type = Streamlet.Type.STRING;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    74
					break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    75
				case ATTRIBUTE_ENTRIES:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    76
					type = Streamlet.Type.INTEGER;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    77
					break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    78
				default:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    79
					throw new IllegalArgumentException("Unsupported attribute: " + o.value);
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    80
			}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    81
			result.add(new Streamlet.AttributeMetadata(getAlias(i, o.value), type));
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    82
			jarAttributes.add(o.value);
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    83
		}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    84
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		return result;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    88
	@Override
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	protected List<Object> getOutputAttributes() {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		List<Object> result = new LinkedList<>();
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    91
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    92
		try {
75
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 74
diff changeset
    93
			JarFile jar = new JarFile(new File(getCurrentFile()));
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    94
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    95
			for (String attributeName : jarAttributes) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    96
				switch (attributeName) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    97
					case ATTRIBUTE_COMMENT:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    98
						result.add(jar.getComment());
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    99
						break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   100
					case ATTRIBUTE_MAIN_CLASS:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   101
						String mainClass = jar.getManifest() == null ? null : jar.getManifest().getMainAttributes().getValue("Main-Class");
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   102
						result.add(mainClass);
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   103
						break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   104
					case ATTRIBUTE_ENTRIES:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   105
						result.add(jar.size());
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   106
						break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   107
				}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   108
			}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   109
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   110
		} catch (Exception e) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   111
			for (String attributeName : jarAttributes) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   112
				result.add(null);
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   113
			}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   114
		}
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
		return result;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
}