org.xwiki.xml
Class ExtractHandler

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.xwiki.xml.ExtractHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class ExtractHandler
extends org.xml.sax.helpers.DefaultHandler

Extracts a well-formed XML fragment by listening to SAX events. The result has the following semantic:
xmlInput.dropAllTags().substring(start, length).unDropAssociatedTags()

So basically we would create an instance like new ExtractHandler(0, 400) in order to obtain an XML fragment with its inner text length of at most 400 characters, starting at position (character) 0 in the source (input) XML's inner text. The ExtractHandler is used in feed plug-in to obtain a preview of an XML (HTML, to be more specific). Another use case could be to paginate an XML source (keeping pages well-formed).

As an example, the result of applying an ExtractHandler(3, 13) to:
<p>click <a href="realyLongURL" title="Here">here</a> to view the result</p>
is:
<p>ck <a href="realyLongURL" title="Here">here</a> to</p>

Since:
1.6M2
Version:
$Id$

Constructor Summary
ExtractHandler(int start, int length)
          Creates a new instance.
 
Method Summary
 void characters(char[] ch, int start, int length)
          
 void endDocument()
          
 void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
          
 java.lang.String getResult()
           
 boolean isFinished()
           
 void startDocument()
          
 void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
          
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExtractHandler

public ExtractHandler(int start,
                      int length)
               throws org.xml.sax.SAXException
Creates a new instance.

Parameters:
start - The character index from where to start the extraction.
length - The number of plain text characters to extract.
Throws:
org.xml.sax.SAXException - if start is less than zero or length is less than or equal to zero.
Method Detail

getResult

public java.lang.String getResult()
Returns:
The extracted text.

isFinished

public boolean isFinished()
Returns:
true if the extraction process has succeeded; false if an exception occurred during the process.

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException
See Also:
DefaultHandler.startDocument()

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException
See Also:
DefaultHandler.startElement(String, String, String, Attributes)

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException
See Also:
DefaultHandler.characters(char[], int, int)

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException
See Also:
DefaultHandler.endElement(String, String, String)

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException
See Also:
DefaultHandler.endDocument()


Copyright © 2004-2011 XWiki. All Rights Reserved.