001 /*
002 * Copyright (C) 2010 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.jcr.command;
021
022 import org.crsh.cmdline.IntrospectionException;
023 import org.crsh.cmdline.ParameterDescriptor;
024 import org.crsh.cmdline.completers.AbstractPathCompleter;
025 import org.crsh.cmdline.spi.Completer;
026 import org.crsh.cmdline.spi.ValueCompletion;
027 import org.crsh.command.CRaSHCommand;
028
029 import javax.jcr.Node;
030 import javax.jcr.NodeIterator;
031 import javax.jcr.PathNotFoundException;
032 import javax.jcr.Session;
033 import java.util.ArrayList;
034 import java.util.Collection;
035 import java.util.List;
036
037 /**
038 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
039 * @version $Revision$
040 */
041 public abstract class JCRCommand extends CRaSHCommand implements Completer {
042
043 protected JCRCommand() throws IntrospectionException {
044 }
045
046 public ValueCompletion complete(ParameterDescriptor<?> parameter, String prefix) throws Exception {
047 if (parameter.getJavaValueType() == Path.class) {
048
049 final Path path = (Path)getProperty("currentPath");
050 final Session session = (Session)getProperty("session");
051
052 //
053 if (session != null) {
054
055 AbstractPathCompleter<Node> pc = new AbstractPathCompleter<Node>() {
056 @Override
057 protected String getCurrentPath() throws Exception {
058 return path != null ? path.getString() : "/";
059 }
060
061 @Override
062 protected Node getPath(String path) throws Exception {
063 try {
064 return (Node)session.getItem(path);
065 }
066 catch (PathNotFoundException e) {
067 return null;
068 }
069 }
070
071 @Override
072 protected boolean exists(Node path) throws Exception {
073 return path != null;
074 }
075
076 @Override
077 protected boolean isDirectory(Node path) throws Exception {
078 return true;
079 }
080
081 @Override
082 protected boolean isFile(Node path) throws Exception {
083 return false;
084 }
085
086 @Override
087 protected Collection<Node> getChilren(Node path) throws Exception {
088 List<Node> children = new ArrayList<Node>();
089 for (NodeIterator i = path.getNodes();i.hasNext();) {
090 Node child = i.nextNode();
091 children.add(child);
092 }
093 return children;
094 }
095
096 @Override
097 protected String getName(Node path) throws Exception {
098 return path.getName();
099 }
100 };
101
102 //
103 return pc.complete(parameter, prefix);
104 }
105 }
106
107 //
108 return ValueCompletion.create();
109 }
110 }