001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.cms.util;
023:
024: import javax.jcr.Node;
025: import javax.jcr.Property;
026: import javax.jcr.PropertyIterator;
027:
028: /**
029: * Helper class for dealing with Nodes. Similar to common file utility functions, for now
030: *
031: * @author <a href="mailto:roy@jboss.org">Roy Russo</a> Date: Jun 13, 2005 Time: 2:37:45 PM
032: */
033: public class NodeUtil {
034: public static final String PATH_SEPARATOR = "/";
035:
036: /**
037: * Returns the parent basePath of the Node.
038: *
039: * @param sPath
040: * @return
041: */
042: public static String getParentPath(String sPath) throws Exception {
043: String parentPath = "";
044:
045: if (!isValidPath(sPath)) {
046: throw new Exception("Not a valid basePath " + sPath);
047: }
048:
049: String[] pathChunks = sPath.split(PATH_SEPARATOR);
050:
051: if (pathChunks.length == 2) {
052: return PATH_SEPARATOR;
053: }
054:
055: for (int i = 0; i < pathChunks.length - 1; i++) {
056: if (!pathChunks[i].equals("")) {
057: parentPath += PATH_SEPARATOR + pathChunks[i];
058: }
059: }
060: return parentPath;
061: }
062:
063: /**
064: * Returns the node name for a given basePath.
065: *
066: * @param path
067: * @return
068: */
069: public static String getNodeName(String path) throws Exception {
070: String[] pathChunks = path.split(PATH_SEPARATOR);
071: if (!isValidPath(path)) {
072: throw new Exception("Not a valid basePath!");
073: }
074: return pathChunks[pathChunks.length - 1];
075: }
076:
077: /**
078: * Validates basePath syntactically.
079: *
080: * @param sPath
081: * @return
082: */
083: public static boolean isValidPath(String sPath) {
084: if ((sPath == null) || (sPath.equals(PATH_SEPARATOR))
085: || (sPath.endsWith(PATH_SEPARATOR))
086: || (!sPath.startsWith(PATH_SEPARATOR))
087: || (sPath.equals(""))) {
088: return false;
089: }
090: return true;
091: }
092:
093: /**
094: * Prints node properties to std out.
095: *
096: * @param node
097: */
098: public static void print(Node node) {
099: try {
100: System.out.println("################### NAME: "
101: + node.getName() + " ###################");
102: System.out.println("################### PATH: "
103: + node.getPath() + " ###################");
104: PropertyIterator pi = node.getProperties();
105: while (pi.hasNext()) {
106: Property p = pi.nextProperty();
107: if (!p.getName().equals("jcr:mixinTypes")
108: && !p.getName().equals("jcr:predecessors")) {
109: System.out.println(p.getName() + " = "
110: + p.getString());
111: }
112: }
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116: }
117: }
|