/* * Created on 30.7.2004 * */ package net.sf.tomp.xml.id; import org.xml.sax.Attributes; import org.xml.sax.helpers.AttributesImpl; /** * @author tomp * */ public class SimpleIdGetterSetter implements IdGetterSetter { /** SAX localname of the ID attribute */ public final String idLocalName; /** NS of the ID attribute */ public final String idNS; /** NS prefix of the ID attribute */ public final String idNSPrefix; protected String getId(Attributes atts) { return atts.getValue(idNS, idLocalName); } protected int getIdIndex(Attributes atts) { return atts.getIndex(idNS, idLocalName); } public SimpleIdGetterSetter() { this("id"); } public SimpleIdGetterSetter(String localName) { this("", localName); } public SimpleIdGetterSetter(String ns, String localName) { this(ns, localName, ""); } public SimpleIdGetterSetter(String ns, String localName, String nsPrefix) { idNS = ns; idLocalName = localName; idNSPrefix = nsPrefix; } public String getId(Element e) { return getId(e.getAttributes()); } /* * (non-Javadoc) * * @see net.sf.tomp.docbook.sax.IDGetter#setId(org.xml.sax.Attributes, * java.lang.String) */ public Attributes setId(Attributes atts, String newId) { AttributesImpl atts2 = new AttributesImpl(atts); int idIndex = getIdIndex(atts2); if (idIndex > -1) { // remove old is if exists atts2.removeAttribute(idIndex); } //add new id if (idNSPrefix == null || idNSPrefix.length() == 0) { atts2.addAttribute(idNS, idLocalName, idLocalName, "CDATA", newId); } else { atts2.addAttribute(idNS, idLocalName, idNSPrefix + ":" + idLocalName, "CDATA", newId); } return atts2; } }