001    /*
002     * Copyright (C) 2012 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.cli.impl.descriptor.IntrospectionException;
023    import org.crsh.cli.descriptor.ParameterDescriptor;
024    import org.crsh.cli.completers.AbstractPathCompleter;
025    import org.crsh.cli.spi.Completer;
026    import org.crsh.cli.spi.Completion;
027    import org.crsh.command.CRaSHCommand;
028    
029    import javax.jcr.Node;
030    import javax.jcr.NodeIterator;
031    import javax.jcr.RepositoryException;
032    import javax.jcr.Session;
033    import java.util.ArrayList;
034    import java.util.Collection;
035    import java.util.List;
036    
037    public abstract class JCRCommand extends CRaSHCommand implements PathCompleter {
038    
039      protected JCRCommand() throws IntrospectionException {
040      }
041    
042      public Completion complete(ParameterDescriptor parameter, String prefix) throws Exception {
043        if (parameter.getCompleterType() == PathCompleter.class) {
044    
045          final Path path = (Path)getProperty("currentPath");
046          final Session session = (Session)getProperty("session");
047    
048          //
049          if (session != null) {
050    
051            AbstractPathCompleter<Node> pc = new AbstractPathCompleter<Node>() {
052              @Override
053              protected String getCurrentPath() throws Exception {
054                return path != null ? path.getValue() : "/";
055              }
056    
057              @Override
058              protected Node getPath(String path) throws Exception {
059                try {
060                  return (Node)session.getItem(path);
061                }
062                catch (RepositoryException e) {
063                  return null;
064                }
065              }
066    
067              @Override
068              protected boolean exists(Node path) throws Exception {
069                return path != null;
070              }
071    
072              @Override
073              protected boolean isDirectory(Node path) throws Exception {
074                return true;
075              }
076    
077              @Override
078              protected boolean isFile(Node path) throws Exception {
079                return false;
080              }
081    
082              @Override
083              protected Collection<Node> getChilren(Node path) throws Exception {
084                List<Node> children = new ArrayList<Node>();
085                for (NodeIterator i = path.getNodes();i.hasNext();) {
086                  Node child = i.nextNode();
087                  children.add(child);
088                }
089                return children;
090              }
091    
092              @Override
093              protected String getName(Node path) throws Exception {
094                return path.getName();
095              }
096            };
097    
098            //
099            return pc.complete(parameter, prefix);
100          }
101        }
102    
103        //
104        return Completion.create();
105      }
106    }