/* PageDefinitions.java Purpose: Description: History: Tue May 31 12:34:43 2005, Created by tomyeh Copyright (C) 2005 Potix Corporation. All Rights Reserved. {{IS_RIGHT This program is distributed under LGPL Version 2.1 in the hope that it will be useful, but WITHOUT ANY WARRANTY. }}IS_RIGHT */ package org.zkoss.zk.ui.metainfo; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.net.URL; import org.zkoss.idom.Document; import org.zkoss.lang.Classes; import org.zkoss.lang.Library; import org.zkoss.util.logging.Log; import org.zkoss.util.resource.Locator; import org.zkoss.web.servlet.Servlets; import org.zkoss.web.util.resource.ResourceCache; import org.zkoss.web.util.resource.ResourceCaches; import org.zkoss.web.util.resource.ResourceLoader; import org.zkoss.web.util.resource.ServletContextLocator; import org.zkoss.zk.ui.Execution; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.UiException; import org.zkoss.zk.ui.WebApp; /** * Utilities to retrieve page definitions. * * @author tomyeh */ public class PageDefinitions { private static final Log log = Log.lookup(PageDefinitions.class); private static final String ATTR_PAGE_CACHE = "org.zkoss.zk.ui.PageCache"; /** Returns the page definition of the specified raw content; never null. * *
This is the lowest method that other getPageDefinitionDirectly depends. * *
Dependency: Execution.createComponentsDirectly -& Execution.getPageDefinitionDirectly * -& UiFactory.getPageDefiitionDirectly -& PageDefintions.getPageDefinitionDirectly * * @param locator the locator used to locate taglib and other resources. * If null, wapp is assumed ({@link WebApp} is also assumed). * @param extension the default extension if the content doesn't specify * an language. In other words, if * the content doesn't specify an language, {@link LanguageDefinition#getByExtension} * is called. * If extension is null and the content doesn't specify a language, * the language called "xul/html" is assumed. * @exception UiException if failed to parse */ public static final PageDefinition getPageDefinitionDirectly(WebApp wapp, Locator locator, String content, String extension) { try { return getPageDefinitionDirectly( wapp, locator, new StringReader(content), extension); } catch (IOException ex) { throw UiException.Aide.wrap(ex); } } /** Returns the page definition of the raw content from the specified * reader; never null. * * @param locator the locator used to locate taglib and other resources. * If null, wapp is assumed ({@link WebApp} is also assumed). * @param extension the default extension if the content doesn't specify * an language. In other words, if * the content doesn't specify an language, {@link LanguageDefinition#getByExtension} * is called. * If extension is null and the content doesn't specify a language, * the language called "xul/html" is assumed. * @exception UiException if failed to parse */ public static final PageDefinition getPageDefinitionDirectly(WebApp wapp, Locator locator, Reader reader, String extension) throws IOException { try { return new Parser(wapp, locator).parse(reader, extension); } catch (IOException ex) { throw ex; } catch (Exception ex) { throw UiException.Aide.wrap(ex); } } /** Returns the page definition of the specified raw content in DOM; * never null. * * @param locator the locator used to locate taglib and other resources. * If null, wapp is assumed ({@link WebApp} is also assumed). * @param extension the default extension if the content doesn't specify * an language. In other words, if * the content doesn't specify an language, {@link LanguageDefinition#getByExtension} * is called. * If extension is null and the content doesn't specify a language, * the language called "xul/html" is assumed. * @exception UiException if failed to parse */ public static final PageDefinition getPageDefinitionDirectly(WebApp wapp, Locator locator, Document doc, String extension) { try { return new Parser(wapp, locator).parse(doc, extension); } catch (Exception ex) { throw UiException.Aide.wrap(ex); } } /** Returns the page definition of the specified path, or null if not * found or failed to parse. * *
This is the lowest method that other getPageDefinition depends. * *
Dependency: Execution.createComponents -& Execution.getPageDefinition
* -& UiFactory.getPageDefiition -& PageDefintions.getPageDefinition
*
* @param locator the locator used to locate taglib and other resources.
* If null, wapp is assumed ({@link WebApp} is also assumed).
*/
public static final
PageDefinition getPageDefinition(WebApp wapp, Locator locator, String path) {
wapp.getConfiguration().invokeURIInterceptors(path);
//give the security a chance to reject
return ResourceCaches.get(
getCache(wapp), wapp.getServletContext(), path, locator);
}
/** Returns the locator for the specified context.
*
* @param path the original path, or null if not available.
* The original path is used to resolve a relative path.
* If not specified, {@link org.zkoss.zk.ui.Desktop#getCurrentDirectory}
* is used.
*/
public static final Locator getLocator(WebApp wapp, String path) {
if (wapp == null) throw new IllegalArgumentException("null");
if (path != null && path.length() > 0 && path.charAt(0) == '/') {
final int j = path.lastIndexOf('/');
path = j > 0 ? path.substring(0, j + 1): "/";
} else {
final Execution exec = Executions.getCurrent();
if (exec != null) path = exec.getDesktop().getCurrentDirectory();
}
return new ServletContextLocator(wapp.getServletContext(), path);
}
@SuppressWarnings("unchecked")
private static final ResourceCache