01: package org.jasig.portal.layout.dlm;
02:
03: import javax.xml.xpath.XPath;
04: import javax.xml.xpath.XPathConstants;
05: import javax.xml.xpath.XPathExpression;
06: import javax.xml.xpath.XPathExpressionException;
07: import javax.xml.xpath.XPathFactory;
08:
09: import org.apache.commons.logging.Log;
10: import org.apache.commons.logging.LogFactory;
11: import org.jasig.portal.layout.IUserLayout;
12: import org.w3c.dom.Document;
13: import org.w3c.dom.Element;
14:
15: /**
16: * Provides centralized tool for obtaining the root folder in a user layout or
17: * fragment layout. Uses a pre-compiled XPATH expression for increased
18: * performance.
19: *
20: * @author mboyd
21: *
22: */
23: public class RootLocator {
24: private static final Log LOG = LogFactory.getLog(RootLocator.class);
25: private static final String cfgPrefix = RootLocator.class.getName()
26: + ".";
27: private static XPathExpression rootLocatorXpathExpression = null;
28:
29: /**
30: * Returns the folder having a type attribute containing the value specified
31: * by IUserLayout.ROOT_NODE_NAME or null if such a folder does not exist in
32: * the layout.
33: *
34: * @param layout
35: * @return
36: */
37: public static Element getRootElement(Document layout) {
38: Element root = null;
39: try {
40: if (rootLocatorXpathExpression == null)
41: createRootLocatorXpathExpression();
42:
43: root = (Element) rootLocatorXpathExpression.evaluate(
44: layout, XPathConstants.NODE);
45: } catch (Exception e) {
46: LOG.error("Unable to locate layout element of type "
47: + IUserLayout.ROOT_NODE_NAME, e);
48: }
49: return root;
50: }
51:
52: /**
53: * Creates a compiled version of the XPATH evaluator for obtaining the
54: * root element of a layout.
55: */
56: private static void createRootLocatorXpathExpression() {
57: String expression = "//layout/folder";
58: try {
59: XPathFactory fac = XPathFactory.newInstance();
60: XPath xpath = fac.newXPath();
61: rootLocatorXpathExpression = xpath.compile(expression);
62: } catch (XPathExpressionException e) {
63: throw new RuntimeException(
64: "Unable to compile XPath expression '" + expression
65: + "' for obtaining root layout element.", e);
66: }
67: }
68: }
|