FileDocCategorySizeDatePackage
GdataCategoryStrategy.javaAPI DocApache Lucene 2.1.06475Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.search.analysis

GdataCategoryStrategy

public class GdataCategoryStrategy extends ContentStrategy
This strategy retrieves the category term and and the scheme from a category element. The content is represented by the term which can be configured via the configuration file.

The category element has at least one attribute with the name "scheme" which is not mandatory. The term can be the default attribute "term" or the text content of the element, this is configured via the path of the field.

<category scheme="http://www.example.com/type" term="blog.post"/>

TODO extend javadoc for search info
author
Simon Willnauer

Fields Summary
protected String
categoryScheme
protected String
categorySchemeField
private static final String
LABEL
private static final String
SCHEME
private static final String
TERM
public static final String
CATEGORY_SCHEMA_NULL_VALUE
the string to search a schema if no schema is specified
public static final String
CATEGORY_SCHEMA_FIELD_SUFFIX
Schema field suffix
Constructors Summary
protected GdataCategoryStrategy(org.apache.lucene.gdata.search.config.IndexSchemaField fieldConfiguration)


       
        super(fieldConfiguration);
        this.categorySchemeField = new StringBuilder(this.fieldName).append(
                CATEGORY_SCHEMA_FIELD_SUFFIX).toString();

    
Methods Summary
public org.apache.lucene.document.Field[]createLuceneField()

see
org.apache.lucene.gdata.search.analysis.ContentStrategy#createLuceneField()

        List<Field> retValue = new ArrayList<Field>(2);
        if (this.fieldName == null)
            throw new GdataIndexerException("Required field 'name' is null -- "
                    + this.config);
        if (this.content == null)
            throw new GdataIndexerException(
                    "Required field 'content' is null -- " + this.config);

        Field categoryTerm = new Field(this.fieldName, this.content,
                this.store, this.index);
        float boost = this.config.getBoost();
        if (boost != 1.0f)
            categoryTerm.setBoost(boost);
        retValue.add(categoryTerm);
        /*
         * if schema is not set index null value to enable search for categories
         * without a schema
         */
        if (this.categoryScheme == null || this.categoryScheme.length() == 0) {
            this.categoryScheme = CATEGORY_SCHEMA_NULL_VALUE;
        }
        Field categoryURN = new Field(this.categorySchemeField,
                this.categoryScheme, Field.Store.YES, Field.Index.UN_TOKENIZED);
        retValue.add(categoryURN);
        return retValue.toArray(new Field[0]);

    
public voidprocessIndexable(Indexable indexable)

throws
NotIndexableException
see
org.apache.lucene.gdata.search.analysis.ContentStrategy#processIndexable(org.apache.lucene.gdata.search.analysis.Indexable)

        String contentPath = this.config.getPath();
        Node node = null;
        try {
            node = indexable.applyPath(contentPath);
        } catch (XPathExpressionException e) {

            throw new NotIndexableException("Can not apply path");
        }
        if (node == null)
            throw new NotIndexableException(
                    "Could not retrieve content for schema field: "
                            + this.config);

        StringBuilder contentBuilder = new StringBuilder();
        StringBuilder schemeBuilder = new StringBuilder();
        String nodeName = node.getNodeName();
        /*
         * enable more than one category element -- check the node name if
         * category strategy is used with an element not named "category"
         */
        while (node != null && nodeName != null
                && nodeName.equals(node.getNodeName())) {
            NamedNodeMap attributeMap = node.getAttributes();
            if (attributeMap == null)
                throw new NotIndexableException(
                        "category term attribute not present");
            /*
             * the "term" is the internal string used by the software to
             * identify the category, while the "label" is the human-readable
             * string presented to a user in a user interface.
             */
            Node termNode = attributeMap.getNamedItem(TERM);
            if (termNode == null)
                throw new NotIndexableException(
                        "category term attribute not present");
            contentBuilder.append(termNode.getTextContent()).append(" ");

            Node labelNode = attributeMap.getNamedItem(LABEL);
            if (labelNode != null)
                contentBuilder.append(labelNode.getTextContent()).append(" ");

            Node schemeNode = attributeMap.getNamedItem(SCHEME);
            if (schemeNode != null)
                schemeBuilder.append(schemeNode.getTextContent());
            node = node.getNextSibling();
        }

        this.content = contentBuilder.toString();
        this.categoryScheme = schemeBuilder.toString();