2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer;
23 import java.io.IOException;
24 import java.io.InputStream;
26 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextCharDelimitedParameters;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
31 * The class CharacterDelimitedTextBlockReader reads the next block of text between two character
32 * tags from an input stream.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
36 public class CharacterDelimitedTextBlockReader implements TextBlockReader {
37 // The logger for this class
38 private static final XLogger LOGGER = XLoggerFactory.getXLogger(CharacterDelimitedTextBlockReader.class);
41 private final char startTagChar;
42 private final char endTagChar;
44 // The input stream for text
45 private InputStream inputStream;
47 // Flag indicating we have seen EOF on the stream
48 private boolean eofOnInputStream = false;
51 * Constructor, set the delimiters.
53 * @param startTagChar The start tag for text blocks
54 * @param endTagChar The end tag for text blocks
56 public CharacterDelimitedTextBlockReader(final char startTagChar, final char endTagChar) {
57 this.startTagChar = startTagChar;
58 this.endTagChar = endTagChar;
62 * Constructor, set the delimiters from a character delimited event protocol parameter class.
64 * @param charDelimitedParameters the character delimited event protocol parameter class
66 public CharacterDelimitedTextBlockReader(final EventProtocolTextCharDelimitedParameters charDelimitedParameters) {
67 this.startTagChar = charDelimitedParameters.getStartChar();
68 this.endTagChar = charDelimitedParameters.getEndChar();
75 * org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlockReader#init(
76 * java.io.InputStream)
79 public void init(final InputStream incomingInputStream) {
80 this.inputStream = incomingInputStream;
86 * @see org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlockReader#
90 public TextBlock readTextBlock() throws IOException {
91 // Check if there was a previous end of a text block with a non-empty text block returned
92 if (eofOnInputStream) {
93 return new TextBlock(eofOnInputStream, null);
96 // The initial nesting level of incoming text blocks is always zero
99 // Holder for the text block
100 final StringBuilder textBlockBuilder = new StringBuilder();
102 // Read the next text block
104 final char nextChar = (char) inputStream.read();
107 if (nextChar == (char) -1) {
108 eofOnInputStream = true;
112 if (nextChar == startTagChar) {
114 } else if (nestingLevel == 0 && !Character.isWhitespace(nextChar)) {
115 LOGGER.warn("invalid input on consumer: " + nextChar);
119 textBlockBuilder.append(nextChar);
121 // Check for end of the text block, we have come back to level 0
122 if (nextChar == endTagChar) {
123 if (nestingLevel > 0) {
127 if (nestingLevel == 0) {
133 // Condition the text block and return it
134 final String textBlock = textBlockBuilder.toString().trim();
135 if (textBlock.length() > 0) {
136 return new TextBlock(eofOnInputStream, textBlock);
138 return new TextBlock(eofOnInputStream, null);