streamlet-examples/Streamlet.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 29 Jan 2020 18:05:13 +0100
branchv_0
changeset 72 f7b9db6fc32b
parent 69 streamlet-examples/streamlet-common.h@52f837fbb216
child 75 ecbf6504915c
permissions -rw-r--r--
streamlet examples: dirty implementation of Java helper classes + demo code
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: 69
diff changeset
     1
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
/**
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Relational pipes
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 *
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * This program is free software: you can redistribute it and/or modify
8c6885543e2c streamlet examples: common functions in C/C++
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
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * the Free Software Foundation, version 3 of the License.
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    18
import java.io.ByteArrayOutputStream;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
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: 69
diff changeset
    20
import java.io.PrintWriter;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    21
import java.io.StringWriter;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    22
import java.util.ArrayList;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    23
import java.util.Arrays;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    24
import java.util.LinkedList;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    25
import java.util.List;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    26
import java.util.Objects;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    27
import java.util.regex.Matcher;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    28
import java.util.regex.Pattern;
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
/**
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    31
 * Unlike the protocol and the message format, these helper classes are not part of the public API. Thus when writing
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    32
 * custom streamlets, it is better to copy this file and review its changes while upgrading to new upstream version.
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 */
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    34
public abstract class Streamlet {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    36
	// TODO: use generated constants
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    37
	private static final int VERSION_SUPPORTED = 100;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    38
	private static final int WAITING_FOR_VERSION = 101;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    39
	private static final int VERSION_ACCEPTED = 102;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    40
	private static final int RELATION_START = 103;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    41
	private static final int INPUT_ATTRIBUTE_METADATA = 104;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    42
	private static final int OUTPUT_ATTRIBUTE_ALIAS = 105;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    43
	private static final int OPTION = 106;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    44
	private static final int COMPLETION_REQUEST = 107;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    45
	private static final int COMPLETION = 108;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    46
	private static final int COMPLETION_END = 109;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    47
	private static final int WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA = 110;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    48
	private static final int OUTPUT_ATTRIBUTE_METADATA = 111;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    49
	private static final int WAITING_FOR_INPUT_ATTRIBUTES = 112;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    50
	private static final int INPUT_ATTRIBUTE = 113;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    51
	private static final int WAITING_FOR_OUTPUT_ATTRIBUTES = 114;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    52
	private static final int OUTPUT_ATTRIBUTE = 115;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    53
	private static final int EXECUTOR_ERROR = 116;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    54
	private static final int STREAMLET_ERROR = 117;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    55
	private static final int STREAMLET_WARNING = 118;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    56
	private static final int RELATION_END = 120;
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    58
	protected static class Message {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    60
		public int code;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    61
		public List<String> parameters;
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    63
		public Message() {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    66
		public Message(int code, String... parameters) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    67
			this.code = code;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    68
			this.parameters = Arrays.asList(parameters);
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    71
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    73
	private static final char SEPARATOR = '\0';
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    75
	private int readInt() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    76
		return Integer.valueOf(readString());
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    77
	}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
    78
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    79
	private String readString() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    80
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    81
		for (int b; (b = System.in.read()) > 0;) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    82
			buffer.write(b);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    83
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    84
		return new String(buffer.toByteArray());
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    87
	private void writeString(String s) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    88
		System.out.print(s);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    89
		System.out.print(SEPARATOR);
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    92
	private void writeInt(int i) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    93
		writeString(String.valueOf(i));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    96
	private void flush() {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    97
		System.out.flush();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   100
	private Message read() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   101
		Message m = new Message();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
		m.code = readInt();
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		int count = readInt();
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   104
		m.parameters = new ArrayList<>(count);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   105
		for (int i = 0; i < count; i++) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   106
			m.parameters.add(readString());
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   107
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		return m;
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   111
	private void copyMatches(Matcher source, List<String> destination) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   112
		for (int i = 1; i < source.groupCount(); i++) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   113
			destination.add(source.group(i));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   117
	private void processMessages() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   118
		OUTER:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   119
		while (true) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   120
			Message m = read();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   121
			switch (m.code) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   122
				case VERSION_SUPPORTED:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   123
					processVersionSupported(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   124
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   125
				case WAITING_FOR_VERSION:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   126
					processWaitingForVersion(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   127
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   128
				case RELATION_START:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   129
					processRelationStart(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   130
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   131
				case INPUT_ATTRIBUTE_METADATA:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   132
					processInputAttributeMetadata(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   133
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   134
				case OUTPUT_ATTRIBUTE_ALIAS:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   135
					processOutputAttributeAlias(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   136
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   137
				case OPTION:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   138
					processOption(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   139
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   140
				case INPUT_ATTRIBUTE:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   141
					processInputAttribute(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   142
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   143
				case WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   144
					processWaitingForOutputAttributesMetadata(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   145
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   146
				case WAITING_FOR_OUTPUT_ATTRIBUTES:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   147
					processWaitingForOutputAttributes(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   148
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   149
				case RELATION_END:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   150
					break OUTER;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   151
				default:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   152
					processUnsupportedMessage(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   153
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   154
			}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   155
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   156
	}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   158
	protected static class AttributeMetadata {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   160
		public String name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   161
		public Type type;
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   162
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   163
		public AttributeMetadata(String name, Type type) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   164
			this.name = name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   165
			this.type = type;
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   166
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	};
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   169
	protected static class Option {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   170
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   171
		public String name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   172
		public String value;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   173
		public List<String> nameMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   174
		public List<String> valueMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   175
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   176
		Option(String name, String value) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   177
			this.name = name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   178
			this.value = value;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   179
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   180
	}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   182
	protected List<String> versionsSupported = new LinkedList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   183
	protected List<AttributeMetadata> inputAttributes = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   184
	protected List<String> outputAttributeAliases = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   185
	protected List<Option> options = new LinkedList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   186
	protected String currentRelation;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   187
	protected String currentFile;
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   188
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   189
	protected static enum Type {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   190
		BOOLEAN,
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   191
		INTEGER,
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   192
		STRING
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   193
	}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   194
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   195
	protected void write(Message m) {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   196
		writeInt(m.code);
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
		writeInt(m.parameters.size());
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   198
		for (String p : m.parameters) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   199
			writeString(p);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   200
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   201
		flush();
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   204
	protected void processVersionSupported(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   205
		versionsSupported.add(m.parameters.get(0));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   208
	protected void processWaitingForVersion(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   209
		for (String v : versionsSupported) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   210
			if ("1".equals(v)) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   211
				write(new Message(VERSION_ACCEPTED, "1"));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   212
				return;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   213
			}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   214
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   215
		write(new Message(STREAMLET_ERROR, "INCOMPATIBLE_VERSION", "Only version 1 is supported by this streamlet."));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   216
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   218
	protected void processRelationStart(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   219
		currentRelation = m.parameters.get(0);
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   220
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   221
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   222
	protected void processInputAttributeMetadata(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   223
		inputAttributes.add(new AttributeMetadata(m.parameters.get(0), Type.valueOf(m.parameters.get(1).toUpperCase())));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   224
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   225
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   226
	protected void processOutputAttributeAlias(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   227
		outputAttributeAliases.add(m.parameters.get(0));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   229
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   230
	protected void processOption(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   231
		options.add(new Option(m.parameters.get(0), m.parameters.get(1)));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   232
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   233
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   234
	protected void processInputAttribute(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   235
		int index = Integer.valueOf(m.parameters.get(0));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   236
		String value = m.parameters.get(1);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   237
		boolean isNull = "true".equals(m.parameters.get(2));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   238
		if ("path".equals(inputAttributes.get(index).name)) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   239
			currentFile = value;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   240
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   241
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   242
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   243
	protected void processWaitingForOutputAttributesMetadata(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   244
		for (AttributeMetadata am : getOutputAttributesMetadata()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   245
			write(new Message(OUTPUT_ATTRIBUTE_METADATA, am.name, am.type.name().toLowerCase()));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   246
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   247
		write(new Message(WAITING_FOR_INPUT_ATTRIBUTES));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   248
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   249
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   250
	protected void processWaitingForOutputAttributes(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   251
		for (Object oa : getOutputAttributes()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   252
			write(new Message(OUTPUT_ATTRIBUTE, String.valueOf(oa), oa == null ? "true" : "false"));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   253
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   254
		write(new Message(WAITING_FOR_INPUT_ATTRIBUTES));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   255
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   256
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   257
	protected void processUnsupportedMessage(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   258
		write(new Message(STREAMLET_ERROR, "UNSUPPORTED_MESSAGE"));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   259
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   260
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   261
	protected String getAlias(int index, String defaultValue) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   262
		if (outputAttributeAliases.size() > index) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   263
			return outputAttributeAliases.get(index);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   264
		} else {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   265
			return defaultValue;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   266
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   267
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   268
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   269
	protected List<Option> getOptions(String name) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   270
		List<Option> result = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   271
		for (Option o : options) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   272
			if (Objects.equals(o.name, name)) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   273
				result.add(o);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   274
			}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   275
		}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   276
		return result;
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   277
	}
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   278
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   279
	protected List<Option> getOptions(Pattern namePattern) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   280
		List<Option> result = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   281
		for (Option o : options) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   282
			Matcher nameMatcher = namePattern.matcher(o.name);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   283
			if (nameMatcher.matches()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   284
				o.nameMatch = o.nameMatch == null ? new ArrayList<>() : o.nameMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   285
				copyMatches(nameMatcher, o.nameMatch);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   286
				result.add(o);
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   287
			}
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   288
		}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   289
		return result;
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   290
	}
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   291
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   292
	protected List<Option> getOptions(Pattern namePattern, Pattern valuePattern) {
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   293
		// TODO: support multiple modes: 
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   294
		//   a) throw an exception if valuePattern does not match
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   295
		//   b) return option even if valuePattern does not match (valueMatch will be empty)
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   296
		//   c) skip options with value not matching (current behavior)
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   297
		List<Option> result = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   298
		for (Option o : options) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   299
			Matcher nameMatcher = namePattern.matcher(o.name);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   300
			Matcher valueMatcher = valuePattern.matcher(o.value);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   301
			if (nameMatcher.matches() && valueMatcher.matches()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   302
				o.nameMatch = o.nameMatch == null ? new ArrayList<>() : o.nameMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   303
				o.valueMatch = o.valueMatch == null ? new ArrayList<>() : o.valueMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   304
				copyMatches(nameMatcher, o.nameMatch);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   305
				copyMatches(valueMatcher, o.valueMatch);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   306
				result.add(o);
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   307
			}
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   308
		}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   309
		return result;
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   310
	}
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   311
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   312
	protected abstract List<AttributeMetadata> getOutputAttributesMetadata();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   313
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   314
	protected abstract List<Object> getOutputAttributes();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   315
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   316
	public int run() {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   317
		try {
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   318
			processMessages();
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   319
			return 0;
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   320
		} catch (Exception e) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   321
			StringWriter stackTraceStringWriter = new StringWriter();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   322
			PrintWriter stackTracePrintWriter = new PrintWriter(stackTraceStringWriter);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   323
			e.printStackTrace(stackTracePrintWriter);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   324
			write(new Message(STREAMLET_ERROR, "xxxx", "Exception in streamlet: " + e.getClass().getName() + ": " + e.getLocalizedMessage() + "\n" + stackTraceStringWriter.toString())); // FIXME: correct error codes
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   325
			return 1;
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   326
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   327
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   328
}