KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > fun > DocumentFun


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
46 import org.w3c.dom.DocumentType JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48
49 import javax.xml.transform.Source JavaDoc;
50 import javax.xml.transform.TransformerException JavaDoc;
51 import javax.xml.transform.URIResolver JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import java.util.logging.Logger JavaDoc;
55
56 /**
57  * The document(...) function.
58  */

59 public class DocumentFun extends XPathFun {
60   static final Logger JavaDoc 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   /**
76    * Evaluate the function.
77    *
78    * @param pattern The context pattern.
79    * @param args The evaluated arguments
80    */

81   public Object JavaDoc eval(Node node, ExprEnvironment env,
82              AbstractPattern pattern, ArrayList JavaDoc args)
83     throws XPathException
84   {
85     if (args.size() < 1)
86       return null;
87
88     Node basenode;
89     String JavaDoc 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 JavaDoc resolver = _transformer.getURIResolver();
98
99     Path path;
100
101     if (name == null || name.equals(""))
102       name = stylesheetPath.getTail();
103
104     String JavaDoc systemId = null;
105
106     DocumentType JavaDoc 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 JavaDoc source = null;
136     if (resolver != null) {
137       try {
138     source = resolver.resolve(name, systemId);
139       } catch (TransformerException JavaDoc 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 JavaDoc e) {
169       log.log(Level.FINE, e.toString(), e);
170       
171       //XXX:throw new XPathException(e);
172
}
173
174     if (env instanceof Env && source == null)
175       ((Env) env).setCache(path, doc);
176
177     return doc;
178   }
179 }
180
Popular Tags