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; }
protected Object doFormat(TypedPosition partition) throws BadLocationException { // determine the enclosing element and the appropriate indent TypedPositionWalker walker = new TypedPositionWalker(fDocumentPositions, fOffset); for (TypedPosition tposition = walker.previous(); tposition != null; tposition = walker.previous()) { String type = tposition.getType(); if (type == ITypeConstants.TAG || type == ITypeConstants.EMPTYTAG) { fInitialIndent = getIndent(tposition.getOffset()); if (type != ITypeConstants.EMPTYTAG) fIndentLevel++; break; } else if (type == ITypeConstants.ENDTAG) { fInitialIndent = getIndent(tposition.getOffset()); break; } } // walk through the partitions and format walker = new TypedPositionWalker(fDocumentPositions, fOffset, partition.length); StringBuffer buffer = new StringBuffer(); TypedPosition tposition = walker.next(); while (tposition != null) { String type = tposition.getType(); if (type == ITypeConstants.TAG || type == ITypeConstants.EMPTYTAG) { formatTag(tposition, buffer, false); if (type == ITypeConstants.TAG) fIndentLevel++; } else if (type == ITypeConstants.ENDTAG) { if (fIndentLevel > 0) fIndentLevel--; formatTag(tposition, buffer, true); } else if (type == ITypeConstants.DECL) { XMLNode artifact = (XMLNode) tposition; String content = artifact.getContent(); if (content.indexOf("DOCTYPE") >= 0) { formatTag(tposition, buffer, true); } else { formatCDATA(tposition, buffer); } } else if (type == ITypeConstants.COMMENT) { formatDefault(tposition, buffer); } else if (type == ITypeConstants.TEXT) { formatDefault(tposition, buffer); } else if (type == ITypeConstants.PI) { formatTag(tposition, buffer, true); } tposition = walker.next(); } // finally, have the line infos update the positions array Iterator it = fLineInfos.iterator(); while (it.hasNext()) { LineInfo info = (LineInfo) it.next(); info.updatePositions(); } return buffer.toString(); }