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.cocoon.components.search;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.apache.cocoon.components.search.components.Indexer;
025: import org.apache.cocoon.components.search.fieldmodel.FieldDefinition;
026:
027: /**
028: * Index Definition class, contain all the index field definitions.
029: *
030: * @author Nicolas Maisonneuve
031: *
032: */
033: public final class IndexStructure {
034:
035: private Map fielddefs;
036:
037: public IndexStructure() {
038: fielddefs = new HashMap();
039:
040: // A index has always an UID field
041: FieldDefinition fielddef = FieldDefinition.create(
042: Indexer.DOCUMENT_UID_FIELD, FieldDefinition.KEYWORD);
043: fielddef.setStore(true);
044: this .addFieldDef(fielddef);
045:
046: }
047:
048: /**
049: * add a fieldDefiniition to the indexDefinition
050: *
051: * @param fielddef
052: */
053: public void addFieldDef(FieldDefinition fielddef) {
054: if (fielddefs.containsKey(fielddef.name())) {
055: throw new IllegalArgumentException(" field with the name "
056: + fielddef.name() + " is already used");
057: }
058: fielddefs.put(fielddef.name(), fielddef);
059: }
060:
061: /**
062: * @return all fieldnames contained in the index
063: */
064: public final String[] getFieldNames() {
065: Set results = fielddefs.keySet();
066: return (String[]) results.toArray(new String[results.size()]);
067: }
068:
069: /**
070: * return all fieldDefinitions
071: *
072: * @return FieldDefinition[]
073: */
074: public final FieldDefinition[] getFieldDef() {
075: Collection results = fielddefs.values();
076: return (FieldDefinition[]) results
077: .toArray(new FieldDefinition[results.size()]);
078: }
079:
080: /**
081: * Return the fieldDefinition associated to the name
082: *
083: * @param fieldname
084: * String the name of the fieldDefiniation
085: * @return FieldDefinition
086: */
087: public final FieldDefinition getFieldDef(String fieldname) {
088: return (FieldDefinition) fielddefs.get(fieldname);
089: }
090:
091: /**
092: * check if this field exist
093: *
094: * @param name
095: * the field's name
096: * @return true if a field with this name exist
097: */
098: public final boolean hasField(String name) {
099: return fielddefs.containsKey(name.intern());
100: }
101:
102: public String toString() {
103: StringBuffer result = new StringBuffer("DocumentFactory:");
104: Iterator iter = this .fielddefs.values().iterator();
105: while (iter.hasNext()) {
106: FieldDefinition item = (FieldDefinition) iter.next();
107: result.append("\n").append(item.toString());
108: }
109: return result.toString();
110: }
111:
112: }
|