Exemple #1
0
 private int getRealEnd() {
   //		if ( end != null) {
   return end.getParentElement().getElementIndex(end.getStartOffset());
   //		} else {
   //			return start.getParentElement().getElementCount();
   //		}
 }
 private boolean indentNeedsIncrementing(Element current, Element next) {
   if ((next.getParentElement() == current) && !inPre) {
     if (indentNext) {
       indentNext = false;
       return true;
     } else if (synthesizedElement(next)) {
       indentNext = true;
     } else if (!synthesizedElement(current)) {
       return true;
     }
   }
   return false;
 }
Exemple #3
0
 private int getRealStart() {
   fixedStart = start.getParentElement().getElementIndex(start.getStartOffset());
   return fixedStart;
 }
  /**
   * Iterates over the Element tree and controls the writing out of all the tags and its attributes.
   *
   * @exception IOException on any I/O error
   * @exception BadLocationException if pos represents an invalid location within the document.
   */
  public void write() throws IOException, BadLocationException {
    ElementIterator it = getElementIterator();
    Element current = null;
    Element next = null;

    wroteHead = false;
    setCurrentLineLength(0);
    replaceEntities = false;
    setCanWrapLines(false);
    if (segment == null) {
      segment = new Segment();
    }
    inPre = false;
    boolean forcedBody = false;
    while ((next = it.next()) != null) {
      if (!inRange(next)) {
        if (completeDoc
            && next.getAttributes().getAttribute(StyleConstants.NameAttribute) == HTML.Tag.BODY) {
          forcedBody = true;
        } else {
          continue;
        }
      }
      if (current != null) {

        /*
          if next is child of current increment indent
        */

        if (indentNeedsIncrementing(current, next)) {
          incrIndent();
        } else if (current.getParentElement() != next.getParentElement()) {
          /*
             next and current are not siblings
             so emit end tags for items on the stack until the
             item on top of the stack, is the parent of the
             next.
          */
          Element top = (Element) blockElementStack.peek();
          while (top != next.getParentElement()) {
            /*
               pop() will return top.
            */
            blockElementStack.pop();
            if (!synthesizedElement(top)) {
              AttributeSet attrs = top.getAttributes();
              if (!matchNameAttribute(attrs, HTML.Tag.PRE) && !isFormElementWithContent(attrs)) {
                decrIndent();
              }
              endTag(top);
            }
            top = (Element) blockElementStack.peek();
          }
        } else if (current.getParentElement() == next.getParentElement()) {
          /*
             if next and current are siblings the indent level
             is correct.  But, we need to make sure that if current is
             on the stack, we pop it off, and put out its end tag.
          */
          Element top = (Element) blockElementStack.peek();
          if (top == current) {
            blockElementStack.pop();
            endTag(top);
          }
        }
      }
      if (!next.isLeaf() || isFormElementWithContent(next.getAttributes())) {
        blockElementStack.push(next);
        startTag(next);
      } else {
        emptyTag(next);
      }
      current = next;
    }
    /* Emit all remaining end tags */

    /* A null parameter ensures that all embedded tags
       currently in the tags vector have their
       corresponding end tags written out.
    */
    closeOutUnwantedEmbeddedTags(null);

    if (forcedBody) {
      blockElementStack.pop();
      endTag(current);
    }
    while (!blockElementStack.empty()) {
      current = (Element) blockElementStack.pop();
      if (!synthesizedElement(current)) {
        AttributeSet attrs = current.getAttributes();
        if (!matchNameAttribute(attrs, HTML.Tag.PRE) && !isFormElementWithContent(attrs)) {
          decrIndent();
        }
        endTag(current);
      }
    }

    if (completeDoc) {
      writeAdditionalComments();
    }

    segment.array = null;
  }