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.spi.Completer;
025    import org.crsh.command.CRaSHCommand;
026    
027    import javax.jcr.Item;
028    import javax.jcr.Node;
029    import javax.jcr.NodeIterator;
030    import javax.jcr.Session;
031    import java.util.Collections;
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    /**
036     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
037     * @version $Revision$
038     */
039    public abstract class JCRCommand extends CRaSHCommand implements Completer {
040    
041      protected JCRCommand() throws IntrospectionException {
042      }
043    
044      public Map<String, Boolean> complete(ParameterDescriptor<?> parameter, String prefix) throws Exception {
045        if (parameter.getJavaValueType() == Path.class) {
046    
047          Path path = (Path)getProperty("currentPath");
048          Session session = (Session)getProperty("session");
049    
050          //
051          if (session != null) {
052    
053            Node relative = null;
054    
055            if (prefix.length() == 0 || prefix.charAt(0) != '/') {
056              if (path != null) {
057                Item item = session.getItem(path.getString());
058                if (item instanceof Node) {
059                  relative = (Node)item;
060                }
061              }
062            } else {
063              relative = session.getRootNode();
064              prefix = prefix.substring(1);
065            }
066    
067            // Now navigate using the prefix
068            if (relative != null) {
069              for (int index = prefix.indexOf('/');index != -1;index = prefix.indexOf('/')) {
070                String name = prefix.substring(0, index);
071                if (relative.hasNode(name)) {
072                  relative = relative.getNode(name);
073                  prefix = prefix.substring(index + 1);
074                } else {
075                  return Collections.emptyMap();
076                }
077              }
078            }
079    
080            // Compute the next possible completions
081            Map<String, Boolean> completions = new HashMap<String, Boolean>();
082            for (NodeIterator i = relative.getNodes(prefix + '*');i.hasNext();) {
083              Node child = i.nextNode();
084              String suffix = child.getName().substring(prefix.length());
085              if (child.hasNodes()) {
086                completions.put(suffix + '/', false);
087    
088              } else {
089                completions.put(suffix, true);
090              }
091            }
092    
093            //
094            return completions;
095          }
096        }
097    
098        //
099        return Collections.emptyMap();
100      }
101    }