Path: blob/master/src/jdk.jartool/share/classes/sun/tools/jar/CommandLine.java
41161 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jar;2627import java.io.IOException;28import java.io.Reader;29import java.io.FileReader;30import java.io.BufferedReader;31import java.io.StreamTokenizer;32import java.util.List;33import java.util.ArrayList;3435/**36* Various utility methods for processing Java tool command line arguments.37*38* <p><b>This is NOT part of any API supported by Oracle. If39* you write code that depends on this, you do so at your own risk.40* This code and its internal interfaces are subject to change or41* deletion without notice.</b>42*/43public class CommandLine {44/**45* Process Win32-style command files for the specified command line46* arguments and return the resulting arguments. A command file argument47* is of the form '@file' where 'file' is the name of the file whose48* contents are to be parsed for additional arguments. The contents of49* the command file are parsed using StreamTokenizer and the original50* '@file' argument replaced with the resulting tokens. Recursive command51* files are not supported. The '@' character itself can be quoted with52* the sequence '@@'.53*/54public static String[] parse(String[] args)55throws IOException56{57List<String> newArgs = new ArrayList<>(args.length);58for (int i = 0; i < args.length; i++) {59String arg = args[i];60if (arg.length() > 1 && arg.charAt(0) == '@') {61arg = arg.substring(1);62if (arg.charAt(0) == '@') {63newArgs.add(arg);64} else {65loadCmdFile(arg, newArgs);66}67} else {68newArgs.add(arg);69}70}71return newArgs.toArray(new String[newArgs.size()]);72}7374private static void loadCmdFile(String name, List<String> args)75throws IOException76{77Reader r = new BufferedReader(new FileReader(name));78StreamTokenizer st = new StreamTokenizer(r);79st.resetSyntax();80st.wordChars(' ', 255);81st.whitespaceChars(0, ' ');82st.commentChar('#');83st.quoteChar('"');84st.quoteChar('\'');85while (st.nextToken() != StreamTokenizer.TT_EOF) {86args.add(st.sval);87}88r.close();89}90}919293