Skip to content

Apache Lucene open-source search software

SOLR-1115: <bool>on</bool> and <bool>yes</bool> in solrconfig.xml work as expected.

Koji Sekiguchi6610c0489d4d
diff --git a/CHANGES.txt b/CHANGES.txt
index 3f2e442612d..622eca79ad2 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -196,6 +196,8 @@ New Features
35. SOLR-1096: Introduced httpConnTimeout and httpReadTimeout in replication slave configuration to avoid stalled
replication. (Jeff Newburn, Noble Paul, shalin)
+36. SOLR-1115: <bool>on</bool> and <bool>yes</bool> work as expected in solrconfig.xml. (koji)
+
Optimizations
----------------------
1. SOLR-374: Use IndexReader.reopen to save resources by re-using parts of the
diff --git a/src/common/org/apache/solr/common/params/SolrParams.java b/src/common/org/apache/solr/common/params/SolrParams.java
index 2415872b7d1..74f6684312e 100644
--- a/src/common/org/apache/solr/common/params/SolrParams.java
+++ b/src/common/org/apache/solr/common/params/SolrParams.java
@@ -25,6 +25,7 @@ import java.util.Map;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.common.util.StrUtils;
/** SolrParams hold request parameters.
*
@@ -85,27 +86,27 @@ public abstract class SolrParams implements Serializable {
/** Returns the Boolean value of the param, or null if not set */
public Boolean getBool(String param) {
String val = get(param);
- return val==null ? null : parseBool(val);
+ return val==null ? null : StrUtils.parseBool(val);
}
/** Returns the boolean value of the param, or def if not set */
public boolean getBool(String param, boolean def) {
String val = get(param);
- return val==null ? def : parseBool(val);
+ return val==null ? def : StrUtils.parseBool(val);
}
/** Returns the Boolean value of the field param,
or the value for param, or null if neither is set. */
public Boolean getFieldBool(String field, String param) {
String val = getFieldParam(field, param);
- return val==null ? null : parseBool(val);
+ return val==null ? null : StrUtils.parseBool(val);
}
/** Returns the boolean value of the field param,
or the value for param, or def if neither is set. */
public boolean getFieldBool(String field, String param, boolean def) {
String val = getFieldParam(field, param);
- return val==null ? def : parseBool(val);
+ return val==null ? def : StrUtils.parseBool(val);
}
/** Returns the Integer value of the param, or null if not set */
@@ -204,17 +205,11 @@ public abstract class SolrParams implements Serializable {
/** how to transform a String into a boolean... more flexible than
* Boolean.parseBoolean() to enable easier integration with html forms.
+ * @deprecated Use org.apache.solr.common.util.StrUtils.parseBool
*/
+ @Deprecated
protected boolean parseBool(String s) {
- if( s != null ) {
- if( s.startsWith("true") || s.startsWith("on") || s.startsWith("yes") ) {
- return true;
- }
- if( s.startsWith("false") || s.startsWith("off") || s.equals("no") ) {
- return false;
- }
- }
- throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "invalid boolean value: "+s );
+ return StrUtils.parseBool(s);
}
/** Create a Map<String,String> from a NamedList given no keys are repeated */
diff --git a/src/common/org/apache/solr/common/util/DOMUtil.java b/src/common/org/apache/solr/common/util/DOMUtil.java
index 4494b976191..797860bd619 100644
--- a/src/common/org/apache/solr/common/util/DOMUtil.java
+++ b/src/common/org/apache/solr/common/util/DOMUtil.java
@@ -137,7 +137,7 @@ public class DOMUtil {
} else if ("double".equals(type)) {
val = Double.valueOf(getText(nd));
} else if ("bool".equals(type)) {
- val = Boolean.valueOf(getText(nd));
+ val = StrUtils.parseBool(getText(nd));
} else if ("lst".equals(type)) {
val = childNodesToNamedList(nd);
} else if ("arr".equals(type)) {
diff --git a/src/common/org/apache/solr/common/util/StrUtils.java b/src/common/org/apache/solr/common/util/StrUtils.java
index e2738e2ee59..9ac05124caa 100644
--- a/src/common/org/apache/solr/common/util/StrUtils.java
+++ b/src/common/org/apache/solr/common/util/StrUtils.java
@@ -22,6 +22,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.io.IOException;
+import org.apache.solr.common.SolrException;
+
/**
* @version $Id$
*/
@@ -219,6 +221,21 @@ public class StrUtils {
char ch = s.length()>0 ? s.charAt(0) : 0;
return (ch=='1' || ch=='t' || ch=='T');
}
+
+ /** how to transform a String into a boolean... more flexible than
+ * Boolean.parseBoolean() to enable easier integration with html forms.
+ */
+ public static boolean parseBool(String s) {
+ if( s != null ) {
+ if( s.startsWith("true") || s.startsWith("on") || s.startsWith("yes") ) {
+ return true;
+ }
+ if( s.startsWith("false") || s.startsWith("off") || s.equals("no") ) {
+ return false;
+ }
+ }
+ throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "invalid boolean value: "+s );
+ }
/**
* URLEncodes a value, replacing only enough chars so that
diff --git a/src/test/org/apache/solr/common/util/DOMUtilTest.java b/src/test/org/apache/solr/common/util/DOMUtilTest.java
new file mode 100644
index 00000000000..2596791a02a
--- /dev/null
+++ b/src/test/org/apache/solr/common/util/DOMUtilTest.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.common.util;
+
+import java.io.ByteArrayInputStream;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+
+import junit.framework.TestCase;
+
+public class DOMUtilTest extends TestCase {
+
+ private DocumentBuilder builder;
+ private static final XPathFactory xpathFactory = XPathFactory.newInstance();
+
+ public void setUp() throws Exception {
+ builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ }
+
+ public void testAddToNamedListPrimitiveTypes() throws Exception {
+ NamedList<Object> namedList = new SimpleOrderedMap<Object>();
+ DOMUtil.addToNamedList( getNode( "<str name=\"String\">STRING</str>", "/str" ), namedList, null );
+ assertTypeAndValue( namedList, "String", "STRING" );
+ DOMUtil.addToNamedList( getNode( "<int name=\"Integer\">100</int>", "/int" ), namedList, null );
+ assertTypeAndValue( namedList, "Integer", Integer.valueOf( 100 ) );
+ DOMUtil.addToNamedList( getNode( "<long name=\"Long\">200</long>", "/long" ), namedList, null );
+ assertTypeAndValue( namedList, "Long", Long.valueOf( 200 ) );
+ DOMUtil.addToNamedList( getNode( "<float name=\"Float\">300</float>", "/float" ), namedList, null );
+ assertTypeAndValue( namedList, "Float", Float.valueOf( 300 ) );
+ DOMUtil.addToNamedList( getNode( "<double name=\"Double\">400</double>", "/double" ), namedList, null );
+ assertTypeAndValue( namedList, "Double", Double.valueOf( 400 ) );
+ DOMUtil.addToNamedList( getNode( "<bool name=\"Boolean\">true</bool>", "/bool" ), namedList, null );
+ assertTypeAndValue( namedList, "Boolean", true );
+ DOMUtil.addToNamedList( getNode( "<bool name=\"Boolean\">on</bool>", "/bool" ), namedList, null );
+ assertTypeAndValue( namedList, "Boolean", true );
+ DOMUtil.addToNamedList( getNode( "<bool name=\"Boolean\">yes</bool>", "/bool" ), namedList, null );
+ assertTypeAndValue( namedList, "Boolean", true );
+ DOMUtil.addToNamedList( getNode( "<bool name=\"Boolean\">false</bool>", "/bool" ), namedList, null );
+ assertTypeAndValue( namedList, "Boolean", false );
+ DOMUtil.addToNamedList( getNode( "<bool name=\"Boolean\">off</bool>", "/bool" ), namedList, null );
+ assertTypeAndValue( namedList, "Boolean", false );
+ DOMUtil.addToNamedList( getNode( "<bool name=\"Boolean\">no</bool>", "/bool" ), namedList, null );
+ assertTypeAndValue( namedList, "Boolean", false );
+ }
+
+ private void assertTypeAndValue( NamedList<Object> namedList, String key, Object value ) throws Exception {
+ Object v = namedList.get( key );
+ assertNotNull( v );
+ assertEquals( key, v.getClass().getSimpleName() );
+ assertEquals( value, v );
+ namedList.remove( key );
+ }
+
+ public Node getNode( String xml, String path ) throws Exception {
+ return getNode( getDocument(xml), path );
+ }
+
+ public Node getNode( Document doc, String path ) throws Exception {
+ XPath xpath = xpathFactory.newXPath();
+ return (Node)xpath.evaluate(path, doc, XPathConstants.NODE);
+ }
+
+ public Document getDocument( String xml ) throws Exception {
+ return builder.parse( new ByteArrayInputStream( xml.getBytes() ) );
+ }
+}