project/com.nokia.carbide.cpp.epoc.engine/src/com/nokia/carbide/internal/cpp/epoc/engine/model/SimpleArgList.java
changeset 0 fb279309251b
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.carbide.internal.cpp.epoc.engine.model;
       
    19 
       
    20 import com.nokia.carbide.internal.api.cpp.epoc.engine.dom.IASTListNode;
       
    21 import com.nokia.carbide.internal.api.cpp.epoc.engine.dom.IASTLiteralTextNode;
       
    22 import com.nokia.cpp.internal.api.utils.core.*;
       
    23 
       
    24 import java.util.ArrayList;
       
    25 import java.util.List;
       
    26 
       
    27 /**
       
    28  * Manage a list of arguments, which quoted strings but not escapes
       
    29  * or parentheses.  This is what the makmake and bldmake tools do,  
       
    30  * Leading and trailing space is retained.
       
    31  *
       
    32  */
       
    33 public class SimpleArgList {
       
    34 	private List<String> argv;
       
    35 	private String initial;
       
    36 	private String terminal;
       
    37 
       
    38 	public SimpleArgList(String text) {
       
    39 		String trimmed = text.trim();
       
    40 		this.initial = text.substring(0, text.indexOf(trimmed));
       
    41 		this.terminal = text.substring(text.length() - this.initial.length() - trimmed.length());
       
    42 		this.argv = splitArguments(trimmed);
       
    43 		
       
    44 	}
       
    45 	
       
    46 	public SimpleArgList(String initial, List<String> newArgs, String terminal) {
       
    47 		Check.checkArg(initial);
       
    48 		Check.checkArg(terminal);
       
    49 		Check.checkArg(newArgs);
       
    50 		this.initial = initial;
       
    51 		this.terminal = terminal;
       
    52 		this.argv = newArgs;
       
    53 	}
       
    54 
       
    55 	public SimpleArgList(String initial, IASTListNode<IASTLiteralTextNode> newArgs, String terminal) {
       
    56 		Check.checkArg(initial);
       
    57 		Check.checkArg(terminal);
       
    58 		Check.checkArg(newArgs);
       
    59 		this.initial = initial;
       
    60 		this.terminal = terminal;
       
    61 		this.argv = new ArrayList<String>();
       
    62 		for (IASTLiteralTextNode node : newArgs)
       
    63 			argv.add(node.getValue());
       
    64 	}
       
    65 
       
    66 	/* (non-Javadoc)
       
    67 	 * @see java.lang.Object#toString()
       
    68 	 */
       
    69 	@Override
       
    70 	public String toString() {
       
    71 		return initial + catenate(argv) + terminal;
       
    72 	}
       
    73 	
       
    74 	private String catenate(List<String> argv) {
       
    75 		StringBuffer buffer = new StringBuffer();
       
    76 		boolean first = true;
       
    77 		for (String arg : argv) { 
       
    78 			if (first)
       
    79 				first = false;
       
    80 			else
       
    81 				buffer.append(' ');
       
    82 			if (arg.matches(".*\\s.*")) { //$NON-NLS-1$
       
    83 				buffer.append('"');
       
    84 				buffer.append(arg);
       
    85 				buffer.append('"');
       
    86 			} else {
       
    87 				buffer.append(arg);
       
    88 			}
       
    89 		}
       
    90 		return buffer.toString();
       
    91 	}
       
    92 
       
    93 	/**
       
    94 	 * Split a string into arguments.
       
    95 	 */
       
    96 	private List<String> splitArguments(String string) {
       
    97 		List<String> args = new ArrayList<String>();
       
    98 		StringBuilder current = new StringBuilder();
       
    99 		int idx = 0;
       
   100 		boolean quoted = false;
       
   101 		while (idx < string.length()) {
       
   102 			char ch = string.charAt(idx);
       
   103 			if (ch == '"') {
       
   104 				quoted = !quoted;
       
   105 				current.append(ch);
       
   106 				idx++;
       
   107 			} else if (!quoted && Character.isWhitespace(ch)) {
       
   108 				if (current.length() > 0) {
       
   109 					// remove any quotes
       
   110 					args.add(current.toString().replaceAll("\"", "")); //$NON-NLS-1$ //$NON-NLS-2$
       
   111 					current.setLength(0);
       
   112 					idx++;				}
       
   113 				while (idx < string.length() && Character.isWhitespace(string.charAt(idx)))
       
   114 					idx++;
       
   115 			} else {
       
   116 				current.append(ch);
       
   117 				idx++;
       
   118 			}
       
   119 		}
       
   120 		if (current.length() > 0)
       
   121 			args.add(current.toString());
       
   122 		return args;
       
   123 	}
       
   124 
       
   125 	/* (non-Javadoc)
       
   126 	 * @see com.nokia.carbide.internal.api.cpp.epoc.engine.dom.makefile.ICommand#getArgv()
       
   127 	 */
       
   128 	public List<String> getArgv() {
       
   129 		return argv;
       
   130 	}
       
   131 	
       
   132 }