org.apache.cocoon.transformation
public class: CleanupTransformer [javadoc |
source]
java.lang.Object
org.apache.avalon.framework.logger.AbstractLogEnabled
org.apache.cocoon.xml.AbstractXMLProducer
org.apache.cocoon.xml.AbstractXMLPipe
org.apache.cocoon.transformation.AbstractTransformer
org.apache.cocoon.transformation.AbstractSAXTransformer
org.apache.cocoon.transformation.CleanupTransformer
All Implemented Interfaces:
CacheableProcessingComponent, org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.service.Serviceable, org.apache.avalon.framework.configuration.Configurable, Transformer, XMLPipe, org.apache.avalon.excalibur.pool.Recyclable, XMLProducer
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.
| Fields inherited from org.apache.cocoon.transformation.AbstractSAXTransformer: |
|---|
| EMPTY_ATTRIBUTES, ignoreWhitespaces, ignoreEmptyCharacters, ignoreEventsCount, ignoreHooksCount, namespaceURI, defaultNamespaceURI, stack, recorderStack, request, response, context, objectModel, parameters, source, manager, resolver, emptyAttributes |
| Method from org.apache.cocoon.transformation.CleanupTransformer Summary: |
|---|
|
characters, configure, endElement, endPrefixMapping, getKey, getValidity, ignorableWhitespace, recycle, setup, startElement, startPrefixMapping |
| Methods from org.apache.cocoon.transformation.AbstractSAXTransformer: |
|---|
|
addRecorder, characters, comment, configure, dispose, endCDATA, endDTD, endDocument, endElement, endEntity, endParametersRecording, endParametersRecording, endPrefixMapping, endRecording, endSAXRecording, endSerializedXMLRecording, endTextRecording, endTransformingElement, findPrefixMapping, getMutableAttributes, ignorableWhitespace, processingInstruction, recycle, removeRecorder, sendEndElementEvent, sendEndElementEventNS, sendEndPrefixMapping, sendEvents, sendParametersEvents, sendStartElementEvent, sendStartElementEvent, sendStartElementEventNS, sendStartElementEventNS, sendStartPrefixMapping, sendTextEvent, service, setDocumentLocator, setup, setupTransforming, skippedEntity, startCDATA, startDTD, startDocument, startElement, startEntity, startParametersRecording, startPrefixMapping, startRecording, startSAXRecording, startSerializedXMLRecording, startTextRecording, startTransformingElement |
| Methods from org.apache.cocoon.xml.AbstractXMLPipe: |
|---|
|
characters, comment, endCDATA, endDTD, endDocument, endElement, endEntity, endPrefixMapping, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity, startCDATA, startDTD, startDocument, startElement, startEntity, startPrefixMapping |
| Method from org.apache.cocoon.transformation.CleanupTransformer Detail: |
public void characters(char[] ch,
int start,
int length) throws SAXException {
int end = start + length;
for (int i=start; i< end; ++i) {
if (!Character.isWhitespace(ch[i])) {
this.contentHandler.characters(ch, start, length);
return;
}
}
this.contentHandler.characters(INDENT, 1, 1);
}
|
public void configure(Configuration conf) throws ConfigurationException {
StringTokenizer st;
Configuration inlineEltChild = conf.getChild("inline-elements");
st = new StringTokenizer(inlineEltChild.getValue(""), ",");
this.inlineElements.clear();
while (st.hasMoreTokens()) {
String nextElement = st.nextToken().trim();
if (nextElement.length() > 0) {
this.inlineElements.add(nextElement);
}
}
this.allowAllURIs = false;
Configuration[] uriChildren = conf.getChildren("preserve-uri");
for (int i=0; i< uriChildren.length; ++i) {
String nextChild = uriChildren[i].getValue("").trim();
if (nextChild.length() == 0) {
continue;
} else if (nextChild.equals("*")) {
this.allowAllURIs = true;
break;
}
this.allowedURIs.add(nextChild);
}
}
|
public void endElement(String uri,
String qName,
String lName) throws SAXException {
if (!inlineElements.contains(qName)) {
--this.numIndents;
if (this.lastElement == null || !this.lastElement.equals(qName)) {
int indentSize = (this.indentSize * this.numIndents) % MAX_INDENT;
this.contentHandler.ignorableWhitespace(INDENT, 0, indentSize + 1);
}
this.lastElement = null;
}
this.contentHandler.endElement(uri, qName, lName);
}
|
public void endPrefixMapping(String prefix) throws SAXException {
if (this.allowAllURIs) {
this.contentHandler.endPrefixMapping(prefix);
} else if (!uriPrefixes.isEmpty()) {
if (uriPrefixes.getLast().toString().equals(prefix)) {
this.contentHandler.endPrefixMapping(prefix);
uriPrefixes.removeLast();
}
}
}
|
public Serializable getKey() {
return Integer.toString(this.indentSize);
}
|
public SourceValidity getValidity() {
return NOPValidity.SHARED_INSTANCE;
}
|
public void ignorableWhitespace(char[] ch,
int start,
int length) throws SAXException {
// Do nothing
}
|
public void recycle() {
super.recycle();
this.numIndents = 0;
this.lastElement = null;
this.uriPrefixes.clear();
}
|
public void setup(SourceResolver resolver,
Map objectModel,
String src,
Parameters par) throws IOException, SAXException, ProcessingException {
super.setup(resolver, objectModel, src, par);
this.indentSize = par.getParameterAsInteger("indent-size", 2);
}
|
public void startElement(String uri,
String qName,
String lName,
Attributes attrs) throws SAXException {
if (!inlineElements.contains(qName)) {
int indentSize = (this.indentSize * this.numIndents) % MAX_INDENT;
this.contentHandler.ignorableWhitespace(INDENT, 0, indentSize + 1);
++this.numIndents;
this.lastElement = qName;
}
this.contentHandler.startElement(uri, qName, lName, attrs);
}
|
public void startPrefixMapping(String prefix,
String uri) throws SAXException {
if (this.allowAllURIs) {
this.contentHandler.startPrefixMapping(prefix, uri);
} else if (this.allowedURIs.contains(uri)) {
this.contentHandler.startPrefixMapping(prefix, uri);
uriPrefixes.add(prefix);
}
}
|