Skip to content

Commit 793ded9

Browse files
committed
Sync with underscore-java.
1 parent a39fb89 commit 793ded9

File tree

4 files changed

+158
-1
lines changed

4 files changed

+158
-1
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright 2015-2020 Valentyn Kolesnikov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.github.underscore;
25+
26+
public class Trie {
27+
static class TrieNode {
28+
// Initialize your data structure here.
29+
TrieNode[] children;
30+
boolean isWord;
31+
public TrieNode() {
32+
children = new TrieNode[1071];
33+
}
34+
}
35+
36+
private final TrieNode root;
37+
private boolean startWith;
38+
39+
public Trie() {
40+
root = new TrieNode();
41+
}
42+
43+
// Inserts a word into the trie.
44+
public void insert(String word) {
45+
insert(word, root, 0);
46+
}
47+
48+
private void insert(String word, TrieNode root, int idx) {
49+
if (idx == word.length()) {
50+
root.isWord = true;
51+
return;
52+
}
53+
int index = word.charAt(idx) - ' ';
54+
if (root.children[index] == null) {
55+
root.children[index] = new TrieNode();
56+
}
57+
insert(word, root.children[index], idx + 1);
58+
}
59+
60+
// Returns if the word is in the trie.
61+
public boolean search(String word) {
62+
return search(word, root, 0);
63+
}
64+
65+
public boolean search(String word, TrieNode root, int idx) {
66+
if (idx == word.length()) {
67+
startWith = true;
68+
return root.isWord;
69+
}
70+
int index = word.charAt(idx) - ' ';
71+
if (root.children[index] == null) {
72+
startWith = false;
73+
return false;
74+
}
75+
76+
return search(word, root.children[index], idx + 1);
77+
}
78+
79+
// Returns if there is any word in the trie
80+
// that starts with the given prefix.
81+
public boolean startsWith(String prefix) {
82+
search(prefix);
83+
return startWith;
84+
}
85+
}

src/main/java/com/github/underscore/lodash/Xml.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ private static void checkLocalMap(final XmlStringBuilder builder, final Map loca
923923
U.<String>newLinkedHashSet(), false);
924924
}
925925
} else {
926-
XmlObject.writeXml(localMap2, null, builder, false, U.<String>newLinkedHashSet(), false);
926+
XmlObject.writeXml(localMap2, getRootName(localMap2), builder, false, U.<String>newLinkedHashSet(), false);
927927
}
928928
}
929929

@@ -966,6 +966,7 @@ private static String getRootName(final Map localMap) {
966966
if (String.valueOf(entry.getKey()).startsWith("-")) {
967967
foundAttrs += 1;
968968
} else if (!String.valueOf(entry.getKey()).startsWith(COMMENT)
969+
&& !String.valueOf(entry.getKey()).startsWith(CDATA)
969970
&& !String.valueOf(entry.getKey()).startsWith("?")) {
970971
if (entry.getValue() instanceof List && ((List) entry.getValue()).size() > 1) {
971972
foundListElements += 1;
@@ -1396,6 +1397,8 @@ private static org.w3c.dom.Document createDocument(final String xml)
13961397
factory.setNamespaceAware(true);
13971398
try {
13981399
factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
1400+
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
1401+
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
13991402
} catch (javax.xml.parsers.ParserConfigurationException ignored) {
14001403
// ignored
14011404
}
@@ -1412,6 +1415,8 @@ private static org.w3c.dom.Document createDocument() {
14121415
factory.setNamespaceAware(true);
14131416
try {
14141417
factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
1418+
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
1419+
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
14151420
} catch (javax.xml.parsers.ParserConfigurationException ignored) {
14161421
// ignored
14171422
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright 2015-2020 Valentyn Kolesnikov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.github.underscore;
25+
26+
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertTrue;
28+
29+
import org.junit.Test;
30+
31+
/**
32+
* Underscore library unit test.
33+
*
34+
* @author Valentyn Kolesnikov
35+
*/
36+
public class TrieTest {
37+
38+
@Test
39+
public void startsWith() {
40+
Trie trie = new Trie();
41+
trie.insert("apple");
42+
trie.insert("apple");
43+
assertTrue(trie.startsWith("app"));
44+
assertFalse(trie.startsWith("b"));
45+
}
46+
47+
@Test
48+
public void startsWith2() {
49+
Trie trie = new Trie();
50+
trie.insert("тест");
51+
trie.insert("тест");
52+
assertTrue(trie.startsWith("те"));
53+
assertFalse(trie.startsWith("с"));
54+
}
55+
56+
@Test
57+
public void startsWith3() {
58+
Trie trie = new Trie();
59+
trie.insert("0123");
60+
trie.insert("0123");
61+
assertTrue(trie.startsWith("012"));
62+
assertFalse(trie.startsWith("3"));
63+
}
64+
}

src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,9 @@ public void toXmlFromJson9() {
24772477
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
24782478
+ "<element><![CDATA[ 1 ]]>\n<![CDATA[ 2 ]]>\n<id>1</id>\n</element>",
24792479
U.toXml((Map<String, Object>) U.fromJson(json2)));
2480+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
2481+
+ "<root><![CDATA[]]></root>",
2482+
U.toXml((Map<String, Object>) U.fromJson("{\"#cdata-section\":\"\"}")));
24802483
}
24812484

24822485
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)