1 34 35 36 package com.micronova.util.codec; 37 38 import java.net.*; 39 import java.io.*; 40 41 import com.micronova.util.*; 42 43 public class CodecURL extends Codec 44 { 45 46 47 public static Object encode(Object object, Object encoding) throws Exception 48 { 49 if (object != null) 50 { 51 String string = object.toString(); 52 53 object = java.net.URLEncoder.encode(string, encoding.toString()); 54 } 55 56 return object; 57 } 58 59 60 61 public static Object encode(Object object) throws Exception 62 { 63 return encode(object, "utf-8"); 64 } 65 66 67 68 public static Object decode(Object object, Object encoding) throws Exception 69 { 70 if (object != null) 71 { 72 String string = object.toString(); 73 74 object = java.net.URLDecoder.decode(string, encoding.toString()); 75 } 76 77 return object; 78 } 79 80 81 82 public static Object decode(Object object) throws Exception 83 { 84 return decode(object, "utf-8"); 85 } 86 87 88 89 public static Object streamType(Object object) 90 { 91 if (object != null) 92 { 93 try 94 { 95 InputStream is = null; 96 97 if (object instanceof String ) 98 { 99 is = new ByteArrayInputStream(StringUtil.fromBinaryString(object.toString())); 100 } 101 else if (object instanceof byte[]) 102 { 103 is = new ByteArrayInputStream((byte[])object); 104 } 105 else if (object instanceof InputStream) 106 { 107 is = (InputStream)object; 108 } 109 110 if (is != null) 111 { 112 String guessedType = URLConnection.guessContentTypeFromStream(is); 113 if (!object.equals(guessedType)) 114 { 115 object = guessedType; 116 } 117 } 118 } 119 catch (Exception e) 120 { 121 object = null; 122 } 123 } 124 125 return object; 126 } 127 128 129 130 public static Object nameType(Object object) 131 { 132 if (object != null) 133 { 134 object = URLConnection.guessContentTypeFromName(object.toString()); 135 } 136 137 return object; 138 } 139 140 public static Object normalize(Object object) 141 { 142 URI uri = null; 143 144 if (object != null) 145 { 146 URI uriObject = TypeUtil.isURI(object); 147 148 if (uriObject != null) 149 { 150 uri = uriObject.normalize(); 151 } 152 } 153 154 return uri; 155 } 156 157 public static Object relativize(Object object, Object base) 158 { 159 URI uri = null; 160 161 if (object != null) 162 { 163 URI uriObject = TypeUtil.isURI(object); 164 165 if (uriObject != null) 166 { 167 URI uriBase = TypeUtil.isURI(base); 168 169 if (uriBase != null) 170 { 171 uri = uriBase.relativize(uriObject); 172 } 173 } 174 } 175 176 return uri; 177 } 178 179 public static Object resolve(Object object, Object base) 180 { 181 URI uri = null; 182 183 if (object != null) 184 { 185 URI uriObject = TypeUtil.isURI(object); 186 187 if (uriObject != null) 188 { 189 URI uriBase = TypeUtil.isURI(base); 190 191 if (uriBase != null) 192 { 193 uri = uriBase.resolve(uriObject); 194 } 195 } 196 } 197 198 return uri; 199 } 200 } 201 | Popular Tags |