Ejemplo n.º 1
0
  public List getAttributes() {
    List attrs = new ArrayList();
    String content = null;
    int state = TAG;
    int start = -1;
    int startLength = 0;
    int endLength = 0;
    if (ITypeConstants.PI.equals(getType())) {
      startLength = 2;
      endLength = 2;
    } else if (ITypeConstants.DECL.equals(getType())) {
      //      if ("!DOCTYPE".equals(getName()))
      //        return getDoctypeAttributes();
      startLength = 1;
      endLength = 1;
    } else if (ITypeConstants.TAG.equals(getType())) {
      startLength = 1;
      endLength = 1;
    } else if (ITypeConstants.EMPTYTAG.equals(getType())) {
      startLength = 1;
      endLength = 2;
    } else {
      return attrs;
    }

    try {
      content = document.get(getOffset(), getLength());
    } catch (BadLocationException e) {
      UIPlugin.log(e);
      return attrs;
    }

    String name = getName();
    int initial = name == null ? 0 : content.indexOf(name) + name.length();

    for (int i = startLength + initial; i < content.length() - endLength; i++) {
      char c = content.charAt(i);
      switch (c) {
        case '"':
          if (state == DOUBLEQUOTE) {
            attrs.add(
                new XMLNode(getOffset() + start, i - start + 1, ITypeConstants.ATTR, document));
            start = -1;
            state = TAG;
          } else if (state == SINGLEQUOTE) {
            break;
          } else if (state != ATTR) {
            start = i;
            state = DOUBLEQUOTE;
          } else {
            state = DOUBLEQUOTE;
          }
          break;
        case '\'':
          if (state == SINGLEQUOTE) {
            attrs.add(
                new XMLNode(getOffset() + start, i - start + 1, ITypeConstants.ATTR, document));
            start = -1;
            state = TAG;
          } else if (state == DOUBLEQUOTE) {
            break;
          } else if (state != ATTR) {
            start = i;
            state = SINGLEQUOTE;
          } else {
            state = SINGLEQUOTE;
          }
          break;
        default:
          if (!Character.isWhitespace(c)) {
            if (state == TAG) {
              start = i;
              state = ATTR;
            }
          } else if (state == ATTR) {
            boolean stop = false;
            int j = i;
            // lookahead to see if this is an attribute name with no value
            for (; j < content.length() - endLength; j++) {
              char lookahead = content.charAt(j);

              switch (lookahead) {
                case '=':
                  break;
                case '"':
                  break;
                case '\'':
                  break;
                default:
                  stop = !Character.isWhitespace(lookahead);
                  break;
              }
              if (stop) break;
            }
            if (stop) {

              attrs.add(
                  new XMLNode(getOffset() + start, i - start + 1, ITypeConstants.ATTR, document));
              start = -1;
              state = TAG;
            }
          }
      }
    }

    if (start != -1) {
      attrs.add(
          new XMLNode(
              getOffset() + start,
              content.length()
                  - startLength
                  - start
                  - (!getType().equals(ITypeConstants.TAG) ? 1 : 0),
              ITypeConstants.ATTR,
              document));
    }
    for (Iterator iter = attrs.iterator(); iter.hasNext(); ) {
      XMLNode attr = (XMLNode) iter.next();
      attr.length = StringUtils.stripEnd(attr.getContent(), null).length();
    }

    return attrs;
  }
Ejemplo n.º 2
0
 public boolean isPI() {
   return ITypeConstants.PI.equals(getType());
 }