Пример #1
0
  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();
  }