chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sfermig...@apache.org
Subject svn commit: r899665 [3/4] - in /incubator/chemistry/trunk/chemistry: ./ chemistry-shell/ chemistry-shell/scripts/ chemistry-shell/src/ chemistry-shell/src/main/ chemistry-shell/src/main/java/ chemistry-shell/src/main/java/org/ chemistry-shell/src/main/...
Date Fri, 15 Jan 2010 15:35:18 GMT
Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/cmds/cmis/SetStream.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/cmds/cmis/SetStream.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/cmds/cmis/SetStream.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/cmds/cmis/SetStream.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,66 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.cmds.cmis;
+
+import java.io.File;
+import java.io.FileInputStream;
+
+import org.apache.chemistry.CMISObject;
+import org.apache.chemistry.shell.app.ChemistryApp;
+import org.apache.chemistry.shell.app.ChemistryCommand;
+import org.apache.chemistry.shell.app.Context;
+import org.apache.chemistry.shell.command.Cmd;
+import org.apache.chemistry.shell.command.CommandException;
+import org.apache.chemistry.shell.command.CommandLine;
+import org.apache.chemistry.shell.util.Path;
+import org.apache.chemistry.shell.util.SimplePropertyManager;
+
+@Cmd(syntax="setstream target filename:file", synopsis="Set the given file content as a stream on the current context object")
+public class SetStream extends ChemistryCommand {
+
+    @Override
+    protected void execute(ChemistryApp app, CommandLine cmdLine)
+            throws Exception {
+
+        String target = cmdLine.getParameterValue("target");
+        String filename = cmdLine.getParameterValue("filename");
+
+        Context ctx = app.resolveContext(new Path(target));
+        CMISObject obj = ctx.as(CMISObject.class);
+
+        if (obj == null) {
+            throw new CommandException("Cannot resolve "+target);
+        }
+
+        File file = app.resolveFile(filename);
+        FileInputStream in = new FileInputStream(file);
+        try {
+            new SimplePropertyManager(obj).setStream(in, file.getName());
+        } finally {
+            in.close();
+        }
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/cmds/cmis/SetStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/cmds/cmis/SetStream.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Cmd.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Cmd.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Cmd.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Cmd.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,39 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE})
+public @interface Cmd {
+
+    String syntax();
+    String synopsis();
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Cmd.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Cmd.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Command.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Command.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Command.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Command.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,101 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.chemistry.shell.app.Application;
+import org.apache.chemistry.shell.app.Console;
+
+public abstract class Command {
+
+    protected CommandSyntax syntax;
+    protected String[] aliases;
+    protected String synopsis;
+
+    public Command() {
+        Cmd anno = getClass().getAnnotation(Cmd.class);
+        synopsis = anno.synopsis();
+        syntax = CommandSyntax.parse(anno.syntax());
+        aliases = syntax.getCommandToken().getNames();
+    }
+
+    public String getName() {
+        return aliases[0];
+    }
+
+    public String[] getAliases() {
+        return aliases;
+    }
+
+    public String getSynopsis() {
+        return synopsis;
+    }
+
+    public CommandSyntax getSyntax() {
+        return syntax;
+    }
+
+    public void ensureConnected(Application app) throws CommandException {
+        if (!app.isConnected()) {
+            throw new CommandException("Not connected");
+        }
+    }
+
+    public String getHelp() {
+        URL url = getClass().getResource("/help/"+getName()+".help");
+        if (url == null) {
+            return "N/A";
+        }
+        InputStream in = null;
+        try {
+            in = url.openStream();
+            return IOUtils.toString(in);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try { if (in != null) in.close(); } catch (IOException e) {}
+        }
+        return "N/A";
+    }
+
+    public void print(InputStream in) throws IOException {
+        Console.getDefault().print(in);
+    }
+
+    public void println(String str) {
+        Console.getDefault().println(str);
+    }
+
+    public boolean isLocal() {
+        return true;
+    }
+
+    public abstract void run(Application app, CommandLine cmdLine) throws Exception;
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Command.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/Command.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandException.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandException.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandException.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandException.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,49 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+/**
+ * Exception thrown by commands.
+ */
+public class CommandException extends Exception {
+
+    private static final long serialVersionUID = 1L;
+
+    public CommandException() {
+    }
+
+    public CommandException(String message) {
+        super(message);
+    }
+
+    public CommandException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public CommandException(Throwable cause) {
+        super(cause);
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandLine.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandLine.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandLine.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandLine.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,195 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.chemistry.shell.app.Application;
+import org.apache.chemistry.shell.app.Console;
+import org.apache.chemistry.shell.util.StringUtils;
+
+public class CommandLine {
+
+    protected List<CommandParameter> params;
+    protected Map<String, CommandParameter> map;
+    protected Command cmd;
+
+    public CommandLine(CommandRegistry registry, String cmd) throws CommandException {
+        this(registry, StringUtils.tokenize(cmd));
+    }
+
+    public CommandLine(CommandRegistry registry, String[] args) throws CommandException {
+        if (args.length == 0) {
+            throw new IllegalArgumentException("CommandLine cannot be empty");
+        }
+        map = new HashMap<String, CommandParameter>();
+        params = new ArrayList<CommandParameter>();
+        cmd = registry.getCommand(args[0]);
+        if (cmd == null) {
+            throw new NoSuchCommandException(args[0]);
+        }
+
+        // build params
+        CommandParameter param = new CommandParameter(args[0], cmd.syntax.tokens.get(0));
+        params.add(param);
+
+        int k = 0;
+        for (int i=1; i<args.length; i++) {
+            String key = args[i];
+            if (param != null && param.token.isValueRequired()) {
+                param.setValue(key);
+                param = null;
+            } else {
+                CommandToken token = cmd.syntax.getToken(key);
+                if (token == null) {
+                    token = cmd.syntax.getArgument(k++);
+                    if (token == null) {
+                        throw new CommandSyntaxException(
+                                cmd, "Syntax Error: Extra argument found on position "+i);
+                    }
+                }
+                if (token.isArgument()) {
+                    param = new CommandParameter(token.getName(), token);
+                    param.setValue(key);
+                } else {
+                    param = new CommandParameter(key, token);
+                }
+                params.add(param);
+                map.put(param.token.getName(), param);
+            }
+        }
+        // check if the last parameter has a value if it requires it
+        if (param != null && param.getValue() == null && param.token.isValueRequired()) {
+            throw new CommandSyntaxException(
+                    cmd, "Syntax Error: Value for parameter "+param.key+" is required");
+        }
+        // check if all required options are present - ignore first token which is the command token
+        for (int i=1, len=cmd.syntax.tokens.size(); i<len; i++) {
+            CommandToken token = cmd.syntax.tokens.get(i);
+            if (!token.isOptional()) {
+                if (!map.containsKey(token.getName())) {
+                    throw new CommandSyntaxException(
+                            cmd, "Syntax Error: Missing required parameter: "+token.getName());
+                }
+            }
+        }
+    }
+
+    public CommandParameter getParameter(String key) {
+        return map.get(key);
+    }
+
+    public String getParameterValue(String key) {
+        CommandParameter param = map.get(key);
+        return param == null ? null : param.getValue();
+    }
+
+    public void run(Application app) throws Exception {
+        cmd.run(app, this);
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder buf = new StringBuilder();
+        for (CommandParameter param : params) {
+            buf.append(param.key).append(" ");
+            if (param.token.isValueRequired()) {
+                String value = param.getValue();
+                if (value != null) {
+                    buf.append(value).append(" ");
+                }
+            }
+        }
+        buf.setLength(buf.length()-1);
+        return buf.toString();
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public Command getCommand() {
+        return cmd;
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public CommandParameter getLastParameter() {
+        if (params.isEmpty()) {
+            return null;
+        }
+        if (params.size() == 1) {
+            return null;
+        }
+        return params.get(params.size()-1);
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public List<CommandParameter> getParameters() {
+        return params;
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public List<CommandParameter> getArguments() {
+        List<CommandParameter> result = new ArrayList<CommandParameter>();
+        for (CommandParameter arg : params) {
+            if (arg.token.isArgument()) {
+                result.add(arg);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public Map<String, Object> toMap() {
+        // preserve params order
+        LinkedHashMap<String, Object> args = new LinkedHashMap<String, Object>();
+        int k = 0;
+        for (CommandParameter param : params) {
+            String key = param.getKey();
+            String value = param.getValue();
+            Object val = value;
+            if (key == null) {
+                key = "_"+(k++);
+            }
+            if (CommandToken.FILE.equals(param.token.getValueType())) {
+                val = Console.getDefault().getApplication().resolveFile(value);
+            }
+            args.put(key, val);
+        }
+        return args;
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandLine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandLine.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandParameter.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandParameter.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandParameter.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandParameter.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,69 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+public class CommandParameter {
+
+    protected final String key;
+    protected final CommandToken token;
+
+    protected String value;
+
+    public CommandParameter(String key, CommandToken token) {
+        this.key = key;
+        this.token = token;
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    @Override
+    public String toString() {
+        return key+" = "+ value;
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public CommandToken getToken() {
+        return token;
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public String getValueOrDefault() {
+        return value == null ? token.getDefaultValue() : value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandParameter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandParameter.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandRegistry.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandRegistry.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandRegistry.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandRegistry.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,186 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.chemistry.shell.cmds.base.Cd;
+import org.apache.chemistry.shell.cmds.base.Connect;
+import org.apache.chemistry.shell.cmds.base.Disconnect;
+import org.apache.chemistry.shell.cmds.base.Exit;
+import org.apache.chemistry.shell.cmds.base.Help;
+import org.apache.chemistry.shell.cmds.base.Id;
+import org.apache.chemistry.shell.cmds.base.LCd;
+import org.apache.chemistry.shell.cmds.base.LPopd;
+import org.apache.chemistry.shell.cmds.base.LPushd;
+import org.apache.chemistry.shell.cmds.base.LPwd;
+import org.apache.chemistry.shell.cmds.base.Ll;
+import org.apache.chemistry.shell.cmds.base.Ls;
+import org.apache.chemistry.shell.cmds.base.Match;
+import org.apache.chemistry.shell.cmds.base.Popd;
+import org.apache.chemistry.shell.cmds.base.Pushd;
+import org.apache.chemistry.shell.cmds.base.Pwd;
+import org.apache.chemistry.shell.util.StringUtils;
+
+public class CommandRegistry {
+
+    protected static final CommandRegistry builtinCommands = new CommandRegistry(null);
+    protected final Map<String, Command> commands;
+
+    static {
+        builtinCommands.registerCommand(new Help());
+        builtinCommands.registerCommand(new Exit());
+        builtinCommands.registerCommand(new Connect());
+        builtinCommands.registerCommand(new Disconnect());
+        builtinCommands.registerCommand(new Cd());
+        builtinCommands.registerCommand(new Pushd());
+        builtinCommands.registerCommand(new Popd());
+        builtinCommands.registerCommand(new Ls());
+        builtinCommands.registerCommand(new Pwd());
+        builtinCommands.registerCommand(new Id());
+        builtinCommands.registerCommand(new LCd());
+        builtinCommands.registerCommand(new LPushd());
+        builtinCommands.registerCommand(new LPopd());
+        builtinCommands.registerCommand(new LPwd());
+        builtinCommands.registerCommand(new Ll());
+        builtinCommands.registerCommand(new Match());
+    }
+
+    public CommandRegistry(Map<String, Command> cmds) {
+        commands = new HashMap<String, Command>();
+        if (cmds != null) {
+            commands.putAll(cmds);
+        }
+    }
+
+    public CommandRegistry() {
+        this(builtinCommands.commands);
+    }
+
+    public void registerCommand(Command cmd) {
+        for (String alias : cmd.getAliases()) {
+            commands.put(alias, cmd);
+        }
+    }
+
+    public void unregisterCommand(String name) {
+        Command cmd = commands.remove(name);
+        if (cmd != null) {
+            for (String alias : cmd.getAliases()) {
+                commands.remove(alias);
+            }
+        }
+    }
+
+    public String[] getCommandNames() {
+        return commands.keySet().toArray(new String[commands.size()]);
+    }
+
+    public Command[] getCommands() {
+        return commands.values().toArray(new Command[commands.size()]);
+    }
+
+    public Command getCommand(String name) {
+        return commands.get(name);
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public String[] getCompletionInfo(String line, int offset) {
+        if (offset == -1) {
+            offset = line.length();
+        }
+        // get the word containing the offset
+        int i = offset-1;
+        while (i >=0) {
+            char c = line.charAt(i);
+            if (Character.isWhitespace(c)) {
+                break;
+            }
+            i--;
+        }
+        String word = offset > i && i>=0 ? line.substring(i+1, offset) : "";
+        String prefix = line.substring(0, i).trim();
+        if (prefix.length() == 0) {
+           return new String[] {CommandToken.COMMAND, word};
+        }
+        String[] segments = StringUtils.tokenize(prefix);
+        String cmdName = segments[0];
+        Command cmd = getCommand(cmdName);
+        if (cmd == null) {
+            return null;
+        }
+        i = segments.length-1;
+        CommandToken token = cmd.syntax.getToken(segments[i]);
+        if (token == null) {
+            i--;
+            int k = 0;
+            while (i > 0) {
+                token = cmd.syntax.getToken(segments[i]);
+                if (token != null) {
+
+                }
+                 i--;
+                 k++;
+             }
+            token = cmd.syntax.getArgument(k);
+        }
+        if (token == null) {
+            return null;
+        }
+        return new String[] {token.getValueType(), word} ;
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public static CommandRegistry getBuiltinCommands() {
+        return builtinCommands;
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public static Command getBuiltinCommand(String name) {
+        return builtinCommands.getCommand(name);
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public static void registerBuiltinCommand(Command cmd) {
+        builtinCommands.registerCommand(cmd);
+    }
+
+    /**
+     * Not used. Will probably be removed.
+     */
+    public static void unregisterBuiltinCommand(Command cmd) {
+        builtinCommands.unregisterCommand(cmd.getName());
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandRegistry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandRegistry.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntax.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntax.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntax.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntax.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,118 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.chemistry.shell.jline.CompositeCompletor;
+import org.apache.chemistry.shell.util.StringUtils;
+
+/**
+ * cmd [-opt|-o:type?defValue] [name:type]
+ * <p>
+ * Supported types: file, dir, command, item. See {@link CompositeCompletor}
+ */
+public class CommandSyntax {
+
+    protected final List<CommandToken> tokens = new ArrayList<CommandToken>();
+    protected final List<CommandToken> args = new ArrayList<CommandToken>();
+    protected final HashMap<String, CommandToken> map = new HashMap<String, CommandToken>();
+
+    /**
+     * Static factory.
+     */
+    public static CommandSyntax parse(String text) {
+        String[] tokens = StringUtils.tokenize(text);
+        if (tokens.length == 0) {
+            throw new IllegalArgumentException("cannot parse empty command lines");
+        }
+        CommandSyntax syntax = new CommandSyntax();
+        if (tokens.length == 0) {
+            return syntax;
+        }
+        CommandToken tok = CommandToken.parseCommand(tokens[0]);
+        syntax.addToken(tok);
+        for (int i=1; i<tokens.length; i++) {
+            tok = CommandToken.parseArg(tokens[i]);
+            syntax.addToken(tok);
+        }
+        return syntax;
+    }
+
+    public CommandToken getCommandToken() {
+        return tokens.get(0);
+    }
+
+    public List<CommandToken> getArguments() {
+        return args;
+    }
+
+    public CommandToken getArgument(int index) {
+        if (index >= args.size()) {
+            return null;
+        }
+        return args.get(index);
+    }
+
+    public List<CommandToken> getTokens() {
+        return tokens;
+    }
+
+    public CommandToken getToken(int i) {
+        return tokens.get(i);
+    }
+
+    public CommandToken getToken(String key) {
+        return map.get(key);
+    }
+
+    /**
+     * Gets all parameter keys.
+     */
+    public String[] getParameterKeys() {
+        ArrayList<String> keys = new ArrayList<String>();
+        for (int i=1,len=tokens.size(); i<len; i++) { // skip first token
+            CommandToken token = tokens.get(i);
+            if (!token.isArgument()) {
+                keys.addAll(Arrays.asList(token.getNames()));
+            }
+        }
+        return keys.toArray(new String[keys.size()]);
+    }
+
+    public void addToken(CommandToken tok) {
+        tokens.add(tok);
+        for (int i=0; i<tok.getNames().length; i++) {
+            map.put(tok.getNames()[i], tok);
+        }
+        if (tok.isArgument()) {
+            args.add(tok);
+        }
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntax.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntax.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntaxException.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntaxException.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntaxException.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntaxException.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,46 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+public class CommandSyntaxException extends CommandException {
+
+    private static final long serialVersionUID = 1L;
+
+    protected final Command cmd;
+
+    public CommandSyntaxException(Command cmd) {
+        this(cmd, "Syntax Error");
+    }
+
+    public CommandSyntaxException(Command cmd, String message) {
+        super(message);
+        this.cmd = cmd;
+    }
+
+    public Command getCommand() {
+        return cmd;
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntaxException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandSyntaxException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandToken.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandToken.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandToken.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandToken.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,107 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+import org.apache.chemistry.shell.util.StringUtils;
+
+public class CommandToken {
+
+    public static final String COMMAND = "command";
+    public static final String ANY = "*";
+    public static final String FILE = "file";
+    public static final String DOCUMENT = "document";
+
+    private String[] names;
+    private String valueType; // null | string | command | file | doc
+    private String defaultValue;
+    private boolean isArgument;
+    private boolean isOptional;
+
+    public static CommandToken parseCommand(String text) {
+        CommandToken tok = new CommandToken();
+        tok.names = StringUtils.split(text, '|', false);
+        tok.valueType = COMMAND;
+        return tok;
+    }
+
+    public static CommandToken parseArg(String text) {
+        CommandToken tok = new CommandToken();
+        if (text.startsWith("[")) {
+            tok.isOptional = true;
+            text = text.substring(1, text.length()-1);
+        }
+        int p = text.indexOf(':');
+        if (p > -1) {
+            tok.valueType = text.substring(p+1);
+            text = text.substring(0, p);
+            p = tok.valueType.indexOf('?');
+            if (p > -1) {
+                tok.defaultValue = tok.valueType.substring(p+1);
+                tok.valueType = tok.valueType.substring(0, p);
+            }
+        }
+        // parse names in text
+        tok.names = StringUtils.split(text, '|', true);
+        tok.isArgument = !tok.names[0].startsWith("-");
+        return tok;
+    }
+
+    public boolean isValueRequired() {
+        return valueType != COMMAND && valueType != null && !isArgument;
+    }
+
+    public boolean isCommand() {
+        return valueType == COMMAND;
+    }
+
+    public boolean isOptional() {
+        return isOptional;
+    }
+
+    public boolean isFlag() {
+        return names[0].startsWith("-");
+    }
+
+    public boolean isArgument() {
+        return isArgument;
+    }
+
+    public String getDefaultValue() {
+        return defaultValue;
+    }
+
+    public String getValueType() {
+        return valueType;
+    }
+
+    public String getName() {
+        return names[0];
+    }
+
+    public String[] getNames() {
+        return names;
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandToken.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/CommandToken.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/ExitException.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/ExitException.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/ExitException.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/ExitException.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,31 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+public class ExitException extends CommandException {
+
+    private static final long serialVersionUID = 1L;
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/ExitException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/ExitException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/NoSuchCommandException.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/NoSuchCommandException.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/NoSuchCommandException.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/NoSuchCommandException.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,35 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.command;
+
+public class NoSuchCommandException extends CommandException {
+
+    private static final long serialVersionUID = 1L;
+
+    public NoSuchCommandException(String cmdName) {
+        super("Command Not Found: "+cmdName);
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/NoSuchCommandException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/command/NoSuchCommandException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CommandCompletor.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CommandCompletor.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CommandCompletor.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CommandCompletor.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,46 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.jline;
+
+import java.util.List;
+
+import jline.SimpleCompletor;
+import org.apache.chemistry.shell.command.CommandRegistry;
+
+public class CommandCompletor extends SimpleCompletor {
+
+    protected final CommandRegistry registry;
+
+    public CommandCompletor(CommandRegistry registry) {
+        super(registry.getCommandNames());
+        this.registry = registry;
+    }
+
+    @Override
+    public int complete(String buffer, int cursor, List clist) {
+        return super.complete(buffer, cursor, clist);
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CommandCompletor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CommandCompletor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CompositeCompletor.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CompositeCompletor.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CompositeCompletor.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CompositeCompletor.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,155 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.jline;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import jline.ArgumentCompletor;
+import jline.Completor;
+import jline.CursorBuffer;
+import jline.FileNameCompletor;
+import jline.ArgumentCompletor.WhitespaceArgumentDelimiter;
+
+import org.apache.chemistry.shell.command.Command;
+import org.apache.chemistry.shell.command.CommandRegistry;
+import org.apache.chemistry.shell.command.CommandToken;
+
+public class CompositeCompletor implements Completor {
+
+    private final CommandRegistry registry;
+    private final CommandCompletor completor;
+    private final JLineConsole console;
+    private final Map<String, Completor> completors = new HashMap<String, Completor>();
+    private final Map<String,Completor> paramCompletors = new HashMap<String, Completor>();
+
+    public CompositeCompletor(JLineConsole console, CommandRegistry registry) {
+        this.registry = registry;
+        this.console = console;
+        completor = new CommandCompletor(registry);
+        completors.put("command", completor);
+        completors.put("file", new FileNameCompletor());
+        completors.put("dir", new DirectoryCompletor());
+        completors.put("item", new ContextItemCompletor());
+
+        //classname completor is parsing system jars at startup. also it doesn't work on mac
+        // I get a java.util.zip.ZipException: error in opening zip file because it tries to load a lib file as a jar
+        // disable it for now
+        //completors.put("class", new ClassNameCompletor());
+    }
+
+    public void setCompletor(String name, Completor completor) {
+        completors.put(name, completor);
+    }
+
+    public void removeCompletor(String name) {
+        completors.remove(name);
+    }
+
+    public int complete(String buffer, int cursor, List candidates) {
+        CursorBuffer buf = console.getReader().getCursorBuffer();
+        ArgumentCompletor.ArgumentList list = new WhitespaceArgumentDelimiter().delimit(
+                buffer, cursor);
+        String[] args = list.getArguments();
+        String argText = list.getCursorArgument();
+        int argIndex = list.getCursorArgumentIndex();
+        int offset = list.getArgumentPosition();
+//        ArgumentList list =  new ArgumentList(buf.toString(), cursor);
+//        String[] args = list.args;
+//        int argIndex = list.argIndex;
+//        int offset = list.offset;
+//        String argText = list.getArg();
+
+        // the prefix of the current arg (the chars on the left of the cursor)
+        String argPrefix = argText == null ? null : argText.substring(0, offset);
+        if (argIndex == 0) {
+            int ret = completor.complete(argPrefix, offset, candidates);
+            return ret + (list.getBufferPosition() - offset);
+        } else {
+//            System.out.println();
+//            System.out.println("TODO completion");
+//            System.out.println("# argtext: "+argText);
+//            System.out.println("# args: "+Arrays.asList(args));
+//            System.out.println("# argIndex: "+argIndex);
+//            System.out.println("# offset: "+offset);
+//            System.out.println("# argPrefix: "+argPrefix);
+
+            Command cmd = registry.getCommand(args[0]);
+            if (cmd == null) {
+                return -1; // no such command
+            }
+            Completor comp = null;
+            // get previous token and test if it requires a value
+            if (argIndex > 1) { // if argIndex is 1 the previous is the command token if 0 then there is no previous
+                CommandToken token = cmd.getSyntax().getToken(args[argIndex-1]);
+                if (token != null) {
+                    if (token.isValueRequired()) {
+                        if (token.getValueType() != null) {
+                            comp = completors.get(token.getValueType());
+                        } else {
+                            return -1; // no completion available
+                        }
+                    }
+                }
+            }
+            // the previous token has no completion available.
+            // test for argument and option completion
+            // 1. if command has arguments find out the next argument index and complete if argument support completion
+            int k = 0;
+            for (int i=1; i<argIndex; i++) {
+                CommandToken token = cmd.getSyntax().getToken(args[i]);
+                if (token != null && !token.isArgument()) { // skip options with values
+                    if (token.isValueRequired()) {
+                        i++; // skip value too
+                    }
+                    continue;
+                }
+                k++;
+            }
+            CommandToken token = cmd.getSyntax().getArgument(k);
+            if (token != null && token.getValueType() != null) {
+                comp = completors.get(token.getValueType());
+            }
+            // 2. if completor not found try to complete parameter names
+            if (comp == null) {
+                comp = paramCompletors.get(cmd.getName());
+                if (comp == null) { // build param completor
+                    comp = new ParameterNameCompletor(cmd);
+                    // TODO we really need to cache this?
+                    paramCompletors.put(cmd.getName(), comp);
+                }
+            }
+            // complete if completor found
+            if (comp != null) {
+                int ret =  comp.complete(argPrefix, offset, candidates);
+                return ret + (list.getBufferPosition() - offset);
+            }
+            return -1;
+        }
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CompositeCompletor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/CompositeCompletor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ContextItemCompletor.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ContextItemCompletor.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ContextItemCompletor.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ContextItemCompletor.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,71 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.jline;
+
+import java.util.List;
+
+import jline.Completor;
+
+import org.apache.chemistry.shell.app.Console;
+import org.apache.chemistry.shell.app.Context;
+import org.apache.chemistry.shell.util.Path;
+
+public class ContextItemCompletor implements Completor {
+
+    protected void collectNames(String[] keys, String prefix, List candidates) {
+        for (String key : keys) {
+            if (key.startsWith(prefix)) {
+                key = key.replace(" ", "\\ ");
+                candidates.add(key);
+            }
+        }
+    }
+
+    public int complete(String buffer, int cursor, List candidates) {
+        if (buffer == null) {
+            buffer = "";
+        }
+
+        Context ctx;
+        Path path = new Path(buffer);
+        String prefix = path.getLastSegment();
+        if (prefix == null) {
+            ctx = Console.getDefault().getApplication().getContext();
+            prefix = "";
+        } else if (path.segmentCount() == 1) {
+            ctx = Console.getDefault().getApplication().getContext();
+        } else {
+            path = path.removeLastSegments(1);
+            ctx = Console.getDefault().getApplication().resolveContext(path);
+        }
+        if (ctx != null) {
+            collectNames(ctx.entries(), prefix, candidates);
+            return buffer.length()-prefix.length();
+        } else {
+            return -1;
+        }
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ContextItemCompletor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ContextItemCompletor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/DirectoryCompletor.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/DirectoryCompletor.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/DirectoryCompletor.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/DirectoryCompletor.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,52 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.jline;
+
+import java.io.File;
+import java.util.List;
+
+import jline.FileNameCompletor;
+
+public class DirectoryCompletor extends FileNameCompletor {
+
+    @Override
+    public int matchFiles(String buffer, String translated, File[] entries,
+            List candidates) {
+        if (entries == null) {
+            return -1;
+        }
+
+        for (File entry : entries) {
+            if (entry.getAbsolutePath().startsWith(translated) && entry.isDirectory()) {
+                candidates.add(entry.getName());
+            }
+        }
+
+        int index = buffer.lastIndexOf(File.separator);
+
+        return index + File.separator.length();
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/DirectoryCompletor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/DirectoryCompletor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ItemNameCompletor.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ItemNameCompletor.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ItemNameCompletor.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ItemNameCompletor.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,85 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.jline;
+
+import java.util.List;
+
+import jline.Completor;
+
+import org.apache.chemistry.shell.util.Path;
+
+/**
+ * Auto-completes remote item names.
+ */
+public class ItemNameCompletor implements Completor {
+
+    private final JLineConsole console;
+
+    public ItemNameCompletor(JLineConsole console) {
+        this.console = console;
+    }
+
+    public int complete(String buffer, int cursor, List candidates) {
+        if (buffer == null) {
+            buffer = "";
+        }
+        Path path = new Path(buffer);
+        String prefix = path.getLastSegment();
+        if (path.hasTrailingSeparator()) {
+            prefix = "";
+        } else {
+            path = path.removeLastSegments(1);
+        }
+
+        if (prefix == null) {
+            prefix = "";
+        }
+
+        try {
+            String[] names = console.getApplication().getContext().entries();
+            if (names == null || names.length == 0) {
+                return -1;
+            }
+
+            if (buffer.length() == 0) {
+                for (String name : names) {
+                    candidates.add(name);
+                }
+            } else {
+                for (String name : names) {
+                    if (name.startsWith(prefix)) {
+                        candidates.add(name);
+                    }
+                }
+            }
+
+            return buffer.length()-prefix.length();
+
+        } catch (Exception e) {
+            return -1;
+        }
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ItemNameCompletor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ItemNameCompletor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/JLineConsole.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/JLineConsole.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/JLineConsole.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/JLineConsole.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,125 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.jline;
+
+import java.io.IOException;
+
+import jline.CandidateListCompletionHandler;
+import jline.CompletionHandler;
+import jline.ConsoleReader;
+
+import org.apache.chemistry.shell.app.Application;
+import org.apache.chemistry.shell.app.Console;
+import org.apache.chemistry.shell.command.CommandException;
+import org.apache.chemistry.shell.command.ExitException;
+import org.apache.chemistry.shell.util.ColorHelper;
+
+public class JLineConsole extends Console {
+
+    protected ConsoleReader console;
+
+    public JLineConsole() throws IOException {
+        if (JLineConsole.instance != null) {
+            throw new IllegalAccessError("Console is already instantiated");
+        }
+        JLineConsole.instance = this;
+        console = new ConsoleReader();
+        CompletionHandler ch = console.getCompletionHandler();
+        if (ch instanceof CandidateListCompletionHandler) {
+            ((CandidateListCompletionHandler) ch).setAlwaysIncludeNewline(false);
+        }
+        ColorHelper.enable();
+    }
+
+    public ConsoleReader getReader() {
+        return console;
+    }
+
+    /**
+     * Executes line.
+     *
+     * @return false if the users has issued an "exit" command, true otherwise
+     */
+    protected boolean execute(String line) throws Exception {
+        try {
+            runCommand(line);
+        } catch (ExitException e) {
+            return false;
+        } catch (CommandException e) {
+            console.printString(e.getMessage());
+        }
+        return true;
+    }
+
+    @Override
+    public void start(Application app) throws IOException {
+        super.start(app);
+        console.setDefaultPrompt("|> ");
+        // register completors
+        console.addCompletor(new CompositeCompletor(this, app.getCommandRegistry()));
+        String line = console.readLine();
+        while (line != null) {
+            line = line.trim();
+            try {
+                if (line.length() > 0) {
+                    if (!execute(line)) {
+                        break;
+                    }
+                    println();
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            line = console.readLine();
+        }
+        console.printString("Bye");
+        console.printNewline();
+    }
+
+    @Override
+    public String promptPassword() throws IOException {
+        return console.readLine(new Character('*'));
+    }
+
+    @Override
+    public void updatePrompt() {
+        if (app.isConnected()) {
+            String path = app.getContext().getPath().getLastSegment();
+            if (path == null) {
+                path = "/";
+            }
+            console.setDefaultPrompt("|"+app.getHost()+":"+path+"> ");
+        } else {
+            console.setDefaultPrompt("|> ");
+        }
+    }
+
+    @Override
+    public void println() throws IOException {
+        console.flushConsole();
+        console.printNewline();
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/JLineConsole.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/JLineConsole.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ParameterNameCompletor.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ParameterNameCompletor.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ParameterNameCompletor.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ParameterNameCompletor.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,37 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.jline;
+
+import jline.SimpleCompletor;
+
+import org.apache.chemistry.shell.command.Command;
+
+public class ParameterNameCompletor extends SimpleCompletor {
+
+    public ParameterNameCompletor(Command cmd) {
+        super(cmd.getSyntax().getParameterKeys());
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ParameterNameCompletor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/jline/ParameterNameCompletor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/ColorHelper.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/ColorHelper.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/ColorHelper.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/ColorHelper.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,191 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import jline.ANSIBuffer;
+
+/**
+ * An utility class to add ANSI colors to objects in folder listings.
+ * <p>
+ * Disabled by default. Enabled only when used in interactive mode.
+ */
+public class ColorHelper {
+
+    public static final int OFF = 0;
+    public static final int BOLD = 1;
+    public static final int UNDERSCORE = 4;
+    public static final int BLINK = 5;
+    public static final int REVERSE = 7;
+    public static final int CONCEALED = 8;
+    public static final int FG_BLACK = 30;
+    public static final int FG_RED = 31;
+    public static final int FG_GREEN = 32;
+    public static final int FG_YELLOW = 33;
+    public static final int FG_BLUE = 34;
+    public static final int FG_MAGENTA = 35;
+    public static final int FG_CYAN = 36;
+    public static final int FG_WHITE = 37;
+    public static final char ESC = 27;
+
+    protected static final Map<String,Integer> ansiCodes = new HashMap<String, Integer>();
+    protected static final Map<String,Integer> colorMap = new HashMap<String, Integer>();
+
+    protected static boolean enabled;
+
+    static {
+        ansiCodes.put("white", FG_WHITE);
+        ansiCodes.put("black", FG_BLACK);
+        ansiCodes.put("blue", FG_BLUE);
+        ansiCodes.put("cyan", FG_CYAN);
+        ansiCodes.put("magenta", FG_MAGENTA);
+        ansiCodes.put("green", FG_GREEN);
+        ansiCodes.put("red", FG_RED);
+        ansiCodes.put("yellow", FG_YELLOW);
+        ansiCodes.put("blink", BLINK);
+        ansiCodes.put("bold", BOLD);
+        ansiCodes.put("underscore", UNDERSCORE);
+        ansiCodes.put("reverse", REVERSE);
+        ansiCodes.put("concealed", CONCEALED);
+
+        Properties props = new Properties();
+        try {
+            String mapStr = System.getProperty("chemistry.shell.colorMap");
+            if (mapStr != null) {
+                props.load(new ByteArrayInputStream(mapStr.getBytes()));
+            } else {
+                URL url = ColorHelper.class.getClassLoader().getResource("color.properties");
+                if (url != null) {
+                    InputStream in = url.openStream();
+                    props.load(in);
+                    in.close();
+                }
+            }
+            for (Map.Entry<Object,Object> entry : props.entrySet()) {
+                String val = (String) entry.getValue();
+                Integer code = ansiCodes.get(val);
+                if (code == null) {
+                    System.err.println("Skipping unknown color code: "+val);
+                } else {
+                    colorMap.put((String) entry.getKey(), code);
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            System.err.println("Failed to load color map");
+        }
+    }
+
+    // Utility class.
+    private ColorHelper() {
+    }
+
+    public static void enable() {
+        enabled = true;
+    }
+
+    private static boolean supportsColor() {
+        String osName = System.getProperty("os.name");
+        return enabled && !osName.toLowerCase().contains("windows");
+    }
+
+    public static String decorateName(String name, String color) {
+        Integer code = ansiCodes.get(color);
+        return code != null ? decorateName(name, code.intValue()) : name;
+    }
+
+    public static String decorateNameByType(String name, String type) {
+        Integer color = colorMap.get(type);
+        if (color != null) {
+            return decorateName(name, color);
+        }
+        return name;
+    }
+
+    public static String decorateName(String name, int color) {
+        // don't add any color for crappy terminals
+        if (!supportsColor()) {
+            return name;
+        }
+        ANSIBuffer buf = new ANSIBuffer();
+        return buf.attrib(name, color).toString();
+    }
+
+    public static String blue(String name) {
+        return decorateName(name, FG_BLUE);
+    }
+
+    public static String green(String name) {
+        return decorateName(name, FG_GREEN);
+    }
+
+    public static String yellow(String name) {
+        return decorateName(name, FG_YELLOW);
+    }
+
+    public static String red(String name) {
+        return decorateName(name, FG_RED);
+    }
+
+    public static String cyan(String name) {
+        return decorateName(name, FG_CYAN);
+    }
+
+    public static String black(String name) {
+        return decorateName(name, FG_BLACK);
+    }
+
+    public static String magenta(String name) {
+        return decorateName(name, FG_MAGENTA);
+    }
+
+    public static String white(String name) {
+        return decorateName(name, FG_WHITE);
+    }
+
+    public static String blink(String name) {
+        return decorateName(name, BLINK);
+    }
+
+    public static String bold(String name) {
+        return decorateName(name, BOLD);
+    }
+
+    public static String underscore(String name) {
+        return decorateName(name, UNDERSCORE);
+    }
+
+    public static String reverse(String name) {
+        return decorateName(name, REVERSE);
+    }
+
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/ColorHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/ColorHelper.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/MimeTypeHelper.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/MimeTypeHelper.java?rev=899665&view=auto
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/MimeTypeHelper.java (added)
+++ incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/MimeTypeHelper.java Fri Jan 15 15:35:14 2010
@@ -0,0 +1,62 @@
+/*
+ * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ *   Bogdan Stefanescu (bs@nuxeo.com), Nuxeo
+ *   Stefane Fermigier (sf@nuxeo.com), Nuxeo
+ *   Florent Guillaume (fg@nuxeo.com), Nuxeo
+ */
+
+package org.apache.chemistry.shell.util;
+
+public class MimeTypeHelper {
+
+    private MimeTypeHelper() {
+    }
+
+    public static String getMimeType(String fileName) {
+
+        if (fileName == null) {
+            return "application/octet-stream";
+        } else if (fileName.endsWith(".doc")) {
+            return "application/msword";
+        } else if (fileName.endsWith(".xls")) {
+            return "application/vnd.ms-excel";
+        } else if (fileName.endsWith(".ppt")) {
+            return "application/vnd.ms-powerpoint";
+        } else if (fileName.endsWith(".txt")) {
+            return "text/plain";
+        } else if (fileName.endsWith(".html")) {
+            return "text/html";
+        } else if (fileName.endsWith(".xml")) {
+            return "text/xml";
+        } else if (fileName.endsWith(".jpg")) {
+            return "image/jpeg";
+        } else if (fileName.endsWith(".jpeg")) {
+            return "image/jpeg";
+        } else if (fileName.endsWith(".gif")) {
+            return "image/gif";
+        } else if (fileName.endsWith(".odt")) {
+            return "application/vnd.oasis.opendocument.text";
+        } else if (fileName.endsWith(".zip")) {
+            return "application/zip";
+        } else {
+            return "application/octet-stream";
+        }
+    }
+}

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/MimeTypeHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/chemistry/trunk/chemistry/chemistry-shell/src/main/java/org/apache/chemistry/shell/util/MimeTypeHelper.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message