cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lorinb...@apache.org
Subject [36/51] [partial] [BlackBerry10] Added support for new platform
Date Tue, 07 May 2013 15:24:41 GMT
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6831bed4/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/globalization/Util.java
----------------------------------------------------------------------
diff --git a/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/globalization/Util.java b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/globalization/Util.java
new file mode 100644
index 0000000..340876e
--- /dev/null
+++ b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/globalization/Util.java
@@ -0,0 +1,721 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cordova.globalization;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Hashtable;
+import java.util.TimeZone;
+
+import net.rim.device.api.i18n.DateFormat;
+import net.rim.device.api.i18n.Locale;
+import net.rim.device.api.i18n.SimpleDateFormat;
+import net.rim.device.api.system.NonPersistableObjectException;
+import net.rim.device.api.system.PersistentObject;
+import net.rim.device.api.system.PersistentStore;
+import net.rim.device.api.util.StringMatch;
+import net.rim.device.api.util.StringUtilities;
+import net.rim.device.api.compress.GZIPInputStream;
+
+import org.apache.cordova.json4j.JSONArray;
+import org.apache.cordova.json4j.JSONObject;
+import org.apache.cordova.json4j.internal.Parser;
+import org.apache.cordova.util.StringUtils;
+import org.apache.cordova.util.Logger;
+
+public class Util {
+
+    /**
+     * Provides manual date string parsing
+     *
+     * @param d
+     *            Date string
+     * @param p
+     *            Pattern string
+     * @return Calendar
+     */
+    public static Calendar dateParserBB(String d, String p) {
+        String time = "";
+        String date = d;
+        String delimiter = Resources.DATEDELIMITER; // "-"
+        try {
+            // replace '/' with '-' (to compensate for delimiters '/' and '-' in
+            // string)
+            date = date.replace('/', '-');
+
+            // extract time first
+            if (date.indexOf(':') > 0) {
+                time = date.substring(date.indexOf(':') - 2, date.length())
+                        .trim();
+                date = date.substring(0, date.indexOf(':') - 2).trim();
+            }
+
+            // determine string delimiter
+            if (date.indexOf(delimiter) == -1) { // is not in short format
+                delimiter = Resources.SPACE; // " "
+            }
+
+            // split date into sections
+            JSONArray str = Util.split(date, delimiter);
+            if (str == null) {
+                throw new Exception(); // incorrect format
+            }
+
+            // remove day of week and other unwanted characters -- will
+            // automatically be set in calendar object
+            str = Util.removeDayOfWeek(str);
+
+            // convert month string into integer: if applicable
+            str = Util.convertMonthString(str);
+
+            // use pattern to determine order of dd, mm, yyyy. If no pattern
+            // will use Locale Default order
+            Hashtable patternFmt = Util.getDatePattern(p);
+
+            // create calendar object
+            Calendar c = Calendar.getInstance(TimeZone.getDefault());
+
+            // set calendar instance:
+            c.set(Calendar.YEAR, Integer.parseInt(removeSymbols(str
+                    .getString(Integer.parseInt(patternFmt.get("year")
+                            .toString())))));
+            c.set(Calendar.MONTH, Integer.parseInt(removeSymbols(str
+                    .getString(Integer.parseInt(patternFmt.get("month")
+                            .toString())))));
+            c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(removeSymbols(str
+                    .getString(Integer.parseInt(patternFmt.get("day")
+                            .toString())))));
+
+            // set time if applicable
+            if (time.length() > 0) {
+                JSONArray t = Util.split(time, Resources.TIMEDELIMITER);
+                // determine if 12hour or 24hour clock
+                int am_pm = getAmPm(t.getString(t.length() - 1).toString());
+                if (!t.isNull(0)) {
+                    c.set(Calendar.HOUR,
+                            Integer.parseInt(removeSymbols(t.getString(0))));
+                }
+                if (!t.isNull(1)) {
+                    c.set(Calendar.MINUTE,
+                            Integer.parseInt(removeSymbols(t.getString(1))));
+                }
+                if (!t.isNull(2)) {
+                    c.set(Calendar.SECOND,
+                            Integer.parseInt(removeSymbols(t.getString(2))));
+                }
+                if (am_pm != -1) {
+                    c.set(Calendar.AM_PM, am_pm);
+                }
+            }
+            return c;
+        } catch (Exception e) {
+        }
+        return null;
+    }
+
+    /**
+     * Returns a pattern string for formatting and parsing dates according to
+     * the client's user preferences.
+     *
+     * @param options
+     *            JSONArray options (user pattern)
+     *
+     * @return String (String}: The date and time pattern for formatting and
+     *         parsing dates. The patterns follow Unicode Technical Standard #35
+     *         http://unicode.org/reports/tr35/tr35-4.html
+     *
+     * @throws GlobalizationError
+     */
+    public static String getBlackBerryDatePattern(JSONArray options)
+            throws GlobalizationError {
+
+        try {
+            // default user preference for date
+            String fmtDate = ((SimpleDateFormat) DateFormat
+                    .getInstance(DateFormat.DATE_SHORT)).toPattern();
+            // default user preference for time
+            String fmtTime = ((SimpleDateFormat) DateFormat
+                    .getInstance(DateFormat.TIME_SHORT)).toPattern();
+            // default SHORT date/time format. ex. dd/MM/yyyy h:mma
+            String fmt = fmtDate + Resources.SPACE + fmtTime;
+
+            // get Date value + options (if available)
+            if (options.getJSONObject(0).length() > 1) {
+                // options were included. get formatLength option
+                if (!((JSONObject) options.getJSONObject(0).get(
+                        Resources.OPTIONS)).isNull(Resources.FORMATLENGTH)) {
+                    String fmtOpt = (String) ((JSONObject) options
+                            .getJSONObject(0).get(Resources.OPTIONS))
+                            .get(Resources.FORMATLENGTH);
+                    // medium
+                    if (fmtOpt.equalsIgnoreCase(Resources.MEDIUM)) {
+                        // default user preference for date
+                        fmtDate = ((SimpleDateFormat) DateFormat
+                                .getInstance(DateFormat.DATE_MEDIUM))
+                                .toPattern();
+                        // default user preference for time
+                        fmtTime = ((SimpleDateFormat) DateFormat
+                                .getInstance(DateFormat.TIME_MEDIUM))
+                                .toPattern();
+                    } else if (fmtOpt.equalsIgnoreCase(Resources.LONG)) { // long/full
+                        // default user preference for date
+                        fmtDate = ((SimpleDateFormat) DateFormat
+                                .getInstance(DateFormat.DATE_LONG)).toPattern();
+                        // default user preference for time
+                        fmtTime = ((SimpleDateFormat) DateFormat
+                                .getInstance(DateFormat.TIME_LONG)).toPattern();
+                    } else if (fmtOpt.equalsIgnoreCase(Resources.FULL)) { // long/full
+                        // default user preference for date
+                        fmtDate = ((SimpleDateFormat) DateFormat
+                                .getInstance(DateFormat.DATE_FULL)).toPattern();
+                        // default user preference for time
+                        fmtTime = ((SimpleDateFormat) DateFormat
+                                .getInstance(DateFormat.TIME_FULL)).toPattern();
+                    }
+                }
+
+                // return pattern type
+                fmt = fmtDate + Resources.SPACE + fmtTime;
+                if (!((JSONObject) options.getJSONObject(0).get(
+                        Resources.OPTIONS)).isNull(Resources.SELECTOR)) {
+                    String selOpt = (String) ((JSONObject) options
+                            .getJSONObject(0).get(Resources.OPTIONS))
+                            .get(Resources.SELECTOR);
+                    if (selOpt.equalsIgnoreCase(Resources.DATE)) {
+                        fmt = fmtDate;
+                    } else if (selOpt.equalsIgnoreCase(Resources.TIME)) {
+                        fmt = fmtTime;
+                    }
+                }
+            }
+            return fmt;
+        } catch (Exception ge) {
+        }
+        return null;
+    }
+
+    /**
+     * Returns a JSONArray of either the names of the months or days of the week
+     * according to the client's user preferences and calendar. Note: Months
+     * will be in order from Jan - Dec, while days of week will begin on random
+     * day due to Locale time differences of defined long date values
+     *
+     * @param item
+     *            String item (days of week or months)
+     * @param pattern
+     *            String pattern (pattern to parse item)
+     * @return JSONArray The array of names starting from either the first month
+     *         in the year or the first day of the week.
+     */
+    public static JSONArray getDateNameString(String item, String pattern) {
+        JSONArray value = new JSONArray();
+
+        // multipliers
+        long day = 1000 * 60 * 60 * 24; // 86,400,000
+        long startDay = day * 3; // starting three days in to avoid locale
+                                 // differences
+        long month = day * 31; // 2,678,400,000
+
+        SimpleDateFormat fmt = new SimpleDateFormat(pattern,
+                Locale.getDefault());
+        Date d = new Date();
+        try {
+            if (item.equalsIgnoreCase(Resources.MONTHS)) {
+                for (int x = 0; x < 13; x++) {
+                    d = new Date(startDay + (month * x));
+                    // testing short Month first
+                    if (!value.contains(fmt.format(d).toString())
+                            && !value.isEmpty()) {
+                        // add day into array
+                        value.put(fmt.format(d).toString());
+                    } else if (value.isEmpty()) {
+                        // Initialize array
+                        value.put(fmt.format(d).toString());
+                    }
+                }
+            } else { // Days
+                for (int x = 3; x < 11; x++) {
+                    d = new Date(day * x);
+                    // testing short day first
+                    if (!value.contains(fmt.format(d).toString())
+                            && !value.isEmpty()) {
+                        // add day into array
+                        value.put(fmt.format(d).toString());
+                    } else if (value.isEmpty()) {
+                        // Initialize array
+                        value.put(fmt.format(d).toString());
+                    }
+                }
+            }
+            return value;
+        } catch (Exception e) {
+        }
+        return null;
+    }
+
+    /**
+     * Parses a String formatted as a percent or currency removing the symbol
+     * returns the corresponding number.
+     *
+     * @return String Corresponding number
+     *
+     * @throws Exception
+     */
+    public static String removeSymbols(String s) throws Exception {
+        StringBuffer sb = new StringBuffer(s.trim());
+        try {
+            // begin removing all characters before string
+            for (int x = 0; x < sb.length(); x++) {
+                if (Character.isDigit(sb.charAt(x))) {
+                    x = sb.length() - 1; // end loop
+                } else {
+                    sb.deleteCharAt(x);
+                }
+            }
+            // begin removing all characters after string
+            for (int x = sb.length() - 1; x > -1; x--) {
+                if (Character.isDigit(sb.charAt(x))) {
+                    x = 0; // end loop
+                } else {
+                    sb.deleteCharAt(x);
+                }
+            }
+            return sb.toString().trim();
+        } catch (Exception e) {
+        }
+        return null;
+    }
+
+    /**
+     * Splits string into a JSONArray Note: Other options are to use
+     * org.apache.cordova.util.StringUtils.split(String strString, String
+     * strDelimiter)
+     *
+     * @param s
+     *            String s (String to split)
+     * @param delimiter
+     *            String delimiter (String used to split s)
+     * @return JSONArray: String objects
+     */
+    public static JSONArray split(String s, String delimiter) {
+        JSONArray result = new JSONArray();
+        String str = s;
+        try {
+            int p = s.indexOf(delimiter);
+            if (p != -1) {
+                while (p != -1) {
+                    result.put(str.substring(0, p).trim());
+                    if (p + 1 <= str.length()) {
+                        str = str.substring(p + 1);
+                    } else {
+                        // delimiter is the last character in the string
+                        str = "";
+                        break;
+                    }
+                    p = str.indexOf(delimiter);
+                }
+                // add remaining characters if any
+                if (str.length() > 0) {
+                    result.put(str);
+                }
+                return result;
+            }
+            return null; // incorrect delimiter
+        } catch (Exception e) {
+        }
+        return null; // error thrown
+    }
+
+    /**
+     * If applicable; removes day of week and other unwanted characters from
+     * JSONArray
+     *
+     * @param s
+     *            JSONArray s (List of date properties)
+     *
+     * @return JSONArray:
+     *          [key: day], [int: position]
+     *          [key: month], [int: position]
+     *          [key: year], [int: position]
+     */
+    public static JSONArray removeDayOfWeek(JSONArray s) {
+        JSONArray str = s;
+        JSONArray list;
+        try {
+            // get week names in short format //$NON-NLS-1$
+            list = getDateNameString(Resources.DAYS, "EEE");
+
+            // remove day of week from JSONArray
+            for (int x = 0; x < str.length(); x++) {
+                // do manual checking due to short or long version of week
+                // validate entry is not already an int
+                if (!Character.isDigit(str.getString(x).charAt(0))) {
+                    // run though short weeks to get match and remove
+                    StringMatch sm;
+                    for (int y = 0; y < list.length(); y++) {
+                        sm = new StringMatch(list.getString(y));
+                        if (sm.indexOf(str.getString(x)) != -1) {// week found
+                            str.removeElementAt(x); // remove day of week
+                            return str;
+                        }
+                        // if end of list reached, load long version of names
+                        // and rerun loop
+                        if (y == list.length() - 1) {
+                            y = -1;
+                            // get week names in long format//$NON-NLS-1$
+                            list = getDateNameString(Resources.DAYS, "EEEE");
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+        }// exception caught, return initial JSONArray
+        return s;
+    }
+
+    /**
+     * If applicable; converts Month String into number
+     *
+     * @param s
+     *            JSONArray s (List of date properties)
+     *
+     * @return JSONArray:
+     *          [key: day], [int: position]
+     *          [key: month], [int: position]
+     *          [key: year], [int: position]
+     */
+    public static JSONArray convertMonthString(JSONArray s) {
+        JSONArray str = s;
+        JSONArray list;
+        try {
+            // get month names in short format
+            list = getDateNameString(Resources.MONTHS, "MMM");
+
+            // convert month string into integer if applicable
+            for (int x = 0; x < str.length(); x++) {
+                // do manual checking due to short or long version of months
+                // validate entry is not already an int
+                if (!Character.isDigit(str.getString(x).charAt(0))) {
+                    // run though short format months to get index number
+                    StringMatch sm;
+                    for (int y = 0; y < list.length(); y++) {
+                        sm = new StringMatch(list.getString(y));
+                        if (sm.indexOf(str.getString(x)) != -1) {// month found
+                            // replace string with integer
+                            str.setElementAt(String.valueOf(y), x);
+                            return str;
+                        }
+                        // if end of list reached load long version of names and
+                        // rerun loop
+                        if (y == list.length() - 1) {
+                            y = -1;
+                            // get month names in long format
+                            list = getDateNameString(Resources.MONTHS, "MMMM");
+                        }
+                    }
+                }
+            }
+            return str;
+        } catch (Exception e) {
+        }// exception caught, return initial JSONArray
+        return s;
+    }
+
+    /**
+     * Determine if am_pm present and return value. if not return -1
+     *
+     * @param time
+     *            String time (time string of date object)
+     *
+     * @return int
+     *          -1 = am_pm not present
+     *           0 = am
+     *           1 = pm
+     */
+    public static int getAmPm(String time) {
+        // multipliers
+        long am_pm = 0; // pm
+        long am_pm_12 = 43200000; // am
+        int value = 1;
+        boolean reloop = true;
+
+        Date d = new Date(am_pm);
+
+        for (int x = 0; x < Resources.AM_PMFORMATS.length; x++) {
+            SimpleDateFormat fmt = new SimpleDateFormat(
+                    Resources.AM_PMFORMATS[x], Locale.getDefault());
+
+            StringMatch sm = new StringMatch(fmt.format(d).toString());
+
+            if (sm.indexOf(time) != -1) {
+                return value;
+            }
+
+            if (x == Resources.AM_PMFORMATS.length - 1 && reloop) {
+                d = new Date(am_pm_12);
+                value = 0;
+                x = -1;
+                reloop = false;
+            }
+        }
+        return -1;
+    }
+
+    /**
+     * Returns Hashtable indicating position of dd, MM, and yyyy in string.
+     * Position will either be 0, 1, or 2
+     *
+     * @param p
+     *            String pattern
+     *
+     * @return Hashtable:
+     *          [key: day], [int: position]
+     *          [key: month], [int: position]
+     *          [key: year], [int: position]
+     *
+     * @throws Exception
+     */
+    public static Hashtable getDatePattern(String p) {
+        Hashtable result = new Hashtable();
+
+        if (p.length() <= 0) {
+            // default device preference for date
+            p = ((SimpleDateFormat) DateFormat
+                    .getInstance(DateFormat.DATE_SHORT)).toPattern();
+        }
+
+        // get positions
+        int day = p.indexOf('d'); //$NON-NLS-1$
+        int month = p.indexOf('M'); //$NON-NLS-1$
+        int year = p.indexOf('y'); //$NON-NLS-1$
+        // int weekDay = p.indexOf('E'); //removed in removeDayOfWeek()
+
+        if (year < day && day < month) { // yyyy/dd/mmmm
+            year = 0;
+            day = 1;
+            month = 2;
+        } else if (day < month && month < year) { // dd/mm/yyyy
+            year = 2;
+            day = 0;
+            month = 1;
+        } else if (year < month && month < day) {// yyyy/mm/dd
+            year = 0;
+            day = 2;
+            month = 1;
+        } else if (month < day && day < year) { // mm/dd/yyyy
+            year = 2;
+            day = 1;
+            month = 0;
+        } else if (month < year && year < day) { // mm/yyyy/dd
+            year = 1;
+            day = 2;
+            month = 0;
+        } else if (day < year && year < month) { // dd/yyyy/mm
+            year = 1;
+            day = 0;
+            month = 2;
+        } else {
+            return null; // an error has occurred
+        }
+        result.put("day", String.valueOf(day)); //$NON-NLS-1$
+        result.put("month", String.valueOf(month)); //$NON-NLS-1$
+        result.put("year", String.valueOf(year)); //$NON-NLS-1$
+        return result;
+    }
+
+    /**
+     * Returns JSONObject of returnType('currency')
+     *
+     * @param _locale
+     *            String _locale (user supplied Locale.toString())
+     * @param code
+     *            String code (The ISO 4217 currency code)
+     *
+     * @return JSONObject: 'currency':
+     *              [key: currencyCodes], [String]
+     *              [key: currencyPattern], [String]
+     *              [key: currencyDecimal], [String]
+     *              [key: currencyFraction], [String]
+     *              [key: currencyGrouping], [String]
+     *              [key: currencyRounding], [String]
+     *
+     * @throws: Exception
+     */
+    public static JSONObject getCurrencyData(String _locale, String code) {
+        JSONObject result = null;
+        try {
+            JSONArray jsonArray;
+            result = getPersistentResourceBundle(_locale);
+            if (result == null) {
+                jsonArray = getResourceBundle(_locale).getJSONArray(
+                        Resources.JSON_CURRENCY);
+            } else {
+                jsonArray = result.getJSONArray(Resources.JSON_CURRENCY);
+            }
+
+            for (int x = 0; x < jsonArray.length(); x++) {
+                JSONObject temp = jsonArray.getJSONObject(x);
+                if (temp.get(Resources.CURRENCYCODE).toString()
+                        .equalsIgnoreCase(code)) {
+                    result = temp;
+                }
+            }
+        } catch (Exception e) {
+
+        }
+        return result;
+    }
+
+    /**
+     * Returns JSONObject of returnType('locale')
+     *
+     * @param _locale
+     *            String _locale (user supplied Locale.toString())
+     *
+     * @return JSONObject: 'locale':
+     *              [key: displayName], [String]
+     *              [key: firstDayOfWeek], [String]
+     *              [key: pattern], [String]
+     *              [key: decimalSymbol], [String]
+     *              [key: currencySymbol], [String]
+     *              [key: percentSymbol], [String]
+     *
+     * @throws Exception
+     */
+    public static JSONObject getLocaleData(String _locale) {
+        JSONObject result = null;
+        try {
+            result = getPersistentResourceBundle(_locale);
+            if (result == null) {
+                result = getResourceBundle(_locale).getJSONObject(
+                        Resources.JSON_LOCALE);
+            } else {
+                result = result.getJSONObject(Resources.JSON_LOCALE);
+            }
+        } catch (Exception e) {
+        }
+        return result;
+    }
+
+    /**
+     * Returns resourceBundle JSONObject cached in PersistentStore
+     *
+     * Note: Recursively searches JSONObject from persistentStore using
+     * Resources.PERSISTENTSTORE_ID for locale by removing sub-parts (separated
+     * by '_')
+     *
+     * @param _locale
+     *            String _locale (user supplied Locale.toString())
+     *
+     * @return JSONObject
+     */
+    private static JSONObject getPersistentResourceBundle(String _locale) {
+        JSONObject result = null;
+        try {
+            // load object
+            result = (JSONObject) PersistentStore.getPersistentObject(
+                    Resources.PERSISTENTSTORE_ID).getContents();
+        } catch (Exception e) {
+            if (StringUtilities.indexOf(_locale, '_', 0, _locale.length()) > 0) {
+                result = getPersistentResourceBundle(removeSubPart(StringUtils
+                        .split(_locale, "_"))); //$NON-NLS-1$
+            } else {
+                result = null;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Returns resourceBundle File as JSONObject from
+     * /resource/resourceBundles/<Locale.toString()>.js.gz
+     *
+     * Note: Recursively searches for locale by removing sub-parts (separated by
+     * '_')
+     *
+     * @param _locale
+     *            String _locale (user supplied Locale.toString())
+     *
+     * @return JSONObject
+     */
+    private static JSONObject getResourceBundle(String _locale) {
+        JSONObject result = null;
+
+        try {
+            if (_locale == null || _locale.length() <= 0) {
+                return null;
+            }
+
+            InputStream is = Util.class.getClass().getResourceAsStream(
+                    Resources.LOCALEINFOPATH + _locale
+                            + Resources.LOCALEINFOPATHEND);
+            Parser parser = new Parser(new InputStreamReader(
+                    new GZIPInputStream(is), "UTF-8"));
+            result = parser.parse();
+
+            // cache resourceBundle as JSONOBJECT
+            // store new object
+            PersistentObject persist = PersistentStore
+                    .getPersistentObject(Resources.PERSISTENTSTORE_ID);
+            // Synchronize on the PersistentObject so that no other object can
+            // acquire the lock before we finish our commit operation.
+            synchronized (persist) {
+                persist.setContents((Hashtable) result);
+                persist.commit();
+            }
+        } catch (NonPersistableObjectException npoe) {
+            Logger.log("Globalization: Failed to persist locale: "
+                    + npoe.getMessage());
+        } catch (Exception e) {
+            // if resourceBundle not found, recursively search for file by
+            // removing substrings from name
+            if (StringUtilities.indexOf(_locale, '_', 0, _locale.length()) > 0) {
+                result = getResourceBundle(removeSubPart(StringUtils.split(
+                        _locale, "_"))); //$NON-NLS-1$
+            } else {
+                result = null;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Returns substring of resourceBundle with the last section removed. Ex.
+     * cs_CZ_PREEURO -> cs_CZ
+     *
+     * @param s
+     *            String[] s (Array of locale split by '_')
+     *
+     * @return JSONObject
+     */
+    private static String removeSubPart(String[] s) {
+        String result = "";
+        for (int x = 0; x < s.length - 1; x++) {
+            result += s[x];
+            if (x != s.length - 2) {// length - 2 to account for starting at
+                                    // zero
+                result += "_"; //$NON-NLS-1$
+            }
+        }
+        return result;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6831bed4/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileTransfer.java
----------------------------------------------------------------------
diff --git a/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileTransfer.java b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileTransfer.java
new file mode 100644
index 0000000..b0f089c
--- /dev/null
+++ b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileTransfer.java
@@ -0,0 +1,473 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cordova.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.microedition.io.Connector;
+import javax.microedition.io.HttpConnection;
+import javax.microedition.io.file.FileConnection;
+
+import org.apache.cordova.api.Plugin;
+import org.apache.cordova.api.PluginResult;
+import org.apache.cordova.file.Entry;
+import org.apache.cordova.json4j.JSONArray;
+import org.apache.cordova.json4j.JSONException;
+import org.apache.cordova.json4j.JSONObject;
+import org.apache.cordova.util.Logger;
+
+import net.rim.device.api.io.FileNotFoundException;
+import net.rim.device.api.io.http.HttpProtocolConstants;
+
+/**
+ * The FileTransfer plugin can be used to transfer files between the device and
+ * a remote server. The following actions are supported:
+ *
+ *      download - Download a file from a server to the device.
+ *      upload   - Upload a file from the device to a server.
+ */
+public class FileTransfer extends Plugin {
+    private static final String LOG_TAG = "FileTransfer: ";
+
+    /**
+     * Error codes
+     */
+    static int FILE_NOT_FOUND_ERR = 1;
+    static int INVALID_URL_ERR = 2;
+    static int CONNECTION_ERR = 3;
+
+    /**
+     * Possible actions
+     */
+    private static final String ACTION_DOWNLOAD = "download";
+    private static final String ACTION_UPLOAD = "upload";
+
+    private static final char SEPARATOR = '/';
+
+    /**
+     * Executes the requested action and returns a PluginResult.
+     *
+     * @param action
+     *            The action to execute.
+     * @param callbackId
+     *            The callback ID to be invoked upon action completion.
+     * @param args
+     *            JSONArry of arguments for the action.
+     * @return A PluginResult object with a status and message.
+     */
+    public PluginResult execute(String action, JSONArray args, String callbackId) {
+        String source = null;
+        String target = null;
+        PluginResult result = null;
+
+        try {
+            // Retrieve the source and target locations from the argument array.
+            source = args.isNull(0) ? null : args.getString(0).trim();
+            target = args.isNull(1) ? null : args.getString(1).trim();
+
+            if (source == null || source.length() == 0 || target == null
+                    || target.length() == 0) {
+                Logger.log(LOG_TAG + "Missing source or target");
+                return new PluginResult(PluginResult.Status.JSON_EXCEPTION,
+                        "Missing source or target");
+            }
+        } catch (JSONException e) {
+            Logger.log(LOG_TAG + e.getMessage());
+            return new PluginResult(PluginResult.Status.JSON_EXCEPTION,
+                    "Invalid or missing parameter");
+        }
+
+        if (ACTION_UPLOAD.equals(action)) {
+            // Source needs to follow the file URI protocol so add "file:///"
+            // prefix if it doesn't exist.
+            if (!source.startsWith("file:///")) {
+                if (source.indexOf(SEPARATOR) != 0) {
+                    source = "file://" + SEPARATOR + source;
+                } else {
+                    source = "file://" + source;
+                }
+            }
+
+            FileUploader uploader = null;
+            try {
+                // Setup the options
+                String fileKey = getArgument(args, 2, "file");
+                String fileName = getArgument(args, 3, "image.jpg");
+                String mimeType = getArgument(args, 4, null);
+                JSONObject params = null;
+                JSONObject headers = null;
+                
+                if (args.length() > 5 && !args.isNull(5)) {
+                    params = args.getJSONObject(5);
+                }
+
+                if(args.length() > 8 && !args.isNull(8)){
+                    headers = args.getJSONObject(8);
+                }
+                uploader = new FileUploader();
+                FileUploadResult r = uploader.upload(source, target, fileKey,
+                        fileName, mimeType, params, headers);
+
+                int status = r.getResponseCode();
+                if (status < 200 || status > 399) {
+                    Logger.log(LOG_TAG + "HTTP Status " + status);
+                    JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, new Integer(status));
+                    return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
+                }
+
+                result = new PluginResult(PluginResult.Status.OK,
+                        r.toJSONObject());
+            } catch (FileNotFoundException e) {
+                Logger.log(LOG_TAG + e.getMessage());
+                JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR,
+                        source, target, uploader);
+                result = new PluginResult(PluginResult.Status.IO_EXCEPTION,
+                        error);
+            } catch (IllegalArgumentException e) {
+                Logger.log(LOG_TAG + e.getMessage());
+                JSONObject error = createFileTransferError(INVALID_URL_ERR,
+                        source, target, uploader);
+                result = new PluginResult(
+                        PluginResult.Status.MALFORMED_URL_EXCEPTION, error);
+            } catch (IOException e) {
+                Logger.log(LOG_TAG + e.getMessage());
+                JSONObject error = createFileTransferError(CONNECTION_ERR,
+                        source, target, uploader);
+                result = new PluginResult(PluginResult.Status.IO_EXCEPTION,
+                        error);
+            } catch (JSONException e) {
+                Logger.log(LOG_TAG + e.getMessage());
+                result = new PluginResult(PluginResult.Status.JSON_EXCEPTION,
+                        "Invalid or missing parameter");
+            }
+        } else if (ACTION_DOWNLOAD.equals(action)) {
+            result = download(source, target);
+        } else {
+            // invalid action
+            result = new PluginResult(PluginResult.Status.INVALID_ACTION,
+                    LOG_TAG + "invalid action " + action);
+        }
+
+        return result;
+    }
+
+    /**
+     * Create an error object based on the passed in errorCode
+     *
+     * @param errorCode
+     *            the error
+     * @return JSONObject containing the error
+     */
+    private JSONObject createFileTransferError(int errorCode, String source,
+            String target) {
+        return createFileTransferError(errorCode, source, target,
+                (Integer) null);
+    }
+
+    /**
+     * Create an error object based on the passed in errorCode
+     *
+     * @param errorCode
+     *            the error
+     * @return JSONObject containing the error
+     */
+    private JSONObject createFileTransferError(int errorCode, String source,
+            String target, FileUploader fileUploader) {
+
+        Integer httpStatus = null;
+
+        if (fileUploader != null) {
+            httpStatus = fileUploader.getResponseCode();
+        }
+        return createFileTransferError(errorCode, source, target, httpStatus);
+    }
+
+    /**
+     * Create an error object based on the passed in errorCode
+     *
+     * @param errorCode
+     *            the error
+     * @return JSONObject containing the error
+     */
+    private JSONObject createFileTransferError(int errorCode, String source,
+            String target, HttpConnection connection) {
+
+        Integer httpStatus = null;
+
+        if (connection != null) {
+            try {
+                httpStatus = new Integer(connection.getResponseCode());
+            } catch (IOException e) {
+                Logger.log(LOG_TAG + " exception getting http response code "
+                        + e.toString());
+            }
+        }
+
+        return createFileTransferError(errorCode, source, target, httpStatus);
+    }
+
+    /**
+     * Create an error object based on the passed in errorCode
+     *
+     * @param errorCode
+     *            the error
+     * @return JSONObject containing the error
+     */
+    private JSONObject createFileTransferError(int errorCode, String source,
+            String target, Integer httpStatus) {
+        JSONObject error = null;
+        try {
+            error = new JSONObject();
+            error.put("code", errorCode);
+            error.put("source", source);
+            error.put("target", target);
+            if (httpStatus != null) {
+                error.put("http_status", httpStatus);
+            }
+        } catch (JSONException e) {
+            Logger.log(LOG_TAG + e.getMessage());
+        }
+        return error;
+    }
+
+    /**
+     * Recurse through a specified path and create any directories that do not
+     * already exist.
+     *
+     * @param path
+     *            directory path to recurse
+     * @throws IOException
+     */
+    private void createSubDirs(String path) throws IOException {
+        FileConnection outputStream = null;
+
+        try {
+            outputStream = (FileConnection) Connector.open(path,
+                    Connector.READ_WRITE);
+            if (!outputStream.exists()) {
+                int dirIndex = path.lastIndexOf(SEPARATOR, path.length() - 2);
+                // This code assumes file protocol is specified so stop
+                // recursion once "file:///" is hit.
+                if (dirIndex != -1 && dirIndex > 7) {
+                    createSubDirs(path.substring(0, dirIndex + 1));
+                }
+                outputStream.mkdir();
+            }
+        } finally {
+            try {
+                if (outputStream != null) {
+                    outputStream.close();
+                }
+            } catch (IOException e) {
+                Logger.log(LOG_TAG + e.getMessage());
+            }
+        }
+    }
+
+    /**
+     * Download a file from a given URL and save it to the specified location.
+     *
+     * @param source
+     *            URL of the server to receive the file
+     * @param target
+     *            Full path of the file on the file system
+     * @return JSONObject a file entry object in JSON form describing the
+     *         downloaded file.
+     */
+    private PluginResult download(String source, String target) {
+        HttpConnection httpConn = null;
+        FileConnection fileConn = null;
+        OutputStream outputStream = null;
+        String filename = null;
+        String path = null;
+
+        Logger.debug(LOG_TAG + "downloading " + source + " to " + target);
+
+        // Target needs to follow the file URI protocol so add "file:///"
+        // prefix if it doesn't exist.
+        if (!target.startsWith("file:///")) {
+            if (target.indexOf(SEPARATOR) != 0) {
+                target = "file://" + SEPARATOR + target;
+            } else {
+                target = "file://" + target;
+            }
+        }
+
+        // Parse the target filename and directory path. If the target does not
+        // specify a file name (only directory), try to get the file name from
+        // the source.
+        int dirIndex = target.lastIndexOf(SEPARATOR);
+        if (dirIndex == (target.length() - 1)) {
+            int srcdirIndex = source.lastIndexOf(SEPARATOR);
+            if (srcdirIndex != (source.length() - 1)) {
+                path = target;
+                filename = source.substring(srcdirIndex + 1);
+                target = path + filename;
+            }
+        } else if (dirIndex != -1) {
+            filename = target.substring(dirIndex + 1);
+            path = target.substring(0, dirIndex + 1);
+        }
+
+        // If no filename or path could be determined for the target, error out.
+        if (filename == null || path == null) {
+            Logger.log(LOG_TAG + "Target filename could not be determined.");
+            JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR,
+                    source, target);
+            return new PluginResult(PluginResult.Status.JSON_EXCEPTION, error);
+        }
+
+        try {
+            try {
+                // Create any directories in the path that do not already exist.
+                createSubDirs(path);
+
+                // Open connection to the target file.
+                fileConn = (FileConnection) Connector.open(target,
+                    Connector.READ_WRITE);
+            } catch (IOException e) {
+                Logger.log(LOG_TAG + "Failed to open target file: " + target);
+                JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source,
+                        target, httpConn);
+                return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
+            }
+
+            // Create the target file if it doesn't exist, otherwise truncate.
+            if (!fileConn.exists()) {
+                fileConn.create();
+            } else {
+                fileConn.truncate(0);
+            }
+
+            // Open the http connection to the server.
+            try {
+                httpConn = HttpUtils.getHttpConnection(source);
+            } catch (IllegalArgumentException e) {
+                JSONObject error = createFileTransferError(INVALID_URL_ERR, source, target, httpConn);
+                return new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, error);
+            }
+            if (httpConn == null) {
+                Logger.log(LOG_TAG + "Failed to create http connection.");
+                // TODO separate malformed url from actual connection error
+                JSONObject error = createFileTransferError(CONNECTION_ERR,
+                        source, target);
+                return new PluginResult(
+                        PluginResult.Status.IO_EXCEPTION, error);
+            }
+
+            // Set the request headers
+            httpConn.setRequestMethod(HttpConnection.GET);
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_USER_AGENT,
+                    System.getProperty("browser.useragent"));
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_KEEP_ALIVE, "300");
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_CONNECTION, "keep-alive");
+
+            // Set the cookie
+            String cookie = HttpUtils.getCookie(source);
+            if (cookie != null) {
+                httpConn.setRequestProperty(
+                        HttpProtocolConstants.HEADER_COOKIE, cookie);
+            }
+
+            InputStream inputStream = httpConn.openInputStream();
+            int status = httpConn.getResponseCode();
+            if (status < 200 || status > 399) {
+                Logger.log(LOG_TAG + "HTTP Status " + status);
+                JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, httpConn);
+                return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
+            }
+
+            outputStream = fileConn.openOutputStream();
+
+            // Read from the connection and write bytes to the file.
+            byte[] buffer = new byte[1024];
+            int bytesRead = 0;
+            while ((bytesRead = inputStream.read(buffer)) > 0) {
+                outputStream.write(buffer, 0, bytesRead);
+            }
+        } catch (IOException e) {
+            Logger.log(LOG_TAG + e.getMessage());
+            JSONObject error = createFileTransferError(CONNECTION_ERR, source,
+                    target, httpConn);
+            return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
+        } catch (ClassCastException e) {
+            // in case something really funky gets passed in
+            Logger.log(LOG_TAG + e.getMessage());
+            JSONObject error = createFileTransferError(INVALID_URL_ERR, source,
+                    target, httpConn);
+            return new PluginResult(
+                    PluginResult.Status.MALFORMED_URL_EXCEPTION, error);
+        } catch (Throwable t) {
+            Logger.log(LOG_TAG + t.toString());
+            JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, httpConn);
+            return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
+        } finally {
+            try {
+                if (httpConn != null) {
+                    httpConn.close();
+                }
+                if (outputStream != null) {
+                    outputStream.close();
+                }
+                if (fileConn != null) {
+                    fileConn.close();
+                }
+            } catch (IOException e) {
+                Logger.log(LOG_TAG + "IOException in finally: "
+                        + e.getMessage());
+            }
+        }
+
+        // create a new Entry
+        Entry entry = new Entry();
+        entry.setDirectory(false);
+        entry.setName(filename);
+        entry.setFullPath(target);
+
+        return new PluginResult(PluginResult.Status.OK, entry.toJSONObject());
+    }
+
+    /**
+     * Convenience method to read a parameter from the list of JSON args.
+     *
+     * @param args
+     *            the args passed to the Plugin
+     * @param position
+     *            the position to retrieve the arg from
+     * @param defaultString
+     *            the default to be used if the arg does not exist
+     * @return String with the retrieved value
+     */
+    private String getArgument(JSONArray args, int position,
+            String defaultString) {
+        String arg = defaultString;
+        if (args.length() >= position) {
+            arg = args.optString(position);
+            if (arg == null || "null".equals(arg)) {
+                arg = defaultString;
+            }
+        }
+        return arg;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6831bed4/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploadResult.java
----------------------------------------------------------------------
diff --git a/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploadResult.java b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploadResult.java
new file mode 100644
index 0000000..4b00159
--- /dev/null
+++ b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploadResult.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cordova.http;
+
+import org.apache.cordova.json4j.JSONException;
+import org.apache.cordova.json4j.JSONObject;
+
+/**
+ * Encapsulates the result and/or status of uploading a file to a remote server.
+ */
+public class FileUploadResult {
+
+    private long bytesSent = 0;         // bytes sent
+    private int responseCode = -1;      // HTTP response code
+    private String response = null;     // HTTP response
+
+    public long getBytesSent() {
+        return bytesSent;
+    }
+
+    public void setBytesSent(long bytes) {
+        this.bytesSent = bytes;
+    }
+
+    public int getResponseCode() {
+        return responseCode;
+    }
+
+    public void setResponseCode(int responseCode) {
+        this.responseCode = responseCode;
+    }
+
+    public String getResponse() {
+        return response;
+    }
+
+    public void setResponse(String response) {
+        this.response = response;
+    }
+
+    public JSONObject toJSONObject() throws JSONException {
+        return new JSONObject(
+                "{bytesSent:" + bytesSent +
+                ",responseCode:" + responseCode +
+                ",response:" + JSONObject.quote(response) + "}");
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6831bed4/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploader.java
----------------------------------------------------------------------
diff --git a/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploader.java b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploader.java
new file mode 100644
index 0000000..75645c7
--- /dev/null
+++ b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/FileUploader.java
@@ -0,0 +1,280 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cordova.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Enumeration;
+
+import javax.microedition.io.Connector;
+import javax.microedition.io.HttpConnection;
+import javax.microedition.io.file.FileConnection;
+
+import org.apache.cordova.CordovaExtension;
+import org.apache.cordova.api.PluginResult;
+import org.apache.cordova.json4j.JSONException;
+import org.apache.cordova.json4j.JSONObject;
+import org.apache.cordova.util.Logger;
+
+import net.rim.device.api.io.FileNotFoundException;
+import net.rim.device.api.io.IOUtilities;
+import net.rim.device.api.io.MIMETypeAssociations;
+import net.rim.device.api.io.http.HttpProtocolConstants;
+import net.rim.device.api.ui.UiApplication;
+
+/**
+ * The FileUploader uses an HTTP multipart request to upload files on the
+ * device to a remote server.  It currently supports a single file per HTTP
+ * request.
+ */
+public class FileUploader {
+
+    /**
+     * Constants
+     */
+    private static final String BOUNDARY = "----0x2fc1b3ef7cecbf14L";
+    private static final String LINE_END = "\r\n";
+    private static final String TD = "--";
+
+    private Integer responseCode = null;
+
+    /**
+     * Uploads the specified file to the server URL provided using an HTTP
+     * multipart request.
+     * @param filePath      Full path of the file on the file system
+     * @param server        URL of the server to receive the file
+     * @param fileKey       Name of file request parameter
+     * @param fileName      File name to be used on server
+     * @param mimeType      Describes file content type
+     * @param params        key:value pairs of user-defined parameters
+     * @return FileUploadResult containing result of upload request
+     */
+    public FileUploadResult upload(String filePath, String server, String fileKey,
+            String fileName, String mimeType, JSONObject params, JSONObject headers)
+    throws FileNotFoundException, IllegalArgumentException, IOException {
+
+        Logger.log(this.getClass().getName() + ": uploading " + filePath + " to " + server);
+        FileUploadResult result = new FileUploadResult();
+
+        InputStream in = null;
+        OutputStream out = null;
+        FileConnection fconn = null;
+        HttpConnection httpConn = null;
+        try {
+            // open connection to the file
+            try {
+                fconn = (FileConnection)Connector.open(filePath, Connector.READ);
+            } catch (ClassCastException e) {
+                // in case something really funky gets passed in
+                throw new IllegalArgumentException("Invalid file path");
+            } catch (IOException e) {
+                throw new FileNotFoundException("Failed to open source file: " + filePath);
+            }
+            if (!fconn.exists()) {
+                throw new FileNotFoundException(filePath + " not found");
+            }
+
+            // determine mime type by
+            //     1) user-provided type
+            //     2) retrieve from file system
+            //     3) default to JPEG
+            if (mimeType == null) {
+                mimeType = MIMETypeAssociations.getMIMEType(filePath);
+                if (mimeType == null) {
+                    mimeType = HttpProtocolConstants.CONTENT_TYPE_IMAGE_JPEG;
+                }
+            }
+
+            // boundary messages
+            String boundaryMsg = getBoundaryMessage(fileKey, fileName, mimeType);
+            String lastBoundary = getEndBoundary();
+
+            // user-defined request parameters
+            String customParams = (params != null) ? getParameterContent(params) : "";
+            Logger.log(this.getClass().getName() + ": params=" + customParams);
+
+            // determine content length
+            long fileSize = fconn.fileSize();
+            Logger.log(this.getClass().getName() + ": " + filePath + " size=" + fileSize + " bytes");
+            long contentLength = fileSize +
+                (long)boundaryMsg.length() +
+                (long)lastBoundary.length() +
+                (long)customParams.length();
+
+            // get HttpConnection
+            httpConn = HttpUtils.getHttpConnection(server);
+            if (httpConn == null) {
+                throw new IOException("Failed to connect to " + server);
+            }
+            Logger.log(this.getClass().getName() + ": server URL=" + httpConn.getURL());
+
+            // set request headers
+            httpConn.setRequestMethod(HttpConnection.POST);
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_USER_AGENT,
+                    System.getProperty("browser.useragent"));
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_KEEP_ALIVE, "300");
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_CONNECTION, "keep-alive");
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_CONTENT_TYPE,
+                    HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
+            httpConn.setRequestProperty(
+                    HttpProtocolConstants.HEADER_CONTENT_LENGTH,
+                    Long.toString(contentLength));
+
+            if(headers != null){
+                for(Enumeration e = headers.keys(); e.hasMoreElements();){
+                    String key = e.nextElement().toString();
+                    String value = headers.optString(key);
+                    Logger.log(this.getClass().getName() + ": key=" + key + " value=" + value);
+                    httpConn.setRequestProperty(key, value);
+                }    
+            }
+            
+            // set cookie
+            String cookie = HttpUtils.getCookie(server);
+            if (cookie != null) {
+                httpConn.setRequestProperty(HttpProtocolConstants.HEADER_COOKIE, cookie);
+                Logger.log(this.getClass().getName() + ": cookie=" + cookie);
+            }
+
+            // write...
+            out = httpConn.openDataOutputStream();
+
+            // parameters
+            out.write(customParams.getBytes());
+
+            // boundary
+            out.write(boundaryMsg.getBytes());
+
+            // file data
+            in = fconn.openInputStream();
+            byte[] data = IOUtilities.streamToBytes(in);
+            out.write(data);
+            in.close();
+
+            // end boundary
+            out.write(lastBoundary.getBytes());
+
+            // send request and get response
+            in = httpConn.openDataInputStream();
+            //int rc = httpConn.getResponseCode();
+            result.setResponse(new String(IOUtilities.streamToBytes(in)));
+            //result.setResponseCode(rc);
+            result.setBytesSent(contentLength);
+            Logger.log(this.getClass().getName() + ": sent " + contentLength + " bytes");
+        }
+        finally {
+
+            if (httpConn != null) {
+                result.setResponseCode(httpConn.getResponseCode());
+                responseCode = new Integer(httpConn.getResponseCode());
+            }
+
+            try {
+                if (fconn != null) fconn.close();
+                if (in != null) in.close();
+                if (out != null) out.close();
+                if (httpConn != null) httpConn.close();
+            }
+            catch (IOException e) {
+                Logger.log(this.getClass().getName() + ": " + e);
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * Sends an upload progress notification back to JavaScript engine.
+     * @param result        FileUploadResult containing bytes sent of total
+     * @param callbackId    identifier of callback function to invoke
+     */
+    protected void sendProgress(FileUploadResult result, final String callbackId) {
+        JSONObject o = null;
+        try {
+            o = result.toJSONObject();
+        }
+        catch (JSONException e) {
+            Logger.log(this.getClass().getName() + ": " + e);
+            return;
+        }
+
+        // send a progress result
+        final PluginResult r = new PluginResult(PluginResult.Status.OK, o);
+        r.setKeepCallback(true);
+        UiApplication.getUiApplication().invokeAndWait(
+            new Runnable() {
+                public void run() {
+                    CordovaExtension.invokeSuccessCallback(callbackId, r);
+                }
+            }
+        );
+    }
+
+    /**
+     * Returns the boundary string that represents the beginning of a file
+     * in a multipart HTTP request.
+     * @param fileKey       Name of file request parameter
+     * @param fileName      File name to be used on server
+     * @param mimeType      Describes file content type
+     * @return string representing the boundary message in a multipart HTTP request
+     */
+    protected String getBoundaryMessage(String fileKey, String fileName, String mimeType) {
+        return (new StringBuffer())
+            .append(TD).append(BOUNDARY).append(LINE_END)
+            .append("Content-Disposition: form-data; name=\"").append(fileKey)
+            .append("\"; filename=\"").append(fileName).append("\"").append(LINE_END)
+            .append("Content-Type: ").append(mimeType).append(LINE_END)
+            .append(LINE_END)
+            .toString();
+    }
+
+    /**
+     * Returns the boundary string that represents the end of a file in a
+     * multipart HTTP request.
+     * @return string representing the end boundary message in a multipart HTTP request
+     */
+    protected String getEndBoundary() {
+        return LINE_END + TD + BOUNDARY + TD + LINE_END;
+    }
+
+    /**
+     * Returns HTTP form content containing specified parameters.
+     */
+    protected String getParameterContent(JSONObject params) {
+        StringBuffer buf = new StringBuffer();
+        for (Enumeration e = params.keys(); e.hasMoreElements();) {
+            String key = e.nextElement().toString();
+            String value = params.optString(key);
+            buf.append(TD).append(BOUNDARY).append(LINE_END)
+                .append("Content-Disposition: form-data; name=\"").append(key).append("\"")
+                .append(LINE_END).append(LINE_END)
+                .append(value).append(LINE_END);
+        }
+        return buf.toString();
+    }
+
+    Integer getResponseCode() {
+        return responseCode;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6831bed4/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/HttpUtils.java
----------------------------------------------------------------------
diff --git a/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/HttpUtils.java b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/HttpUtils.java
new file mode 100644
index 0000000..af6ada6
--- /dev/null
+++ b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/http/HttpUtils.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cordova.http;
+
+import javax.microedition.io.HttpConnection;
+
+import org.apache.cordova.CordovaExtension;
+
+import net.rim.device.api.io.transport.ConnectionDescriptor;
+import net.rim.device.api.io.transport.ConnectionFactory;
+
+/**
+ * BlackBerry devices can connect to the network using a variety of transport
+ * types, such as: WI-FI, BES/MDS, BIS, WAP (cellular).  A connection URL must
+ * have the appropriate suffix to match the transport type.  This class contains
+ * utility methods to retrieve the correct URL for the appropriate transport.
+ */
+public class HttpUtils
+{
+    /**
+     * This method will open an HTTP connection over the best available transport type.
+     * @param url   Connection URL
+     */
+    public static HttpConnection getHttpConnection(String url)
+    {
+        HttpConnection httpConn = null;
+
+        // Create ConnectionFactory
+        ConnectionFactory factory = new ConnectionFactory();
+
+        // use the factory to get a connection
+        ConnectionDescriptor conDescriptor = factory.getConnection(url);
+
+        if (conDescriptor != null) {
+           // using the connection
+           httpConn = (HttpConnection) conDescriptor.getConnection();
+        }
+
+        return httpConn;
+    }
+
+    /**
+     * Retrieves the cookie from the application browser instance for the specified URL.
+     * @param url   Connection URL
+     */
+    public static String getCookie(String url)
+    {
+        return CordovaExtension.getBrowserField().getCookieManager().getCookie(url);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6831bed4/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/json4j/JSON.java
----------------------------------------------------------------------
diff --git a/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/json4j/JSON.java b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/json4j/JSON.java
new file mode 100644
index 0000000..adbe514
--- /dev/null
+++ b/lib/cordova-blackberry/blackberry/framework/ext/src/org/apache/cordova/json4j/JSON.java
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cordova.json4j;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.apache.cordova.json4j.internal.JSON4JPBackReader;
+
+/**
+ * Helper class that does generic parsing of a JSON stream and returns the appropriate
+ * JSON structure (JSONArray or JSONObject).  Note that it is slightly more efficient to directly
+ * parse with the appropriate object than to use this class to do a generalized parse.
+ */
+public class JSON {
+
+    /**
+     * A constant for representing null.
+     * In this case, it is just null.
+     */
+    public static final Object NULL = null;
+
+    /**
+     * Parse a Reader of JSON text into a JSONArtifact.
+     * @param reader The character reader to read the JSON data from.
+     * @param order Boolean flag indicating if the order of the JSON data should be preserved.  This parameter only has an effect if the stream is JSON Object { ... } formatted data.
+     * Note:  The provided reader is not closed on completion of read; that is left to the caller.
+     * Note:  This is the same as calling parse(reader, order, false);
+     *
+     * @return Returns an instance of JSONArtifact (JSONObject, OrderedJSONObject, or JSONArray), corrisponding to if the input stream was Object or Array notation.
+     *
+     * @throws JSONException Thrown on errors during parse.
+     * @throws NullPointerException Thrown if reader is null
+     */
+    public static JSONArtifact parse(Reader reader, boolean order) throws JSONException, NullPointerException {
+        return parse(reader,order,false);
+    }
+
+    /**
+     * Parse a Reader of JSON text into a JSONArtifact.
+     * @param reader The character reader to read the JSON data from.
+     * @param order Boolean flag indicating if the order of the JSON data should be preserved.  This parameter only has an effect if the stream is JSON Object { ... } formatted data.
+     * @param strict Boolean flag to indicate if the content should be parsed in strict mode or not, meaning comments and unquoted strings are not allowed.
+     * Note:  The provided reader is not closed on completion of read; that is left to the caller.
+     *
+     * @return Returns an instance of JSONArtifact (JSONObject, OrderedJSONObject, or JSONArray), corrisponding to if the input stream was Object or Array notation.
+     *
+     * @throws JSONException Thrown on errors during parse.
+     * @throws NullPointerException Thrown if reader is null
+     */
+    public static JSONArtifact parse(Reader reader, boolean order, boolean strict) throws JSONException, NullPointerException {
+
+        try {
+            if (reader != null) {
+
+                JSON4JPBackReader pReader = null;
+
+                //Determine if we should buffer-wrap the reader before passing it on
+                //to the appropriate parser.
+                //boolean bufferIt = false;
+
+                Class readerClass = reader.getClass();
+
+               /* if (!StringReader.class.isAssignableFrom(readerClass) &&
+                    !CharArrayReader.class.isAssignableFrom(readerClass) &&
+                    !PushbackReader.class.isAssignableFrom(readerClass) &&
+                    !BufferedReader.class.isAssignableFrom(readerClass)) {
+                    bufferIt = true;
+                } */
+
+                //MSN IMPLEMENT PUSHBACKREADER!!
+                if (JSON4JPBackReader.class.isAssignableFrom(readerClass)) {
+                    pReader = (JSON4JPBackReader) reader;
+                } else {
+                    pReader = new JSON4JPBackReader(reader);
+                }
+
+                Reader rdr = pReader;
+                int ch = pReader.read();
+                while (ch != -1) {
+                    switch (ch) {
+                        case '{':
+                            pReader.unread(ch);
+                           /* if (bufferIt) {
+                                rdr = new BufferedReader(pReader);
+                            } */
+                            return new JSONObject(rdr,strict);
+                        case '[':
+                            pReader.unread(ch);
+                            /*if (bufferIt) {
+                                rdr = new BufferedReader(pReader);
+                            } */
+                            return new JSONArray(rdr, strict);
+                        case ' ':
+                        case '\t':
+                        case '\f':
+                        case '\r':
+                        case '\n':
+                        case '\b':
+                            ch = pReader.read();
+                            break;
+                        default:
+                            throw new JSONException("Unexpected character: [" + (char)ch + "] while scanning JSON String for JSON type.  Invalid JSON.");
+                    }
+                }
+                throw new JSONException("Encountered end of stream before JSON data was read.  Invalid JSON");
+            } else {
+                throw new NullPointerException("reader cannot be null.");
+            }
+        } catch (IOException iox) {
+            JSONException jex = new JSONException("Error occurred during input read.");
+            jex.setCause(iox);
+            throw jex;
+        }
+    }
+
+    /**
+     * Parse a Reader of JSON text into a JSONArtifact.
+     * This call is the same as JSON.parse(reader, false, false).
+     * Note that the provided reader is not closed on completion of read; that is left to the caller.
+     * @param reader The character reader to read the JSON data from.
+     *
+     * @return Returns an instance of JSONArtifact (JSONObject, OrderedJSONObject, or JSONArray), corrisponding to if the input stream was Object or Array notation.
+     *
+     * @throws JSONException Thrown on errors during parse.
+     * @throws NullPointerException Thrown if reader is null
+     */
+    public static JSONArtifact parse(Reader reader) throws JSONException, NullPointerException {
+        return parse(reader,false, false);
+    }
+
+    /**
+     * Parse a InputStream of JSON text into a JSONArtifact.
+     * Note:  The provided InputStream is not closed on completion of read; that is left to the caller.
+     * @param is The input stream to read from.  The content is assumed to be UTF-8 encoded and handled as such.
+     * @param order Boolean flag indicating if the order of the JSON data should be preserved.  This parameter only has an effect if the stream is JSON Object { ... } formatted data.
+     *
+     * @return Returns an instance of JSONArtifact (JSONObject or JSONArray), corrisponding to if the input stream was Object or Array notation.
+     *
+     * @throws JSONException Thrown on errors during parse.
+     * @throws NullPointerException Thrown if reader is null
+     */
+    public static JSONArtifact parse(InputStream is, boolean order) throws JSONException, NullPointerException {
+        return parse(is,order, false);
+    }
+
+    /**
+     * Parse a InputStream of JSON text into a JSONArtifact.
+     * Note that the provided InputStream is not closed on completion of read; that is left to the caller.
+     * @param is The input stream to read from.  The content is assumed to be UTF-8 encoded and handled as such.
+     * @param order Boolean flag indicating if the order of the JSON data should be preserved.  This parameter only has an effect if the stream is JSON Object { ... } formatted data.
+     * @param strict Boolean flag to indicate if the content should be parsed in strict mode or not, meaning comments and unquoted strings are not allowed.
+     *
+     * @return Returns an instance of JSONArtifact (JSONObject or JSONArray), corrisponding to if the input stream was Object or Array notation.
+     *
+     * @throws JSONException Thrown on errors during parse.
+     * @throws NullPointerException Thrown if reader is null
+     */
+    public static JSONArtifact parse(InputStream is, boolean order, boolean strict) throws JSONException, NullPointerException {
+        if (is != null) {
+            //BufferedReader reader = null;
+            InputStreamReader reader = null;
+            try {
+                reader = new InputStreamReader(is, "UTF-8");
+            } catch (Exception ex) {
+                JSONException iox = new JSONException("Could not construct UTF-8 character reader for the InputStream");
+                iox.setCause(ex);
+                throw iox;
+            }
+            return parse(reader,order);
+        } else {
+            throw new NullPointerException("is cannot be null");
+        }
+    }
+
+    /**
+     * Parse an InputStream of JSON text into a JSONArtifact.
+     * This call is the same as JSON.parse(is, false, false).
+     * Note that the provided InputStream is not closed on completion of read; that is left to the caller.
+     * @param is The input stream to read from.  The content is assumed to be UTF-8 encoded and handled as such.
+     *
+     * @return Returns an instance of JSONArtifact (JSONObject, OrderedJSONObject, or JSONArray), corrisponding to if the input stream was Object or Array notation.
+     *
+     * @throws JSONException Thrown on errors during parse.
+     * @throws NullPointerException Thrown if reader is null
+     */
+    public static JSONArtifact parse(InputStream is) throws JSONException, NullPointerException {
+        return parse(is,false, false);
+    }
+}


Mime
View raw message