/**
   * Look up an attribute's value by index.
   *
   * <p>If the attribute value is a list of tokens (IDREFS, ENTITIES, or NMTOKENS), the tokens will
   * be concatenated into a single string with each token separated by a single space.
   *
   * @param index The attribute index (zero-based).
   * @return The attribute's value as a string, or null if the index is out of range.
   * @see #getLength
   */
  public String getValue(int index) {
    Attribute attribute;
    String ret;

    attribute = (Attribute) (mTag.getAttributesEx().elementAt(index + 1));
    ret = attribute.getValue();
    if (null == ret) ret = "";

    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);
 }
Beispiel #3
0
  /**
   * Returns the value of an attribute.
   *
   * @param name Name of attribute, case insensitive.
   * @return The value associated with the attribute or null if it does not exist, or is a
   *     stand-alone or
   */
  public String getAttribute(String name) {
    Attribute attribute;
    String ret;

    ret = null;

    attribute = getAttributeEx(name);
    if (null != attribute) ret = attribute.getValue();

    return (ret);
  }
Beispiel #4
0
  /**
   * 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("&#", "&amp;#"));
        }

        newAttributes.add(a);
      }
    }

    tag.setAttributesEx(newAttributes);
  }
Beispiel #5
0
    public boolean accept(Node node) {
      Tag tag;
      Attribute attribute;
      boolean ret;

      ret = false;
      if (node instanceof Tag) {
        tag = (Tag) node;
        attribute = tag.getAttributeEx(mAttribute);
        ret = null != attribute;
        if (ret && (null != mValue)) ret = attribute.getValue().startsWith(mValue);
      }

      return (ret);
    }
Beispiel #6
0
  /**
   * Set the name of this tag. This creates or replaces the first attribute of the tag (the zeroth
   * element of the attribute vector).
   *
   * @param name The tag name.
   */
  public void setTagName(String name) {
    Attribute attribute;
    Vector attributes;
    Attribute zeroth;

    attribute = new Attribute(name, null, (char) 0);
    attributes = getAttributesEx();
    if (null == attributes) {
      attributes = new Vector();
      setAttributesEx(attributes);
    }
    if (0 == attributes.size())
      // nothing added yet
      attributes.addElement(attribute);
    else {
      zeroth = (Attribute) attributes.elementAt(0);
      // check for attribute that looks like a name
      if ((null == zeroth.getValue()) && (0 == zeroth.getQuote()))
        attributes.setElementAt(attribute, 0);
      else attributes.insertElementAt(attribute, 0);
    }
  }
Beispiel #7
0
  /**
   * 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. &lt;tag/&gt;,
   *     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);
    }
  }