001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2005-02-22 10:25:37 +0100 (Di, 22 Feb 2005) $
025: * $Author: til132 $
026: * $Revision: 24 $
027: */
028: package net.sf.regain.crawler.config;
029:
030: import java.util.ArrayList;
031: import java.util.Map;
032:
033: /**
034: * The configuration of a preparator. Consists of a set of sections which
035: * contain a set of key-value-pairs.
036: *
037: * @author Til Schneider, www.murfman.de
038: */
039: public class PreparatorConfig {
040:
041: /**
042: * The sections of this config. A section is a Object[] with two elements:<br>
043: * 0 (String): The name of the section<br>
044: * 1 (Map): The key-value-pairs of the section.
045: * <p>
046: * We use no HashMap here, because two sections may have the same name.
047: */
048: private ArrayList mSectionList;
049:
050: /**
051: * Adds a section to the config
052: *
053: * @param name The name of the section to add.
054: * @param content The key-value-pairs of the section to add.
055: */
056: void addSection(String name, Map content) {
057: if (mSectionList == null) {
058: mSectionList = new ArrayList();
059: }
060:
061: mSectionList.add(new Object[] { name, content });
062: }
063:
064: /**
065: * Gets the number of sections this config has.
066: *
067: * @return The number of sections.
068: */
069: public int getSectionCount() {
070: if (mSectionList == null) {
071: return 0;
072: } else {
073: return mSectionList.size();
074: }
075: }
076:
077: /**
078: * Gets the name of a section.
079: *
080: * @param index The index of the section.
081: * @return The name of the section.
082: */
083: public String getSectionName(int index) {
084: Object[] section = (Object[]) mSectionList.get(index);
085: return (String) section[0];
086: }
087:
088: /**
089: * Gets the key-value-pairs of a section.
090: *
091: * @param index The index of the section.
092: * @return The key-value-pairs of the section.
093: */
094: public Map getSectionContent(int index) {
095: Object[] section = (Object[]) mSectionList.get(index);
096: return (Map) section[1];
097: }
098:
099: /**
100: * Gets the first section with the given name.
101: *
102: * @param name The name of the sections
103: * @return The first section with the given name or <code>null</code> if there
104: * is no such section.
105: */
106: public Map getSectionWithName(String name) {
107: for (int i = 0; i < getSectionCount(); i++) {
108: if (name.equalsIgnoreCase(getSectionName(i))) {
109: return getSectionContent(i);
110: }
111: }
112:
113: return null;
114: }
115:
116: /**
117: * Gets all sections with the given name.
118: *
119: * @param name The name of the sections
120: * @return All sections with the given name.
121: */
122: public Map[] getSectionsWithName(String name) {
123: ArrayList list = new ArrayList();
124: for (int i = 0; i < getSectionCount(); i++) {
125: if (name.equalsIgnoreCase(getSectionName(i))) {
126: list.add(getSectionContent(i));
127: }
128: }
129:
130: // Convert the list into an array
131: Map[] sectionArr = new Map[list.size()];
132: list.toArray(sectionArr);
133: return sectionArr;
134: }
135:
136: }
|