1 package com.mvnforum.db.ldap; 2 3 import java.sql.Date ; 4 import java.sql.Timestamp ; 5 6 import javax.naming.directory.Attribute ; 7 import javax.naming.directory.Attributes ; 8 9 public class AttributeUtil { 10 11 public static String attributeToString (Attributes attrs, String name) { 12 if (attrs == null) return ""; 13 Attribute attr = attrs.get(name); 14 if (attr == null) return ""; 15 try { 16 Object obj = attr.get(); 17 if(obj instanceof byte[]) return new String ((byte[]) obj) ; 18 return (String ) obj ; 19 } catch (Exception e) { 20 e.printStackTrace(); 21 return ""; 22 } 23 } 24 25 public static int attributeToInt (Attributes attrs, String name) { 26 if (attrs == null) return 0; 27 Attribute attr = attrs.get(name); 28 if (attr == null) return 0; 29 try { 30 Object obj = attr.get(); 31 return Integer.parseInt(obj.toString()); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 return 0; 35 } 36 } 37 38 public static Timestamp attributeToTimeStamp (Attributes attrs, String name) { 39 if (attrs == null) return new Timestamp (0); 40 Attribute attr = attrs.get(name); 41 if (attr == null) return new Timestamp (0); 42 try { 43 Object obj = attr.get(); 44 String a = obj.toString(); 45 String timeS = a.substring(1, a.length() - 1); 46 return new Timestamp (Long.parseLong(timeS)); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 return new Timestamp (0); 50 } 51 } 52 53 public static Date attributeToDate (Attributes attrs, String name) { 54 if (attrs == null) return new Date (0); 55 Attribute attr = attrs.get(name); 56 if (attr == null) return new Date (0); 57 try { 58 Object obj = attr.get(); 59 String a = obj.toString(); 60 String timeS = a.substring(1, a.length() - 1); 61 return new Date (Long.parseLong(timeS)); 62 } catch (Exception e) { 63 e.printStackTrace(); 64 return new Date (0); 65 } 66 } 67 } 68 | Popular Tags |