Skip to content

Apache Lucene open-source search software

SOLR-614 -- Allow components to be configured with NamedListInitializedPlugin using arbitary XML in solrconfig without using XPath

Shalin Shekhar Mangard816b977b4b0
diff --git a/CHANGES.txt b/CHANGES.txt
index bf6592ccba7..935e1d3ee46 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -345,6 +345,9 @@ New Features
69. SOLR-506: Emitting HTTP Cache headers can be enabled or disabled through configuration
on a per-handler basis (shalin)
+
+70. SOLR-614: Allow components to be configured with NamedListInitializedPlugin using arbitary XML in solrconfig
+ without using XPath (Noble Paul, shalin)
Changes in runtime behavior
1. SOLR-559: use Lucene updateDocument, deleteDocuments methods. This
diff --git a/src/java/org/apache/solr/common/util/DOMUtil.java b/src/java/org/apache/solr/common/util/DOMUtil.java
index 11d337b31af..71bb9a94804 100644
--- a/src/java/org/apache/solr/common/util/DOMUtil.java
+++ b/src/java/org/apache/solr/common/util/DOMUtil.java
@@ -89,7 +89,8 @@ public class DOMUtil {
// Should these be moved to Config? Should all of these things?
//////////////////////////////////////////////////////////
public static NamedList<Object> childNodesToNamedList(Node nd) {
- return nodesToNamedList(nd.getChildNodes());
+ NamedList<Object> nl = nodesToNamedList(nd.getChildNodes());
+ return nl;
}
public static List childNodesToList(Node nd) {
@@ -146,12 +147,51 @@ public class DOMUtil {
val = childNodesToNamedList(nd);
} else if ("arr".equals(type)) {
val = childNodesToList(nd);
+ } else {
+ name = type;
+ val = readFlexibleNamedList(nd);
}
if (nlst != null) nlst.add(name,val);
if (arr != null) arr.add(val);
}
+ private static Object readFlexibleNamedList(Node nd) {
+ if (hasChildren(nd)) {
+ NamedList nl = new NamedList<Object>();
+ readAttributes(nd, nl);
+ NodeList nlist = nd.getChildNodes();
+ if (nlist != null) {
+ for (int i = 0; i < nlist.getLength(); i++) {
+ if (nlist.item(i).getNodeType() == Node.ELEMENT_NODE)
+ addToNamedList(nlist.item(i), nl, null);
+ }
+ }
+ return nl;
+ } else {
+ return getText(nd);
+ }
+ }
+
+ private static void readAttributes(Node nd, NamedList nl) {
+ NamedNodeMap attrs = nd.getAttributes();
+ if (attrs != null) {
+ for (int i = 0; i < attrs.getLength(); i++) {
+ Node nameNd = attrs.item(i);
+ nl.add("@"+nameNd.getNodeName(), nameNd.getNodeValue());
+ }
+ }
+ }
+
+ private static boolean hasChildren(Node nd){
+ if(nd.hasAttributes()) return true;
+ NodeList nlst = nd.getChildNodes();
+ for (int i=0; i<nlst.getLength(); i++) {
+ if(nlst.item(i).getNodeType() == Node.ELEMENT_NODE) return true;
+ }
+ return false;
+ }
+
/**
* Drop in replacement for Node.getTextContent().
*
diff --git a/src/java/org/apache/solr/core/SolrConfig.java b/src/java/org/apache/solr/core/SolrConfig.java
index 9b691df4dc4..5636c976c8b 100644
--- a/src/java/org/apache/solr/core/SolrConfig.java
+++ b/src/java/org/apache/solr/core/SolrConfig.java
@@ -18,6 +18,7 @@
package org.apache.solr.core;
import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.DOMUtil;
import org.apache.solr.handler.PingRequestHandler;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
@@ -27,13 +28,13 @@ import org.apache.solr.update.SolrIndexConfig;
import org.apache.lucene.search.BooleanQuery;
import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPathConstants;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.StringTokenizer;
+import java.util.*;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
@@ -210,6 +211,32 @@ public class SolrConfig extends Config {
return new LocalSolrQueryRequest(core, pingQueryParams);
}
+ /**
+ * Get a list of NamedList, one for each node matched by the given XPath
+ *
+ * @param xpath the XPath to match
+ * @return a list of NamedList instances corresponding to each node matched by the given XPath
+ */
+ public List<NamedList> getAllNodesAsNamedList(String xpath) {
+ NodeList nodes = (NodeList) evaluate(xpath, XPathConstants.NODESET);
+ List<NamedList> result = new ArrayList<NamedList>();
+ for (int i = 0; i < nodes.getLength(); i++) {
+ result.add(DOMUtil.childNodesToNamedList(nodes.item(i)));
+ }
+ return result;
+ }
+
+ /**
+ * Get the NamedList corresponding to the first node matched by the given XPath
+ *
+ * @param xpath the XPath to match
+ * @return the NamedList corresponding to the first node matched by the given XPath
+ */
+ public NamedList getNodeAsNamedList(String xpath) {
+ List<NamedList> list = getAllNodesAsNamedList(xpath);
+ return list.isEmpty() ? null : list.get(0);
+ }
+
public static class JmxConfiguration {
public boolean enabled = false;
diff --git a/src/test/org/apache/solr/common/util/TestDOMUtil.java b/src/test/org/apache/solr/common/util/TestDOMUtil.java
new file mode 100644
index 00000000000..0f5bc3361ef
--- /dev/null
+++ b/src/test/org/apache/solr/common/util/TestDOMUtil.java
@@ -0,0 +1,71 @@
+package org.apache.solr.common.util;
+
+import org.junit.Test;
+import org.junit.Assert;
+import org.xml.sax.InputSource;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.StringReader;
+
+public class TestDOMUtil {
+ @Test
+ public void readFlexiSchemaNodes() throws Exception {
+ String xml ="<root>\n" +
+ " <a>val a</a>\n" +
+ " <b>val b</b>\n" +
+ " <c>\n" +
+ " <d>val d</d>\n" +
+ " <e>val e</e>\n" +
+ " </c>\n" +
+ "</root>";
+ NamedList nl = DOMUtil.childNodesToNamedList(getRootNode(xml));
+ Assert.assertEquals("val a",nl.get("a"));
+ Assert.assertEquals("val b",nl.get("b"));
+ NamedList c = (NamedList) nl.get("c");
+ Assert.assertEquals("val d",c.get("d"));
+ Assert.assertEquals("val e",c.get("e"));
+ }
+
+ @Test
+ public void readFlexiSchemaNodesWithAttributes() throws Exception {
+ String xml ="<root>\n" +
+ " <defaults a=\"A\" b=\"B\" c=\"C\">\n" +
+ " <x>X1</x>\n" +
+ " <x>X2</x>\n" +
+ " </defaults>\n" +
+ "</root>";
+ NamedList nl = DOMUtil.childNodesToNamedList(getRootNode(xml));
+ NamedList defaults = (NamedList) nl.get("defaults");
+ Assert.assertEquals("A",defaults.get("@a"));
+ Assert.assertEquals("B",defaults.get("@b"));
+ Assert.assertEquals("C",defaults.get("@c"));
+ Assert.assertEquals("X1",defaults.getVal(3));
+ Assert.assertEquals("X2",defaults.getVal(4));
+ }
+ @Test
+ public void readFlexiSchemaNodesWithAttributesWithOldFormat() throws Exception {
+ String xml ="<root>\n" +
+ " <defaults a=\"A\" b=\"B\" c=\"C\">\n" +
+ " <str name=\"x\">X1</str>\n" +
+ " <str name=\"x\">X2</str>\n" +
+ " <bool name=\"boo\">true</bool>\n" +
+ " </defaults>\n" +
+ "</root>";
+ NamedList nl = DOMUtil.childNodesToNamedList(getRootNode(xml));
+ NamedList defaults = (NamedList) nl.get("defaults");
+ Assert.assertEquals("A",defaults.get("@a"));
+ Assert.assertEquals("B",defaults.get("@b"));
+ Assert.assertEquals("C",defaults.get("@c"));
+ Assert.assertEquals("X1",defaults.getVal(3));
+ Assert.assertEquals("X2",defaults.getVal(4));
+ Assert.assertEquals(Boolean.TRUE,defaults.get("boo"));
+ }
+
+ private Node getRootNode(String xml) throws Exception {
+ javax.xml.parsers.DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document doc = builder.parse(new InputSource(new StringReader(xml)));
+ return doc.getDocumentElement();
+ }
+}