001: /*
002: * StandardKeywordHandler.java: Implementation of KeywordHandler.
003: *
004: * Copyright (C) 2002 Heiko Blau
005: *
006: * This file belongs to the JTopas Library.
007: * JTopas is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as published by the
009: * Free Software Foundation; either version 2.1 of the License, or (at your
010: * option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License along
018: * with JTopas. If not, write to the
019: *
020: * Free Software Foundation, Inc.
021: * 59 Temple Place, Suite 330,
022: * Boston, MA 02111-1307
023: * USA
024: *
025: * or check the Internet: http://www.fsf.org
026: *
027: * Contact:
028: * email: heiko@susebox.de
029: */
030:
031: package de.susebox.jtopas.spi;
032:
033: //-----------------------------------------------------------------------------
034: // Imports
035: //
036: import de.susebox.jtopas.TokenizerProperty;
037: import de.susebox.jtopas.TokenizerProperties;
038:
039: //-----------------------------------------------------------------------------
040: // class StandardKeywordHandler
041: //
042:
043: /**<p>
044: * Simple implementation of the {@link KeywordHandler} interface. This class
045: * works only with the {@link de.susebox.jtopas.TokenizerProperties} interface
046: * methods and is aware of changes in these properties. It does not cache any
047: * information and is therefore a more or less slow way to handle keywords.
048: *</p><p>
049: * This class is a bridge between arbitrary {@link de.susebox.jtopas.Tokenizer}
050: * implementations using the SPI interface {@link KeywordHandler} and any
051: * {@link de.susebox.jtopas.TokenizerProperties} implementation that does not
052: * implement the <code>KeywordHandler</code> interface itself.
053: *</p>
054: *
055: * @see KeywordHandler
056: * @see de.susebox.jtopas.Tokenizer
057: * @see de.susebox.jtopas.TokenizerProperties
058: * @author Heiko Blau
059: */
060: public class StandardKeywordHandler implements KeywordHandler {
061:
062: /**
063: * The constructor takes the {@link de.susebox.jtopas.TokenizerProperties}
064: * that provide the keywords.
065: *
066: * @param props the {@link de.susebox.jtopas.TokenizerProperties} to take the
067: * keywords from
068: */
069: public StandardKeywordHandler(TokenizerProperties props) {
070: _properties = props;
071: }
072:
073: /**
074: * This method can be used by a {@link de.susebox.jtopas.Tokenizer} implementation
075: * for a fast detection if keyword matching must be performed at all. If the method
076: * returns <code>false</code> time-consuming preparations can be skipped.
077: *
078: * @return <code>true</code> if there actually are pattern that can be tested
079: * for a match, <code>false</code> otherwise.
080: */
081: public boolean hasKeywords() {
082: if (_properties != null) {
083: return _properties.getKeywords().hasNext();
084: } else {
085: return false;
086: }
087: }
088:
089: /**
090: * This method checks if the character range given through the
091: * {@link DataProvider} comprises a keyword.
092: *
093: * @param dataProvider the source to get the data from, that are checked
094: * @return a {@link de.susebox.jtopas.TokenizerProperty} if a keyword could be
095: * found, <code>null</code> otherwise
096: * @throws TokenizerException failure while reading more data
097: * @throws NullPointerException if no {@link DataProvider} is given
098: */
099: public TokenizerProperty isKeyword(DataProvider dataProvider)
100: throws NullPointerException {
101: if (_properties != null) {
102: return _properties.getKeyword(dataProvider.toString());
103: } else {
104: return null;
105: }
106: }
107:
108: //---------------------------------------------------------------------------
109: // Members
110: //
111:
112: /**
113: * The {@link TokenizerProperties} that provide the keywords and the
114: * control flags.
115: */
116: private TokenizerProperties _properties = null;
117: }
|