1 package org.ejen.util; 22 23 import org.ejen.EjenConstants; 24 import java.io.StringReader ; 25 import java.lang.reflect.Constructor ; 26 import javax.xml.transform.TransformerException ; 27 import javax.xml.transform.TransformerFactory ; 28 import javax.xml.transform.ErrorListener ; 29 import javax.xml.transform.stream.StreamSource ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 import org.w3c.dom.NamedNodeMap ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.traversal.NodeIterator; 35 import org.apache.xml.utils.PrefixResolverDefault; 36 import org.apache.xml.utils.WrappedRuntimeException; 37 import org.apache.xalan.extensions.XSLProcessorContext; 38 import org.apache.xalan.extensions.ExpressionContext; 39 import org.apache.xalan.templates.ElemExtensionCall; 40 import org.apache.xalan.templates.AVT; 41 import org.apache.xalan.processor.StylesheetHandler; 42 import org.apache.xalan.processor.TransformerFactoryImpl; 43 import org.apache.xalan.transformer.TransformerImpl; 44 import org.apache.xpath.NodeSet; 45 import org.apache.xpath.XPathContext; 46 import org.apache.xpath.XPathContext.XPathExpressionContext; 47 import org.apache.xpath.XPath; 48 import org.apache.xpath.objects.XObject; 49 50 55 public class XSLUtil { 56 57 60 protected XSLUtil() {} 61 62 70 public static TransformerImpl getDefaultTransformer(TransformerFactoryImpl tfi) { 71 try { 72 return (TransformerImpl) (tfi.newTransformer(new StreamSource (new StringReader (EjenConstants.DEFAULT_XSL_DATA)))); 73 } catch (Exception e) { 74 throw new WrappedRuntimeException(e); 75 } 76 } 77 78 89 public static String getAttribute(XSLProcessorContext context, 90 ElemExtensionCall elem, 91 String name, 92 boolean throwsIfNull) { 93 try { 94 String value = elem.getAttribute(name, context.getContextNode(), 95 context.getTransformer()); 96 97 if (value == null && throwsIfNull) { 98 throw new IllegalArgumentException ("no \"" + name 99 + "\" attribute"); 100 } 101 return value; 102 } catch (Exception e) { 103 throw new WrappedRuntimeException(e); 104 } 105 } 106 107 119 public static XObject getXOAttribute(XSLProcessorContext context, 120 ElemExtensionCall elem, 121 String name, 122 boolean isAVT, 123 boolean throwsIfNull) { 124 String value = null; 125 126 try { 127 if (isAVT) { 128 value = elem.getAttribute(name, context.getContextNode(), 129 context.getTransformer()); 130 } else { 131 value = elem.getAttribute(name); 132 } 133 if (value == null && throwsIfNull) { 134 throw new IllegalArgumentException ("no \"" + name 135 + "\" attribute"); 136 } 137 } catch (Exception e) { 138 throw new WrappedRuntimeException(e); 139 } 140 return (value == null) ? null : evaluate(context, elem, value); 141 } 142 143 158 public static Object getOAttribute(XSLProcessorContext context, 159 ElemExtensionCall elem, 160 String name, 161 Class clazz, 162 boolean isAVT, 163 boolean throwsIfNull) { 164 XObject xo = getXOAttribute(context, elem, name, isAVT, throwsIfNull); 165 166 if (xo == null) { 167 return null; 168 } 169 if (xo.object() == null 170 || clazz.isAssignableFrom(xo.object().getClass())) { 171 return xo.object(); 172 } 173 throw new WrappedRuntimeException(new ClassCastException ("Class of \"" 174 + name + "\" attribute should be " + clazz)); 175 } 176 177 185 public static String evaluate(XSLProcessorContext context, ElemExtensionCall elem) { 186 return getAttribute(context, elem, "avt", true); 187 } 188 189 198 public static String evaluate(ExpressionContext context, String avt) { 199 try { 200 if (!(context instanceof XPathExpressionContext)) { 201 throw new IllegalArgumentException ("bad context: " + context); 202 } 203 XPathExpressionContext xpec = (XPathExpressionContext) context; 204 StylesheetHandler sh = (StylesheetHandler) ( 205 ((TransformerFactoryImpl) (TransformerFactory.newInstance())).newTemplatesHandler() 206 ); 207 208 return evaluateAttribute(sh, xpec.getXPathContext(), 209 xpec.getContextNode(), avt); 210 } catch (Exception e) { 211 throw new WrappedRuntimeException(e); 212 } 213 } 214 215 223 public static XObject evaluate(XSLProcessorContext context, 224 ElemExtensionCall elem, 225 String expression) { 226 try { 227 XPathContext xpc = context.getTransformer().getXPathContext(); 228 XPath xp = new XPath(expression, elem, xpc.getNamespaceContext(), 229 XPath.SELECT); 230 231 return xp.execute(xpc, context.getContextNode(), elem); 232 } catch (Exception e) { 233 throw new WrappedRuntimeException(e); 234 } 235 } 236 237 244 public static XObject evaluate(Node contextNode, String str) { 245 return evaluate(contextNode, str, contextNode, null); 246 } 247 248 256 public static XObject evaluate(Node contextNode, String str, ErrorListener el) { 257 return evaluate(contextNode, str, contextNode, el); 258 } 259 260 269 public static XObject evaluate(Node contextNode, String str, Node namespaceNode, ErrorListener el) { 270 try { 271 XPathContext xpc = new XPathContext(); 272 PrefixResolverDefault pr = new PrefixResolverDefault((namespaceNode.getNodeType() 273 == Node.DOCUMENT_NODE) 274 ? ((Document ) namespaceNode).getDocumentElement() 275 : namespaceNode); 276 XPath xp = new XPath(str, null, pr, XPath.SELECT, el); 277 int iContextNode = xpc.getDTMHandleFromNode(contextNode); 278 279 return xp.execute(xpc, iContextNode, pr); 280 } catch (Exception e) { 281 throw new WrappedRuntimeException(e); 282 } 283 } 284 285 private static AVT constructAVT(StylesheetHandler handler, 288 String uri, 289 String name, 290 String rawName, 291 String stringedValue) throws Exception { 292 Class [] parameterType = new Class [6]; 293 Constructor avtConstr = null; 294 AVT avt = null; 295 296 try { 297 parameterType[0] = Class.forName("org.apache.xalan.processor.StylesheetHandler"); 298 parameterType[1] = Class.forName("java.lang.String"); 299 parameterType[2] = Class.forName("java.lang.String"); 300 parameterType[3] = Class.forName("java.lang.String"); 301 parameterType[4] = Class.forName("java.lang.String"); 302 parameterType[5] = Class.forName("org.apache.xalan.templates.ElemTemplateElement"); 303 avtConstr = Class.forName("org.apache.xalan.templates.AVT").getConstructor(parameterType); 304 305 Object [] parameter = new Object [6]; 306 307 parameter[0] = handler; 308 parameter[1] = uri; 309 parameter[2] = name; 310 parameter[3] = rawName; 311 parameter[4] = stringedValue; 312 parameter[5] = null; 313 avt = (AVT) avtConstr.newInstance(parameter); 314 } catch (Exception e) { 315 parameterType = new Class [5]; 316 parameterType[0] = Class.forName("org.apache.xalan.processor.StylesheetHandler"); 317 parameterType[1] = Class.forName("java.lang.String"); 318 parameterType[2] = Class.forName("java.lang.String"); 319 parameterType[3] = Class.forName("java.lang.String"); 320 parameterType[4] = Class.forName("java.lang.String"); 321 avtConstr = Class.forName("org.apache.xalan.templates.AVT").getConstructor(parameterType); 322 323 Object [] parameter = new Object [5]; 324 325 parameter[0] = handler; 326 parameter[1] = uri; 327 parameter[2] = name; 328 parameter[3] = rawName; 329 parameter[4] = stringedValue; 330 avt = (AVT) avtConstr.newInstance(parameter); 331 } 332 333 return avt; 334 } 335 336 338 349 public static String evaluateAttribute(StylesheetHandler sh, 350 XPathContext xpc, 351 Node node, 352 String expr) 353 throws TransformerException { 354 String uri = node.getNamespaceURI(); 355 356 if (uri == null) { 357 uri = ""; 358 } 359 360 String name = node.getNodeName(); 361 362 if (name == null) { 363 name = ""; 364 } 365 String prefix = node.getPrefix(); 366 String rawName = ((prefix != null) ? prefix + ":" : "") + name; 367 AVT avt = null; 370 371 try { 372 avt = constructAVT(sh, uri, name, rawName, expr); 373 } catch (Exception e) { 374 e.printStackTrace(); 375 throw new TransformerException ("AVT constructor not found"); 376 } 377 PrefixResolverDefault pr = new PrefixResolverDefault((node.getNodeType() 379 == Node.DOCUMENT_NODE) 380 ? ((Document ) node).getDocumentElement() 381 : node); 382 383 return avt.evaluate(xpc, xpc.getDTMHandleFromNode(node), pr).toString(); 385 } 386 387 394 public static boolean equals(NodeIterator ni1, NodeIterator ni2) { 395 NodeSet ns1 = new NodeSet(ni1); 396 NodeSet ns2 = new NodeSet(ni2); 397 398 if (ns1.getLength() != ns2.getLength()) { 399 return false; 400 } 401 for (int i = 0; i < ns1.getLength(); i++) { 402 if (!equals(ns1.elementAt(i), ns2.elementAt(i))) { 403 return false; 404 } 405 } 406 return true; 407 } 408 409 416 public static boolean equals(Node n1, Node n2) { 417 if (n1 == null && n2 == null) { 418 return true; 419 } 420 if (n1 == null || n2 == null) { 421 return false; 422 } 423 if (n1.getNodeType() != n2.getNodeType()) { 424 return false; 425 } 426 if (!n1.getNodeName().equals(n2.getNodeName())) { 427 return false; 428 } 429 String v1 = n1.getNodeValue(); 430 String v2 = n2.getNodeValue(); 431 432 if ((v1 == null && v2 != null) || (v1 != null && !v1.equals(v2))) { 433 return false; 434 } 435 NamedNodeMap nnm1 = n1.getAttributes(); 436 NamedNodeMap nnm2 = n2.getAttributes(); 437 438 if ((nnm1 == null || nnm2 == null) && nnm1 != nnm2) { 439 return false; 440 } 441 if (nnm1 != null) { 442 int nnm1Length = nnm1.getLength(); 443 444 if (nnm1Length != nnm2.getLength()) { 445 return false; 446 } 447 for (int i = 0; i < nnm1Length; i++) { 448 if (!equals(nnm1.item(i), nnm2.item(i))) { 449 return false; 450 } 451 } 452 } 453 454 NodeList nl1 = n1.getChildNodes(); 455 NodeList nl2 = n2.getChildNodes(); 456 int nl1Length = nl1.getLength(); 457 458 if (nl1Length != nl2.getLength()) { 459 return false; 460 } 461 for (int i = 0; i < nl1Length; i++) { 462 if (!equals(nl1.item(i), nl2.item(i))) { 463 return false; 464 } 465 } 466 467 return true; 468 } 469 470 476 public static Document getContextDocument(ExpressionContext context) { 477 try { 478 return context.getContextNode().getOwnerDocument(); 479 } catch (Exception e) { 480 throw new WrappedRuntimeException("Failed to get Document from ExpressionContext", 481 e); 482 } 483 } 484 } 485 | Popular Tags |