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