Added: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/JCRTagUtils.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/JCRTagUtils.java?rev=171077&view=auto ============================================================================== --- incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/JCRTagUtils.java (added) +++ incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/JCRTagUtils.java Fri May 20 01:13:01 2005 @@ -0,0 +1,203 @@ +/* + * Copyright 2004-2005 The Apache Software Foundation or its licensors, + * as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.taglib.utils; + +import javax.jcr.Item; +import javax.jcr.Node; +import javax.jcr.PathNotFoundException; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.PageContext; +import javax.servlet.jsp.tagext.Tag; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jackrabbit.taglib.bean.BeanFactory; +import org.apache.taglibs.standard.tag.common.core.NullAttributeException; +import org.apache.taglibs.standard.tag.el.core.ExpressionUtil; + +/** + * JCR taglib utils + * + * @author Edgar Poce + */ +public class JCRTagUtils +{ + private static Log log = LogFactory.getLog(JCRTagUtils.class); + + /** + * Get an object from jndi + * + * @param name + * @return @throws + * JspException + */ + public static Object lookup(String name) + { + Object o = null; + try + { + InitialContext ctx = new InitialContext(); + Context env = (Context) ctx.lookup("java:comp/env"); + o = env.lookup(name); + } catch (NamingException e) + { + String msg = "Unable to get object from jndi: " + name + ". " + + e.getMessage(); + log.error(msg, e); + } + return o; + } + + /** + * Get a session for the given key + * + * @param pc + * @param key + * @throws JspException + */ + public static Session getSession(String tagName, String expression, + Tag tag, PageContext pageCtx) throws JspException + { + Session session = null; + try + { + session = (Session) ExpressionUtil.evalNotNull(tagName, "session", + expression, Object.class, tag, pageCtx); + if (log.isDebugEnabled()) + { + log.debug("Session found. User=" + session.getUserID()); + } + + } catch (ClassCastException e) + { + String msg = "Unable to get session for expression= " + expression + + ". " + e.getMessage(); + log.error(msg, e); + throw new IllegalArgumentException(msg); + } + return session; + } + + /** + *
+ * Get a node. + *
+ *+ * The value can be a String or a EL expression referencing a Node instance. + *
+ * + * @param tagName + * @param attribute + * @param expression + * @param tag + * @param pageCtx + * @param session + * @return a node + * @throws JspException + * @throws RepositoryException + * @throws PathNotFoundException + */ + public static Item getItem(String tagName, String expression, Tag tag, + PageContext pageCtx, Session session) throws JspException, + PathNotFoundException, RepositoryException + { + Item item = null; + Object o = (Object) ExpressionUtil.evalNotNull(tagName, "node", + expression, Object.class, tag, pageCtx); + // Path to the node + if (o instanceof String) + { + String path = (String) o; + if (path.startsWith("/")) + { // Absolute path + item = (Item) session.getItem(path); + } else + { // Relative path + item = getCD(tagName, tag, pageCtx, session).getNode(path); + } + } else if (o instanceof Item) + { + item = (Item) o; + if (!item.getSession().equals(session)) + { + throw new JspException( + "The referenced node belongs to another session."); + } + } else + { + String msg = "The node attribute evaluation " + + "returned an unexpected type. " + o.getClass().getName(); + log.warn(msg); + throw new JspException(msg); + } + return item; + } + + /** + * Get the current working directory + * + * @param session + * @param pc + * @return a node + * @throws RepositoryException + * @throws JspException + */ + private static Node getCD(String tagName, Tag tag, PageContext pageCtx, + Session session) throws RepositoryException, JspException + { + Node item = null ; + try { + item = (Node) ExpressionUtil.evalNotNull(tagName, "node", "${" + + JCRTagConstants.KEY_CD + "}", Object.class, tag, pageCtx); + } catch (NullAttributeException e) { + item = session.getRootNode(); + } + return item; + } + + /** + * Create a bean for the class specified in the given jndi entry + * + * @param jndi + * @return a bean + */ + public static Object getBean(String id) + { + BeanFactory factory = (BeanFactory) lookup(JCRTagConstants.JNDI_BEAN_FACTORY); + Object bean = factory.getBean(id); + if (bean == null) + { + log.warn("No bean for id = " + id); + } + return bean; + } + + /** + * Get the message from the Exception + * @param e + * @return + */ + public static String getMessage(Exception e) { + return e.getClass().getName() + ". " + e.getMessage() ; + } + +} \ No newline at end of file Propchange: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/JCRTagUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/package.html URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/package.html?rev=171077&view=auto ============================================================================== --- incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/package.html (added) +++ incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/package.html Fri May 20 01:13:01 2005 @@ -0,0 +1,3 @@ + +Contains Utils classes. + \ No newline at end of file Propchange: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/java/org/apache/jackrabbit/taglib/utils/package.html ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/InitServlet.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/InitServlet.java?rev=171077&view=auto ============================================================================== --- incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/InitServlet.java (added) +++ incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/InitServlet.java Fri May 20 01:13:01 2005 @@ -0,0 +1,93 @@ +package org.apache.jackrabbit.taglib; + +import javax.jcr.Node; +import javax.jcr.Repository; +import javax.jcr.Session; +import javax.jcr.SimpleCredentials; +import javax.jcr.query.Query; +import javax.jcr.query.QueryResult; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; + +import org.apache.commons.collections.IteratorUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jackrabbit.taglib.utils.JCRTagConstants; + +/** + * Create test nodes + */ +public class InitServlet extends HttpServlet +{ + private static Log log = LogFactory.getLog(InitServlet.class); + + private Repository repo; + + public void destroy() + { + } + + public void init() throws ServletException + { + this.createTree() ; + } + + private void createTree(){ + try { + Repository repo = this.getRepository() ; + Session s = repo.login(new SimpleCredentials("admin", "".toCharArray())) ; + Node root = s.getRootNode() ; + if (!root.hasNode("TestA")) { + // Tree + Node ta = root.addNode("TestA", "nt:unstructured") ; + ta.setProperty("prop1", "prop1 value V0"); + Node tb= root.addNode("TestB") ; + + // Versionable + ta.addMixin("mix:versionable"); + s.save() ; + ta.checkin() ; + ta.checkout() ; + ta.setProperty("prop1", "prop1 value V1"); + ta.save() ; + ta.checkin() ; + + // Level 2 + ta.checkout() ; + Node ta2= ta.addNode("A-L2") ; + ta2.setProperty("msg", "test message"); + tb.addNode("B-L2(1)"); + tb.addNode("B-L2(2)"); + // Level 3 + Node ta3 = ta2.addNode("A-L3") ; + Node ta4_1 = ta3.addNode("A-L4_1") ; + Node ta4_2 = ta3.addNode("A-L4_3") ; + s.save() ; + + Query q = s.getWorkspace().getQueryManager().createQuery("SELECT * FROM nt:unstructured WHERE jcr:path LIKE '/A/%'", Query.SQL) ; + QueryResult qr = q.execute() ; + if (IteratorUtils.toList(qr.getNodes()).size()==0) { + log.error("Index is not working"); + } + } + s.logout() ; + } catch (Exception e) { + log.error("Unable to init repo",e); + } + + } + + private Repository getRepository() throws ServletException { + try { + InitialContext ctx = new InitialContext() ; + Context env = (Context) ctx.lookup("java:comp/env"); + Repository repo = (Repository) env.lookup(JCRTagConstants.JNDI_DEFAULT_REPOSITORY); + return repo ; + }catch (Exception e) { + throw new ServletException(e.toString(), e); + } + } + +} \ No newline at end of file Propchange: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/InitServlet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/test/BeanFactoryTest.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/test/BeanFactoryTest.java?rev=171077&view=auto ============================================================================== --- incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/test/BeanFactoryTest.java (added) +++ incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/test/BeanFactoryTest.java Fri May 20 01:13:01 2005 @@ -0,0 +1,48 @@ +/* + * Copyright 2004-2005 The Apache Software Foundation or its licensors, + * as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.taglib.test; + +import org.apache.jackrabbit.taglib.bean.SimpleBeanFactory; +import org.apache.jackrabbit.taglib.bean.SpringBeanFactory; +import org.apache.jackrabbit.taglib.traverser.PreorderTraverser; +import org.apache.jackrabbit.taglib.traverser.Traverser; + +import junit.framework.TestCase; + +/** + * Bean factory test + */ +public class BeanFactoryTest extends TestCase +{ + public void testSpring() { + SpringBeanFactory factory = new SpringBeanFactory() ; + factory.setConfig("jcrtaglib-beans.xml") ; + Traverser traverser = (Traverser) factory.getBean("traverser.preorder") ; + if (traverser==null) { + fail("Traverser not found") ; + } + } + + public void testSimple() { + SimpleBeanFactory factory = new SimpleBeanFactory() ; + Traverser traverser = (Traverser) factory.getBean(PreorderTraverser.class.getName()) ; + if (traverser==null) { + fail("Traverser not found") ; + } + } + +} Propchange: incubator/jackrabbit/trunk/contrib/jcrtaglib/src/test/org/apache/jackrabbit/taglib/test/BeanFactoryTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/jackrabbit/trunk/contrib/jcrtaglib/xdocs/configuration.xml URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcrtaglib/xdocs/configuration.xml?rev=171077&view=auto ============================================================================== --- incubator/jackrabbit/trunk/contrib/jcrtaglib/xdocs/configuration.xml (added) +++ incubator/jackrabbit/trunk/contrib/jcrtaglib/xdocs/configuration.xml Fri May 20 01:13:01 2005 @@ -0,0 +1,95 @@ + + +
+ See:
+ 1. web.xml - /src/examples/web/WEB-INF/web.xml
+ 1. context.xml - /src/examples/web/jcrtaglib.xml
+
+ The default JCR repository must be set under the + following JNDI address: "jcr/repositoryFactory". +
++ The default BeanFactory must be set under the + following JNDI address: "jcr/beanFactory". +
++ The default traverser ID must be set under the + following JNDI address: "jcr/traverser/default" +
++ The default filter ID must be set under the + following JNDI address: "jcr/filter/default" +
++ The default sort ID must be set under the following + JNDI address: "jcr/comparator/default" +
++ The default template Engine ID must be set under the + following JNDI address: + "jcr/template/engine/default" +
++ The default SizeCalculator ID must be set under + the following JNDI address: "jcr/size/default" +
+
+ This information is used when no user and password
+ is provided in jcr:session tag, and the user is not
+ logged through container managed security.
+
+ The default user name must be set under the
+ following JNDI address: "jcr/login/anonuser"
+
+ The password for the default user must be set under
+ the following JNDI address: "jcr/login/anonpwd"
+
+ The BeanFactory is responsible of the creation and
+ lifecycle of beans. It can be a simple implementation or
+ it can be a wrapper of an IoC framework such as
+ Spring (included).
+ Traverser, ItemFilter, ItemComparator and
+ SizeCalculator instances are provided by this class
+ based on the ID. The beanFactory is accessed through a
+ JNDI address (see
+
+ configuration
+
+ ).
+
+ The Traverser is a visitor which walks through the tree
+ and returns a Collection of nodes.
+
+ Included traversers are:
+
+ An ItemFilter implementation is responsible of
+ evaluating whether the given javax.jcr.Item should be
+ included based on the given expression (see filterExp
+ attribute in
+ jcr:nodes
+ ).
+
+ A JEXLItemFilter implementation is provided. It
+ evaluates any javax.jcr.Item based on a JEXL valid
+ expression which returns a Boolean instance. The
+ javax.jcr.Item to be evaluated is added to the
+ JEXLContext under the key "item". A valid JEXL
+ expression would be "item.name.equals('MyNodeName')".
+
+ An ItemComparator implementation is responsible of
+ handling comparison of any javax.jcr.Item based on the
+ given expression.
+
+ A JEXLItemComparator implementation is provided. It
+ compares any javax.jcr.Item based on a JEXL valid
+ expression which returns a comparable instance. The
+ javax.jcr.Item is added to the JEXLContext with the name
+ of "item". A valid JEXL expression would be "item.name".
+
+ A TemplateEngine can be a simple template engine or can + be a wrapper of a powerful template engine such as + Velocity. It writes nodes and properties with the given + template to the given writer. +
++ A SizeCalculator implementation is responsible of + calculating the size that uses a given Node or Property. +
++ JCR tags contains a few Traversers which will help you + on drawing trees. +
++ With this traverser you get a menu with only one + node expanded. +
+
+ <jcr:nodes
+
+ node="/"
+
+ var="node"
+
+ traverserID="org.apache.jackrabbit.tags.traverser.ExpandedNodeTraverser"
+
+ traverserParam="/pathToMyNode/myNode"
+
+ traverserDepth="5">
+
+
+ <c:forEach begin="0" end="${node.depth}">
+
+
+
+ </c:forEach>
+
+ / <c:out value="${node.name}"/><br/>
+
+ </jcr:nodes>
+
+
+ This project contains a set of custom tags that lets you
+ read and display information from a JCR repository.
+ Generated on may 12th, 2005.
+
+ This software requires a JSP container that supports the Java Servlet 2.3 and JavaServer Pages 1.2 specifications. +
++ Download sources. +
+
+ Creates a session for the given repository.
+
+ It creates a jcr Session and stores it in a page scope
+ variable.
+
+ <jcr:session>
+
+ ... Use the session here
+
+ </jcr:session>
+
+
+ Sets the current directory. Relative paths in jcr tags + will be be relative to the current directory. +
+
+
+ <jcr:session>
+
+ <jcr:cd node="/myNode"/>
+
+ ...
+
+ </jcr:cd>
+
+ </jcr:session>
+
+ Iterates through the traversed nodes from the given + node. +
+
+ Using jstl core taglib to show the node's path.
+
+ <jcr:session>
+
+ <jcr:nodes node="/" var="node"
+ traverserDepth="1">
+
+ <c:out value="${node.path}"/>
+
+ </jcr:nodes>
+
+ </jcr:session>
+
Iterates through the properties of the given node.
+
+ Using jstl core taglib to show the property's path.
+
+ <jcr:session>
+
+ <jcr:properties node="/" var="property">
+
+ <c:out value="${property.path}"/>
+
+ </jcr:properties>
+
+ </jcr:session>
+
+ Stores the given node or property in a page context + scoped variable. +
+
+ Displaying the path to a node.
+
+ <jcr:session>
+
+ <jcr:set node="pahtToMyNode/myNode" var="myNode"
+ />
+
+ <c:out value="${myNode.path}"/>
+
+ </jcr:session>
+
+ Displays Node and property values through the given
+ template engine and template.
+
+ See
+
+ configuration
+
+ .
+
+ Displaying a node with the default template engine.
+
+ <jcr:session>
+
+ <jcr:out node="/" />
+
+
+ Displaying a property with the default template
+ engine.
+
+ <jcr:out node="/" property="jcr:primaryType"
+ />
+
+ </jcr:session>
+
Counts the nodes returned by the given Traverser.
+
+ Displaying the number of children for the root node
+ for the default traverser.
+
+ <jcr:session>
+
+ <jcr:count node="/" traverserDepth="1">
+
+ </jcr:session>
+
+ Estimates the cumulative size of the nodes returned by + the given Traverser and displays the value. +
+
+ Displaying the size of a node.
+
+ <jcr:session>
+
+ <jcr:size node="/" traverserDepth="0" /> bytes
+
+
+ Displaying the size of the node plus the children
+ nodes.
+
+ <jcr:size node="/" traverserDepth="1" /> bytes
+
+ </jcr:session>
+
Iterates through the versions of the given node.
+
+ Using jstl core taglib to show the node's path.
+
+ <jcr:session>
+
+ <jcr:versions node="/" var="version">
+
+ <c:out value="${version.created}"/>
+
+ </jcr:versions>
+
+ </jcr:session>
+
Iterates through the query result nodes.
+
+
+ <jcr:session>
+
+ <jcr:query stmt="//*" var="node" lang="xpath">
+
+ Node: >c:out value="${node.path}"/> <br>
+
+ </jcr:query>
+
+ </jcr:session>
+
+ Conditional tag that evaluates the existence of the + given node. +
+
+
+ <jcr:session>
+
+ <jcr:ifPresent node="/pahtToMyNode/myNode" />
+
+ ... do some stuff
+
+ </jcr:ifPresent>
+
+ </jcr:session>
+