1 23 package org.apache.slide.index; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.IOException ; 27 import java.io.Reader ; 28 import java.io.StringReader ; 29 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.parsers.SAXParser ; 32 import javax.xml.parsers.SAXParserFactory ; 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 ; 40 import org.xml.sax.helpers.DefaultHandler ; 41 42 46 public class XMLContentIndexer extends TextContentIndexer { 47 48 private SAXParser 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 e) { 56 getLogger().log("Error creating parser for indexer", LOG_CHANNEL, Logger.ERROR); 57 throw new ServiceInitializationFailedException(this, e); 58 } 59 catch (SAXException 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 readContent(NodeRevisionDescriptor revisionDescriptor, 66 NodeRevisionContent revisionContent) throws IOException { 67 if (revisionDescriptor.getContentType().equals("text/xml")) { 68 try { 69 final XMLContentIndexerHandler handler = new XMLContentIndexerHandler(); 70 m_parser.parse(new ByteArrayInputStream (revisionContent.getContentBytes()), handler); 71 return new StringReader (handler.getText()); 72 } catch (SAXException 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 { 80 81 private final StringBuffer m_text = new StringBuffer (); 82 83 public void characters(char[] ch, int start, int length) throws SAXException { 84 m_text.append(ch, start, length); 85 } 86 87 public void endElement(String uri, String localName, String qName) throws SAXException { 88 super.endElement(uri, localName, qName); 89 m_text.append(' '); 90 } 91 92 public String getText() { 93 return m_text.toString(); 94 } 95 96 } 97 98 99 } 100 | Popular Tags |