001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.runtimecatalog;
018
019import java.io.BufferedReader;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.InputStreamReader;
023import java.io.LineNumberReader;
024import java.util.List;
025
026public final class CatalogHelper {
027
028    private CatalogHelper() {
029    }
030
031    /**
032     * Loads the entire stream into memory as a String and returns it.
033     * <p/>
034     * <b>Notice:</b> This implementation appends a <tt>\n</tt> as line
035     * terminator at the of the text.
036     * <p/>
037     * Warning, don't use for crazy big streams :)
038     */
039    public static void loadLines(InputStream in, List<String> lines) throws IOException {
040        try (final InputStreamReader isr = new InputStreamReader(in);
041            final BufferedReader reader = new LineNumberReader(isr)) {
042            String line;
043            while ((line = reader.readLine()) != null) {
044                lines.add(line);
045            }
046        }
047    }
048
049    /**
050     * Loads the entire stream into memory as a String and returns it.
051     * <p/>
052     * <b>Notice:</b> This implementation appends a <tt>\n</tt> as line
053     * terminator at the of the text.
054     * <p/>
055     * Warning, don't use for crazy big streams :)
056     */
057    public static String loadText(InputStream in) throws IOException {
058        StringBuilder builder = new StringBuilder();
059        try (final InputStreamReader isr = new InputStreamReader(in);
060            final BufferedReader reader = new LineNumberReader(isr)) {
061            String line;
062            while ((line = reader.readLine()) != null) {
063                builder.append(line);
064                builder.append("\n");
065            }
066            return builder.toString();
067        }
068    }
069
070    /**
071     * Matches the name with the pattern.
072     *
073     * @param name  the name
074     * @param pattern the pattern
075     * @return <tt>true</tt> if matched, or <tt>false</tt> if not
076     */
077    public static boolean matchWildcard(String name, String pattern) {
078        // we have wildcard support in that hence you can match with: file* to match any file endpoints
079        if (pattern.endsWith("*") && name.startsWith(pattern.substring(0, pattern.length() - 1))) {
080            return true;
081        }
082        return false;
083    }
084
085    /**
086     * Returns the string after the given token
087     *
088     * @param text  the text
089     * @param after the token
090     * @return the text after the token, or <tt>null</tt> if text does not contain the token
091     */
092    public static String after(String text, String after) {
093        if (!text.contains(after)) {
094            return null;
095        }
096        return text.substring(text.indexOf(after) + after.length());
097    }
098
099    /**
100     * Returns the string before the given token
101     *
102     * @param text  the text
103     * @param before the token
104     * @return the text before the token, or <tt>null</tt> if text does not contain the token
105     */
106    public static String before(String text, String before) {
107        if (!text.contains(before)) {
108            return null;
109        }
110        return text.substring(0, text.indexOf(before));
111    }
112
113    /**
114     * Returns the string between the given tokens
115     *
116     * @param text  the text
117     * @param after the before token
118     * @param before the after token
119     * @return the text between the tokens, or <tt>null</tt> if text does not contain the tokens
120     */
121    public static String between(String text, String after, String before) {
122        text = after(text, after);
123        if (text == null) {
124            return null;
125        }
126        return before(text, before);
127    }
128
129    /**
130     * Tests whether the value is <tt>null</tt> or an empty string.
131     *
132     * @param value  the value, if its a String it will be tested for text length as well
133     * @return true if empty
134     */
135    public static boolean isEmpty(Object value) {
136        return !isNotEmpty(value);
137    }
138
139    /**
140     * Tests whether the value is <b>not</b> <tt>null</tt> or an empty string.
141     *
142     * @param value  the value, if its a String it will be tested for text length as well
143     * @return true if <b>not</b> empty
144     */
145    public static boolean isNotEmpty(Object value) {
146        if (value == null) {
147            return false;
148        } else if (value instanceof String) {
149            String text = (String) value;
150            return text.trim().length() > 0;
151        } else {
152            return true;
153        }
154    }
155
156    /**
157     * Removes all leading and ending quotes (single and double) from the string
158     *
159     * @param s  the string
160     * @return the string without leading and ending quotes (single and double)
161     */
162    public static String removeLeadingAndEndingQuotes(String s) {
163        if (isEmpty(s)) {
164            return s;
165        }
166
167        String copy = s.trim();
168        if (copy.startsWith("'") && copy.endsWith("'")) {
169            return copy.substring(1, copy.length() - 1);
170        }
171        if (copy.startsWith("\"") && copy.endsWith("\"")) {
172            return copy.substring(1, copy.length() - 1);
173        }
174
175        // no quotes, so return as-is
176        return s;
177    }
178
179}