All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.joinedworkz.common.helper.StringHelper.xtend Maven / Gradle / Ivy

There is a newer version: 1.3.46
Show newest version
package org.joinedworkz.common.helper

import javax.inject.Singleton
import java.util.List
import java.util.Collections

@Singleton
class StringHelper {

    public static final char NL = '\n'
    public static final char CR = '\r'
    public static final char BRACKET_OPEN = '('
    public static final char BRACKET_CLOSE = ')'
    

    def boolean isNewLineChar(char c) {
        return c == NL || c == CR
    }

    def boolean beginsLineChar(StringBuilder buffer) {
        return buffer.length == 0 || isNewLineChar(buffer.charAt(buffer.length - 1))
    }
    
    /**
     * Replaces the occurrence of {@code begin} in a string with {@code replacement}.
     * In case the string {@code str is null or empty} the original value is returned
     */
    def String replaceBegin(String str, String begin, String replacement) {
        if (str !== null && begin !== null) {
            val beginLength = begin.length;
            if (beginLength <= str.length && str.startsWith(begin)) {
                if (replacement !== null && replacement.length > 0) {
                    val buffer = new StringBuilder()
                    buffer.append(replacement)
                    buffer.append(str.substring(beginLength));
                    return buffer.toString
                } else {
                    return begin.substring(beginLength);
                }
            }
        }
    }

    def trimLastLineBreak(String str) {
        if (str !== null) {
            var totalCount = 0
            var crCount = 0
            var nlCount = 0
            for (var i = str.length - 1; i >= 0; i--) {
                totalCount++
                val c = str.charAt(i)
                if (c == CR) {
                    crCount++
                }
                if (c == NL) {
                    nlCount++
                }
                if (totalCount > crCount + nlCount) {
                    if (totalCount == 1) {
                        return str
                    } else {
                        return str.substring(0, str.length - totalCount + 1)
                    }
                }

            }

        }
    }
    
   	def textInLineAfterMarker(String string, String markerString) {
		if (string !== null) {
			val markerIndex = string.indexOf(markerString)
			if (markerIndex >= 0) {
				return contentOfLineAfter(string, markerString.length + 1)
			}
		}
	}
	


    def String contentOfLineBefore(String str, int index) {
        for (var i = index; i >= 0; i--) {
            val c = str.charAt(i)
            if (c == CR || c == NL) {
                return str.substring(i + 1, index);
            }
        }
        return str
    }

    def String contentOfLineAfter(String str, int index) {
        val strLength = str.length
        for (var i = index; i < strLength; i++) {
            val c = str.charAt(i)
            if (c == CR || c == NL) {
                return str.substring(index, i);
            }
        }
        return str.substring(index)
    }
    
    def int indexOfLineBreakAfter(String str, int index) {
        val strLength = str.length
        for (var i = index; i < strLength; i++) {
            val c = str.charAt(i)
            if (c == CR || c == NL) {
                return i;
            }
        }
        return -1;
    }

    def boolean containsLineBreak(String str) {
        if (str !== null) {
            return str.indexOf(CR) >= 0 || str.indexOf(NL) >= 0
        } else {
            return false
        }
    }
    
    def insertLinesOfString(StringBuilder buffer, String str, String indention) {
        insertLinesOfString(buffer, str, indention, true)
    }

    def insertLinesOfString(StringBuilder buffer, String str, String indention, boolean ignoreBlankString) {

        if (ignoreBlankString) {
            val hasContent = str.trim.length > 0
            if (!hasContent) {
                return
            }
        }

        val lines = str.split("\\r?\\n")
        val lastChar = str.charAt(str.length - 1)

        val hasIndention = indention.length > 0
        val int lineCount = lines.length
        val int lastLine = lineCount - 1
        for (var int i = 0; i < lineCount; i++) {
            val line = lines.get(i)
            if (hasIndention && beginsLineChar(buffer)) {
                buffer.append(indention)
            }
            buffer.append(line)
            if (i < lastLine || isNewLineChar(lastChar)) {
                addLineBreak(buffer)
            }
        }
        
        if (!ignoreBlankString && lineCount == 0 && (str.contains("\r") || str.contains("\n")) ) {
            buffer.append(str);
        }
    }
    
    def addLineBreak(StringBuilder buffer) {
        buffer.append("\r\n")
    }
    
    def int indexOfKeywordOnSameLevel(int startIndex, String sql, String keyword) {
    	
    	/* remaining string is empty*/
    	if (startIndex == sql.length() -1 ) {
    		return -1;
    	}
    	
    	val String[] parts = sql.substring(startIndex).split(keyword);
    	
    	/* substring does not contain any 'FROM' */
    	if (parts.length == 1) {
    		return -1;
    	}
    	var int index = startIndex;
    	var int subLevel = 0;
    	for (String part : parts) {
    		val int partLength = part.length();
    		for (var int i = 0;i splitByCharacterOnSameLevel(String sql, char splitBy) {

		if (sql === null) {
			return Collections.emptyList();
		}
		if (sql.indexOf(splitBy) === -1) {
			return List.of(sql);
		}
    	
    	val parts = newArrayList
    	var int begin = 0;
    	var int subLevel = 0;
    	var index = 0;
    	val length = sql.length
    	for (;index < length;index++) {
    		val char c = sql.charAt(index);
		    if (c == BRACKET_OPEN) {
				subLevel++;
			} else if (c === BRACKET_CLOSE) {
				subLevel--;
			}
			if (subLevel === 0 && c === splitBy) {
				
				val part = sql.substring(begin, index);
				parts.add(part)
				begin = index + 1
			}
    	}
    	
    	val lastPart = sql.substring(begin, index)
    	parts.add(lastPart)
    	
    	return parts
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy