001package org.apache.maven.cli;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024
025public class CleanArgument
026{
027    public static String[] cleanArgs( String[] args )
028    {
029        List<String> cleaned = new ArrayList<>();
030
031        StringBuilder currentArg = null;
032
033        for ( String arg : args )
034        {
035            boolean addedToBuffer = false;
036
037            if ( arg.startsWith( "\"" ) )
038            {
039                // if we're in the process of building up another arg, push it and start over.
040                // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
041                if ( currentArg != null )
042                {
043                    cleaned.add( currentArg.toString() );
044                }
045
046                // start building an argument here.
047                currentArg = new StringBuilder( arg.substring( 1 ) );
048                addedToBuffer = true;
049            }
050
051            // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
052            if ( addedToBuffer && arg.endsWith( "\"" ) )
053            {
054                String cleanArgPart = arg.substring( 0, arg.length() - 1 );
055
056                // if we're building an argument, keep doing so.
057                if ( currentArg != null )
058                {
059                    // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
060                    if ( addedToBuffer )
061                    {
062                        currentArg.setLength( currentArg.length() - 1 );
063                    }
064                    // otherwise, we trim the trailing " and append to the buffer.
065                    else
066                    {
067                        // TODO: introducing a space here...not sure what else to do but collapse whitespace
068                        currentArg.append( ' ' ).append( cleanArgPart );
069                    }
070
071                    cleaned.add( currentArg.toString() );
072                }
073                else
074                {
075                    cleaned.add( cleanArgPart );
076                }
077
078                currentArg = null;
079                addedToBuffer = false;
080                continue;
081            }
082
083            // if we haven't added this arg to the buffer, and we ARE building an argument
084            // buffer, then append it with a preceding space...again, not sure what else to
085            // do other than collapse whitespace.
086            // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
087            if ( !addedToBuffer )
088            {
089                if ( currentArg != null )
090                {
091                    currentArg.append( ' ' ).append( arg );
092                }
093                else
094                {
095                    cleaned.add( arg );
096                }
097            }
098        }
099
100        if ( currentArg != null )
101        {
102            cleaned.add( currentArg.toString() );
103        }
104
105        int cleanedSz = cleaned.size();
106
107        String[] cleanArgs;
108
109        if ( cleanedSz == 0 )
110        {
111            cleanArgs = args;
112        }
113        else
114        {
115            cleanArgs = cleaned.toArray( new String[cleanedSz] );
116        }
117
118        return cleanArgs;
119    }
120
121
122}