package org.apache.commons.digester3.xmlrules;
import static java.lang.String.format;
import org.apache.commons.digester3.binder.LinkedRuleBuilder;
import org.apache.commons.digester3.binder.NodeCreateRuleProvider;
import org.apache.commons.digester3.binder.NodeCreateRuleProvider.NodeType;
import org.apache.commons.digester3.binder.RulesBinder;
import org.xml.sax.Attributes;
final class NodeCreateRule
extends AbstractXmlRule
{
public NodeCreateRule( RulesBinder targetRulesBinder, PatternStack patternStack )
{
super( targetRulesBinder, patternStack );
}
@Override
protected void bindRule( LinkedRuleBuilder linkedRuleBuilder, Attributes attributes )
throws Exception
{
NodeCreateRuleProvider nodeProvider = linkedRuleBuilder.createNode();
String nodeType = attributes.getValue( "type" );
if ( nodeType != null && nodeType.length() > 0 )
{
if ( "element".equals( nodeType ) )
{
nodeProvider.ofType( NodeType.ELEMENT );
}
else if ( "fragment".equals( nodeType ) )
{
nodeProvider.ofType( NodeType.DOCUMENT_FRAGMENT );
}
else
{
throw new RuntimeException( format(
"Unrecognized node type: %s. This attribute is optional or can have a value of element|fragment.",
nodeType ) );
}
}
}
}