001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl.fun;
030:
031: import com.caucho.log.Log;
032: import com.caucho.vfs.Path;
033: import com.caucho.xml.CauchoNode;
034: import com.caucho.xml.LooseHtml;
035: import com.caucho.xml.QDocument;
036: import com.caucho.xml.Xml;
037: import com.caucho.xpath.Env;
038: import com.caucho.xpath.Expr;
039: import com.caucho.xpath.ExprEnvironment;
040: import com.caucho.xpath.XPathException;
041: import com.caucho.xpath.XPathFun;
042: import com.caucho.xpath.pattern.AbstractPattern;
043: import com.caucho.xsl.TransformerImpl;
044:
045: import org.w3c.dom.Document;
046: import org.w3c.dom.DocumentType;
047: import org.w3c.dom.Node;
048:
049: import javax.xml.transform.Source;
050: import javax.xml.transform.TransformerException;
051: import javax.xml.transform.URIResolver;
052: import java.util.ArrayList;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055:
056: /**
057: * The document(...) function.
058: */
059: public class DocumentFun extends XPathFun {
060: static final Logger log = Log.open(DocumentFun.class);
061:
062: TransformerImpl _transformer;
063: boolean _isHtml;
064:
065: public DocumentFun(TransformerImpl transformer) {
066: _transformer = transformer;
067: }
068:
069: public void setHtml(boolean isHtml) {
070: _isHtml = isHtml;
071: }
072:
073: /**
074: * Evaluate the function.
075: *
076: * @param pattern The context pattern.
077: * @param args The evaluated arguments
078: */
079: public Object eval(Node node, ExprEnvironment env,
080: AbstractPattern pattern, ArrayList args)
081: throws XPathException {
082: if (args.size() < 1)
083: return null;
084:
085: Node basenode;
086: String name = Expr.toString(args.get(0));
087:
088: if (args.size() > 1)
089: basenode = Expr.toNode(args.get(1));
090: else
091: basenode = Expr.toNode(args.get(0));
092:
093: Path stylesheetPath = env.getStylesheetEnv().getPath();
094: URIResolver resolver = _transformer.getURIResolver();
095:
096: Path path;
097:
098: if (name == null || name.equals(""))
099: name = stylesheetPath.getTail();
100:
101: String systemId = null;
102:
103: DocumentType dtd = null;
104: Document owner = null;
105:
106: if (basenode == null) {
107: } else if (basenode.getOwnerDocument() != null) {
108: owner = basenode.getOwnerDocument();
109: dtd = owner.getDoctype();
110: } else if (basenode instanceof Document) {
111: owner = (Document) basenode;
112: dtd = owner.getDoctype();
113: }
114:
115: if (basenode instanceof CauchoNode)
116: systemId = ((CauchoNode) basenode).getBaseURI();
117:
118: Path pwd = stylesheetPath.getParent();
119:
120: if (systemId == null && owner instanceof QDocument)
121: systemId = ((QDocument) owner).getSystemId();
122:
123: if (systemId == null && dtd != null)
124: systemId = dtd.getSystemId();
125:
126: if (systemId == null)
127: systemId = stylesheetPath.getURL();
128:
129: Node doc = null;
130: Source source = null;
131: if (resolver != null) {
132: try {
133: source = resolver.resolve(name, systemId);
134: } catch (TransformerException e) {
135: throw new XPathException(e);
136: }
137: }
138:
139: if (source != null) {
140: systemId = source.getSystemId();
141: path = pwd.lookup(systemId);
142: } else if (systemId != null) {
143: pwd = pwd.lookup(systemId).getParent();
144:
145: path = pwd.lookup(name);
146: } else
147: path = pwd.lookup(name);
148:
149: _transformer.addCacheDepend(path);
150:
151: if (env instanceof Env)
152: doc = (Node) ((Env) env).getCache(path);
153: if (doc != null)
154: return doc;
155:
156: try {
157: if (_isHtml)
158: doc = new LooseHtml().parseDocument(path);
159: else
160: doc = new Xml().parseDocument(path);
161: } catch (Exception e) {
162: log.log(Level.FINE, e.toString(), e);
163:
164: //XXX:throw new XPathException(e);
165: }
166:
167: if (env instanceof Env && source == null)
168: ((Env) env).setCache(path, doc);
169:
170: return doc;
171: }
172: }
|