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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils.helper;
038:
039: import java.util.Locale;
040: import java.util.Properties;
041: import org.netbeans.installer.utils.StringUtils;
042: import org.netbeans.installer.utils.SystemUtils;
043:
044: /**
045: *
046: * @author ks152834
047: */
048: public class NbiProperties extends Properties {
049: public NbiProperties() {
050: super ();
051: }
052:
053: public NbiProperties(final Properties properties) {
054: super ();
055:
056: putAll(properties);
057: }
058:
059: @Override
060: public String getProperty(final String name) {
061: return getProperty(name, SystemUtils.getCurrentPlatform(),
062: Locale.getDefault());
063: }
064:
065: public String getProperty(final String name,
066: final Platform platform, final Locale locale) {
067: final String[] platformParts = getPlatformParts(platform);
068: final String[] localeParts = getLocaleParts(locale);
069:
070: for (int i = platformParts.length; i >= 0; i--) {
071: for (int j = localeParts.length; j >= 0; j--) {
072: final String platformString = StringUtils.asString(
073: platformParts, 0, i, "-");
074: final String localeString = StringUtils.asString(
075: localeParts, 0, j, "_");
076:
077: final String candidateName = name
078: + (platformString.equals("") ? "" : "."
079: + platformString)
080: + (localeString.equals("") ? "" : "."
081: + localeString);
082:
083: final String value = super .getProperty(candidateName);
084: if (value != null) {
085: return value;
086: }
087: }
088: }
089:
090: return null;
091: }
092:
093: @Override
094: public Object setProperty(final String name, final String value) {
095: return setProperty(name, value, SystemUtils
096: .getCurrentPlatform(), Locale.getDefault());
097: }
098:
099: public Object setProperty(final String name, final String value,
100: final Platform platform, final Locale locale) {
101: String realName = name;
102:
103: if (platform != null) {
104: realName += "." + platform.toString();
105: }
106:
107: if (locale != null) {
108: realName += "." + locale.toString();
109: }
110:
111: return super .setProperty(realName, value);
112: }
113:
114: // private //////////////////////////////////////////////////////////////////////
115: private String[] getPlatformParts(final Platform platform) {
116: if (platform == null) {
117: return new String[0];
118: }
119:
120: if (platform.getOsFamily() != null) {
121: if (platform.getHardwareArch() != null) {
122: if (platform.getOsVersion() != null) {
123: if (platform.getOsFlavor() != null) {
124: return new String[] { platform.getOsFamily(),
125: platform.getHardwareArch(),
126: platform.getOsVersion(),
127: platform.getOsFlavor() };
128: }
129:
130: return new String[] { platform.getOsFamily(),
131: platform.getHardwareArch(),
132: platform.getOsVersion() };
133: }
134:
135: return new String[] { platform.getOsFamily(),
136: platform.getHardwareArch() };
137: }
138:
139: return new String[] { platform.getOsFamily() };
140: }
141:
142: return new String[0];
143: }
144:
145: private String[] getLocaleParts(final Locale locale) {
146: if (locale == null) {
147: return new String[0];
148: }
149:
150: if (!locale.getLanguage().equals("")) {
151: if (!locale.getCountry().equals("")) {
152: if (!locale.getVariant().equals("")) {
153: return new String[] { locale.getLanguage(),
154: locale.getCountry(), locale.getVariant() };
155: }
156:
157: return new String[] { locale.getLanguage(),
158: locale.getCountry() };
159: }
160:
161: return new String[] { locale.getLanguage() };
162: }
163:
164: return new String[0];
165: }
166: }
|