this.body = text;
}
}
TestSubRule tsr = new TestSubRule();
Digester digester = new Digester();
digester.addRule( "alpha/beta", tsr );
// it's not easy to transform dirty harry into the mighty circus - but let's give it a try
String xml =
"<?xml version='1.0'?><alpha><beta forname='Dirty' surname='Harry'>Do you feel luck punk?</beta></alpha>";
InputSource in = new InputSource( new StringReader( xml ) );
digester.parse( in );
assertEquals( "Unsubstituted body text", "Do you feel luck punk?", tsr.body );
assertEquals( "Unsubstituted number of attributes", 2, tsr.attributes.getLength() );
assertEquals( "Unsubstituted forname attribute value", "Dirty", tsr.attributes.getValue( "forname" ) );
assertEquals( "Unsubstituted surname attribute value", "Harry", tsr.attributes.getValue( "surname" ) );
digester.setSubstitutor( new Substitutor()
{
@Override
public Attributes substitute( Attributes attributes )
{
AttributesImpl results = new AttributesImpl();
results.addAttribute( "", "python", "python", "CDATA", "Cleese" );
return results;
}
@Override
public String substitute( String bodyText )
{
return "And now for something completely different...";
}
} );
// now transform into the full monty
in = new InputSource( new StringReader( xml ) );
digester.parse( in );
assertEquals( "Substituted body text", "And now for something completely different...", tsr.body );
assertEquals( "Substituted number of attributes", 1, tsr.attributes.getLength() );
assertEquals( "Substituted python attribute value", "Cleese", tsr.attributes.getValue( "", "python" ) );
}