df5e6861bd9072d04e7d74d19c6fa0b5ac1dae4e.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package com.sinosoft.cm.common;
  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import java.sql.SQLException;
  5. import java.util.Collections;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.UUID;
  10. import com.sinosoft.common.excel.JsonPluginsUtil;
  11. import net.sf.json.JSONObject;
  12. import nl.justobjects.pushlet.util.Sys;
  13. import flex.messaging.io.ArrayList;
  14. public class StringUtils {
  15. public StringUtils() {
  16. }
  17. /**
  18. * 将clob转换为字符串
  19. * @param clob
  20. * @return
  21. * @throws SQLException
  22. */
  23. public static String clobToString(java.sql.Clob clob) throws SQLException{
  24. if(clob == null) {
  25. return null;
  26. }
  27. Reader inStreamDoc = clob.getCharacterStream();
  28. char[] tempDoc = new char[(int) clob.length()];
  29. try {
  30. inStreamDoc.read(tempDoc);
  31. inStreamDoc.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. return new String(tempDoc);
  36. }
  37. public static String listToQueryIn(List<String> list) throws Exception{
  38. if(list==null||0==list.size())throw new Exception("List集合不能为空或者长度为0");
  39. StringBuffer sb=new StringBuffer();
  40. for(int i=0;i<list.size();i++){
  41. if(0==i){
  42. sb.append("'"+list.get(i)+"'");
  43. }else{
  44. sb.append(", '"+list.get(i)+"'");
  45. }
  46. }
  47. return sb.toString();
  48. }
  49. /**
  50. * 将list集合转换为map集合
  51. * @param list
  52. * @return
  53. * @throws Exception
  54. */
  55. public static Map<String,List<String>> listMapToMapList(List<Map<String,String>> list) throws Exception{
  56. Map<String,List<String>> map=new HashMap<String, List<String>>();
  57. if(null==list)throw new Exception("listMapToMapList(List<Map<String,String>> list):参数为空或者长度为0;");
  58. for(String listName : list.get(0).keySet()){
  59. map.put(listName, new ArrayList());
  60. }
  61. for(Map<String,String> listItem : list){
  62. for(String listItemKey:listItem.keySet() ){
  63. map.get(listItemKey).add(listItem.get(listItemKey));
  64. }
  65. }
  66. return map;
  67. }
  68. public static String listToQueryInString(List<String> list){
  69. StringBuffer sb=new StringBuffer();
  70. for(String str : list){
  71. sb.append("'"+new StringBuffer(str)+"',");
  72. }
  73. sb.delete(sb.length()-1, sb.length());
  74. return sb.toString();
  75. }
  76. public static List<String> stringToList(String str,String splite){
  77. String spliteStr=str.substring(1, str.lastIndexOf("'"));
  78. String[] bb=spliteStr.split(splite);
  79. List<String> list=new ArrayList();
  80. for(int i=0,len=bb.length;i<len;i++){
  81. list.add(bb[i]);
  82. }
  83. //System.out.println("stringToList="+list);
  84. return list;
  85. }
  86. /**
  87. * 将返回结果转换为json对象
  88. * @param state
  89. * @param count
  90. * @param data
  91. * @return
  92. */
  93. public static String resultToJSON(String state,int count ,String data){
  94. return "{\"state\":\""+state+"\",\"total\":"+count+",\"rows\":"+data+"}";
  95. }
  96. /**
  97. * 将返回结果转换为json对象
  98. * @param state
  99. * @param count
  100. * @param data
  101. * @return
  102. */
  103. public static String resultToJSONSum(String state,int count , String sum,String unsum,String data){
  104. return "{\"state\":\""+state+"\",\"total\":"+count+",\"sum\":"+sum+",\"unsum\":"+unsum+",\"rows\":"+data+"}";
  105. }
  106. /**
  107. * 处理汇总的结果
  108. * @param state
  109. * @param count
  110. * @param data
  111. * @param sum
  112. * @return
  113. */
  114. public static String resultToJSON(String state,int count ,String data,String sum){
  115. return "{\"state\":\""+state+"\",\"total\":"+count+",\"rows\":"+data+",\"data\":"+sum+"}";
  116. }
  117. public static String ToJson(Map<String,Map<String,String>> map){
  118. StringBuffer sb=new StringBuffer("{");
  119. for(String key:map.keySet()){
  120. if(map.get(key) instanceof Map){
  121. sb.append("\""+key+"\":").append(mapToJson(map.get(key))).append(",");
  122. }else{
  123. sb.append("\""+key+"\":\"").append(map.get(key)).append("\",");
  124. }
  125. }
  126. sb.deleteCharAt(sb.lastIndexOf(","));
  127. sb.append("}");
  128. return sb.toString();
  129. }
  130. /* public static void main(String[] args) {
  131. Map<String ,Map<String,String>> map=new HashMap<String, Map<String,String>>();
  132. for(int index=0;index<10;index++){
  133. Map<String,String> map1=new HashMap<String, String>();
  134. map1.put("avc"+index, "abcd");
  135. map1.put("avw"+index, "abcd");
  136. map.put("aaaa"+index, map1);
  137. }
  138. String str=ToJson(map);
  139. System.out.println("======"+str);
  140. }*/
  141. /**
  142. * 将map<String,List<Map<String,String>>>集合转换为json对象;
  143. * @param map
  144. * @return
  145. */
  146. /**
  147. * 将map<String,List<Map<String,String>>>集合转换为json对象;
  148. * @param map
  149. * @return
  150. */
  151. public static List<String> MapToJson(Map<String,List<Map<String,String>>> map){
  152. List<String> list=new ArrayList();
  153. for(String strName : map.keySet()){
  154. List<String> strList=listToJson(map.get(strName));
  155. String str="{\"name\":\""+strName+"\" ,\"rows\":"+ listToJson(strList)+"}";
  156. list.add(str);
  157. }
  158. //System.out.println("MapToJson="+list);
  159. return list;
  160. }
  161. public static Map<String,List<Map<String,String>>> packetMap(List<Map<String,String>> list,String packetKey){
  162. Map<String,List<Map<String,String>>> mapList=new HashMap<String, List<Map<String,String>>>();
  163. for(Map<String,String> map:list){
  164. if(!mapList.containsKey(map.get(packetKey))){
  165. mapList.put(map.get(packetKey), new ArrayList());
  166. }
  167. mapList.get(map.get(packetKey)).add(map);
  168. }
  169. return mapList;
  170. }
  171. /**
  172. * 将字符串集合转换为JSON字符串
  173. * @param list
  174. * @return
  175. */
  176. public static String listToJson(List<String> list){
  177. StringBuffer sb=new StringBuffer("[");
  178. for(String str : list){
  179. sb.append(new StringBuffer(str)).append(",");
  180. }
  181. if(sb.length()>1){
  182. sb.delete(sb.length()-1,sb.length());
  183. }
  184. sb.append("]");
  185. //System.out.println("listToJson sb.toString()="+sb.toString());
  186. return sb.toString();//JsonPluginsUtil.beanListToJson(list);
  187. }
  188. /**
  189. * 将list集合
  190. * @param list
  191. * @return
  192. */
  193. public static List<String> listToJson(List<Map<String,String>> list){
  194. List<String> listJson=new ArrayList();
  195. for(Map<String,String> map : list){
  196. listJson.add(mapToJson(map));
  197. //System.out.println("mapInlistToJson="+listJson);
  198. }
  199. //System.out.println("listToJson="+listJson);
  200. return listJson;
  201. }
  202. /**
  203. * 将map转换为json字符串
  204. * @param map
  205. * @return
  206. */
  207. public static String mapToJson(Map<String,String> map){
  208. StringBuffer sb=new StringBuffer("{");
  209. for(String key:map.keySet()){
  210. sb.append("\""+new StringBuffer(key)+"\":").append("\""+new StringBuffer(map.get(key))+"\",");
  211. }
  212. sb.deleteCharAt(sb.length()-1);
  213. sb.append("}");
  214. //System.out.println("mapToJson="+sb.toString());
  215. return sb.toString();
  216. }
  217. /**
  218. *
  219. * @param sql
  220. * @return
  221. */
  222. public static List<String> stringToList2(String str,String split){
  223. List<String> list=new ArrayList();
  224. if(!str.contains(split)){
  225. list.add(str);
  226. }else{
  227. String[] strArr=str.split(split);
  228. Collections.addAll(list,strArr);
  229. }
  230. return list;
  231. }
  232. /**
  233. * 将sql语句截取会可接受查询结果集的
  234. * @param str
  235. * @param split
  236. * @return
  237. */
  238. public static List<String> sqlStringToListLabel(String str,String split){
  239. List<String> list=new ArrayList();
  240. str=subSqlLabel(str.trim());
  241. String[] strArr=str.split(split);
  242. getSqlLabelFromArr(list,strArr);
  243. return list;
  244. }
  245. /**
  246. * 将查询字段数组转化为可接受查询结果集字段
  247. * @param list
  248. * @param arr
  249. * @return
  250. */
  251. public static List<String> getSqlLabelFromArr(List<String> list, String[] arr){
  252. for(int index=0,len=arr.length;index<len;index++){
  253. String temStr=arr[index].trim();
  254. if(temStr.contains(" ")){
  255. temStr=temStr.substring(temStr.lastIndexOf(" ")).trim();
  256. }
  257. list.add(temStr);
  258. }
  259. return list;
  260. }
  261. /**
  262. * 截取sql语句中的查询字段
  263. * @param sqlString
  264. * @return
  265. */
  266. public static String subSqlLabel(String sqlString){
  267. String regex="(SELECT)|(FROM)";
  268. String str=sqlString.replaceAll(regex, "|").trim();
  269. str=str.substring(str.indexOf("|")+1).trim();
  270. str=str.substring(0,str.indexOf("|")).trim();
  271. return str;
  272. }
  273. /**
  274. *
  275. * @param str 需要被替换的字符串
  276. * @param reg 被替换的字符
  277. * @param list 替换字符串集合
  278. * @return
  279. * @throws Exception
  280. */
  281. public static String replace(String str,String reg, List<String> list) throws Exception {
  282. if(list==null)
  283. throw new Exception("集合长度为空");
  284. System.out.println(str.split(reg).length+":"+list.size());
  285. if((str.split(reg.replace("\\", "")).length-1)!=list.size())
  286. throw new Exception("替换长度不匹配");
  287. for(int index=0,len=list.size();index<len;index++){
  288. str=str.replaceFirst(reg, list.get(index));
  289. }
  290. return str;
  291. }
  292. public static String replace(String str,String reg ,String[] arr) throws Exception{
  293. if(arr==null)
  294. throw new Exception("数组为空");
  295. /* if(str.split(reg.replace("\\?", "")).length-1!=arr.length)
  296. throw new Exception("替换长度不匹配");*/
  297. for(int index=0,len=arr.length;index<len;index++){
  298. str=str.replaceFirst(reg, arr[index]);
  299. }
  300. return str;
  301. }
  302. /**
  303. *
  304. * @param list 键值对
  305. * @param columnLabel 拼接的字段
  306. * @param insertStr 插入的字符串
  307. * @return
  308. */
  309. public static String mapSingleColumToString(List<Map<String,String>> list,String columnLabel,String insertStr) {
  310. StringBuffer sb=new StringBuffer();
  311. if(list.size()==0 || columnLabel==null || columnLabel.toUpperCase()=="NULL"|| "".equals(columnLabel))
  312. return null;
  313. if(list.size()==0)
  314. return list.get(0).get(columnLabel);
  315. for(int index=0,len=list.size();index<len;index++){
  316. sb.append(list.get(index).get(columnLabel)).append(new StringBuffer(insertStr));
  317. }
  318. sb.delete(sb.length()-insertStr.length(), sb.length());
  319. return sb.toString();
  320. }
  321. /**
  322. *
  323. * @param list 拼接的集合
  324. * @param insertStr 插入的字符串
  325. * @return
  326. */
  327. public static String listToString(List<String> list,String insertStr) {
  328. if(list==null)
  329. return null;
  330. if(list.size()==0)
  331. return list.get(0);
  332. StringBuffer sb=new StringBuffer();
  333. for(int index=0,len=list.size();index<len;index++){
  334. sb.append(list.get(index)).append(new StringBuffer(insertStr));
  335. }
  336. sb.delete(sb.length()-insertStr.length(), sb.length());
  337. return sb.toString();
  338. }
  339. public static String arrayToString (String[] arr,String insertStr){
  340. if(arr==null)return null;
  341. if(arr.length==0) return null;
  342. StringBuffer sb=new StringBuffer();
  343. for(int index=0,len=arr.length;index<len;index++){
  344. sb.append(arr[index]).append(insertStr);
  345. }
  346. sb.delete(sb.length()-insertStr.length(), sb.length());
  347. return sb.toString();
  348. }
  349. public static String getFdObjectId(){
  350. return UUID.randomUUID().toString().replace("-", "");
  351. }
  352. /**
  353. *
  354. * CREATE OR REPLACE PROCEDURE search(SQL_STRING varchar2,NUM_START varchar2,NUM_END varchar2)AS
  355. DECLARE
  356. SQL_STR VARCHAR2:=REGEXP_REPLACE(SQL_STRING,'SELECT',
  357. 'SELECT ('||
  358. REGEXP_REPLACE(SQL_STRING,'(SELECT)[^\\(|\\)]*(\\([^\\(|\\)]*\\))*[^\\(|\\)]*(FROM)','SELECT COUNT(*) FROM',1,1)
  359. ||') TOTAL, ',1,1
  360. )||' LIMIT ?,?;';
  361. BEGIN
  362. EXECUTE IMMEDIATE SQL_STR USING NUM_START,NUM_END;
  363. END ;
  364. CALL search('SELECT * FROM SYS_DEPARTMENT WHERE PARENT_ID=''11''',0,30);
  365. */
  366. public static String getTotalSqlString(String sqlStr){
  367. String tempsql=sqlStr.replaceFirst("(SELECT)[^\\(|\\)]*(\\([^\\(|\\)]*\\))*[^\\(|\\)]*(FROM)","SELECT COUNT(*) FROM");
  368. return sqlStr.replaceFirst("SELECT ", "SELECT ("+tempsql+") TOTAL,")+" LIMIT ?,?;";
  369. }
  370. /*public static void main(String[] args) {
  371. System.out.println(getTotalSqlString("SELECT * FROM (SELECT * FROM SYS_DEPARTMENT)"));
  372. }
  373. */
  374. }