Package org.eclipse.wst.sse.core.internal.provisional.text

Examples of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion


   * @param domRegion
   * @param parentConstraints
   * @param formatRange
   */
  private void formatSiblings(TextEdit edit, DOMRegion domRegion, XMLFormattingConstraints parentConstraints, Position formatRange) {
    IStructuredDocumentRegion previousRegion = null;
    IStructuredDocumentRegion currentRegion = domRegion.documentRegion;
    IDOMNode currentDOMNode = domRegion.domNode;
    while (currentDOMNode != null && currentRegion != null && formatRange.overlapsWith(currentRegion.getStartOffset(), currentRegion.getLength()) && (fProgressMonitor == null || !fProgressMonitor.isCanceled())) {
      domRegion.documentRegion = currentRegion;
      domRegion.domNode = currentDOMNode;

      // need to make sure current document region and current
      // dom node match up
View Full Code Here


   * @param textRegions
   */
  private DOMRegion formatStartTag(TextEdit textEdit, Position formatRange, XMLFormattingConstraints parentConstraints, DOMRegion currentDOMRegion, IStructuredDocumentRegion previousDocumentRegion) {
    // determine proper indent by referring to parent constraints,
    // previous node, and current node
    IStructuredDocumentRegion currentDocumentRegion = currentDOMRegion.documentRegion;
    IDOMNode currentDOMNode = currentDOMRegion.domNode;

    // create a constraint for this tag
    XMLFormattingConstraints thisConstraints = new XMLFormattingConstraints();
    XMLFormattingConstraints childrenConstraints = new XMLFormattingConstraints();
    updateFormattingConstraints(parentConstraints, thisConstraints, childrenConstraints, currentDOMRegion);

    if(XMLFormattingConstraints.DEFAULT.equals(childrenConstraints.getWhitespaceStrategy()))
      childrenConstraints.setWhitespaceStrategy((new XMLFormattingPreferences()).getElementWhitespaceStrategy());
     
    String whitespaceStrategy = thisConstraints.getWhitespaceStrategy();
    String indentStrategy = thisConstraints.getIndentStrategy();
    int availableLineWidth = thisConstraints.getAvailableLineWidth();

    // format space before start tag
    // do not format space before start tag if preserving spaces
    if (!XMLFormattingConstraints.PRESERVE.equals(whitespaceStrategy)) {
      // format like indent strategy says
      if (XMLFormattingConstraints.INDENT.equals(indentStrategy) || XMLFormattingConstraints.NEW_LINE.equals(indentStrategy)) {
        availableLineWidth = indentIfPossible(textEdit, thisConstraints, currentDocumentRegion, previousDocumentRegion, whitespaceStrategy, indentStrategy, true, true);
        if (availableLineWidth > 0)
          thisConstraints.setAvailableLineWidth(availableLineWidth);
      }
    }
    // format the start tag itself
    boolean tagEnded = formatWithinTag(textEdit, thisConstraints, currentDocumentRegion, previousDocumentRegion);

    // format children
    if (!tagEnded) {
      // update childConstraints with thisConstraint's indentLevel &
      // availableLineWidth
      childrenConstraints.setIndentLevel(thisConstraints.getIndentLevel());
      childrenConstraints.setAvailableLineWidth(thisConstraints.getAvailableLineWidth());

      previousDocumentRegion = currentDocumentRegion;
      IDOMNode childDOMNode = (IDOMNode) currentDOMNode.getFirstChild();
      IStructuredDocumentRegion nextRegion = currentDocumentRegion.getNext();
      boolean passedFormatRange = false;
      // as long as there is one child
      if (childDOMNode != null && nextRegion != null) {
        while (childDOMNode != null && nextRegion != null && !passedFormatRange && (fProgressMonitor == null || !fProgressMonitor.isCanceled())) {
          DOMRegion childDOMRegion = new DOMRegion();
          childDOMRegion.documentRegion = nextRegion;
          childDOMRegion.domNode = childDOMNode;
          if (nextRegion.equals(childDOMNode.getFirstStructuredDocumentRegion())) {
            // format children. pass in child constraints
            childDOMRegion = formatRegion(textEdit, formatRange, childrenConstraints, childDOMRegion, previousDocumentRegion);
          }
          else {
            // TODO: what happens if they dont match up?
          }

          // update childDOMRegion with next dom/region node
          if (childDOMRegion.domNode != null) {
            childDOMNode = (IDOMNode) childDOMRegion.domNode.getNextSibling();
          }
          else {
            childDOMNode = null;
          }
          previousDocumentRegion = childDOMRegion.documentRegion;
          nextRegion = previousDocumentRegion.getNext();
          if (nextRegion != null)
            passedFormatRange = !formatRange.overlapsWith(nextRegion.getStartOffset(), nextRegion.getLength());
        }
      }
      else {
        // there were no children, so keep end tag inlined
        childrenConstraints.setWhitespaceStrategy(XMLFormattingConstraints.COLLAPSE);
        childrenConstraints.setIndentStrategy(XMLFormattingConstraints.INLINE);
      }

      if (!passedFormatRange) {
        // update the dom region with the last formatted region/dom
        // node should be end tag and this tag's DOMNode
        currentDOMRegion.documentRegion = nextRegion;
        currentDOMRegion.domNode = currentDOMNode;

        // end tag's indent level should be same as start tag's
        childrenConstraints.setIndentLevel(thisConstraints.getIndentLevel());
        // format end tag
        boolean formatEndTag = false;
        if (nextRegion != null && currentDOMNode != null) {
          ITextRegionList rs = nextRegion.getRegions();
          if (rs.size() > 1) {
            ITextRegion r = rs.get(0);
            if (r != null && DOMRegionContext.XML_END_TAG_OPEN.equals(r.getType())) {
              r = rs.get(1);
              if (r != null && DOMRegionContext.XML_TAG_NAME.equals(r.getType())) {
                String tagName = nextRegion.getText(r);
                if (tagName != null && tagName.equals(currentDOMNode.getNodeName()))
                  formatEndTag = true;
              }
            }

View Full Code Here

 
  /**
   * Format an XML comment structured document region.
   */
  private void formatComment(TextEdit textEdit, Position formatRange, XMLFormattingConstraints parentConstraints, DOMRegion currentDOMRegion, IStructuredDocumentRegion previousRegion) {
    IStructuredDocumentRegion currentRegion = currentDOMRegion.documentRegion;
    int lineWidth = parentConstraints.getAvailableLineWidth() - currentRegion.getFullText().length();
    // Don't format if we're not exceeding the available line width, or if the whitespace
    // strategy is to preserve whitespace - But update line width.
    if(currentRegion == null ||  XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy()) || !fPreferences.getFormatCommentText()) {
      parentConstraints.setAvailableLineWidth(lineWidth);
      return;
    }

    Iterator it = currentRegion.getRegions().iterator();
    ITextRegion previous = null;
    if (previousRegion == null)
      previousRegion = currentRegion.getPrevious();
    // Iterate over each text region of the comment
    Node parent = currentDOMRegion.domNode.getParentNode();
    while(it.hasNext()) {
      ITextRegion text = (ITextRegion) it.next();
      String type = text.getType();
View Full Code Here

   *            can be null
   * @param currentDOMRegion
   *            cannot be null
   */
  protected void updateFormattingConstraints(XMLFormattingConstraints parentConstraints, XMLFormattingConstraints thisConstraints, XMLFormattingConstraints childConstraints, DOMRegion currentDOMRegion) {
    IStructuredDocumentRegion currentRegion = currentDOMRegion.documentRegion;
    IDOMNode currentNode = currentDOMRegion.domNode;

    // default to whatever parent's constraint said to do
    if (parentConstraints != null) {
      if (thisConstraints != null) {
        thisConstraints.copyConstraints(parentConstraints);
      }
      if (childConstraints != null) {
        childConstraints.copyConstraints(parentConstraints);
        // if whitespace strategy was only a hint, null it out so
        // defaults are taken instead
        if (parentConstraints.isWhitespaceStrategyAHint())
          childConstraints.setWhitespaceStrategy(null);
      }
    }

    // set up constraints for direct children of document root
    Node parentNode = currentNode.getParentNode();
    if (parentNode != null && parentNode.getNodeType() == Node.DOCUMENT_NODE) {
      if (thisConstraints != null) {
        thisConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
        thisConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
        thisConstraints.setIndentLevel(0);
      }
      if (childConstraints != null) {
        childConstraints.setWhitespaceStrategy(null);
        childConstraints.setIndentStrategy(null);
        childConstraints.setIndentLevel(0);
      }
    }

    // other conditions to check when setting up child constraints
    if (childConstraints != null) {
      XMLFormattingPreferences preferences = getFormattingPreferences();

      // if we're at document root, child tags should always just start
      // on a new line and have an indent level of 0
      if (currentNode.getNodeType() == Node.DOCUMENT_NODE) {
        childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
        childConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
        childConstraints.setIndentLevel(0);
      }
      else {
        // BUG108074 & BUG84688 - preserve whitespace in xsl:text &
        // xsl:attribute
        String nodeNamespaceURI = currentNode.getNamespaceURI();
        if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
          String nodeName = ((Element) currentNode).getLocalName();
          if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
            childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
          }
        }
        else {
          // search within current tag for xml:space attribute
          ITextRegionList textRegions = currentRegion.getRegions();
          int i = 0;
          boolean xmlSpaceFound = false;
          boolean preserveFound = false;
          while (i < textRegions.size() && !xmlSpaceFound) {
            ITextRegion textRegion = textRegions.get(i);
            if (DOMRegionContext.XML_TAG_ATTRIBUTE_NAME.equals(textRegion.getType())) {
              String regionText = currentRegion.getText(textRegion);
              if (XML_SPACE.equals(regionText)) {
                if ((i + 1) < textRegions.size()) {
                  ++i;
                  textRegion = textRegions.get(i);
                  if (DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS.equals(textRegion.getType()) && ((i + 1) < textRegions.size())) {
                    ++i;
                    textRegion = textRegions.get(i);
                    regionText = currentRegion.getText(textRegion);
                    if (PRESERVE.equals(regionText) || PRESERVE_QUOTED.equals(regionText)) {
                      preserveFound = true;
                    }
                  }
                }
View Full Code Here

   * This is a simple utility to count nodes. Used only for debug
   * statements.
   */
  protected int _countNodes(IStructuredDocumentRegion nodes) {
    int result = 0;
    IStructuredDocumentRegion countNode = nodes;
    while (countNode != null) {
      result++;
      countNode = countNode.getNext();
    }
    return result;
  }
View Full Code Here

  /**
   * @return IStructuredDocumentRegion
   */
  protected IStructuredDocumentRegion createStructuredDocumentRegion(String type) {
    IStructuredDocumentRegion newNode = null;
    if (type == DOMRegionContext.BLOCK_TEXT)
      newNode = XMLStructuredRegionFactory.createRegion(XMLStructuredRegionFactory.XML_BLOCK);
    else
      newNode = XMLStructuredRegionFactory.createRegion(XMLStructuredRegionFactory.XML);
    return newNode;
View Full Code Here

  /**
   * @return IStructuredDocumentRegion
   */
  public IStructuredDocumentRegion getDocumentRegions() {
    IStructuredDocumentRegion headnode = null;
    if (headnode == null) {
      if (Debug.perfTest) {
        startTime = System.currentTimeMillis();
      }
      headnode = parseNodes();
View Full Code Here

  /**
   * Return the full list of known regions. Typically getNodes should be
   * used instead of this method.
   */
  public List getRegions() {
    IStructuredDocumentRegion headNode = null;
    if (!getTokenizer().isEOF()) {
      headNode = getDocumentRegions();
      // throw new IllegalStateException("parsing has not finished");
    }
    // for memory recovery, we assume if someone
View Full Code Here

   * @param headNode
   * @return List
   */
  protected List getRegions(IStructuredDocumentRegion headNode) {
    List allRegions = new ArrayList();
    IStructuredDocumentRegion currentNode = headNode;
    while (currentNode != null) {
      ITextRegionList nodeRegions = currentNode.getRegions();
      for (int i = 0; i < nodeRegions.size(); i++) {
        allRegions.add(nodeRegions.get(i));
      }
      currentNode = currentNode.getNext();
    }
    return allRegions;
  }
View Full Code Here

  protected IStructuredDocumentRegion parseNodes() {
    // regions are initially reported as complete offsets within the
    // scanned input
    // they are adjusted here to be indexes from the currentNode's start
    // offset
    IStructuredDocumentRegion headNode = null;
    IStructuredDocumentRegion lastNode = null;
    ITextRegion region = null;
    IStructuredDocumentRegion currentNode = null;
    String type = null;

    while ((region = getNextRegion()) != null) {
      type = region.getType();
      // these types (might) demand a IStructuredDocumentRegion for each
      // of them
      if (type == DOMRegionContext.BLOCK_TEXT) {
        if (currentNode != null && currentNode.getLastRegion().getType() == DOMRegionContext.BLOCK_TEXT) {
          // multiple block texts indicated embedded containers; no
          // new IStructuredDocumentRegion
          currentNode.addRegion(region);
          currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
          region.adjustStart(-currentNode.getStart());
          // DW 4/16/2003 regions no longer have parents
          // region.setParent(currentNode);
        }
        else {
          // not continuing a IStructuredDocumentRegion
          if (currentNode != null) {
            // ensure that any existing node is at least
            // terminated
            if (!currentNode.isEnded()) {
              currentNode.setLength(region.getStart() - currentNode.getStart());
              // fCurrentNode.setTextLength(region.getStart() -
              // fCurrentNode.getStart());
            }
            lastNode = currentNode;
          }
          fireNodeParsed(currentNode);
          currentNode = createStructuredDocumentRegion(type);
          if (lastNode != null) {
            lastNode.setNext(currentNode);
          }
          currentNode.setPrevious(lastNode);
          currentNode.setStart(region.getStart());
          currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
          currentNode.setEnded(true);
          region.adjustStart(-currentNode.getStart());
          currentNode.addRegion(region);
          // DW 4/16/2003 regions no longer have parents
          // region.setParent(currentNode);
        }
      }
      // the following contexts OPEN new StructuredDocumentRegions
      else if ((currentNode != null && currentNode.isEnded()) || (type == DOMRegionContext.XML_CONTENT) || (type == DOMRegionContext.XML_CHAR_REFERENCE) || (type == DOMRegionContext.XML_ENTITY_REFERENCE) || (type == DOMRegionContext.XML_PI_OPEN) || (type == DOMRegionContext.XML_TAG_OPEN) || (type == DOMRegionContext.XML_END_TAG_OPEN) || (type == DOMRegionContext.XML_COMMENT_OPEN) || (type == DOMRegionContext.XML_CDATA_OPEN) || (type == DOMRegionContext.XML_DECLARATION_OPEN)) {
        if (currentNode != null) {
          // ensure that any existing node is at least terminated
          if (!currentNode.isEnded()) {
            currentNode.setLength(region.getStart() - currentNode.getStart());
            // fCurrentNode.setTextLength(region.getStart() -
            // fCurrentNode.getStart());
          }
          lastNode = currentNode;
        }
        fireNodeParsed(currentNode);
        currentNode = createStructuredDocumentRegion(type);
        if (lastNode != null) {
          lastNode.setNext(currentNode);
        }
        currentNode.setPrevious(lastNode);
        currentNode.setStart(region.getStart());
        currentNode.addRegion(region);
        currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
        region.adjustStart(-currentNode.getStart());
        // DW 4/16/2003 regions no longer have parents
        // region.setParent(currentNode);
      }
      // the following contexts neither open nor close
      // StructuredDocumentRegions; just add to them
      else if ((type == DOMRegionContext.XML_TAG_NAME) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) || (type == DOMRegionContext.XML_COMMENT_TEXT) || (type == DOMRegionContext.XML_PI_CONTENT) || (type == DOMRegionContext.XML_DOCTYPE_INTERNAL_SUBSET)) {
        currentNode.addRegion(region);
        currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
        region.adjustStart(-currentNode.getStart());
        // DW 4/16/2003 regions no longer have parents
        // region.setParent(currentNode);
      }
      // the following contexts close off StructuredDocumentRegions
      // cleanly
      else if ((type == DOMRegionContext.XML_PI_CLOSE) || (type == DOMRegionContext.XML_TAG_CLOSE) || (type == DOMRegionContext.XML_EMPTY_TAG_CLOSE) || (type == DOMRegionContext.XML_COMMENT_CLOSE) || (type == DOMRegionContext.XML_DECLARATION_CLOSE) || (type == DOMRegionContext.XML_CDATA_CLOSE)) {
        currentNode.setEnded(true);
        currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
        currentNode.addRegion(region);
        region.adjustStart(-currentNode.getStart());
        // DW 4/16/2003 regions no longer have parents
        // region.setParent(currentNode);
      }
      // this is extremely rare, but valid
      else if (type == DOMRegionContext.WHITE_SPACE) {
        ITextRegion lastRegion = currentNode.getLastRegion();
        // pack the embedded container with this region
        if (lastRegion instanceof ITextRegionContainer) {
          ITextRegionContainer container = (ITextRegionContainer) lastRegion;
          container.getRegions().add(region);
          // containers must have parent set ...
          // setting for EACH subregion is redundent, but not sure
          // where else to do, so will do here for now.
          container.setParent(currentNode);
          // DW 4/16/2003 regions no longer have parents
          // region.setParent(container);
          region.adjustStart(container.getLength() - region.getStart());
        }
        currentNode.getLastRegion().adjustLength(region.getLength());
        currentNode.adjustLength(region.getLength());
      }
      else if (type == DOMRegionContext.UNDEFINED && currentNode != null) {
        // skip on a very-first region situation as the default
        // behavior is good enough
        // combine with previous if also undefined
        if (currentNode.getLastRegion() != null && currentNode.getLastRegion().getType() == DOMRegionContext.UNDEFINED) {
          currentNode.getLastRegion().adjustLength(region.getLength());
          currentNode.adjustLength(region.getLength());
        }
        // previous wasn't undefined
        else {
          currentNode.addRegion(region);
          currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
          region.adjustStart(-currentNode.getStart());
        }
      }
      else {
        // if an unknown type is the first region in the document,
        // ensure that a node exists
        if (currentNode == null) {
          currentNode = createStructuredDocumentRegion(type);
          currentNode.setStart(region.getStart());
        }
        currentNode.addRegion(region);
        currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
        region.adjustStart(-currentNode.getStart());
        // DW 4/16/2003 regions no longer have parents
        // region.setParent(currentNode);
        if (Debug.debugTokenizer)
          System.out.println(getClass().getName() + " found region of not specifically handled type " + region.getType() + " @ " + region.getStart() + "[" + region.getLength() + "]"); //$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
        //$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
      }

      // these regions also get their own node, so close them cleanly
      // NOTE: these regions have new StructuredDocumentRegions created
      // for them above; it may
      // be more readable if that is handled here as well, but the
      // current layout
      // ensures that they open StructuredDocumentRegions the same way
      if ((type == DOMRegionContext.XML_CONTENT) || (type == DOMRegionContext.XML_CHAR_REFERENCE) || (type == DOMRegionContext.XML_ENTITY_REFERENCE)) {
        currentNode.setEnded(true);
      }
      if (headNode == null && currentNode != null) {
        headNode = currentNode;
      }
    }
    if (currentNode != null) {
      fireNodeParsed(currentNode);
      currentNode.setPrevious(lastNode);
    }
    // fStringInput = null;
    primReset();
    return headNode;
  }
View Full Code Here

TOP

Related Classes of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.