KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > index > XMLContentIndexer


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/XMLContentIndexer.java,v 1.1 2004/08/03 15:01:46 unico Exp $
3  * $Revision: 1.1 $
4  * $Date: 2004/08/03 15:01:46 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23 package org.apache.slide.index;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.StringReader JavaDoc;
29
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import javax.xml.parsers.SAXParserFactory JavaDoc;
33
34 import org.apache.slide.common.NamespaceAccessToken;
35 import org.apache.slide.common.ServiceInitializationFailedException;
36 import org.apache.slide.content.NodeRevisionContent;
37 import org.apache.slide.content.NodeRevisionDescriptor;
38 import org.apache.slide.util.logger.Logger;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41
42 /**
43  * Extends TextContentIndexer for handling XML content
44  * by only indexing the actual character data.
45  */

46 public class XMLContentIndexer extends TextContentIndexer {
47     
48     private SAXParser JavaDoc m_parser;
49     
50     public void initialize(NamespaceAccessToken token) throws ServiceInitializationFailedException {
51         super.initialize(token);
52         try {
53             m_parser = SAXParserFactory.newInstance().newSAXParser();
54         }
55         catch (ParserConfigurationException JavaDoc e) {
56             getLogger().log("Error creating parser for indexer", LOG_CHANNEL, Logger.ERROR);
57             throw new ServiceInitializationFailedException(this, e);
58         }
59         catch (SAXException JavaDoc e) {
60             getLogger().log("Error creating parser for indexer", LOG_CHANNEL, Logger.ERROR);
61             throw new ServiceInitializationFailedException(this, e);
62         }
63     }
64
65     protected synchronized Reader JavaDoc readContent(NodeRevisionDescriptor revisionDescriptor,
66                                  NodeRevisionContent revisionContent) throws IOException JavaDoc {
67         if (revisionDescriptor.getContentType().equals("text/xml")) {
68             try {
69                 final XMLContentIndexerHandler handler = new XMLContentIndexerHandler();
70                 m_parser.parse(new ByteArrayInputStream JavaDoc(revisionContent.getContentBytes()), handler);
71                 return new StringReader JavaDoc(handler.getText());
72             } catch (SAXException JavaDoc e) {
73                 getLogger().log("Error parsing xml content for indexer", LOG_CHANNEL, Logger.ERROR);
74             }
75         }
76         return super.readContent(revisionDescriptor, revisionContent);
77     }
78     
79     private static final class XMLContentIndexerHandler extends DefaultHandler JavaDoc {
80
81         private final StringBuffer JavaDoc m_text = new StringBuffer JavaDoc();
82
83         public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
84             m_text.append(ch, start, length);
85         }
86
87         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
88             super.endElement(uri, localName, qName);
89             m_text.append(' ');
90         }
91
92         public String JavaDoc getText() {
93             return m_text.toString();
94         }
95
96     }
97
98     
99 }
100
Popular Tags