
com.wedeploy.api.query.Util Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of Liferay, Inc. nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.wedeploy.api.query;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Utility class for Query components parsing.
*/
public class Util {
public static String toString(Iterable value) {
StringBuilder builder = new StringBuilder();
builder.append('[');
Iterator iterator = value.iterator();
while (iterator.hasNext()) {
builder.append(toString(iterator.next()));
if (iterator.hasNext()) {
builder.append(",");
}
}
builder.append(']');
return builder.toString();
}
public static String toString(Map value) {
if (value.size() == 0) {
return "{}";
}
StringBuilder builder = new StringBuilder();
builder.append('{');
for (Map.Entry entry : value.entrySet()) {
builder.append(toString(entry.getKey())).append(':');
builder.append(toString(entry.getValue())).append(",");
}
builder.setCharAt(builder.length() - 1, '}');
return builder.toString();
}
public static String toString(Object value) {
if (value instanceof Embodied) {
return toString(((Embodied) value).body());
}
if (value instanceof Iterable) {
return toString((Iterable) value);
}
if (value instanceof Map) {
return toString((Map) value);
}
if (value instanceof String) {
return toString((String) value);
}
if (value.getClass().isArray()) {
int length = Array.getLength(value);
if (length == 0) {
return "[]";
}
StringBuilder builder = new StringBuilder();
builder.append('[');
for (int i = 0; i < length; i++) {
builder.append(toString(Array.get(value, i))).append(',');
}
builder.setCharAt(builder.length() - 1, ']');
return builder.toString();
}
return value.toString();
}
/**
* Wraps a (key, value)
pair into a {@link Map}.
*
* @param key the map key
* @param value the value associated to the key
* @return a map with a entry (key, value)
*/
public static Map wrap(String key, Object value) {
Map map = new HashMap();
map.put(key, value);
return map;
}
protected Util() {
}
private static String toString(String value) {
StringBuilder sb = new StringBuilder();
sb.append('"');
int len = value.length();
for (int i = 0; i < len; i++) {
char c = value.charAt(i);
switch (c) {
case '"':
sb.append("\\\"");
break;
case '\\':
sb.append("\\\\");
break;
case '/':
sb.append("\\/");
break;
case '\b':
sb.append("\\b");
break;
case '\f':
sb.append("\\f");
break;
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
default:
sb.append(c);
}
}
sb.append('"');
return sb.toString();
}
}