org.apache.cocoon.transformation
Class CleanupTransformer

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.cocoon.xml.AbstractXMLProducer
          extended by org.apache.cocoon.xml.AbstractXMLPipe
              extended by org.apache.cocoon.transformation.AbstractTransformer
                  extended by org.apache.cocoon.transformation.AbstractSAXTransformer
                      extended by org.apache.cocoon.transformation.CleanupTransformer
All Implemented Interfaces:
Poolable, Recyclable, Disposable, Component, Configurable, LogEnabled, Serviceable, CacheableProcessingComponent, SitemapModelComponent, Transformer, XMLPipe, XMLProducer, XMLConsumer, ContentHandler, LexicalHandler

public class CleanupTransformer
extends AbstractSAXTransformer
implements CacheableProcessingComponent

Cleanup transformer: Removes excess whitespace while adding some where needed for legibility. Strips unwanted namespace declarations.

The cleanup transformer can be used for basically any document as-is or customized by schema (inline vs. block elements) for easier reading.

Transformer declaration: <map:components> <map:transformers> <map:transformer name="htmlcleanup" src="org.apache.cocoon.transformation.CleanupTransformer"> <preserve-uri>*</preserve-uri> </map:transformer> <map:transformer name="xhtmlcleanup" src="org.apache.cocoon.transformation.CleanupTransformer"> <inline-elements>a,abbr,acronym,b,br,font,i,u,img</inline-elements> <preserve-uri>http://www.w3.org/1999/xhtml</preserve-uri> </map:transformer> </map:transformers> </map:components>

The "inline-elements" configuration element refers to a list of element names that are not to be indented. The "preserve-uri" configuration element specifies a namespace uri mapping that is meant for output. All other namespace declarations are stripped from the output. The "preserve-uri" element may appear more than once. If "preserve-uri" is omitted, all namespaces/prefixes are removed from the output.

Transformer usage: <transform type="xhtmlcleanup"> <map:parameter name="indent-size" value="4"/> </transform>

The optional parameter "indent-size" specifies the number of additional space characters appearing at each level of the output document. The default value is 2.

Bugs: Nested namespace declarations with the same namespace prefix will break the code.

Author:
Miles Elam

Field Summary
 
Fields inherited from class org.apache.cocoon.transformation.AbstractSAXTransformer
context, defaultNamespaceURI, EMPTY_ATTRIBUTES, emptyAttributes, ignoreEmptyCharacters, ignoreEventsCount, ignoreHooksCount, ignoreWhitespaces, manager, namespaceURI, objectModel, parameters, recorderStack, request, resolver, response, source, stack
 
Fields inherited from class org.apache.cocoon.xml.AbstractXMLProducer
contentHandler, EMPTY_CONTENT_HANDLER, lexicalHandler, xmlConsumer
 
Fields inherited from interface org.apache.cocoon.transformation.Transformer
ROLE
 
Constructor Summary
CleanupTransformer()
           
 
Method Summary
 void characters(char[] ch, int start, int length)
          Process the SAX event.
 void configure(Configuration conf)
           
 void endElement(String uri, String qName, String lName)
          Process the SAX event.
 void endPrefixMapping(String prefix)
          Process the SAX event.
 Serializable getKey()
          Generate the unique key.
 SourceValidity getValidity()
          Generate the validity object.
 void ignorableWhitespace(char[] ch, int start, int length)
          Process the SAX event.
 void recycle()
          Recycle the producer by removing references, and resetting handlers to null (empty) implementations.
 void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
          Set the SourceResolver, objectModel Map, the source and sitemap Parameters used to process the request.
 void startElement(String uri, String qName, String lName, Attributes attrs)
          Process the SAX event.
 void startPrefixMapping(String prefix, String uri)
          Process the SAX event.
 
Methods inherited from class org.apache.cocoon.transformation.AbstractSAXTransformer
addRecorder, comment, dispose, endCDATA, endDocument, endDTD, endEntity, endParametersRecording, endParametersRecording, endRecording, endSAXRecording, endSerializedXMLRecording, endTextRecording, endTransformingElement, findPrefixMapping, getMutableAttributes, processingInstruction, removeRecorder, sendEndElementEvent, sendEndElementEventNS, sendEndPrefixMapping, sendEvents, sendParametersEvents, sendStartElementEvent, sendStartElementEvent, sendStartElementEventNS, sendStartElementEventNS, sendStartPrefixMapping, sendTextEvent, service, setDocumentLocator, setupTransforming, skippedEntity, startCDATA, startDocument, startDTD, startEntity, startParametersRecording, startRecording, startSAXRecording, startSerializedXMLRecording, startTextRecording, startTransformingElement
 
Methods inherited from class org.apache.cocoon.xml.AbstractXMLProducer
setConsumer, setContentHandler, setLexicalHandler
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.cocoon.xml.XMLProducer
setConsumer
 

Constructor Detail

CleanupTransformer

public CleanupTransformer()
Method Detail

configure

public void configure(Configuration conf)
               throws ConfigurationException
Specified by:
configure in interface Configurable
Overrides:
configure in class AbstractSAXTransformer
Throws:
ConfigurationException
See Also:
AbstractSAXTransformer.configure(org.apache.avalon.framework.configuration.Configuration)

setup

public void setup(SourceResolver resolver,
                  Map objectModel,
                  String src,
                  Parameters par)
           throws ProcessingException,
                  SAXException,
                  IOException
Description copied from interface: SitemapModelComponent
Set the SourceResolver, objectModel Map, the source and sitemap Parameters used to process the request.

Specified by:
setup in interface SitemapModelComponent
Overrides:
setup in class AbstractSAXTransformer
Throws:
ProcessingException
SAXException
IOException
See Also:
AbstractSAXTransformer.setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)

recycle

public void recycle()
Description copied from class: AbstractXMLProducer
Recycle the producer by removing references, and resetting handlers to null (empty) implementations.

Specified by:
recycle in interface Recyclable
Overrides:
recycle in class AbstractSAXTransformer
See Also:
AbstractSAXTransformer.recycle()

getKey

public Serializable getKey()
Description copied from interface: CacheableProcessingComponent
Generate the unique key. This key must be unique inside the space of this component. This method must be invoked before the getValidity() method.

Specified by:
getKey in interface CacheableProcessingComponent
Returns:
The generated key or null if the component is currently not cacheable.
See Also:
CacheableProcessingComponent.getKey()

getValidity

public SourceValidity getValidity()
Description copied from interface: CacheableProcessingComponent
Generate the validity object. Before this method can be invoked the getKey() method must be invoked.

Specified by:
getValidity in interface CacheableProcessingComponent
Returns:
The generated validity object or null if the component is currently not cacheable.
See Also:
CacheableProcessingComponent.getValidity()

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String uri)
                        throws SAXException
Description copied from class: AbstractSAXTransformer
Process the SAX event.

Specified by:
startPrefixMapping in interface ContentHandler
Overrides:
startPrefixMapping in class AbstractSAXTransformer
Parameters:
prefix - The Namespace prefix being declared.
uri - The Namespace URI the prefix is mapped to.
Throws:
SAXException
See Also:
AbstractSAXTransformer.startPrefixMapping(java.lang.String, java.lang.String)

endPrefixMapping

public void endPrefixMapping(String prefix)
                      throws SAXException
Description copied from class: AbstractSAXTransformer
Process the SAX event.

Specified by:
endPrefixMapping in interface ContentHandler
Overrides:
endPrefixMapping in class AbstractSAXTransformer
Parameters:
prefix - The prefix that was being mapping.
Throws:
SAXException
See Also:
AbstractSAXTransformer.endPrefixMapping(java.lang.String)

startElement

public void startElement(String uri,
                         String qName,
                         String lName,
                         Attributes attrs)
                  throws SAXException
Description copied from class: AbstractSAXTransformer
Process the SAX event. The namespace of the event is checked. If it is the defined namespace for this transformer, the AbstractSAXTransformer.startTransformingElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) hook is called.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class AbstractSAXTransformer
Parameters:
uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
qName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
lName - The raw XML 1.0 name (with prefix), or the empty string if raw names are not available.
attrs - The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
Throws:
SAXException
See Also:
AbstractSAXTransformer.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)

endElement

public void endElement(String uri,
                       String qName,
                       String lName)
                throws SAXException
Description copied from class: AbstractSAXTransformer
Process the SAX event. The namespace of the event is checked. If it is the defined namespace for this transformer, the AbstractSAXTransformer.endTransformingElement(java.lang.String, java.lang.String, java.lang.String) hook is called.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class AbstractSAXTransformer
Parameters:
uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
qName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
lName - The raw XML 1.0 name (with prefix), or the empty string if raw names are not available.
Throws:
SAXException
See Also:
AbstractSAXTransformer.endElement(java.lang.String, java.lang.String, java.lang.String)

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws SAXException
Description copied from class: AbstractSAXTransformer
Process the SAX event.

Specified by:
characters in interface ContentHandler
Overrides:
characters in class AbstractSAXTransformer
Parameters:
ch - The characters from the XML document.
start - The start position in the array.
length - The number of characters to read from the array.
Throws:
SAXException
See Also:
AbstractSAXTransformer.characters(char[], int, int)

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws SAXException
Description copied from class: AbstractSAXTransformer
Process the SAX event.

Specified by:
ignorableWhitespace in interface ContentHandler
Overrides:
ignorableWhitespace in class AbstractSAXTransformer
Parameters:
ch - The characters from the XML document.
start - The start position in the array.
length - The number of characters to read from the array.
Throws:
SAXException
See Also:
AbstractSAXTransformer.ignorableWhitespace(char[], int, int)


Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.