Example #1
0
 private void parse(Element node) {
   if (!showThumbs) {
     // do not show any thumbnails
     return;
   }
   int count = node.getElementCount();
   for (int i = 0; i < count; i++) {
     Element elm = node.getElement(i);
     Debug.log(8, elm.toString());
     if (elm.isLeaf()) {
       int start = elm.getStartOffset(), end = elm.getEndOffset();
       parseRange(start, end);
     } else {
       parse(elm);
     }
   }
 }
Example #2
0
 private int getFunctionStartOffset(String func, Element node) throws BadLocationException {
   Document doc = getDocument();
   int count = node.getElementCount();
   Pattern patDef = Pattern.compile("def\\s+" + func + "\\s*\\(");
   for (int i = 0; i < count; i++) {
     Element elm = node.getElement(i);
     if (elm.isLeaf()) {
       int start = elm.getStartOffset(), end = elm.getEndOffset();
       String line = doc.getText(start, end - start);
       Matcher matcher = patDef.matcher(line);
       if (matcher.find()) {
         return start;
       }
     } else {
       int p = getFunctionStartOffset(func, elm);
       if (p >= 0) {
         return p;
       }
     }
   }
   return -1;
 }
  private Element process(final javax.swing.text.Element textElement) throws BadLocationException {
    if (textElement.isLeaf()) {
      final int endOffset = textElement.getEndOffset();
      final int startOffset = textElement.getStartOffset();
      final String text = textElement.getDocument().getText(startOffset, endOffset - startOffset);
      final Element result = new Element();
      result.setElementType(LabelType.INSTANCE);
      result.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.VALUE, text);
      configureStyle(textElement.getAttributes(), result);
      return result;
    }

    final Band band = new Band();
    configureStyle(textElement.getAttributes(), band);
    configureBand(textElement, band);
    final int size = textElement.getElementCount();
    for (int i = 0; i < size; i++) {
      final Element element = process(textElement.getElement(i));
      band.addElement(element);
    }
    return band;
  }
  /**
   * @param pre the preformatted Element containing Scilab's code
   * @return the code
   */
  private static String getCode(Element pre) {
    int size = pre.getElementCount();
    Document doc = pre.getDocument();
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < size; i++) {
      Element line = pre.getElement(i);
      int ssize = line.getElementCount();
      for (int j = 0; j < ssize; j++) {
        Element content = line.getElement(j);
        if (content.isLeaf()) {
          try {
            buffer.append(
                doc.getText(
                    content.getStartOffset(), content.getEndOffset() - content.getStartOffset()));
          } catch (BadLocationException e) {
          }
        }
      }
    }

    return buffer.toString().trim();
  }
  /**
   * 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;
  }