String name = element.getName();
boolean attributes = element.getAttributes() != null;
// If we have a registered code for this element
ElementNameCode nameCode = context.getElementNames().create(name);
WBSAXContentHandler contentHandler = context.getContentHandler();
if (nameCode != null) {
// Start the element with the name code
contentHandler.startElement(nameCode, attributes, content);
} else {
// There was no code for the element in the token table.
// So, we fall back to starting the element with a literal
// name, and hope the device understands it.
StringReference nameRef =
context.getReferences().createReference(name);
contentHandler.startElement(nameRef, attributes, content);
}
if (attributes) {
contentHandler.startAttributes();
for (Attribute attribute = element.getAttributes();
attribute != null;
attribute = attribute.getNext()) {
String attrName = attribute.getName();
String attrValue = attribute.getValue();
// NOTE: code below is a kind of generic attribute value
// processor - it searches all attribute names rather than
// the more specific kind we have already implemented. Then
// we could register it externally if finalOutput was true.
// If we are generating the final output (no WBDOM) and
// the attribute had a value and
// we are listening for urls and
// this attribute is registered as containing a URL...
if (context.isFinalOutput() && attrValue != null &&
context.getUrlListener() != null &&
context.getConfiguration().isURLAttribute(name,
attrName)) {
if (logger.isDebugEnabled()) {
logger.debug("Generating URL Attribute Event " +
"for: " + name + " " + attrName + "='" +
attrValue + "'");
}
// Then generate the URL event for the listener.
context.getUrlListener().foundURL(attrValue);
}
// Add the attribute start code
AttributeStartCode attrStart =
context.getAttributeStarts().create(attrName, attrValue);
contentHandler.addAttribute(attrStart);
// And figure out how to add the attribute value...
// If the start code included a prefix
String prefix = attrStart.getValuePrefix();
if (prefix != null) {
// Remove the prefix from the value we are about to add
// as a string.
attrValue = attrValue.substring(prefix.length());
}
// Process the attribute value.
// Here we attempt to look up a registered attribute
// processor and if that fails we fall back to the default.
WBSAXValueProcessor attrValueProcessor =
context.getAttributeValueProcessor(attrName);
if (attrValueProcessor == null) {
attrValueProcessor = defaultAttributeValueProcessor;
}
attrValueProcessor.value(attrValue.toCharArray(),
attrValue.length());
}
contentHandler.endAttributes();
}
}