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

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@765199 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Koji Sekiguchi 2009-04-15 13:47:58 +00:00
parent a31c7a692f
commit 6610c0489d
5 changed files with 116 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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

View File

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