123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- package com.sinosoft.cm.common;
- import java.io.IOException;
- import java.io.Reader;
- import java.sql.SQLException;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- import com.sinosoft.common.excel.JsonPluginsUtil;
- import net.sf.json.JSONObject;
- import nl.justobjects.pushlet.util.Sys;
- import flex.messaging.io.ArrayList;
- public class StringUtils {
- public StringUtils() {
-
- }
- /**
- * 将clob转换为字符串
- * @param clob
- * @return
- * @throws SQLException
- */
- public static String clobToString(java.sql.Clob clob) throws SQLException{
- if(clob == null) {
- return null;
- }
- Reader inStreamDoc = clob.getCharacterStream();
- char[] tempDoc = new char[(int) clob.length()];
- try {
- inStreamDoc.read(tempDoc);
- inStreamDoc.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return new String(tempDoc);
- }
-
- public static String listToQueryIn(List<String> list) throws Exception{
- if(list==null||0==list.size())throw new Exception("List集合不能为空或者长度为0");
- StringBuffer sb=new StringBuffer();
- for(int i=0;i<list.size();i++){
- if(0==i){
- sb.append("'"+list.get(i)+"'");
- }else{
- sb.append(", '"+list.get(i)+"'");
- }
- }
- return sb.toString();
- }
-
-
- /**
- * 将list集合转换为map集合
- * @param list
- * @return
- * @throws Exception
- */
- public static Map<String,List<String>> listMapToMapList(List<Map<String,String>> list) throws Exception{
- Map<String,List<String>> map=new HashMap<String, List<String>>();
- if(null==list)throw new Exception("listMapToMapList(List<Map<String,String>> list):参数为空或者长度为0;");
- for(String listName : list.get(0).keySet()){
- map.put(listName, new ArrayList());
- }
- for(Map<String,String> listItem : list){
- for(String listItemKey:listItem.keySet() ){
- map.get(listItemKey).add(listItem.get(listItemKey));
- }
- }
- return map;
- }
-
- public static String listToQueryInString(List<String> list){
- StringBuffer sb=new StringBuffer();
- for(String str : list){
- sb.append("'"+new StringBuffer(str)+"',");
- }
- sb.delete(sb.length()-1, sb.length());
- return sb.toString();
- }
- public static List<String> stringToList(String str,String splite){
- String spliteStr=str.substring(1, str.lastIndexOf("'"));
- String[] bb=spliteStr.split(splite);
- List<String> list=new ArrayList();
- for(int i=0,len=bb.length;i<len;i++){
- list.add(bb[i]);
- }
- //System.out.println("stringToList="+list);
- return list;
- }
- /**
- * 将返回结果转换为json对象
- * @param state
- * @param count
- * @param data
- * @return
- */
- public static String resultToJSON(String state,int count ,String data){
- return "{\"state\":\""+state+"\",\"total\":"+count+",\"rows\":"+data+"}";
- }
-
- /**
- * 将返回结果转换为json对象
- * @param state
- * @param count
- * @param data
- * @return
- */
- public static String resultToJSONSum(String state,int count , String sum,String unsum,String data){
- return "{\"state\":\""+state+"\",\"total\":"+count+",\"sum\":"+sum+",\"unsum\":"+unsum+",\"rows\":"+data+"}";
- }
-
- /**
- * 处理汇总的结果
- * @param state
- * @param count
- * @param data
- * @param sum
- * @return
- */
- public static String resultToJSON(String state,int count ,String data,String sum){
- return "{\"state\":\""+state+"\",\"total\":"+count+",\"rows\":"+data+",\"data\":"+sum+"}";
- }
-
- public static String ToJson(Map<String,Map<String,String>> map){
- StringBuffer sb=new StringBuffer("{");
- for(String key:map.keySet()){
- if(map.get(key) instanceof Map){
- sb.append("\""+key+"\":").append(mapToJson(map.get(key))).append(",");
- }else{
- sb.append("\""+key+"\":\"").append(map.get(key)).append("\",");
- }
- }
- sb.deleteCharAt(sb.lastIndexOf(","));
- sb.append("}");
- return sb.toString();
- }
-
- /* public static void main(String[] args) {
- Map<String ,Map<String,String>> map=new HashMap<String, Map<String,String>>();
- for(int index=0;index<10;index++){
- Map<String,String> map1=new HashMap<String, String>();
- map1.put("avc"+index, "abcd");
- map1.put("avw"+index, "abcd");
- map.put("aaaa"+index, map1);
-
- }
- String str=ToJson(map);
- System.out.println("======"+str);
- }*/
- /**
- * 将map<String,List<Map<String,String>>>集合转换为json对象;
- * @param map
- * @return
- */
- /**
- * 将map<String,List<Map<String,String>>>集合转换为json对象;
- * @param map
- * @return
- */
- public static List<String> MapToJson(Map<String,List<Map<String,String>>> map){
- List<String> list=new ArrayList();
- for(String strName : map.keySet()){
- List<String> strList=listToJson(map.get(strName));
- String str="{\"name\":\""+strName+"\" ,\"rows\":"+ listToJson(strList)+"}";
-
- list.add(str);
- }
- //System.out.println("MapToJson="+list);
- return list;
- }
- public static Map<String,List<Map<String,String>>> packetMap(List<Map<String,String>> list,String packetKey){
- Map<String,List<Map<String,String>>> mapList=new HashMap<String, List<Map<String,String>>>();
- for(Map<String,String> map:list){
- if(!mapList.containsKey(map.get(packetKey))){
- mapList.put(map.get(packetKey), new ArrayList());
- }
- mapList.get(map.get(packetKey)).add(map);
- }
- return mapList;
- }
- /**
- * 将字符串集合转换为JSON字符串
- * @param list
- * @return
- */
- public static String listToJson(List<String> list){
- StringBuffer sb=new StringBuffer("[");
- for(String str : list){
- sb.append(new StringBuffer(str)).append(",");
- }
- if(sb.length()>1){
- sb.delete(sb.length()-1,sb.length());
- }
- sb.append("]");
- //System.out.println("listToJson sb.toString()="+sb.toString());
- return sb.toString();//JsonPluginsUtil.beanListToJson(list);
- }
- /**
- * 将list集合
- * @param list
- * @return
- */
- public static List<String> listToJson(List<Map<String,String>> list){
- List<String> listJson=new ArrayList();
- for(Map<String,String> map : list){
- listJson.add(mapToJson(map));
- //System.out.println("mapInlistToJson="+listJson);
- }
- //System.out.println("listToJson="+listJson);
- return listJson;
- }
- /**
- * 将map转换为json字符串
- * @param map
- * @return
- */
- public static String mapToJson(Map<String,String> map){
- StringBuffer sb=new StringBuffer("{");
- for(String key:map.keySet()){
- sb.append("\""+new StringBuffer(key)+"\":").append("\""+new StringBuffer(map.get(key))+"\",");
- }
- sb.deleteCharAt(sb.length()-1);
- sb.append("}");
- //System.out.println("mapToJson="+sb.toString());
- return sb.toString();
- }
- /**
- *
- * @param sql
- * @return
- */
- public static List<String> stringToList2(String str,String split){
- List<String> list=new ArrayList();
- if(!str.contains(split)){
- list.add(str);
- }else{
- String[] strArr=str.split(split);
- Collections.addAll(list,strArr);
- }
- return list;
- }
- /**
- * 将sql语句截取会可接受查询结果集的
- * @param str
- * @param split
- * @return
- */
- public static List<String> sqlStringToListLabel(String str,String split){
- List<String> list=new ArrayList();
- str=subSqlLabel(str.trim());
- String[] strArr=str.split(split);
- getSqlLabelFromArr(list,strArr);
- return list;
- }
- /**
- * 将查询字段数组转化为可接受查询结果集字段
- * @param list
- * @param arr
- * @return
- */
- public static List<String> getSqlLabelFromArr(List<String> list, String[] arr){
- for(int index=0,len=arr.length;index<len;index++){
- String temStr=arr[index].trim();
- if(temStr.contains(" ")){
- temStr=temStr.substring(temStr.lastIndexOf(" ")).trim();
- }
- list.add(temStr);
- }
- return list;
- }
- /**
- * 截取sql语句中的查询字段
- * @param sqlString
- * @return
- */
- public static String subSqlLabel(String sqlString){
- String regex="(SELECT)|(FROM)";
- String str=sqlString.replaceAll(regex, "|").trim();
- str=str.substring(str.indexOf("|")+1).trim();
- str=str.substring(0,str.indexOf("|")).trim();
- return str;
- }
-
- /**
- *
- * @param str 需要被替换的字符串
- * @param reg 被替换的字符
- * @param list 替换字符串集合
- * @return
- * @throws Exception
- */
- public static String replace(String str,String reg, List<String> list) throws Exception {
- if(list==null)
- throw new Exception("集合长度为空");
- System.out.println(str.split(reg).length+":"+list.size());
- if((str.split(reg.replace("\\", "")).length-1)!=list.size())
- throw new Exception("替换长度不匹配");
- for(int index=0,len=list.size();index<len;index++){
- str=str.replaceFirst(reg, list.get(index));
- }
- return str;
- }
-
- public static String replace(String str,String reg ,String[] arr) throws Exception{
- if(arr==null)
- throw new Exception("数组为空");
- /* if(str.split(reg.replace("\\?", "")).length-1!=arr.length)
- throw new Exception("替换长度不匹配");*/
- for(int index=0,len=arr.length;index<len;index++){
- str=str.replaceFirst(reg, arr[index]);
- }
- return str;
- }
-
-
- /**
- *
- * @param list 键值对
- * @param columnLabel 拼接的字段
- * @param insertStr 插入的字符串
- * @return
- */
- public static String mapSingleColumToString(List<Map<String,String>> list,String columnLabel,String insertStr) {
- StringBuffer sb=new StringBuffer();
- if(list.size()==0 || columnLabel==null || columnLabel.toUpperCase()=="NULL"|| "".equals(columnLabel))
- return null;
- if(list.size()==0)
- return list.get(0).get(columnLabel);
- for(int index=0,len=list.size();index<len;index++){
- sb.append(list.get(index).get(columnLabel)).append(new StringBuffer(insertStr));
- }
- sb.delete(sb.length()-insertStr.length(), sb.length());
- return sb.toString();
- }
-
- /**
- *
- * @param list 拼接的集合
- * @param insertStr 插入的字符串
- * @return
- */
- public static String listToString(List<String> list,String insertStr) {
- if(list==null)
- return null;
- if(list.size()==0)
- return list.get(0);
- StringBuffer sb=new StringBuffer();
- for(int index=0,len=list.size();index<len;index++){
- sb.append(list.get(index)).append(new StringBuffer(insertStr));
- }
- sb.delete(sb.length()-insertStr.length(), sb.length());
- return sb.toString();
- }
-
- public static String arrayToString (String[] arr,String insertStr){
- if(arr==null)return null;
- if(arr.length==0) return null;
- StringBuffer sb=new StringBuffer();
- for(int index=0,len=arr.length;index<len;index++){
- sb.append(arr[index]).append(insertStr);
- }
- sb.delete(sb.length()-insertStr.length(), sb.length());
- return sb.toString();
- }
-
- public static String getFdObjectId(){
- return UUID.randomUUID().toString().replace("-", "");
- }
-
-
-
- /**
- *
- * CREATE OR REPLACE PROCEDURE search(SQL_STRING varchar2,NUM_START varchar2,NUM_END varchar2)AS
- DECLARE
- SQL_STR VARCHAR2:=REGEXP_REPLACE(SQL_STRING,'SELECT',
- 'SELECT ('||
- REGEXP_REPLACE(SQL_STRING,'(SELECT)[^\\(|\\)]*(\\([^\\(|\\)]*\\))*[^\\(|\\)]*(FROM)','SELECT COUNT(*) FROM',1,1)
- ||') TOTAL, ',1,1
- )||' LIMIT ?,?;';
- BEGIN
- EXECUTE IMMEDIATE SQL_STR USING NUM_START,NUM_END;
- END ;
- CALL search('SELECT * FROM SYS_DEPARTMENT WHERE PARENT_ID=''11''',0,30);
- */
- public static String getTotalSqlString(String sqlStr){
- String tempsql=sqlStr.replaceFirst("(SELECT)[^\\(|\\)]*(\\([^\\(|\\)]*\\))*[^\\(|\\)]*(FROM)","SELECT COUNT(*) FROM");
- return sqlStr.replaceFirst("SELECT ", "SELECT ("+tempsql+") TOTAL,")+" LIMIT ?,?;";
- }
- /*public static void main(String[] args) {
- System.out.println(getTotalSqlString("SELECT * FROM (SELECT * FROM SYS_DEPARTMENT)"));
- }
- */
- }
|