📄 util.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.util;
import java.awt.*;
import java.awt.font.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;
/**
* General Utilities
*
* @author Jorg Janke
* @version $Id: Util.java,v 1.23 2006/02/09 01:03:42 jjanke Exp $
*/
public class Util
{
/** Logger */
private static Logger log = Logger.getLogger(Util.class.getName());
/**
* Replace String values.
* @param value string to be processed
* @param oldPart old part
* @param newPart replacement - can be null or ""
* @return String with replaced values
*/
public static String replace (String value, String oldPart, String newPart)
{
if (value == null || value.length() == 0
|| oldPart == null || oldPart.length() == 0)
return value;
//
int oldPartLength = oldPart.length();
String oldValue = value;
StringBuffer retValue = new StringBuffer();
int pos = oldValue.indexOf(oldPart);
while (pos != -1)
{
retValue.append (oldValue.substring(0, pos));
if (newPart != null && newPart.length() > 0)
retValue.append(newPart);
oldValue = oldValue.substring(pos+oldPartLength);
pos = oldValue.indexOf(oldPart);
}
retValue.append(oldValue);
// log.fine( "Env.replace - " + value + " - Old=" + oldPart + ", New=" + newPart + ", Result=" + retValue.toString());
return retValue.toString();
} // replace
/**
* Remove CR / LF from String
* @param in input
* @return cleaned string
*/
public static String removeCRLF (String in)
{
char[] inArray = in.toCharArray();
StringBuffer out = new StringBuffer (inArray.length);
for (int i = 0; i < inArray.length; i++)
{
char c = inArray[i];
if (c == '\n' || c == '\r')
;
else
out.append(c);
}
return out.toString();
} // removeCRLF
/**
* Mask HTML content.
* i.e. replace characters with &values;
* @param content content
* @return masked content
*/
public static String maskHTML (String content)
{
if (content == null || content.length() == 0 || content.equals(" "))
return " ";
//
StringBuffer out = new StringBuffer();
char[] chars = content.toCharArray();
for (int i = 0; i < chars.length; i++)
{
char c = chars[i];
switch (c)
{
case '<': out.append("<"); break;
case '>': out.append(">"); break;
case '&': out.append("&"); break;
case '"': out.append("""); break;
case '\'': out.append("'"); break;
//
default:
int ii = (int)c;
if (ii > 255) // Write Unicode
out.append("&#").append(ii).append(";");
else
out.append(c);
break;
}
}
return out.toString();
} // maskHTML
/**
* Get the number of occurances of countChar in string.
* @param string String to be searched
* @param countChar to be counted character
* @return number of occurances
*/
public static int getCount (String string, char countChar)
{
if (string == null || string.length() == 0)
return 0;
int counter = 0;
char[] array = string.toCharArray();
for (int i = 0; i < array.length; i++)
{
if (array[i] == countChar)
counter++;
}
return counter;
} // getCount
/**
* Is String Empty
* @param str string
* @return true if >= 1 char
*/
public static boolean isEmpty (String str)
{
return (str == null || str.length() == 0);
} // isEmpty
/**************************************************************************
* Find index of search character in str.
* This ignores content in () and 'texts'
* @param str string
* @param search search character
* @return index or -1 if not found
*/
public static int findIndexOf (String str, char search)
{
return findIndexOf(str, search, search);
} // findIndexOf
/**
* Find index of search characters in str.
* This ignores content in () and 'texts'
* @param str string
* @param search1 first search character
* @param search2 second search character (or)
* @return index or -1 if not found
*/
public static int findIndexOf (String str, char search1, char search2)
{
if (str == null)
return -1;
//
int endIndex = -1;
int parCount = 0;
boolean ignoringText = false;
int size = str.length();
while (++endIndex < size)
{
char c = str.charAt(endIndex);
if (c == '\'')
ignoringText = !ignoringText;
else if (!ignoringText)
{
if (parCount == 0 && (c == search1 || c == search2))
return endIndex;
else if (c == ')')
parCount--;
else if (c == '(')
parCount++;
}
}
return -1;
} // findIndexOf
/**
* Find index of search character in str.
* This ignores content in () and 'texts'
* @param str string
* @param search search character
* @return index or -1 if not found
*/
public static int findIndexOf (String str, String search)
{
if (str == null || search == null || search.length() == 0)
return -1;
//
int endIndex = -1;
int parCount = 0;
boolean ignoringText = false;
int size = str.length();
while (++endIndex < size)
{
char c = str.charAt(endIndex);
if (c == '\'')
ignoringText = !ignoringText;
else if (!ignoringText)
{
if (parCount == 0 && c == search.charAt(0))
{
if (str.substring(endIndex).startsWith(search))
return endIndex;
}
else if (c == ')')
parCount--;
else if (c == '(')
parCount++;
}
}
return -1;
} // findIndexOf
/**************************************************************************
* Return Hex String representation of byte b
* @param b byte
* @return Hex
*/
static public String toHex (byte b)
{
char hexDigit[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
return new String(array);
}
/**
* Return Hex String representation of char c
* @param c character
* @return Hex
*/
static public String toHex (char c)
{
byte hi = (byte) (c >>> 8);
byte lo = (byte) (c & 0xff);
return toHex(hi) + toHex(lo);
} // toHex
/**************************************************************************
* Init Cap Words With Spaces
* @param in string
* @return init cap
*/
public static String initCap (String in)
{
if (in == null || in.length() == 0)
return in;
//
boolean capitalize = true;
char[] data = in.toCharArray();
for (int i = 0; i < data.length; i++)
{
if (data[i] == ' ' || Character.isWhitespace(data[i]))
capitalize = true;
else if (capitalize)
{
data[i] = Character.toUpperCase (data[i]);
capitalize = false;
}
else
data[i] = Character.toLowerCase (data[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -