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

📄 utils.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *
     * @param cms. The CmsObject.
     * @param changedLinks A vector of STrings with the links that have changed
     *       during the publishing.
     */
    public static void getModulPublishMethods(CmsObject cms, Vector changedLinks) throws CmsException{
        // now publish the module masters
        Vector publishModules = new Vector();
        cms.getRegistry().getModulePublishables(publishModules, cms.C_PUBLISH_METHOD_LINK);

        for(int i = 0; i < publishModules.size(); i++){
            // call the publishProject method of the class with parameters:
            // cms, changedLinks
            try{
                Class.forName((String)publishModules.elementAt(i)).getMethod("publishLinks",
                                        new Class[] {CmsObject.class, Vector.class}).invoke(
                                        null, new Object[] {cms, changedLinks});
            } catch(Exception ex){
            ex.printStackTrace();
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                    A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO, "Error when publish data of module "+(String)publishModules.elementAt(i)+"!: "+ex.getMessage());
                }
            }
        }
    }

    /**
     * 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(Exception 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
     * @exception 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.
     * @exception CmsException.
     */
    public static boolean isHttpsResource(CmsObject cms, CmsResource res) throws CmsException{
        while(!res.getAbsolutePath().equals(C_ROOT)){
            // check for the property export
            String prop = cms.readProperty(res.getAbsolutePath(), 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.
     * @param cms Cms Object for accessign files.
     * @param unsortedFiles Vector containing a list of unsorted files
     * @param sorting The sorting method to be used.
     * @return Vector of sorted CmsFile objects
     */

    public static Vector sort(CmsObject cms, Vector unsortedFiles, int sorting) {
        Vector v = new Vector();
        Enumeration enu = unsortedFiles.elements();
        CmsFile[] field = new CmsFile[unsortedFiles.size()];
        CmsFile file;
        String docloader;
        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(cms, 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_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(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);
                }
            }
        }
    }

}

⌨️ 快捷键说明

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