001: package org.compass.core.lucene;
002:
003: import java.io.Reader;
004:
005: import org.apache.lucene.analysis.TokenStream;
006: import org.apache.lucene.document.Field;
007: import org.compass.core.Property;
008: import org.compass.core.Resource;
009: import org.compass.core.ResourceFactory;
010: import org.compass.core.engine.RepeatableReader;
011: import org.compass.core.engine.SearchEngineException;
012: import org.compass.core.lucene.engine.LuceneSearchEngineFactory;
013: import org.compass.core.lucene.util.LuceneUtils;
014: import org.compass.core.mapping.ResourcePropertyMapping;
015: import org.compass.core.util.StringUtils;
016: import org.compass.core.util.reader.ReverseStringReader;
017:
018: /**
019: * @author kimchy
020: */
021: public class LuceneResourceFactory implements ResourceFactory {
022:
023: private LuceneSearchEngineFactory searchEngineFactory;
024:
025: public LuceneResourceFactory(
026: LuceneSearchEngineFactory searchEngineFactory) {
027: this .searchEngineFactory = searchEngineFactory;
028: }
029:
030: public String getNullValue() {
031: return "";
032: }
033:
034: public boolean isNullValue(String value) {
035: return value == null || value.length() == 0;
036: }
037:
038: public Resource createResource(String alias)
039: throws SearchEngineException {
040: return new LuceneMultiResource(alias, searchEngineFactory);
041: }
042:
043: public Property createProperty(String value,
044: ResourcePropertyMapping mapping)
045: throws SearchEngineException {
046: return createProperty(mapping.getPath().getPath(), value,
047: mapping);
048: }
049:
050: public Property createProperty(String value,
051: ResourcePropertyMapping mapping, Property.Store store,
052: Property.Index index) throws SearchEngineException {
053: return createProperty(mapping.getPath().getPath(), value,
054: mapping, store, index);
055: }
056:
057: public Property createProperty(String name, String value,
058: ResourcePropertyMapping mapping)
059: throws SearchEngineException {
060: return createProperty(name, value, mapping, mapping.getStore(),
061: mapping.getIndex());
062: }
063:
064: public Property createProperty(String name, String value,
065: ResourcePropertyMapping mapping, Property.Store store,
066: Property.Index index) throws SearchEngineException {
067: Property property;
068: if (mapping.getReverse() == ResourcePropertyMapping.ReverseType.NO) {
069: property = createProperty(name, value, store, index,
070: mapping.getTermVector());
071: } else if (mapping.getReverse() == ResourcePropertyMapping.ReverseType.READER) {
072: property = createProperty(name, new ReverseStringReader(
073: value), mapping.getTermVector());
074: } else if (mapping.getReverse() == ResourcePropertyMapping.ReverseType.STRING) {
075: property = createProperty(name, StringUtils.reverse(value),
076: store, index, mapping.getTermVector());
077: } else {
078: throw new SearchEngineException(
079: "Unsupported Reverse type [" + mapping.getReverse()
080: + "]");
081: }
082: property.setBoost(mapping.getBoost());
083: property.setOmitNorms(mapping.isOmitNorms());
084: ((LuceneProperty) property).setPropertyMapping(mapping);
085: return property;
086: }
087:
088: public Property createProperty(String name, String value,
089: Property.Store store, Property.Index index)
090: throws SearchEngineException {
091: return createProperty(name, value, store, index,
092: Property.TermVector.NO);
093: }
094:
095: public Property createProperty(String name, String value,
096: Property.Store store, Property.Index index,
097: Property.TermVector termVector)
098: throws SearchEngineException {
099: Field.Store fieldStore = LuceneUtils.getFieldStore(store);
100: Field.Index fieldIndex = LuceneUtils.getFieldIndex(index);
101: Field.TermVector fieldTermVector = LuceneUtils
102: .getFieldTermVector(termVector);
103: Field field = new Field(name, value, fieldStore, fieldIndex,
104: fieldTermVector);
105: return new LuceneProperty(field);
106: }
107:
108: public Property createProperty(String name,
109: TokenStream tokenStream, Property.TermVector termVector) {
110: Field.TermVector fieldTermVector = LuceneUtils
111: .getFieldTermVector(termVector);
112: Field field = new Field(name, tokenStream, fieldTermVector);
113: return new LuceneProperty(field);
114: }
115:
116: public Property createProperty(String name, Reader value) {
117: return createProperty(name, value, Property.TermVector.NO);
118: }
119:
120: public Property createProperty(String name, byte[] value,
121: Property.Store store) throws SearchEngineException {
122: Field.Store fieldStore = LuceneUtils.getFieldStore(store);
123: Field field = new Field(name, value, fieldStore);
124: return new LuceneProperty(field);
125: }
126:
127: public Property createProperty(String name, Reader value,
128: Property.TermVector termVector) {
129: Field.TermVector fieldTermVector = LuceneUtils
130: .getFieldTermVector(termVector);
131: Field field = new Field(name, value, fieldTermVector);
132: if (value instanceof RepeatableReader) {
133: return new LuceneProperty(field, (RepeatableReader) value);
134: }
135: return new LuceneProperty(field);
136: }
137: }
|