/**
   * 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);
  }
Esempio n. 2
0
  /**
   * 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 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);
  }
  /**
   * 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);
  }
Esempio n. 5
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);
  }
Esempio n. 6
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);
    }
Esempio n. 7
0
  /**
   * Set attribute with given key, value pair. Figures out a quote character to use if necessary.
   *
   * @param key The name of the attribute.
   * @param value The value of the attribute.
   */
  public void setAttribute(String key, String value) {
    char ch;
    boolean needed;
    boolean singleq;
    boolean doubleq;
    String ref;
    StringBuffer buffer;
    char quote;
    Attribute attribute;

    // first determine if there's whitespace in the value
    // and while we'return at it find a suitable quote character
    needed = false;
    singleq = true;
    doubleq = true;
    if (null != value)
      for (int i = 0; i < value.length(); i++) {
        ch = value.charAt(i);
        if (Character.isWhitespace(ch)) needed = true;
        else if ('\'' == ch) singleq = false;
        else if ('"' == ch) doubleq = false;
      }

    // now apply quoting
    if (needed) {
      if (doubleq) quote = '"';
      else if (singleq) quote = '\'';
      else {
        // uh-oh, we need to convert some quotes into character references
        // convert all double quotes into &#34;
        quote = '"';
        ref = "&quot;"; // Translate.encode (quote);
        // JDK 1.4: value = value.replaceAll ("\"", ref);
        buffer = new StringBuffer(value.length() * 5);
        for (int i = 0; i < value.length(); i++) {
          ch = value.charAt(i);
          if (quote == ch) buffer.append(ref);
          else buffer.append(ch);
        }
        value = buffer.toString();
      }
    } else quote = 0;
    attribute = getAttributeEx(key);
    if (null != attribute) { // see if we can splice it in rather than replace it
      attribute.setValue(value);
      if (0 != quote) attribute.setQuote(quote);
    } else setAttribute(key, value, quote);
  }
Esempio n. 8
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);
    }
  }
Esempio n. 9
0
  /**
   * Is this an empty xml tag of the form &lt;tag/&gt;.
   *
   * @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);
  }
Esempio n. 10
0
  /**
   * Render the tag as HTML. A call to a tag's <code>toHtml()</code> method will render it in HTML.
   *
   * @param verbatim If <code>true</code> return as close to the original page text as possible.
   * @return The tag as an HTML fragment.
   * @see org.htmlparser.Node#toHtml()
   */
  public String toHtml(boolean verbatim) {
    int length;
    int size;
    Vector attributes;
    Attribute attribute;
    StringBuffer ret;

    length = 2;
    attributes = getAttributesEx();
    size = attributes.size();
    for (int i = 0; i < size; i++) {
      attribute = (Attribute) attributes.elementAt(i);
      length += attribute.getLength();
    }
    ret = new StringBuffer(length);
    ret.append("<");
    for (int i = 0; i < size; i++) {
      attribute = (Attribute) attributes.elementAt(i);
      attribute.toString(ret);
    }
    ret.append(">");

    return (ret.toString());
  }
Esempio n. 11
0
  /**
   * 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);
  }
Esempio n. 12
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);
  }
Esempio n. 13
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);
    }
  }
Esempio n. 14
0
 @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);
 }