/** * Get the optional object value associated with an index.<p> * * @param index the index must be between 0 and length() - 1 * @return an object value, or null if there is no object at that index */ public Object opt(int index) { return ((index < 0) || (index >= length())) ? null : m_myArrayList.get(index); }
/** * Get the optional object value associated with an index.<p> * * @param index the index must be between 0 and length() - 1 * @return an object value, or null if there is no object at that index */ public Object opt(int index) { return (index < 0 || index >= length()) ? null : this.m_myArrayList.get(index); }
/** * Make a string from the contents of this JSONArray.<p> * * The <code>separator</code> string is inserted between each element.<p> * * Warning: This method assumes that the data structure is acyclical.<p> * * @param separator a string that will be inserted between the elements * @return a string * @throws JSONException if the array contains an invalid number */ public String join(String separator) throws JSONException { int len = length(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator); } sb.append(JSONObject.valueToString(this.m_myArrayList.get(i))); } return sb.toString(); }
/** * Make a string from the contents of this JSONArray.<p> * * The <code>separator</code> string is inserted between each element.<p> * * Warning: This method assumes that the data structure is acyclical.<p> * * @param separator a string that will be inserted between the elements * @return a string * @throws JSONException if the array contains an invalid number */ public String join(String separator) throws JSONException { int len = length(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator); } sb.append(JSONObject.valueToString(m_myArrayList.get(i))); } return sb.toString(); }
/** Parses the list of query items for the query facet. * @param queryFacetObject JSON object representing the node with the query facet. * @return list of query options * @throws JSONException if the list cannot be parsed. */ private List<I_CmsFacetQueryItem> parseFacetQueryItems(final JSONObject queryFacetObject) throws JSONException { final JSONArray items = queryFacetObject.getJSONArray(JSON_KEY_QUERY_FACET_QUERY); List<I_CmsFacetQueryItem> result = new ArrayList<I_CmsFacetQueryItem>(items.length()); for (int i = 0; i < items.length(); i++) { I_CmsFacetQueryItem item = parseFacetQueryItem(items.getJSONObject(i)); if (item != null) { result.add(item); } } return result; }
/** * Produce a JSONObject by combining a JSONArray of names with the values * of this JSONArray.<p> * * @param names a JSONArray containing a list of key strings. These will be * paired with the values * @return a JSONObject, or null if there are no names or if this JSONArray * has no values * @throws JSONException if any of the names are null */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if ((names == null) || (names.length() == 0) || (length() == 0)) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), opt(i)); } return jo; }
/** * Converts the given JSON object into a valid parameter map.<p> * * @param params the JSON object to convert * * @return the parameter map from the given JSON object */ public static Map<String, String[]> getParameterMapFromJSON(JSONObject params) { Map<String, String[]> result = new HashMap<String, String[]>(); Iterator<String> itKeys = params.keys(); while (itKeys.hasNext()) { String key = itKeys.next(); JSONArray paramValue = params.optJSONArray(key); result.put(key, new String[paramValue.length()]); for (int i = 0, l = paramValue.length(); i < l; i++) { result.get(key)[i] = paramValue.optString(i); } } return result; }
/** * Produce a JSONObject by combining a JSONArray of names with the values * of this JSONArray.<p> * * @param names a JSONArray containing a list of key strings. These will be * paired with the values * @return a JSONObject, or null if there are no names or if this JSONArray * has no values * @throws JSONException if any of the names are null */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
/** * Produce a comma delimited text from a JSONArray of JSONObjects using * a provided list of names. The list of names is not included in the * output.<p> * * @param names A JSONArray of strings * @param ja A JSONArray of JSONObjects * @return A comma delimited text * @throws JSONException if something goes wrong */ public static String toString(JSONArray names, JSONArray ja) throws JSONException { if ((names == null) || (names.length() == 0)) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < ja.length(); i += 1) { JSONObject jo = ja.optJSONObject(i); if (jo != null) { sb.append(rowToString(jo.toJSONArray(names))); } } return sb.toString(); }
/** * Produce a comma delimited text from a JSONArray of JSONObjects using * a provided list of names. The list of names is not included in the * output.<p> * * @param names A JSONArray of strings * @param ja A JSONArray of JSONObjects * @return A comma delimited text * @throws JSONException if something goes wrong */ public static String toString(JSONArray names, JSONArray ja) throws JSONException { if (names == null || names.length() == 0) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < ja.length(); i += 1) { JSONObject jo = ja.optJSONObject(i); if (jo != null) { sb.append(rowToString(jo.toJSONArray(names))); } } return sb.toString(); }
/** * Produce a JSONArray containing the names of the elements of this JSONObject.<p> * * @return a JSONArray containing the key strings, or null if the JSONObject is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator<String> keys = keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
/** * Produce a JSONArray containing the names of the elements of this JSONObject.<p> * * @return a JSONArray containing the key strings, or null if the JSONObject is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator keys = keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
/** * Decodes a parameter which has been encoded from a string list using encodeStringsAsBase64Parameter.<p> * * @param data the data to decode * @return the list of strings */ public static List<String> decodeStringsFromBase64Parameter(String data) { data = StringUtils.replaceChars(data, BASE64_EXTRA_REPLACEMENTS, BASE64_EXTRA); byte[] bytes = deobfuscateBytes(Base64.decodeBase64(data)); try { JSONArray json = new JSONArray(new String(bytes, "UTF-8")); List<String> result = Lists.newArrayList(); for (int i = 0; i < json.length(); i++) { result.add(json.getString(i)); } return result; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { throw new IllegalArgumentException("Decoding failed: " + data, e); } return null; }
/** Helper for reading a mandatory String value list - throwing an Exception if parsing fails. * @param json The JSON object where the list should be read from. * @param key The key of the value to read. * @return The value from the JSON. * @throws JSONException thrown when parsing fails. */ protected List<String> parseMandatoryStringValues(final JSONObject json, final String key) throws JSONException { List<String> list = null; JSONArray array = json.getJSONArray(key); list = new ArrayList<String>(array.length()); for (int i = 0; i < array.length(); i++) { try { String entry = array.getString(i); list.add(entry); } catch (JSONException e) { LOG.info(Messages.get().getBundle().key(Messages.LOG_OPTIONAL_STRING_ENTRY_UNPARSABLE_1, key), e); } } return list; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject.<p> * * @param names a JSONArray containing a list of key strings. This * determines the sequence of the values in the result * @return a JSONArray of values * @throws JSONException if any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if ((names == null) || (names.length() == 0)) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(opt(names.getString(i))); } return ja; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject.<p> * * @param names a JSONArray containing a list of key strings. This * determines the sequence of the values in the result * @return a JSONArray of values * @throws JSONException if any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
/** Returns the list of the configured sort options, or the empty list if no sort options are configured. * @return The list of the configured sort options, or the empty list if no sort options are configured. */ private List<I_CmsSearchConfigurationSortOption> getSortOptions() { final List<I_CmsSearchConfigurationSortOption> options = new LinkedList<I_CmsSearchConfigurationSortOption>(); try { final JSONArray sortOptions = m_configObject.getJSONArray(JSON_KEY_SORTOPTIONS); for (int i = 0; i < sortOptions.length(); i++) { final I_CmsSearchConfigurationSortOption option = parseSortOption(sortOptions.getJSONObject(i)); if (option != null) { options.add(option); } } } catch (final JSONException e) { LOG.info(Messages.get().getBundle().key(Messages.LOG_NO_SORT_CONFIG_0), e); } return options; }
/** * @see org.opencms.jsp.search.config.parser.I_CmsSearchConfigurationParser#parseFieldFacets() */ @Override public Map<String, I_CmsSearchConfigurationFacetField> parseFieldFacets() { final Map<String, I_CmsSearchConfigurationFacetField> facetConfigs = new LinkedHashMap<String, I_CmsSearchConfigurationFacetField>(); try { final JSONArray fieldFacets = m_configObject.getJSONArray(JSON_KEY_FIELD_FACETS); for (int i = 0; i < fieldFacets.length(); i++) { final I_CmsSearchConfigurationFacetField config = parseFieldFacet(fieldFacets.getJSONObject(i)); if (config != null) { facetConfigs.put(config.getName(), config); } } } catch (final JSONException e) { LOG.info(Messages.get().getBundle().key(Messages.LOG_NO_FACET_CONFIG_0), e); } return facetConfigs; }
/** * @see org.opencms.jsp.search.config.parser.I_CmsSearchConfigurationParser#parseRangeFacets() */ public Map<String, I_CmsSearchConfigurationFacetRange> parseRangeFacets() { final Map<String, I_CmsSearchConfigurationFacetRange> facetConfigs = new LinkedHashMap<String, I_CmsSearchConfigurationFacetRange>(); try { final JSONArray rangeFacets = m_configObject.getJSONArray(JSON_KEY_RANGE_FACETS); for (int i = 0; i < rangeFacets.length(); i++) { final I_CmsSearchConfigurationFacetRange config = parseRangeFacet(rangeFacets.getJSONObject(i)); if (config != null) { facetConfigs.put(config.getName(), config); } } } catch (final JSONException e) { LOG.info(Messages.get().getBundle().key(Messages.LOG_NO_FACET_CONFIG_0), e); } return facetConfigs; }
/** * Reads the folder filters for the current site.<p> * * @return the folder filters */ private Set<String> readFolderFilters() { JSONObject storedFilters = readUserFolderFilters(); Set<String> result = null; if (storedFilters.has(getCmsObject().getRequestContext().getSiteRoot())) { try { org.opencms.json.JSONArray folders = storedFilters.getJSONArray( getCmsObject().getRequestContext().getSiteRoot()); result = new HashSet<String>(); for (int i = 0; i < folders.length(); i++) { result.add(folders.getString(i)); } } catch (JSONException e) { LOG.error(e.getLocalizedMessage(), e); } } return result; }