Package de.innovationgate.eclipse.editors.tml

Source Code of de.innovationgate.eclipse.editors.tml.HTMLXMLTagRule

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.tml;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;

import de.innovationgate.eclipse.editors.helpers.CharacterScannerWrapper;

/**
* rule for html/xml tags
* startsequence is '<'
* endsequence is ' '
*
* if endsequence is found this rule checks backwards if tag is really open so there should be no '>' before
* this is to prevent detecting ABC in the following example:
*
* <b>ABC test</b>
*
* in such a case the scanner will be positioned to the '>' character in the first "<b>"-Tag and the rule will return on endSequenceDetected - this way only the sequence "<b>" will be detected
*
* NOTE: this rule can currently have only a single char start- && endSequence
*
*
*
*/
public class HTMLXMLTagRule extends MultiLineRule {

  private String _startSequence;
  private String _endSequence;
 
  public HTMLXMLTagRule(IToken token) {
    super("<", " ", token, '\\', false);
    _startSequence = "<";
    _endSequence = " ";
  }
 
 

  @Override
  protected boolean endSequenceDetected(ICharacterScanner scanner) {
    boolean result;
    try {
      result = super.endSequenceDetected(scanner);
    } catch (RuntimeException e) {
      e.printStackTrace();
      throw e;
    }
    CharacterScannerWrapper scannerWrapper = new CharacterScannerWrapper(scanner);
    int currentChar = scannerWrapper.read();
    scannerWrapper.unread();
    if (result && currentChar != ICharacterScanner.EOF) {
      scannerWrapper.unread();
      char c = (char)scannerWrapper.read();
      StringBuffer readSoFar = new StringBuffer()
      readSoFar.append(c);
      while (!readSoFar.toString().endsWith(reverse(_startSequence))) {       
        if (c == '>') {
          return true
        }
        scannerWrapper.unread();
        scannerWrapper.unread();
        c = (char) scannerWrapper.read();
        readSoFar.append(c);
      }
    }
    scannerWrapper.reset();
    return result;
  }

  private String reverse(String source) {
      int i, len = source.length();
      StringBuffer dest = new StringBuffer(len);
      for (i = (len - 1); i >= 0; i--) {
        dest.append(source.charAt(i));
      }
      return dest.toString();   
  }



  /**
   * if endsequence ' ' is found we have to check if this space is behind an close tag
   * for e.g. "<h1>Help</h1> "
   * in this case the trailing space will result in from tag detection
   * if we find an ' ' we now rewind the scanner until _startSequence is found
   * then we check if next char is an '/' and set result to false in this case
   */
  @Override 
  protected boolean sequenceDetected(ICharacterScanner scanner, char[] sequence, boolean eofAllowed) {   
    boolean result = super.sequenceDetected(scanner, sequence, eofAllowed);
    CharacterScannerWrapper scannerWrapper = new CharacterScannerWrapper(scanner);
    scannerWrapper.unread();
    if (result && new String(sequence).equals(_endSequence)) {
      scannerWrapper.unread();
      char c = (char)scannerWrapper.read();
      StringBuffer readSoFar = new StringBuffer()
      readSoFar.append(c);
      while (!readSoFar.toString().endsWith(reverse(_startSequence))) {               
        scannerWrapper.unread();
        scannerWrapper.unread();
        c = (char) scannerWrapper.read();
        readSoFar.append(c);
      }
      c = (char) scannerWrapper.read();
      result = !( c == '/');
    }
    scannerWrapper.reset();
    return result;
  }
}
TOP

Related Classes of de.innovationgate.eclipse.editors.tml.HTMLXMLTagRule

TOP
Copyright © 2018 www.massapi.com. 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.