1 package ch
.cyberduck
.cli
;
4 * Copyright (c) 2002-2014 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.io
21 import ch
.cyberduck
.core
.Host
;
22 import ch
.cyberduck
.core
.HostParser
;
23 import ch
.cyberduck
.core
.Protocol
;
24 import ch
.cyberduck
.core
.ProtocolFactory
;
25 import ch
.cyberduck
.core
.transfer
.Transfer
;
26 import ch
.cyberduck
.core
.transfer
.TransferAction
;
28 import org
.apache
.commons
.cli
.CommandLine
;
29 import org
.apache
.commons
.cli
.Option
;
30 import org
.apache
.commons
.lang3
.StringUtils
;
32 import java
.util
.Arrays
;
33 import java
.util
.HashSet
;
34 import java
.util
.List
;
40 public class TerminalOptionsInputValidator
{
42 private Console console
= new Console();
44 public boolean validate(final CommandLine input
) {
45 for(Option o
: input
.getOptions()) {
46 if(Option
.UNINITIALIZED
== o
.getArgs()) {
49 if(o
.hasOptionalArg()) {
52 if(o
.getArgs() != o
.getValuesList().size()) {
53 console
.printf("Missing argument for option %s%n", o
.getLongOpt());
57 final TerminalAction action
= TerminalActionFinder
.get(input
);
59 console
.printf("%s%n", "Missing argument");
62 if(input
.hasOption(TerminalOptionsBuilder
.Params
.existing
.name())) {
63 final String arg
= input
.getOptionValue(TerminalOptionsBuilder
.Params
.existing
.name());
64 if(null == TransferAction
.forName(arg
)) {
65 final Set
<TransferAction
> actions
= new HashSet
<TransferAction
>(TransferAction
.forTransfer(Transfer
.Type
.download
));
66 actions
.add(TransferAction
.cancel
);
67 console
.printf("Invalid argument '%s' for option %s. Must be one of %s%n",
68 arg
, TerminalOptionsBuilder
.Params
.existing
.name(), Arrays
.toString(actions
.toArray()));
73 if(!validate(arg
, Transfer
.Type
.download
)) {
78 if(!validate(arg
, Transfer
.Type
.upload
)) {
83 if(!validate(arg
, Transfer
.Type
.sync
)) {
88 if(!validate(arg
, Transfer
.Type
.copy
)) {
98 if(!validate(input
.getOptionValue(action
.name()))) {
105 if(!validate(input
.getOptionValue(action
.name()))) {
113 private boolean validate(final String arg
, final Transfer
.Type type
) {
114 final List
<TransferAction
> actions
= TransferAction
.forTransfer(type
);
115 if(!actions
.contains(TransferAction
.forName(arg
))) {
116 console
.printf("Invalid argument '%s' for option %s. Must be one of %s%n",
117 arg
, TerminalOptionsBuilder
.Params
.existing
.name(), Arrays
.toString(actions
.toArray()));
126 protected boolean validate(final String uri
) {
127 if(uri
.indexOf("://", 0) != -1) {
128 final Protocol protocol
= ProtocolFactory
.forName(uri
.substring(0, uri
.indexOf("://", 0)));
129 if(null == protocol
) {
130 console
.printf("Missing protocol in URI %s%n", uri
);
134 final Host host
= HostParser
.parse(uri
);
135 switch(host
.getProtocol().getType()) {
141 if(StringUtils
.isBlank(host
.getHostname())) {
142 console
.printf("Missing hostname in URI %s%n", uri
);
146 if(StringUtils
.isBlank(host
.getDefaultPath())) {
147 console
.printf("Missing path in URI %s%n", uri
);