streamlet-examples/JarInfo.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 29 Jan 2020 18:41:42 +0100
branchv_0
changeset 73 1a067a217454
parent 72 f7b9db6fc32b
child 74 a2aa84f310a5
permissions -rw-r--r--
streamlet examples: jar_info/zip_info: return real data
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
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
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
    25
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    26
	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
    27
	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
    28
	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
    29
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    30
	private List<String> jarAttributes = new LinkedList<>();
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    31
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	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
    33
		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
    34
		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
    35
		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
    36
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		// TODO: return real values:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		JarFile jar = new JarFile(new File(args[0]));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		String mainClass = jar.getManifest() == null ? null : jar.getManifest().getMainAttributes().getValue("Main-Class");
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		System.out.println("Name:       " + jar.getName());
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		System.out.println("Comment:    " + jar.getComment());
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		System.out.println("Entries:    " + jar.stream().count());
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		System.out.println("Main class: " + mainClass);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	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
    47
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    48
		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
    49
		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
    50
			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
    51
			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
    52
		}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    53
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		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
    55
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    56
		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
    57
			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
    58
			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
    59
			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
    60
				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
    61
				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
    62
					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
    63
					break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    64
				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
    65
					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
    66
					break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    67
				default:
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    68
					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
    69
			}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    70
			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
    71
			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
    72
		}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    73
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		return result;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
73
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    77
	@Override
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	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
    79
		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
    80
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    81
		try {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    82
			JarFile jar = new JarFile(new File(currentFile));
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
			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
    85
				switch (attributeName) {
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    86
					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
    87
						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
    88
						break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    89
					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
    90
						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
    91
						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
    92
						break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    93
					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
    94
						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
    95
						break;
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    96
				}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    97
			}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    98
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
    99
		} 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
   100
			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
   101
				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
   102
			}
1a067a217454 streamlet examples: jar_info/zip_info: return real data
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   103
		}
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
		return result;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
}