1 34 35 36 package com.micronova.util.codec; 37 38 import java.util.*; 39 import com.micronova.util.*; 40 import org.w3c.dom.*; 41 import javax.xml.parsers.*; 42 import org.xml.sax.*; 43 import java.io.*; 44 import java.net.*; 45 import javax.xml.transform.*; 46 import javax.xml.transform.dom.*; 47 import javax.xml.transform.sax.*; 48 import javax.xml.transform.stream.*; 49 50 51 52 public class CodecXML extends Codec 53 { 54 55 56 public static final Object parse(Object object, Object control) throws Exception 57 { 58 return XMLUtil.parse(object, TypeUtil.isNestedMap(control)); 59 } 60 61 62 63 public static Object parse(Object object) throws Exception 64 { 65 return parse(object, null); 66 } 67 68 69 70 public static final Object encode(Object object) throws Exception 71 { 72 return encode(object, null); 73 } 74 75 76 77 public static final Object encode(Object object, Object spec) throws Exception 78 { 79 if (object != null) 80 { 81 if ("~".equals(spec)) 82 { 83 spec = "[&<>]"; 84 } 85 86 object = XMLUtil.encode(object.toString(), TypeUtil.isPattern(spec)); 87 } 88 89 return object; 90 } 91 92 93 94 public static final Object decode(Object object) throws Exception 95 { 96 if (object != null) 97 { 98 object = XMLUtil.decode(object.toString()); 99 } 100 101 return object; 102 } 103 104 105 106 public static final Object output(Object object, Object control) throws Exception 107 { 108 return XMLUtil.output(XMLUtil.createSource(object), new NestedMap(control)); 109 } 110 111 112 113 public static final Object output(Object object) throws Exception 114 { 115 return output(object, "omit-xml-declaration=yes"); 116 } 117 118 119 120 public static final Object parseHtml(Object object, Object control) throws Exception 121 { 122 if (object != null) 123 { 124 object = XMLUtil.parseHtml(object, TypeUtil.isNestedMap(control)); 125 } 126 127 return object; 128 } 129 130 public static final Object parseHtml(Object object) throws Exception 131 { 132 return parseHtml(object, null); 133 } 134 135 136 137 public static final Object removeNode(Object object) throws Exception 138 { 139 Node node = TypeUtil.isNode(object); 140 141 if (node != null) 142 { 143 Node parentNode = node.getParentNode(); 144 145 if (parentNode != null) 146 { 147 parentNode.removeChild(node); 148 } 149 } 150 151 return object; 152 } 153 154 155 156 public static final Object insertNodeBefore(Object object, Object before) throws Exception 157 { 158 Node beforeNode = TypeUtil.isNode(before); 159 160 if (beforeNode != null) 161 { 162 Node node = TypeUtil.isNode(object, beforeNode.getOwnerDocument()); 163 164 if (node != null) 165 { 166 Node parentNode = beforeNode.getParentNode(); 167 168 if (parentNode != null) 169 { 170 parentNode.insertBefore(node, beforeNode); 171 } 172 } 173 } 174 175 return object; 176 } 177 178 179 180 public static final Object appendNode(Object object, Object parent) throws Exception 181 { 182 Node parentNode = TypeUtil.isNode(parent); 183 184 if (parentNode != null) 185 { 186 Node node = TypeUtil.isNode(object, parentNode.getOwnerDocument()); 187 188 if (node != null) 189 { 190 parentNode.appendChild(node); 191 } 192 } 193 194 return object; 195 } 196 197 198 199 public static final Object cloneNode(Object object) throws Exception 200 { 201 Node node = TypeUtil.isNode(object); 202 203 if (node != null) 204 { 205 node = node.cloneNode(false); 206 } 207 208 return node; 209 } 210 211 212 213 public static final Object copyNode(Object object) throws Exception 214 { 215 Node node = TypeUtil.isNode(object); 216 217 if (node != null) 218 { 219 node = node.cloneNode(true); 220 } 221 222 return node; 223 } 224 225 226 227 public static final Object replaceNode(Object object, Object old) throws Exception 228 { 229 Node node = null; 230 Node oldNode = TypeUtil.isNode(old); 231 232 if (oldNode != null) 233 { 234 node = TypeUtil.isNode(object, oldNode.getOwnerDocument()); 235 236 if (node != null) 237 { 238 Node parentNode = oldNode.getParentNode(); 239 240 if (parentNode != null) 241 { 242 parentNode.replaceChild(node, oldNode); 243 } 244 } 245 } 246 247 return node; 248 } 249 250 251 252 public static final Object normalizeNode(Object object) throws Exception 253 { 254 Node node = TypeUtil.isNode(object); 255 256 if (node != null) 257 { 258 node.normalize(); 259 } 260 261 return node; 262 } 263 264 265 266 public static final Object setAttribute(Object object, Object key, Object value) throws Exception 267 { 268 Node node = TypeUtil.isNode(object); 269 270 if (node instanceof Element) 271 { 272 if (key != null) 273 { 274 String keyString = key.toString(); 275 276 if (value == null) 277 { 278 ((Element)node).removeAttribute(keyString); 279 } 280 else 281 { 282 ((Element)node).setAttribute(keyString, value.toString()); 283 } 284 } 285 } 286 287 return object; 288 } 289 290 291 292 public static final Object getAttribute(Object object, Object key) throws Exception 293 { 294 Node node = TypeUtil.isNode(object); 295 296 if (node instanceof Element) 297 { 298 if (key != null) 299 { 300 object = ((Element)node).getAttribute(key.toString()); 301 } 302 } 303 304 return object; 305 } 306 } 307 308 | Popular Tags |