001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.util;
029
030import org.opencms.util.CmsStringUtil;
031import org.opencms.util.CmsUUID;
032
033import com.google.gwt.core.client.JavaScriptObject;
034import com.google.gwt.user.client.Random;
035
036/**
037 * Additional string related helper methods.<p>
038 *
039 * @since 8.0.0
040 *
041 * @see org.opencms.util.CmsStringUtil
042 */
043public final class CmsClientStringUtil {
044
045    /**
046     * Prevent instantiation.<p>
047     */
048    private CmsClientStringUtil() {
049
050        // empty
051    }
052
053    /**
054     * Checks if the given string looks like a path or link to an SVG file.<p>
055     *
056     * @param path the path to check
057     *
058     * @return true if the path looks like a path or link to an SVG file
059     */
060    public static boolean checkIsPathOrLinkToSvg(String path) {
061
062        if (path == null) {
063            return false;
064        }
065        path = path.trim().toLowerCase();
066        int paramPos = path.indexOf("?");
067        if (paramPos >= 0) {
068            path = path.substring(0, paramPos);
069        }
070        return path.endsWith(".svg");
071    }
072
073    /**
074     * Returns the exception message.<p>
075     *
076     * @param t the exception to get the message for
077     *
078     * @return the exception message
079     */
080    public static String getMessage(Throwable t) {
081
082        String message = t.getLocalizedMessage();
083        if (message == null) {
084            message = t.getMessage();
085        }
086        if (message == null) {
087            message = t.getClass().getName();
088        }
089        return message;
090    }
091
092    /**
093     * Returns the stack trace of the Throwable as a string.<p>
094     *
095     * @param t the Throwable for which the stack trace should be returned
096     * @param separator the separator between the lines of the stack trace
097     *
098     * @return a string representing a stack trace
099     */
100    public static String getStackTrace(Throwable t, String separator) {
101
102        Throwable cause = t;
103        String result = "";
104        while (cause != null) {
105            result += getStackTraceAsString(cause.getStackTrace(), separator);
106            cause = cause.getCause();
107        }
108        return result;
109    }
110
111    /**
112     * Returns the stack trace as a string.<p>
113     *
114     * @param trace the stack trace
115     * @param separator the separator between the lines of the stack trace
116     *
117     * @return a string representing a stack trace
118     */
119    public static String getStackTraceAsString(StackTraceElement[] trace, String separator) {
120
121        String result = "";
122        for (StackTraceElement elem : trace) {
123            result += elem.toString();
124            result += separator;
125        }
126        return result;
127    }
128
129    /**
130     * The parseFloat() function parses a string and returns a float.<p>
131     *
132     * Only the first number in the string is returned. Leading and trailing spaces are allowed.
133     *
134     * @param str the string to be parsed
135     *
136     * @return the parsed number
137     */
138    public static native double parseFloat(String str) /*-{
139        var ret = parseFloat(str, 10);
140        if (isNaN(ret)) {
141            return 0;
142        }
143        return ret;
144    }-*/;
145
146    /**
147     * The parseInt() function parses a string and returns an integer.<p>
148     *
149     * Only the first number in the string is returned. Leading and trailing spaces are allowed.
150     * If the first character cannot be converted to a number, parseInt() returns zero.<p>
151     *
152     * @param str the string to be parsed
153     *
154     * @return the parsed number
155     */
156    public static native int parseInt(String str) /*-{
157        var ret = parseInt(str, 10);
158        if (isNaN(ret)) {
159            return 0;
160        }
161        return ret;
162    }-*/;
163
164    /**
165     * Pushes a String into a javascript array.<p>
166     *
167     * @param array the array to push the String into
168     * @param s the String to push into the array
169     */
170    public static native void pushArray(JavaScriptObject array, String s) /*-{
171        array.push(s);
172    }-*/;
173
174    /**
175     * Generates a purely random uuid.<p>
176     *
177     * @return the generated uuid
178     */
179    public static String randomUUID() {
180
181        String base = CmsUUID.getNullUUID().toString();
182        String hexDigits = "0123456789abcdef";
183        StringBuffer result = new StringBuffer();
184        for (int i = 0; i < base.length(); i++) {
185            char ch = base.charAt(i);
186            if (ch == '-') {
187                result.append(ch);
188            } else if (ch == '0') {
189                result.append(hexDigits.charAt(Random.nextInt(16)));
190            }
191        }
192        return result.toString();
193    }
194
195    /**
196     * Shortens the string to the given maximum length.<p>
197     *
198     * Will include HTML entity ellipses replacing the cut off text.<p>
199     *
200     * @param text the string to shorten
201     * @param maxLength the maximum length
202     *
203     * @return the shortened string
204     */
205    public static String shortenString(String text, int maxLength) {
206
207        if (text.length() <= maxLength) {
208            return text;
209        }
210        String newText = text.substring(0, maxLength - 1);
211        if (text.startsWith("/")) {
212            // file name?
213            newText = CmsStringUtil.formatResourceName(text, maxLength);
214        } else if (maxLength > 2) {
215            // enough space for ellipsis?
216            newText += CmsDomUtil.Entity.hellip.html();
217        }
218        if (CmsStringUtil.isEmptyOrWhitespaceOnly(newText)) {
219            // if empty, it could break the layout
220            newText = CmsDomUtil.Entity.nbsp.html();
221        }
222        return newText;
223    }
224
225}