try
{
if (TransformerImpl.S_DEBUG)
transformer.getTraceManager().fireTraceEvent(sourceNode, mode, this);
ResultTreeHandler rhandler = transformer.getResultTreeHandler();
XPathContext xctxt = transformer.getXPathContext();
// The attribute name has to be evaluated as an AVT.
String attrName = m_name_avt.evaluate(xctxt, sourceNode, this);
String origAttrName = attrName; // save original attribute name
// Get the children of the xsl:attribute element as the string value.
String val = transformer.transformToString(this, sourceNode, mode);
// If they are trying to add an attribute when there isn't an
// element pending, it is an error.
if (!rhandler.isElementPending())
{
transformer.getMsgMgr().warn(this,
XSLTErrorResources.WG_ILLEGAL_ATTRIBUTE_NAME,
new Object[]{ origAttrName });
return;
// warn(templateChild, sourceNode, "Trying to add attribute after element child has been added, ignoring...");
}
if (null == attrName)
return;
String attrNameSpace = ""; // by default
// Did they declare a namespace attribute?
if (null != m_namespace_avt)
{
// The namespace attribute is an AVT also.
attrNameSpace = m_namespace_avt.evaluate(xctxt, sourceNode, this);
if (null != attrNameSpace && attrNameSpace.length() > 0)
{
// Get the prefix for that attribute in the result namespace.
String prefix = rhandler.getPrefix(attrNameSpace);
// If we didn't find the prefix mapping, make up a prefix
// and have it declared in the result tree.
if (null == prefix)
{
prefix = rhandler.getNewUniqueNSPrefix();
rhandler.startPrefixMapping(prefix, attrNameSpace, false);
}
// add the prefix to the attribute name.
attrName = (prefix + ":" + QName.getLocalPart(attrName));
}
}
// Is the attribute xmlns type?
else if (QName.isXMLNSDecl(origAttrName))
{
// Then just declare the namespace prefix and get out.
String prefix = QName.getPrefixFromXMLNSDecl(origAttrName);
String ns = rhandler.getURI(prefix);
if (null == ns)
rhandler.startPrefixMapping(prefix, val, false);
return;
}
// Note we are using original attribute name for these tests.
else
{
// Does the attribute name have a prefix?
String nsprefix = QName.getPrefixPart(origAttrName);
if (null == nsprefix)
nsprefix = "";
// We're going to claim that this must be resolved in
// the result tree namespace.
try
{
attrNameSpace = getNamespaceForPrefix(nsprefix);
// System.out.println("attrNameSpace: "+attrNameSpace);
if ((null == attrNameSpace) && (nsprefix.length() > 0))
{
transformer.getMsgMgr().warn(this,
XSLTErrorResources.WG_COULD_NOT_RESOLVE_PREFIX,
new Object[]{ nsprefix });
return;
}
else if(null == attrNameSpace)
attrNameSpace = "";
}
catch (Exception ex)
{
// Could not resolve prefix
transformer.getMsgMgr().warn(this,
XSLTErrorResources.WG_COULD_NOT_RESOLVE_PREFIX,
new Object[]{ nsprefix });
return;
}
}
String localName = QName.getLocalPart(attrName);
// System.out.println("rhandler.addAttribute("+attrNameSpace+", "
// +localName+", "
// +attrName+", "
// +"CDATA"+", "
// +val+");");
rhandler.addAttribute(attrNameSpace, localName, attrName, "CDATA", val);
}
catch(org.xml.sax.SAXException se)
{
throw new TransformerException(se);
}