I am extending Android TagHandler class to handle custom html tags within a TextField. So far so good, I am able to intercept the tags and assing custom functionality to the "onClick()" for these. However - I am not able to capture any attributes for these custom tags, for example:
"This is an example of a custom mark-up tag embedded in text that a text field would handle <custom id='1233' uri='0023'> CUSTOM </custom>and that I need to capture."
I am able to capture the occurrence of the custom tag but not the attributes with the following:
public class SpecialTagHandler implements TagHandler
{
@Override
public void handleTag(
boolean opening,
String tag,
Editable output,
XMLReader xmlReader)
{
if(tag.equalsIgnoreCase("custom")) {
// handle the custom tag
if(opening) {
Log.e(TAG, "found custom tag OPENING");
try {
Field elementField = xmlReader.getClass().getDeclaredField("theNewElement");
elementField.setAccessible(true);
try {
Object element = elementField.get(xmlReader);
Field attsField = element.getClass().getDeclaredField("theAtts");
attsField.setAccessible(true);
Object atts = attsField.get(element);
Field dataField = atts.getClass().getDeclaredField("data");
dataField.setAccessible(true);
String[] data = (String[])dataField.get(atts);
Field lengthField = atts.getClass().getDeclaredField("length");
lengthField.setAccessible(true);
int length = (Integer)lengthField.get(atts);
String mIdAttribute = null;
String mUrlAttribute = null;
for(int i = 0; i < length; i++) {
if("id".equals(data[i*5 + 1])) {
mIdAttribute = data[i*5 + 4];
} else if("uri".equals(data[i*5 + 1])) {
mUrlAttribute = data[i*5 + 4];
}
}
Log.e(TAG, "id: " + mIdAttribute + " url: " + mUrlAttribute);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
}
}
}
}
}
Any suggestions on how to read the id and uri attributes as well? There was a discussion about using XMLReader that is passed to this function. Thanks in advance!
-------- HAVE AN ANSWER! ---------
Well - some more digging and Voila! YES - you do have access to your custom attributes in your tag in marked-up html text. I have to include what appears to me dark magic from @rekire using reflection to access xmlReader attributes. (none of these elements are "visible" to a "naked" eye). The link is here: link. No need to resort to duplicate java classes or funky tag names that actually include id in it's designation! Thanks again - @rekire Added the code paraphrased from linked post that will do the trick.