📄 utils.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/util/Utils.java,v $
* Date : $Date: 2003/02/26 10:30:37 $
* Version: $Revision: 1.42 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.util;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.core.OpenCms;
import com.opencms.defaults.I_CmsLifeCycle;
import com.opencms.file.CmsFile;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsResource;
import com.opencms.file.CmsUser;
import com.opencms.file.I_CmsRegistry;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Vector;
/**
* This is a general helper class.
*
* @author Andreas Schouten
* @author Alexander Lucas <alexander.lucas@framfab.de>
*/
public class Utils {
/** Constant for sorting files upward by name */
public static final int C_SORT_NAME_UP = 1;
/** Constant for sorting files downward by name */
public static final int C_SORT_NAME_DOWN = 2;
/** Constant for sorting files upward by lastmodified date */
public static final int C_SORT_LASTMODIFIED_UP = 3;
/** Constant for sorting files downward by lastmodified date */
public static final int C_SORT_LASTMODIFIED_DOWN = 4;
/**
* This method makes the sorting desicion for the creation of index and archive pages,
* depending on the sorting method to be used.<p>
*
* @param cms Cms Object for accessign files.
* @param sorting The sorting method to be used.
* @param fileA One of the two CmsFile objects to be compared.
* @param fileB The second of the two CmsFile objects to be compared.
* @return <code>true</code> or <code>false</code>, depending if the two file objects have to be sorted.
* @throws CmsException Is thrown when file access failed.
*
*/
private static boolean compare(int sorting, CmsFile fileA, CmsFile fileB)
throws CmsException {
boolean cmp = false;
String titleA = fileA.getName();
String titleB = fileB.getName();
long lastModifiedA = fileA.getDateLastModified();
long lastModifiedB = fileB.getDateLastModified();
switch(sorting) {
case C_SORT_NAME_UP:
cmp = (titleA.compareTo(titleB) > 0);
break;
case C_SORT_NAME_DOWN:
cmp = (titleB.compareTo(titleA) > 0);
break;
case C_SORT_LASTMODIFIED_UP:
cmp = (lastModifiedA > lastModifiedB);
break;
case C_SORT_LASTMODIFIED_DOWN:
cmp = (lastModifiedA < lastModifiedB);
break;
default:
cmp = false;
}
return cmp;
}
/**
* Returns the AbsolutePath of an resource based on the base and the relative
* Path to the resource. There are three cases for the relative path:
* case 1: a / at the beginning of the relPath -> return the relPath
* case 2: one or more ../ at the beginning! of the relPath -> return the absolute path to the resource
* case 3: ./ or something else at the beginning -> return base + relpath (without the ./ of course)
*
* @param basePath The folder where the relativePath starts (or the absolute path of
* a file (no folder) in this path)
* @param relativePath The relative path to a reaource.
*/
public static String mergeAbsolutePath(String basePath, String relativePath){
if(relativePath == null || "".equals(relativePath)){
return basePath;
}
if(relativePath.startsWith("/") || basePath == null){
// case 1: a / at the beginning of the relPath -> return the relPath
return relativePath;
}
basePath = basePath.substring(0,basePath.lastIndexOf('/')+1);
String result = null;
if(relativePath.startsWith("./")){
// case 3 a: ./ at the beginning -> return base + relpath (without the ./ of course)
relativePath = relativePath.substring(2);
}
if(relativePath.startsWith("../")){
// case 2: one or more ../ at the beginning! of the relPath -> return the absolute path to the resource
int lastIndexOfSlash = relativePath.lastIndexOf("../");
int count = (lastIndexOfSlash / 3)+1;
int baseCount =basePath.lastIndexOf('/') -1;
for(int i=0; i<count; i++){
baseCount = basePath.lastIndexOf('/',baseCount) -1;
}
result = basePath.substring(0,baseCount+2) + relativePath.substring(lastIndexOfSlash+3);
}else{
// case 3b: something else at the beginning -> return base + relpath (without the ./ of course)
result = basePath + relativePath;
}
return result;
}
/**
* Returns a string representation of the full name of a user.
* @param user The user to get the full name from
* @return a string representation of the user fullname.
*/
public static String getFullName(CmsUser user) {
String retValue = "";
if(user != null) {
retValue += user.getFirstname() + " ";
retValue += user.getLastname() + " (";
retValue += user.getName() + ")";
}
return retValue;
}
/**
* Gets a formated time string form a long time value.
* @param time The time value as a long.
* @return Formated time string.
*/
public static String getNiceDate(long time) {
StringBuffer niceTime = new StringBuffer();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date(time));
String day = "0" + new Integer(cal.get(Calendar.DAY_OF_MONTH)).intValue();
String month = "0" + new Integer(cal.get(Calendar.MONTH) + 1).intValue();
String year = new Integer(cal.get(Calendar.YEAR)).toString();
String hour = "0" + new Integer(cal.get(Calendar.HOUR) + 12
* cal.get(Calendar.AM_PM)).intValue();
String minute = "0" + new Integer(cal.get(Calendar.MINUTE));
if(day.length() == 3) {
day = day.substring(1, 3);
}
if(month.length() == 3) {
month = month.substring(1, 3);
}
if(hour.length() == 3) {
hour = hour.substring(1, 3);
}
if(minute.length() == 3) {
minute = minute.substring(1, 3);
}
niceTime.append(day + ".");
niceTime.append(month + ".");
niceTime.append(year + " ");
niceTime.append(hour + ":");
niceTime.append(minute);
return niceTime.toString();
}
/**
* Gets a formated time string form a long time value.
* @param time The time value as a long.
* @return Formated time string.
*/
public static String getNiceShortDate(long time) {
StringBuffer niceTime = new StringBuffer();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date(time));
String day = "0" + new Integer(cal.get(Calendar.DAY_OF_MONTH)).intValue();
String month = "0" + new Integer(cal.get(Calendar.MONTH) + 1).intValue();
String year = new Integer(cal.get(Calendar.YEAR)).toString();
if(day.length() == 3) {
day = day.substring(1, 3);
}
if(month.length() == 3) {
month = month.substring(1, 3);
}
niceTime.append(day + ".");
niceTime.append(month + ".");
niceTime.append(year);
return niceTime.toString();
}
/**
*
* @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, CmsObject.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());
}
}
}
}
/**
* Calls the startup methode on all module classes that are registerd in the registry.
*
* @param cms. The CmsObject.
*/
public static void getModulStartUpMethods(CmsObject cms) throws CmsException{
Vector startUpModules = new Vector();
cms.getRegistry().getModuleLifeCycle(startUpModules);
for(int i = 0; i < startUpModules.size(); i++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -