com.github.underscore.U Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of underscore-kotlin Show documentation
Show all versions of underscore-kotlin Show documentation
The kotlin port of Underscore.js
The newest version!
/*
* The MIT License (MIT)
*
* Copyright 2015-2024 Valentyn Kolesnikov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.underscore;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
@SuppressWarnings({
"java:S135",
"java:S1168",
"java:S3655",
"java:S3740",
"java:S3776",
"java:S4423",
"java:S4830",
"java:S5843",
"java:S5996",
"java:S5998"
})
public class U extends Underscore {
private static final int DEFAULT_TRUNC_LENGTH = 30;
private static final String DEFAULT_TRUNC_OMISSION = "...";
private static final java.util.regex.Pattern RE_LATIN_1 =
java.util.regex.Pattern.compile("[\\xc0-\\xd6\\xd8-\\xde\\xdf-\\xf6\\xf8-\\xff]");
private static final java.util.regex.Pattern RE_PROP_NAME =
java.util.regex.Pattern.compile(
"[^.\\[\\]]+|\\[(?:(-?\\d+(?:\\.\\d+)?)|([\"'])((?:(?!\2)\\[^\\]|\\.)*?)\2)\\]|"
+ "(?=(\\.|\\[\\])(?:\4|$))");
private static final Map DEBURRED_LETTERS = new LinkedHashMap<>();
private static final Map> DEFAULT_HEADER_FIELDS = new HashMap<>();
private static final Set SUPPORTED_HTTP_METHODS =
new HashSet<>(Arrays.asList("GET", "POST", "PUT", "DELETE"));
private static final int BUFFER_LENGTH_1024 = 1024;
private static final int RESPONSE_CODE_400 = 400;
private static final String ROOT = "root";
private static final String UPPER = "[A-Z\\xc0-\\xd6\\xd8-\\xde\\u0400-\\u04FF]";
private static final String LOWER = "[a-z\\xdf-\\xf6\\xf8-\\xff]+";
private static final String SELF_CLOSING = "-self-closing";
private static final String NIL_KEY = "-nil";
private static final String OMIT_XML_DECL = "#omit-xml-declaration";
private static final java.util.regex.Pattern RE_WORDS =
java.util.regex.Pattern.compile(
UPPER
+ "+(?="
+ UPPER
+ LOWER
+ ")|"
+ UPPER
+ "?"
+ LOWER
+ "|"
+ UPPER
+ "+|\\d+");
static {
String[] deburredLetters =
new String[] {
"\u00c0", "A", "\u00c1", "A", "\u00c2", "A", "\u00c3", "A", "\u00c4", "A",
"\u00c5", "A", "\u00e0", "a", "\u00e1", "a", "\u00e2", "a", "\u00e3", "a",
"\u00e4", "a", "\u00e5", "a", "\u00c7", "C", "\u00e7", "c", "\u00d0", "D",
"\u00f0", "d", "\u00c8", "E", "\u00c9", "E", "\u00ca", "E", "\u00cb", "E",
"\u00e8", "e", "\u00e9", "e", "\u00ea", "e", "\u00eb", "e", "\u00cC", "I",
"\u00cd", "I", "\u00ce", "I", "\u00cf", "I", "\u00eC", "i", "\u00ed", "i",
"\u00ee", "i", "\u00ef", "i", "\u00d1", "N", "\u00f1", "n", "\u00d2", "O",
"\u00d3", "O", "\u00d4", "O", "\u00d5", "O", "\u00d6", "O", "\u00d8", "O",
"\u00f2", "o", "\u00f3", "o", "\u00f4", "o", "\u00f5", "o", "\u00f6", "o",
"\u00f8", "o", "\u00d9", "U", "\u00da", "U", "\u00db", "U", "\u00dc", "U",
"\u00f9", "u", "\u00fa", "u", "\u00fb", "u", "\u00fc", "u", "\u00dd", "Y",
"\u00fd", "y", "\u00ff", "y", "\u00c6", "Ae", "\u00e6", "ae", "\u00de", "Th",
"\u00fe", "th", "\u00df", "ss"
};
for (int index = 0; index < deburredLetters.length; index += 2) {
DEBURRED_LETTERS.put(deburredLetters[index], deburredLetters[index + 1]);
}
DEFAULT_HEADER_FIELDS.put(
"Content-Type", Arrays.asList("application/json", "charset=utf-8"));
}
public enum XmlToJsonMode {
REPLACE_SELF_CLOSING_WITH_NULL,
REPLACE_SELF_CLOSING_WITH_STRING,
REPLACE_EMPTY_VALUE_WITH_NULL,
REPLACE_EMPTY_TAG_WITH_NULL,
REPLACE_EMPTY_TAG_WITH_STRING,
REMOVE_FIRST_LEVEL,
WITHOUT_NAMESPACES,
REPLACE_MINUS_WITH_AT,
REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT
}
public enum JsonToXmlMode {
FORCE_ATTRIBUTE_USAGE,
DEFINE_ROOT_NAME,
REPLACE_NULL_WITH_EMPTY_VALUE,
REPLACE_EMPTY_STRING_WITH_EMPTY_VALUE,
ADD_ROOT,
REMOVE_ARRAY_ATTRIBUTE,
REMOVE_ATTRIBUTES
}
public U(final Iterable iterable) {
super(iterable);
}
public U(final String string) {
super(string);
}
public static class Chain extends Underscore.Chain {
public Chain(final T item) {
super(item);
}
public Chain(final List list) {
super(list);
}
public Chain(final Map map) {
super(map);
}
@Override
public Chain first() {
return new Chain<>(Underscore.first(value()));
}
@Override
public Chain first(int n) {
return new Chain<>(Underscore.first(value(), n));
}
@Override
public Chain firstOrNull() {
return new Chain<>(Underscore.firstOrNull(value()));
}
@Override
public Chain firstOrNull(final Predicate pred) {
return new Chain<>(Underscore.firstOrNull(value(), pred));
}
@Override
public Chain initial() {
return new Chain<>(Underscore.initial(value()));
}
@Override
public Chain initial(int n) {
return new Chain<>(Underscore.initial(value(), n));
}
@Override
public Chain last() {
return new Chain<>(Underscore.last(value()));
}
@Override
public Chain last(int n) {
return new Chain<>(Underscore.last(value(), n));
}
@Override
public Chain lastOrNull() {
return new Chain<>(Underscore.lastOrNull(value()));
}
@Override
public Chain lastOrNull(final Predicate pred) {
return new Chain<>(Underscore.lastOrNull(value(), pred));
}
@Override
public Chain rest() {
return new Chain<>(Underscore.rest(value()));
}
@Override
public Chain rest(int n) {
return new Chain<>(Underscore.rest(value(), n));
}
@Override
public Chain compact() {
return new Chain<>(Underscore.compact(value()));
}
@Override
public Chain compact(final T falsyValue) {
return new Chain<>(Underscore.compactList(value(), falsyValue));
}
@Override
public Chain flatten() {
return new Chain<>(Underscore.flatten(value()));
}
@Override
public Chain map(final Function super T, F> func) {
return new Chain<>(Underscore.map(value(), func));
}
@Override
public Chain mapMulti(final BiConsumer super T, ? super Consumer> mapper) {
return new Chain<>(Underscore.mapMulti(value(), mapper));
}
@Override
public Chain mapIndexed(final BiFunction func) {
return new Chain<>(Underscore.mapIndexed(value(), func));
}
@Override
public Chain filter(final Predicate pred) {
return new Chain<>(Underscore.filter(value(), pred));
}
@Override
public Chain filterIndexed(final PredicateIndexed pred) {
return new Chain<>(Underscore.filterIndexed(value(), pred));
}
@Override
public Chain rejectIndexed(final PredicateIndexed pred) {
return new Chain<>(Underscore.rejectIndexed(value(), pred));
}
@Override
public Chain reject(final Predicate pred) {
return new Chain<>(Underscore.reject(value(), pred));
}
@Override
public Chain filterFalse(final Predicate pred) {
return new Chain<>(Underscore.filterFalse(value(), pred));
}
@Override
public Chain reduce(final BiFunction func, final F zeroElem) {
return new Chain<>(Underscore.reduce(value(), func, zeroElem));
}
@Override
public Chain> reduce(final BinaryOperator func) {
return new Chain<>(Underscore.reduce(value(), func));
}
@Override
public Chain reduceRight(final BiFunction func, final F zeroElem) {
return new Chain<>(Underscore.reduceRight(value(), func, zeroElem));
}
@Override
public Chain> reduceRight(final BinaryOperator func) {
return new Chain<>(Underscore.reduceRight(value(), func));
}
@Override
public Chain> find(final Predicate pred) {
return new Chain<>(Underscore.find(value(), pred));
}
@Override
public Chain> findLast(final Predicate pred) {
return new Chain<>(Underscore.findLast(value(), pred));
}
@Override
@SuppressWarnings("unchecked")
public Chain max() {
return new Chain<>(Underscore.max((Collection) value()));
}
@Override
public > Chain max(final Function func) {
return new Chain<>(Underscore.max(value(), func));
}
@Override
@SuppressWarnings("unchecked")
public Chain min() {
return new Chain<>(Underscore.min((Collection) value()));
}
@Override
public > Chain min(final Function func) {
return new Chain<>(Underscore.min(value(), func));
}
@Override
@SuppressWarnings("unchecked")
public Chain sort() {
return new Chain<>(Underscore.sort((List) value()));
}
@Override
@SuppressWarnings("unchecked")
public > Chain sortWith(final Comparator comparator) {
return new Chain<>(Underscore.sortWith((List) value(), comparator));
}
@Override
public > Chain sortBy(final Function func) {
return new Chain<>(Underscore.sortBy(value(), func));
}
@Override
@SuppressWarnings("unchecked")
public Chain