org.apache.camel.catalog.CatalogHelper Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.catalog;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.List;
public final class CatalogHelper {
private CatalogHelper() {
}
/**
* Loads the entire stream into memory as a String and returns it.
*
* Notice: This implementation appends a \n as line
* terminator at the of the text.
*
* Warning, don't use for crazy big streams :)
*/
public static void loadLines(InputStream in, List lines) throws IOException {
InputStreamReader isr = new InputStreamReader(in);
try {
BufferedReader reader = new LineNumberReader(isr);
while (true) {
String line = reader.readLine();
if (line != null) {
lines.add(line);
} else {
break;
}
}
} finally {
isr.close();
in.close();
}
}
/**
* Loads the entire stream into memory as a String and returns it.
*
* Notice: This implementation appends a \n as line
* terminator at the of the text.
*
* Warning, don't use for crazy big streams :)
*/
public static String loadText(InputStream in) throws IOException {
StringBuilder builder = new StringBuilder();
InputStreamReader isr = new InputStreamReader(in);
try {
BufferedReader reader = new LineNumberReader(isr);
while (true) {
String line = reader.readLine();
if (line != null) {
builder.append(line);
builder.append("\n");
} else {
break;
}
}
return builder.toString();
} finally {
isr.close();
in.close();
}
}
/**
* Matches the name with the pattern.
*
* @param name the name
* @param pattern the pattern
* @return true if matched, or false if not
*/
public static boolean matchWildcard(String name, String pattern) {
// we have wildcard support in that hence you can match with: file* to match any file endpoints
if (pattern.endsWith("*") && name.startsWith(pattern.substring(0, pattern.length() - 1))) {
return true;
}
return false;
}
/**
* Returns the string after the given token
*
* @param text the text
* @param after the token
* @return the text after the token, or null if text does not contain the token
*/
public static String after(String text, String after) {
if (!text.contains(after)) {
return null;
}
return text.substring(text.indexOf(after) + after.length());
}
/**
* Returns the string before the given token
*
* @param text the text
* @param before the token
* @return the text before the token, or null if text does not contain the token
*/
public static String before(String text, String before) {
if (!text.contains(before)) {
return null;
}
return text.substring(0, text.indexOf(before));
}
/**
* Returns the string between the given tokens
*
* @param text the text
* @param after the before token
* @param before the after token
* @return the text between the tokens, or null if text does not contain the tokens
*/
public static String between(String text, String after, String before) {
text = after(text, after);
if (text == null) {
return null;
}
return before(text, before);
}
}