⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            try{
                I_CmsLifeCycle lifeClass = (I_CmsLifeCycle)Class.forName((String)startUpModules.elementAt(i)).getConstructor(new Class[] {}).newInstance(new Class[] {});
                lifeClass.startUp(cms);
            } catch(Exception ex){
            }
        }
    }

    /**
     * Calls the startup methode on all module classes that are registerd in the registry.
     *
     * @param cms. The CmsObject.
     */
    public static void getModulShutdownMethods(I_CmsRegistry reg) throws CmsException{

        Vector startUpModules = new Vector();
        reg.getModuleLifeCycle(startUpModules);
        for(int i = 0; i < startUpModules.size(); i++){
            try{
                I_CmsLifeCycle lifeClass = (I_CmsLifeCycle)Class.forName((String)startUpModules.elementAt(i)).getConstructor(new Class[] {}).newInstance(new Class[] {});
                lifeClass.shutDown();
            } catch(Exception ex){
            }
        }
    }

    /**
     * Gets the stack-trace of a exception, and returns it as a string.
     * @param e The exception to get the stackTrace from.
     * @return the stackTrace of the exception.
     */
    public static String getStackTrace(Throwable e) {

        // print the stack-trace into a writer, to get its content
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        e.printStackTrace(writer);
        if(e instanceof CmsException) {
            CmsException cmsException = (CmsException)e;
            if(cmsException.getException() != null) {
                cmsException.getException().printStackTrace(writer);
            }
        }
        try {
            writer.close();
            stringWriter.close();
        }
        catch(Exception err) {


        // ignore
        }
        return stringWriter.toString();
    }

    /**
     * Replaces all line breaks in a given string object by
     * white spaces. All lines will be <code>trim</code>ed to
     * delete all unnecessary white spaces.
     * @param s Input string
     * @return Output String
     * @throws CmsException
     */

    public static String removeLineBreaks(String s) throws CmsException {
        StringBuffer result = new StringBuffer();
        BufferedReader br = new BufferedReader(new StringReader(s));
        String lineStr = null;
        try {
            while((lineStr = br.readLine()) != null) {
                result.append(lineStr.trim());
                result.append(" ");
            }
        }
        catch(IOException e) {
            throw new CmsException("Error while reading input stream in com.opencms.util.Utils.removeLineBreaks: " + e);
        }
        return result.toString();
    }

    /**
     * Checks if a resource needs the https scheme. Thats the case if the resource
     * itself or a parent folder has the property 'export' set to 'https'.
     *
     * @param cms The cms Object, used for reading the parent folder and the properties.
     * @param res The resource to be checked.
     * @throws CmsException.
     */
    public static boolean isHttpsResource(CmsObject cms, CmsResource res) throws CmsException{
        while(!res.getAbsolutePath().equals(I_CmsConstants.C_ROOT)){
            // check for the property export
            String prop = cms.readProperty(res.getAbsolutePath(), I_CmsConstants.C_PROPERTY_EXPORT);
            if((prop != null) && "https".equalsIgnoreCase(prop)){
                // found one
                return true;
            }
            res = cms.readFileHeader(res.getParent());
        }
        return false;
    }

    /**
     * Sorts a Vector of CmsFile objects according to an included sorting method.<p>
     * 
     * @param unsortedFiles Vector containing a list of unsorted files
     * @param sorting The sorting method to be used.
     * @return Vector of sorted CmsFile objects
     * @deprecated this method is deprecated and will be removed in a later OpenCms release
     */
    public static Vector sort(Vector unsortedFiles, int sorting) {
        Vector v = new Vector();
        Enumeration enu = unsortedFiles.elements();
        CmsFile[] field = new CmsFile[unsortedFiles.size()];
        CmsFile file;
        int max = 0;
        try {

            // create an array with all unsorted files in it. This arre is later sorted in with
            // the sorting algorithem.
            while(enu.hasMoreElements()) {
                file = (CmsFile)enu.nextElement();
                field[max] = file;
                max++;
            }

            // Sorting algorithm
            // This method uses an insertion sort algorithem
            int in, out;
            int nElem = max;
            for(out = 1;out < nElem;out++) {
                CmsFile temp = field[out];
                in = out;
                while(in > 0 && compare(sorting, field[in - 1], temp)) {
                    field[in] = field[in - 1];
                    --in;
                }
                field[in] = temp;
            }

            // take sorted array and create a new vector of files out of it
            for(int i = 0;i < max;i++) {
                v.addElement(field[i]);
            }
        }
        catch(Exception e) {
            if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_CRITICAL) ) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_CRITICAL, "[Utils] :" + e.toString());
            }
        }
        return v;
    }

    /**
     * This method splits a overgiven string into substrings.
     *
     * @param toSplit the String to split.
     * @param at the delimeter.
     *
     * @return an Array of Strings.
     */
    public static final String[] split(String toSplit, String at) {
        Vector parts = new Vector();
        int index = 0;
        int nextIndex = toSplit.indexOf(at);
        while(nextIndex != -1) {
            parts.addElement((Object)toSplit.substring(index, nextIndex));
            index = nextIndex + at.length();
            nextIndex = toSplit.indexOf(at, index);
        }
        parts.addElement((Object)toSplit.substring(index));
        String partsArray[] = new String[parts.size()];
        parts.copyInto((Object[])partsArray);
        return (partsArray);
    }

    /**
     * This method replaces all occurences of the replaceKey in the toReplace string with the replaceWith String.
     *
     * @param toReplace the String to replace something in.
     * @param replaceKey the String that will be replaced.
     * @param replaceWith The string that is inserted in the place marked with the replaceKey.
     *
     * @return String.
     */
    public static final String replace(String toReplace, String replaceKey, String replaceWith) {
        if(toReplace == null){
            return null;
        }
        StringBuffer retValue = new StringBuffer();

        int index = 0;
        int nextIndex = toReplace.indexOf(replaceKey);
        while(nextIndex != -1) {
            retValue.append(toReplace.substring(index, nextIndex))
                    .append(replaceWith );
            index = nextIndex + replaceKey.length();
            nextIndex = toReplace.indexOf(replaceKey, index);
        }
        retValue.append(toReplace.substring(index));
        return retValue.toString();
    }

    /**
     * Converts date string to a long value.
     * @param dateString The date as a string.
     * @return long value of date.
     */

    public static long splitDate(String dateString) {
        long result = 0;
        if(dateString != null && !"".equals(dateString)) {
            String splittetDate[] = Utils.split(dateString, ".");
            GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(splittetDate[2]),
                    Integer.parseInt(splittetDate[1]) - 1, Integer.parseInt(splittetDate[0]), 0, 0, 0);
            result = cal.getTime().getTime();
        }
        return result;
    }

    /**
     * Sorts two vectors using bubblesort. This is a quick hack to display templates sorted by title instead of
     * by name in the template dropdown, because it is the title that is shown in the dropdown.
     * Creation date: (10/24/00 13:55:12)
     * @param names The vector to sort
     * @param data Vector with data that accompanies names.
     */

    public static void bubblesort(Vector names, Vector data) {
        for(int i = 0;i < names.size() - 1;i++) {
            int len = names.size() - i - 1;
            for(int j = 0;j < len;j++) {
                String a = (String)names.elementAt(j);
                String b = (String)names.elementAt(j + 1);
                if(a.toLowerCase().compareTo(b.toLowerCase()) > 0) {
                    names.setElementAt(a, j + 1);
                    names.setElementAt(b, j);
                    a = (String)data.elementAt(j);
                    data.setElementAt(data.elementAt(j + 1), j);
                    data.setElementAt(a, j + 1);
                }
            }
        }
    }

    /**
     * This method checks if a new password sticks to the rules for
     * new passwords (i.e. a new password must have at least 4 characters).
     * For this purpose a class defined in the opencms.properties is called.
     * If this class throws no exception the password is ok. The default class
     * only checks for the min 4 characters rule.
     *
     * @param cms The CmsObject.
     * @param password The new password that has to be checked.
     * @param oldPassword The old password or null if not needed.
     *
     * @throws CmsException is thrown if the password is not valid.
     */
    public static void validateNewPassword(CmsObject cms, String password, String oldPassword)throws CmsException{

        // first get the class from the properties
        String className = OpenCms.getPasswordValidatingClass();
        try{
            I_PasswordValidation pwClass = (I_PasswordValidation)Class.forName(className).getConstructor(new Class[] {}).newInstance(new Class[] {});
            pwClass.checkNewPassword(cms, password, oldPassword);
        }catch(Exception e){
            if(e instanceof CmsException){
                throw (CmsException)e;
            }else{
                throw new CmsException("could not validate password with class:"+className,
                            CmsException.C_UNKNOWN_EXCEPTION, e);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -