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

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@683288 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2008-08-06 15:04:36 +00:00
parent ac0377be48
commit d816b977b4
4 changed files with 145 additions and 4 deletions

View File

@ -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

View File

@ -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().
*

View File

@ -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;

View File

@ -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();
}
}