001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.makeproject.api.runprofiles;
043:
044: import java.util.Enumeration;
045: import java.util.StringTokenizer;
046: import java.util.Vector;
047:
048: /**
049: * Generic manipulation and management of unix-like environment variables.
050: * Allows easy setup of environment variables in order to be passed to
051: * Runtime.exec().
052: */
053:
054: public class Env {
055: private Vector environ;
056:
057: public Env() {
058: environ = new Vector();
059: }
060:
061: public void removeAll() {
062: environ = new Vector();
063: }
064:
065: /**
066: * Remove the entry with the given name
067: */
068: public void removeByName(String name) {
069: if (name == null)
070: return;
071:
072: String[] entry = getenvAsPair(name);
073: environ.removeElement(entry);
074: }
075:
076: /**
077: * Returns the whole entry in the form of <code>name=value</code>.
078: */
079: public String getenvEntry(String name) {
080: String value = getenv(name);
081: if (value != null)
082: return name + "=" + value; // NOI18N
083: else
084: return null;
085: }
086:
087: /**
088: * Returns the entry in the form of String[2]
089: */
090: public String[] getenvAsPair(String name) {
091: for (Enumeration e = environ.elements(); e.hasMoreElements();) {
092: String[] nameValue = (String[]) e.nextElement();
093: if (nameValue[0].equals(name)) {
094: return nameValue;
095: }
096: }
097: return null;
098: }
099:
100: /**
101: * Returns just the value, like getenv(3).
102: */
103: public String getenv(String name) {
104: for (Enumeration e = environ.elements(); e.hasMoreElements();) {
105: String[] nameValue = (String[]) e.nextElement();
106: if (nameValue[0].equals(name)) {
107: return nameValue[1];
108: }
109: }
110: return null;
111: }
112:
113: public String toString() {
114: String[] envStrings = getenv();
115: boolean addSep = false;
116: StringBuilder envString = new StringBuilder();
117: for (int i = 0; i < envStrings.length; i++) {
118: if (addSep)
119: envString.append(";"); // NOI18N
120: envString.append(envStrings[i]);
121: addSep = true;
122: }
123: return envString.toString();
124: }
125:
126: public String encode() {
127: return toString();
128: }
129:
130: public void decode(String envlist) {
131: StringTokenizer tokenizer = new StringTokenizer(envlist, " ;"); // NOI18N
132: while (tokenizer.hasMoreTokens()) {
133: putenv(tokenizer.nextToken());
134: }
135: }
136:
137: public boolean equals(Object o) {
138: boolean eq = false;
139: if (o instanceof Env) {
140: Env env = (Env) o;
141: eq = toString().equals(env.toString());
142: }
143: return eq;
144: }
145:
146: /**
147: * Takes <code>name=value</code> format.
148: */
149: public void putenv(String entry) {
150: int equalx = entry.indexOf('='); // NOI18N
151: if (equalx == -1) {
152: System.err.println("Env.putenv(): odd entry '" + entry
153: + "'"); // NOI18N
154: return;
155: }
156: String name = entry.substring(0, equalx);
157: String value = entry.substring(equalx + 1);
158: putenv(name, value);
159: }
160:
161: /**
162: * Sets or creates a new environment variable
163: */
164: public void putenv(String name, String value) {
165: String[] entry = getenvAsPair(name);
166: if (entry != null)
167: entry[1] = value;
168: else
169: environ.add(new String[] { name, value });
170: }
171:
172: /**
173: * Convert the internal representation to an array of Strings
174: * Suitable for passing to Runtime.exec.
175: */
176: public String[] getenv() {
177: String array[] = new String[environ.size()];
178:
179: int index = 0;
180: for (Enumeration e = environ.elements(); e.hasMoreElements();) {
181: String[] nameValue = (String[]) e.nextElement();
182: array[index++] = nameValue[0] + "=" + nameValue[1]; // NOI18N
183: }
184: return array;
185: }
186:
187: /**
188: * Converts the internal representation to an array of variable/value pairs
189: */
190: public String[][] getenvAsPairs() {
191: String array[][] = new String[environ.size()][2];
192:
193: int index = 0;
194: for (Enumeration e = environ.elements(); e.hasMoreElements();) {
195: String[] nameValue = (String[]) e.nextElement();
196: array[index++] = nameValue;
197: }
198: return array;
199: }
200:
201: public void assign(Env env) {
202: removeAll();
203: String[][] pairs = env.getenvAsPairs();
204: for (int i = 0; i < pairs.length; i++)
205: putenv(pairs[i][0], pairs[i][1]);
206: }
207:
208: /**
209: * Clone the environment creating an identical copy.
210: */
211: public Env cloneEnv() {
212: return (Env) clone();
213: }
214:
215: public Object clone() {
216: Env clone = new Env();
217: String[][] pairs = getenvAsPairs();
218: for (int i = 0; i < pairs.length; i++)
219: clone.putenv(pairs[i][0], pairs[i][1]);
220: return clone;
221: }
222: }
|