Solr-4.7.1.jar中ClientUtils类源码
public class ClientUtils {
public static final String TEXT_XML = "application/xml; charset=UTF-8";
/** @deprecated */
@Deprecated
public static final Collection<String> fmts;
/** @deprecated */
@Deprecated
public static TimeZone UTC;
public ClientUtils() {
}
public static Collection<ContentStream> toContentStreams(String str, String contentType) {
if(str == null) {
return null;
} else {
ArrayList streams = new ArrayList(1);
StringStream ccc = new StringStream(str);
ccc.setContentType(contentType);
streams.add(ccc);
return streams;
}
}
public static SolrInputDocument toSolrInputDocument(SolrDocument d) {
SolrInputDocument doc = new SolrInputDocument();
Iterator i$ = d.getFieldNames().iterator();
while(i$.hasNext()) {
String name = (String)i$.next();
doc.addField(name, d.getFieldValue(name), 1.0F);
}
return doc;
}
public static SolrDocument toSolrDocument(SolrInputDocument d) {
SolrDocument doc = new SolrDocument();
Iterator i$ = d.iterator();
while(i$.hasNext()) {
SolrInputField field = (SolrInputField)i$.next();
doc.setField(field.getName(), field.getValue());
}
return doc;
}
public static void writeXML(SolrInputDocument doc, Writer writer) throws IOException {
writer.write("<doc boost=\"" + doc.getDocumentBoost() + "\">");
Iterator i$ = doc.iterator();
label61:
while(i$.hasNext()) {
SolrInputField childDocument = (SolrInputField)i$.next();
float boost = childDocument.getBoost();
String name = childDocument.getName();
Iterator i$1 = childDocument.iterator();
while(true) {
label42:
while(true) {
if(!i$1.hasNext()) {
continue label61;
}
Object v = i$1.next();
String update = null;
if(v instanceof Map) {
Iterator i$2 = ((Map)v).entrySet().iterator();
while(true) {
while(true) {
if(!i$2.hasNext()) {
continue label42;
}
Entry entry = (Entry)i$2.next();
update = entry.getKey().toString();
v = entry.getValue();
if(v instanceof Collection) {
Collection values = (Collection)v;
for(Iterator i$3 = values.iterator(); i$3.hasNext(); boost = 1.0F) {
Object value = i$3.next();
writeVal(writer, boost, name, value, update);
}
} else {
writeVal(writer, boost, name, v, update);
boost = 1.0F;
}
}
}
} else {
writeVal(writer, boost, name, v, update);
boost = 1.0F;
}
}
}
}
if(doc.hasChildDocuments()) {
i$ = doc.getChildDocuments().iterator();
while(i$.hasNext()) {
SolrInputDocument childDocument1 = (SolrInputDocument)i$.next();
writeXML(childDocument1, writer);
}
}
writer.write("</doc>");
}
private static void writeVal(Writer writer, float boost, String name, Object v, String update) throws IOException {
if(v instanceof Date) {
v = DateUtil.getThreadLocalDateFormat().format((Date)v);
} else if(v instanceof byte[]) {
byte[] bytes = (byte[])((byte[])v);
v = Base64.byteArrayToBase64(bytes, 0, bytes.length);
} else if(v instanceof ByteBuffer) {
ByteBuffer bytes1 = (ByteBuffer)v;
v = Base64.byteArrayToBase64(bytes1.array(), bytes1.position(), bytes1.limit() - bytes1.position());
}
if(update == null) {
if(boost != 1.0F) {
XML.writeXML(writer, "field", v.toString(), new Object[]{"name", name, "boost", Float.valueOf(boost)});
} else if(v != null) {
XML.writeXML(writer, "field", v.toString(), new Object[]{"name", name});
}
} else if(boost != 1.0F) {
XML.writeXML(writer, "field", v.toString(), new Object[]{"name", name, "boost", Float.valueOf(boost), "update", update});
} else if(v == null) {
XML.writeXML(writer, "field", (String)null, new Object[]{"name", name, "update", update, "null", Boolean.valueOf(true)});
} else {
XML.writeXML(writer, "field", v.toString(), new Object[]{"name", name, "update", update});
}
}
public static String toXML(SolrInputDocument doc) {
StringWriter str = new StringWriter();
try {
writeXML(doc, str);
} catch (Exception var3) {
;
}
return str.toString();
}
/** @deprecated */
@Deprecated
public static Date parseDate(String d) throws ParseException {
return DateUtil.parseDate(d);
}
/** @deprecated */
@Deprecated
public static DateFormat getThreadLocalDateFormat() {
return DateUtil.getThreadLocalDateFormat();
}
public static String escapeQueryChars(String s) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if(c == 92 || c == 43 || c == 45 || c == 33 || c == 40 || c == 41 || c == 58 || c == 94 || c == 91 || c == 93 || c == 34 || c == 123 || c == 125 || c == 126 || c == 42 || c == 63 || c == 124 || c == 38 || c == 59 || c == 47 || Character.isWhitespace(c)) {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}
public static String toQueryString(SolrParams params, boolean xml) {
StringBuilder sb = new StringBuilder(128);
try {
String e = xml?"&":"&";
boolean first = true;
Iterator names = params.getParameterNamesIterator();
while(true) {
while(names.hasNext()) {
String key = (String)names.next();
String[] valarr = params.getParams(key);
if(valarr == null) {
sb.append(first?"?":e);
sb.append(key);
first = false;
} else {
String[] arr$ = valarr;
int len$ = valarr.length;
for(int i$ = 0; i$ < len$; ++i$) {
String val = arr$[i$];
sb.append(first?"?":e);
sb.append(key);
if(val != null) {
sb.append('=');
sb.append(URLEncoder.encode(val, "UTF-8"));
}
first = false;
}
}
}
return sb.toString();
}
} catch (IOException var12) {
throw new RuntimeException(var12);
}
}
public static void addSlices(Map<String, Slice> target, String collectionName, Collection<Slice> slices, boolean multiCollection) {
Slice slice;
String key;
for(Iterator i$ = slices.iterator(); i$.hasNext(); target.put(key, slice)) {
slice = (Slice)i$.next();
key = slice.getName();
if(multiCollection) {
key = collectionName + "_" + key;
}
}
}
static {
fmts = DateUtil.DEFAULT_DATE_FORMATS;
UTC = DateUtil.UTC;
}
}