bd7310d0a539273e944b9f0bdb352b98849b1e78
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextCharDelimitedParameters;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * The class CharacterDelimitedTextBlockReader reads the next block of text between two character
32  * tags from an input stream.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class CharacterDelimitedTextBlockReader implements TextBlockReader {
37     // The logger for this class
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(CharacterDelimitedTextBlockReader.class);
39
40     // The character tags
41     private final char startTagChar;
42     private final char endTagChar;
43
44     // The input stream for text
45     private InputStream inputStream;
46
47     // Flag indicating we have seen EOF on the stream
48     private boolean eofOnInputStream = false;
49
50     /**
51      * Constructor, set the delimiters.
52      *
53      * @param startTagChar The start tag for text blocks
54      * @param endTagChar The end tag for text blocks
55      */
56     public CharacterDelimitedTextBlockReader(final char startTagChar, final char endTagChar) {
57         this.startTagChar = startTagChar;
58         this.endTagChar = endTagChar;
59     }
60
61     /**
62      * Constructor, set the delimiters from a character delimited event protocol parameter class.
63      *
64      * @param charDelimitedParameters the character delimited event protocol parameter class
65      */
66     public CharacterDelimitedTextBlockReader(final EventProtocolTextCharDelimitedParameters charDelimitedParameters) {
67         this.startTagChar = charDelimitedParameters.getStartChar();
68         this.endTagChar = charDelimitedParameters.getEndChar();
69     }
70
71     /*
72      * (non-Javadoc)
73      * 
74      * @see
75      * org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlockReader#init(
76      * java.io.InputStream)
77      */
78     @Override
79     public void init(final InputStream incomingInputStream) {
80         this.inputStream = incomingInputStream;
81     }
82
83     /*
84      * (non-Javadoc)
85      * 
86      * @see org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlockReader#
87      * readTextBlock()
88      */
89     @Override
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);
94         }
95
96         // Read the block of text
97         final StringBuilder textBlockBuilder = readTextBlockText();
98
99         // Condition the text block and return it
100         final String textBlock = textBlockBuilder.toString().trim();
101         if (textBlock.length() > 0) {
102             return new TextBlock(eofOnInputStream, textBlock);
103         } else {
104             return new TextBlock(eofOnInputStream, null);
105         }
106     }
107
108     /**
109      * Read a block of text.
110      * @return A string builder containing the text
111      * @throws IOException on read errors
112      */
113     private StringBuilder readTextBlockText() throws IOException {
114         // Holder for the text block
115         final StringBuilder textBlockBuilder = new StringBuilder();
116
117         int nestingLevel = 0;
118         
119         // Read the next text block
120         while (true) {
121             final char nextChar = (char) inputStream.read();
122
123             // Check for EOF
124             if (nextChar == (char) -1) {
125                 eofOnInputStream = true;
126                 return textBlockBuilder;
127             }
128
129             if (nextChar == startTagChar) {
130                 nestingLevel++;
131             } else if (nestingLevel == 0 && !Character.isWhitespace(nextChar)) {
132                 LOGGER.warn("invalid input on consumer: {}", nextChar);
133                 continue;
134             }
135
136             textBlockBuilder.append(nextChar);
137
138             // Check for end of the text block, we have come back to level 0
139             if (nextChar == endTagChar) {
140                 if (nestingLevel > 0) {
141                     nestingLevel--;
142                 }
143
144                 if (nestingLevel == 0) {
145                     return textBlockBuilder;
146                 }
147             }
148         }
149     }
150 }