/** * Set an attribute. This replaces an attribute of the same name. To set the zeroth attribute (the * tag name), use setTagName(). * * @param attribute The attribute to set. */ public void setAttribute(Attribute attribute) { boolean replaced; Vector attributes; int length; String name; Attribute test; String test_name; replaced = false; attributes = getAttributesEx(); length = attributes.size(); if (0 < length) { name = attribute.getName(); for (int i = 1; i < attributes.size(); i++) { test = (Attribute) attributes.elementAt(i); test_name = test.getName(); if (null != test_name) if (test_name.equalsIgnoreCase(name)) { attributes.setElementAt(attribute, i); replaced = true; } } } if (!replaced) { // add whitespace between attributes if ((0 != length) && !((Attribute) attributes.elementAt(length - 1)).isWhitespace()) attributes.addElement(new Attribute(" ")); attributes.addElement(attribute); } }
/** * Look up the index of an attribute by Namespace name. * * @param uri The Namespace URI, or the empty string if the name has no Namespace URI. * @param localName The attribute's local name. * @return The index of the attribute, or -1 if it does not appear in the list. */ public int getIndex(String uri, String localName) { Vector attributes; int size; Attribute attribute; String string; int ret; ret = -1; attributes = mTag.getAttributesEx(); if (null != attributes) { size = attributes.size(); for (int i = 1; i < size; i++) { attribute = (Attribute) attributes.elementAt(i); string = attribute.getName(); if (null != string) // not whitespace { mSupport.processName(string, mParts, true); if (uri.equals(mParts[0]) & localName.equalsIgnoreCase(mParts[1])) { ret = i; i = size; // exit fast } } } } return (ret); }
/** * Look up an attribute's XML qualified (prefixed) name by index. * * @param index The attribute index (zero-based). * @return The XML qualified name, or the empty string if none is available, or null if the index * is out of range. * @see #getLength */ public String getQName(int index) { Attribute attribute; String ret; attribute = (Attribute) (mTag.getAttributesEx().elementAt(index + 1)); if (attribute.isWhitespace()) ret = "#text"; else ret = attribute.getName(); return (ret); }
@Override public void visitTag(Tag tag) { Element e = Document.get().createElement(tag.getTagName()); map.put(tag, e); for (Object o : tag.getAttributesEx()) { Attribute a = (Attribute) o; if ("id".equalsIgnoreCase(a.getName())) { e.setId(a.getValue()); } else if ("style".equalsIgnoreCase(a.getName())) { processStyle(e, a.getValue()); } else if ("class".equalsIgnoreCase(a.getName())) { e.setClassName(a.getValue()); } else if (!a.isEmpty() && !a.isWhitespace() && a.isValued()) { e.setAttribute(a.getName(), a.getValue()); } } Element parent = getParent(tag.getParent()); parent.appendChild(e); }
/** * Given a tag, check its attributes, removing those unwanted or not secure * * @param tag The tag to analyze * @param checkIfAttributeIsWelcome true if the attribute name should be matched against the list * of welcome attributes, set in the main configuration file. */ private void checkAndValidateAttributes(Tag tag, boolean checkIfAttributeIsWelcome) { Vector<Attribute> newAttributes = new Vector<Attribute>(); for (Iterator<?> iter = tag.getAttributesEx().iterator(); iter.hasNext(); ) { Attribute a = (Attribute) iter.next(); String name = a.getName(); if (name == null) { newAttributes.add(a); } else { name = name.toUpperCase(); if (a.getValue() == null) { newAttributes.add(a); continue; } String value = a.getValue().toLowerCase(); if (checkIfAttributeIsWelcome && !this.isAttributeWelcome(name)) { continue; } if (!this.isAttributeSafe(name, value)) { continue; } if (a.getValue().indexOf("&#") > -1) { a.setValue(a.getValue().replaceAll("&#", "&#")); } newAttributes.add(a); } } tag.setAttributesEx(newAttributes); }
/** * Is this an empty xml tag of the form <tag/>. * * @return true if the last character of the last attribute is a '/'. */ public boolean isEmptyXmlTag() { Vector attributes; int size; Attribute attribute; String name; int length; boolean ret; ret = false; attributes = getAttributesEx(); size = attributes.size(); if (0 < size) { attribute = (Attribute) attributes.elementAt(size - 1); name = attribute.getName(); if (null != name) { length = name.length(); ret = name.charAt(length - 1) == '/'; } } return (ret); }
/** * Returns the attribute with the given name. * * @param name Name of attribute, case insensitive. * @return The attribute or null if it does not exist. */ public Attribute getAttributeEx(String name) { Vector attributes; int size; Attribute attribute; String string; Attribute ret; ret = null; attributes = getAttributesEx(); if (null != attributes) { size = attributes.size(); for (int i = 0; i < size; i++) { attribute = (Attribute) attributes.elementAt(i); string = attribute.getName(); if ((null != string) && name.equalsIgnoreCase(string)) { ret = attribute; i = size; // exit fast } } } return (ret); }
/** * Set this tag to be an empty xml node, or not. Adds or removes an ending slash on the tag. * * @param emptyXmlTag If true, ensures there is an ending slash in the node, i.e. <tag/>, * otherwise removes it. */ public void setEmptyXmlTag(boolean emptyXmlTag) { Vector attributes; int size; Attribute attribute; String name; String value; int length; attributes = getAttributesEx(); size = attributes.size(); if (0 < size) { attribute = (Attribute) attributes.elementAt(size - 1); name = attribute.getName(); if (null != name) { length = name.length(); value = attribute.getValue(); if (null == value) if (name.charAt(length - 1) == '/') { // already exists, remove if requested if (!emptyXmlTag) if (1 == length) attributes.removeElementAt(size - 1); else { // this shouldn't happen, but covers the case // where no whitespace separates the slash // from the previous attribute name = name.substring(0, length - 1); attribute = new Attribute(name, null); attributes.removeElementAt(size - 1); attributes.addElement(attribute); } } else { // ends with attribute, add whitespace + slash if requested if (emptyXmlTag) { attribute = new Attribute(" "); attributes.addElement(attribute); attribute = new Attribute("/", null); attributes.addElement(attribute); } } else { // some valued attribute, add whitespace + slash if requested if (emptyXmlTag) { attribute = new Attribute(" "); attributes.addElement(attribute); attribute = new Attribute("/", null); attributes.addElement(attribute); } } } else { // ends with whitespace, add if requested if (emptyXmlTag) { attribute = new Attribute("/", null); attributes.addElement(attribute); } } } else // nothing there, add if requested if (emptyXmlTag) { attribute = new Attribute("/", null); attributes.addElement(attribute); } }