001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.schema;
017:
018: import org.apache.lucene.search.SortField;
019: import org.apache.solr.search.function.ValueSource;
020: import org.apache.solr.search.function.OrdFieldSource;
021: import org.apache.lucene.analysis.Token;
022: import org.apache.lucene.analysis.Analyzer;
023: import org.apache.lucene.analysis.TokenStream;
024: import org.apache.lucene.analysis.Tokenizer;
025: import org.apache.lucene.document.Fieldable;
026: import org.apache.solr.request.XMLWriter;
027: import org.apache.solr.request.TextResponseWriter;
028: import org.apache.solr.analysis.SolrAnalyzer;
029:
030: import java.util.Map;
031: import java.io.Reader;
032: import java.io.IOException;
033:
034: /**
035: * @author yonik
036: * @version $Id: BoolField.java 479793 2006-11-27 22:40:21Z klaas $
037: */
038: public class BoolField extends FieldType {
039: protected void init(IndexSchema schema, Map<String, String> args) {
040: }
041:
042: public SortField getSortField(SchemaField field, boolean reverse) {
043: return getStringSort(field, reverse);
044: }
045:
046: public ValueSource getValueSource(SchemaField field) {
047: return new OrdFieldSource(field.name);
048: }
049:
050: // avoid instantiating every time...
051: protected final static Token TRUE_TOKEN = new Token("T", 0, 1);
052: protected final static Token FALSE_TOKEN = new Token("F", 0, 1);
053:
054: ////////////////////////////////////////////////////////////////////////
055: // TODO: look into creating my own queryParser that can more efficiently
056: // handle single valued non-text fields (int,bool,etc) if needed.
057:
058: protected final static Analyzer boolAnalyzer = new SolrAnalyzer() {
059: public TokenStream tokenStream(String fieldName, Reader reader) {
060: return new Tokenizer(reader) {
061: boolean done = false;
062:
063: public Token next() throws IOException {
064: if (done)
065: return null;
066: done = true;
067: int ch = input.read();
068: if (ch == -1)
069: return null;
070: return (ch == 't' || ch == 'T' || ch == '1') ? TRUE_TOKEN
071: : FALSE_TOKEN;
072: }
073: };
074: }
075: };
076:
077: public Analyzer getAnalyzer() {
078: return boolAnalyzer;
079: }
080:
081: public Analyzer getQueryAnalyzer() {
082: return boolAnalyzer;
083: }
084:
085: public String toInternal(String val) {
086: char ch = (val != null && val.length() > 0) ? val.charAt(0) : 0;
087: return (ch == '1' || ch == 't' || ch == 'T') ? "T" : "F";
088: }
089:
090: public String toExternal(Fieldable f) {
091: return indexedToReadable(f.stringValue());
092: }
093:
094: public String indexedToReadable(String indexedForm) {
095: char ch = indexedForm.charAt(0);
096: return ch == 'T' ? "true" : "false";
097: }
098:
099: public void write(XMLWriter xmlWriter, String name, Fieldable f)
100: throws IOException {
101: xmlWriter.writeBool(name, f.stringValue().charAt(0) == 'T');
102: }
103:
104: public void write(TextResponseWriter writer, String name,
105: Fieldable f) throws IOException {
106: writer.writeBool(name, f.stringValue().charAt(0) == 'T');
107: }
108: }
|