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

com.predic8.membrane.core.util.TextUtil Maven / Gradle / Ivy

/* Copyright 2009, 2012 predic8 GmbH, www.predic8.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

package com.predic8.membrane.core.util;


import com.predic8.beautifier.HtmlBeautifierFormatter;
import com.predic8.beautifier.PlainBeautifierFormatter;
import com.predic8.beautifier.XMLBeautifier;
import com.predic8.beautifier.XMLBeautifierFormatter;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;

import static java.lang.Character.toUpperCase;
import static javax.xml.stream.XMLInputFactory.*;


public class TextUtil {
	private static final Logger log = LoggerFactory.getLogger(TextUtil.class.getName());

	private static final XMLInputFactory xmlInputFactory = newInstance();

	private static final char[] source;
	private static final String[] replace;

	static {
		source = new char[] {    '*',    '?',  '.',    '\\',      '(' ,    ')',    '+',      '|',    '^',     '$',    '%',       '@'    };
		replace = new String[] { ".*",   ".",  "\\.",  "\\\\",   "\\(",   "\\)",   "\\+",   "\\|",  "\\^",   "\\$",    "\\%",   "\\@"   };
	}


	public static String formatXML(Reader reader) {
		return formatXML(reader, false);
	}

	public static String formatXML(Reader reader, boolean asHTML) {
		StringWriter out = new StringWriter();

		try {
			XMLBeautifierFormatter formatter = asHTML ? new HtmlBeautifierFormatter(out, 0) : new PlainBeautifierFormatter(out, 0);
			XMLBeautifier beautifier = new XMLBeautifier(formatter);
			beautifier.parse(reader);
		}
		catch (Exception e){
			log.error("", e);
		} finally {
			try {
				out.close();
				reader.close();
			} catch (IOException e) {
				log.error("", e);
			}
		}
		return out.toString();
	}

	public static boolean isNullOrEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static String globToRegExp(String glob) {
		StringBuilder buf = new StringBuilder();
		buf.append("^");
		for(int i = 0; i < glob.length(); i ++) {
			appendReplacement(glob.charAt(i), buf);
		}
		buf.append("$");
		return buf.toString();
	}

	private static void appendReplacement(char c, StringBuilder buf) {
		for (int j = 0; j < source.length; j++) {
			if (c == source[j]) {
				buf.append(replace[j]);
				return;
			}
		}
		buf.append(c);
	}

	public static String toEnglishList(String conjuction, String... args) {
		ArrayList l = new ArrayList<>();
		for (String arg : args)
			if (arg != null && arg.length() > 0)
				l.add(arg);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < l.size(); i++) {
			sb.append(l.get(i));
			if (i == l.size() - 2) {
				sb.append(" ");
				sb.append(conjuction);
				sb.append(" ");
			}
			if (i < l.size() - 2)
				sb.append(", ");
		}
		return sb.toString();
	}

	public static Object capitalize(String english) {
		if (english.length() == 0)
			return "";
		return toUpperCase(english.charAt(0)) + english.substring(1);
	}



	static {
		xmlInputFactory.setProperty(IS_REPLACING_ENTITY_REFERENCES, false);
		xmlInputFactory.setProperty(IS_SUPPORTING_EXTERNAL_ENTITIES, false);
	}

	/**
	 * Checks whether s is a valid (well-formed and balanced) XML snippet.
	 * Note that attributes escaped by single quotes are accepted (which is illegal by spec).
	 */
	public static boolean isValidXMLSnippet(String s) {
		try {
			XMLEventReader parser;
			synchronized (xmlInputFactory) {
				parser = xmlInputFactory.createXMLEventReader(new StringReader("" + s + ""));
			}
			XMLEvent event = null;
			while (parser.hasNext()) {
				event = (XMLEvent) parser.next();
			}
			return event != null && event.isEndDocument();
		} catch (Exception e) {
			log.error("", e);
			return false;
		}
	}

	public static String linkURL(String url) {
		if (url.startsWith("http://") || url.startsWith("https://")) {
			url = StringEscapeUtils.escapeHtml4(url);
			return "" + url + "";
		}
		return StringEscapeUtils.escapeHtml4(url);
	}

	public static Object removeFinalChar(String s) {
		StringBuilder sb = new StringBuilder(s);
		if (sb.length() > 0)
			sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}

	/**
	 * Counts from 1 cause this is needed for getting lines from Javascript source code.
	 *
	 * @param s Multiline string
	 * @param lineNumber number of line to return. Counts from 1
	 * @return line
	 */
	public static String getLineFromMultilineString(String s,int lineNumber) {
		return s.split("\n")[lineNumber-1];
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy