/** * 生成sel的document * * @param normalName normal普通状态的图片名 * @param specialName 特殊状态(pressed按下/checked选中)的图片名 * @param end 特殊状态(pressed按下/checked选中)后缀名 * @return */ public static Document createSelector(String normalName, String specialName, String end) { Document doc = XmlUtil.read("res\\drawable\\sel.xml"); Element rootElement = doc.getRootElement(); List<Element> elements = XmlUtil.getAllElements(doc); for (Element element : elements) { Attribute attr = element.attribute("drawable"); if (attr == null) { continue; } String value = attr.getStringValue(); if (value.contains(end)) { // 替换特殊状态(pressed/checked)的item加后缀 value = value.replace(end, specialName); attr.setValue(value); } else if (element.attributeCount() > 1) { // 移除不需要的element rootElement.remove(element); } else { // normal状态的item不加后缀 value = value.replace("normal", normalName); attr.setValue(value); } } return doc; }
public static Entity parseElement(Element ele, int level) { Entity entity = new Entity(); entity.name = ele.getName(); entity.content = ele.getTextTrim(); entity.level = level; if (ele.attributeCount() > 0) { entity.attributes = new HashMap<String, String>(); for (int i = 0; i < ele.attributeCount(); i++) { Attribute attr = ele.attribute(i); entity.attributes.put(attr.getName(), attr.getText()); } } List<Element> cEs = ele.elements(); if (cEs.size() > 0) { entity.children = new HashMap<String, Entity>(); for (int i = 0; i < cEs.size(); i++) { entity.children.put(cEs.get(i).getName(), parseElement(cEs.get(i), level + 1)); } } return entity; }
/** * Writes the attributes of the given element * * @param element DOCUMENT ME! * @throws IOException DOCUMENT ME! */ protected void writeAttributes(Element element) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if ((ns != null) && (ns != Namespace.NO_NAMESPACE) && (ns != Namespace.XML_NAMESPACE)) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { writeNamespace(ns); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have // already written that declaration elsewhere (if that's the case, // it must be in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
@SuppressWarnings("unchecked") Descriptor(Element el) { // copy in sub-properties (child nodes) List<Element> elements = el.elements(); for (Element element : elements) { put(element); } // copy in attributes for (int i = 0; i < el.attributeCount(); i++) { if (this.attributesMap == null) { this.attributesMap = new HashMap<String, String>(); } Attribute attribute = (Attribute) el.attribute(i); String value = attribute.getValue(); if (value != null) { this.attributesMap.put(attribute.getName(), value); } } }
@Override public String[] getAttributeNames() { String[] ret = new String[internal.attributeCount()]; for (int x = 0; x < ret.length; ++x) ret[x] = internal.attribute(x).getQualifiedName(); return ret; }