1 28 29 package com.caucho.xsl.fun; 30 31 import com.caucho.log.Log; 32 import com.caucho.vfs.Path; 33 import com.caucho.xml.CauchoNode; 34 import com.caucho.xml.LooseHtml; 35 import com.caucho.xml.QDocument; 36 import com.caucho.xml.Xml; 37 import com.caucho.xpath.Env; 38 import com.caucho.xpath.Expr; 39 import com.caucho.xpath.ExprEnvironment; 40 import com.caucho.xpath.XPathException; 41 import com.caucho.xpath.XPathFun; 42 import com.caucho.xpath.pattern.AbstractPattern; 43 import com.caucho.xsl.TransformerImpl; 44 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.DocumentType ; 47 import org.w3c.dom.Node ; 48 49 import javax.xml.transform.Source ; 50 import javax.xml.transform.TransformerException ; 51 import javax.xml.transform.URIResolver ; 52 import java.util.ArrayList ; 53 import java.util.logging.Level ; 54 import java.util.logging.Logger ; 55 56 59 public class DocumentFun extends XPathFun { 60 static final Logger log = Log.open(DocumentFun.class); 61 62 TransformerImpl _transformer; 63 boolean _isHtml; 64 65 public DocumentFun(TransformerImpl transformer) 66 { 67 _transformer = transformer; 68 } 69 70 public void setHtml(boolean isHtml) 71 { 72 _isHtml = isHtml; 73 } 74 75 81 public Object eval(Node node, ExprEnvironment env, 82 AbstractPattern pattern, ArrayList args) 83 throws XPathException 84 { 85 if (args.size() < 1) 86 return null; 87 88 Node basenode; 89 String name = Expr.toString(args.get(0)); 90 91 if (args.size() > 1) 92 basenode = Expr.toNode(args.get(1)); 93 else 94 basenode = Expr.toNode(args.get(0)); 95 96 Path stylesheetPath = env.getStylesheetEnv().getPath(); 97 URIResolver resolver = _transformer.getURIResolver(); 98 99 Path path; 100 101 if (name == null || name.equals("")) 102 name = stylesheetPath.getTail(); 103 104 String systemId = null; 105 106 DocumentType dtd = null; 107 Document owner = null; 108 109 if (basenode == null) { 110 } 111 else if (basenode.getOwnerDocument() != null) { 112 owner = basenode.getOwnerDocument(); 113 dtd = owner.getDoctype(); 114 } 115 else if (basenode instanceof Document) { 116 owner = (Document) basenode; 117 dtd = owner.getDoctype(); 118 } 119 120 if (basenode instanceof CauchoNode) 121 systemId = ((CauchoNode) basenode).getBaseURI(); 122 123 Path pwd = stylesheetPath.getParent(); 124 125 if (systemId == null && owner instanceof QDocument) 126 systemId = ((QDocument) owner).getSystemId(); 127 128 if (systemId == null && dtd != null) 129 systemId = dtd.getSystemId(); 130 131 if (systemId == null) 132 systemId = stylesheetPath.getURL(); 133 134 Node doc = null; 135 Source source = null; 136 if (resolver != null) { 137 try { 138 source = resolver.resolve(name, systemId); 139 } catch (TransformerException e) { 140 throw new XPathException(e); 141 } 142 } 143 144 if (source != null) { 145 systemId = source.getSystemId(); 146 path = pwd.lookup(systemId); 147 } 148 else if (systemId != null) { 149 pwd = pwd.lookup(systemId).getParent(); 150 151 path = pwd.lookup(name); 152 } 153 else 154 path = pwd.lookup(name); 155 156 _transformer.addCacheDepend(path); 157 158 if (env instanceof Env) 159 doc = (Node) ((Env) env).getCache(path); 160 if (doc != null) 161 return doc; 162 163 try { 164 if (_isHtml) 165 doc = new LooseHtml().parseDocument(path); 166 else 167 doc = new Xml().parseDocument(path); 168 } catch (Exception e) { 169 log.log(Level.FINE, e.toString(), e); 170 171 } 173 174 if (env instanceof Env && source == null) 175 ((Env) env).setCache(path, doc); 176 177 return doc; 178 } 179 } 180 | Popular Tags |