/*******************************************************************************
* 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;
/**
* this tag rule allows endSequence in tag attribute sections
* attribute sections must be enclosed with '"'
* an '"' in an attribute value can be escaped with '\'
*
*
*/
public class TagRule extends MultiLineRule {
private String _startSequence;
public TagRule(String startSequence, String endSequence, IToken token, char escapeCharacter, boolean breaksOnEOF) {
super(startSequence, endSequence, token, escapeCharacter, breaksOnEOF);
_startSequence = startSequence;
}
@Override
protected boolean endSequenceDetected(ICharacterScanner scanner) {
boolean result = super.endSequenceDetected(scanner);
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);
int count = 0;
// search backwards an count unexcaped '"' - stop at _startSequence - if (count % 2) != 0 we have an open attribute
while (!readSoFar.toString().endsWith(reverse(_startSequence))) {
if (c == '"') {
// check if '"' is unescaped
CharacterScannerWrapper wrapper = new CharacterScannerWrapper(scanner);
wrapper.unread();
wrapper.unread();
c = (char) wrapper.read();
if (c != '\\') {
count++;
}
wrapper.reset();
}
scannerWrapper.unread();
scannerWrapper.unread();
//bug fix #00000822
if (c == '\n') {
break;
}
c = (char) scannerWrapper.read();
readSoFar.append(c);
}
scannerWrapper.reset();
if (count%2 != 0) {
// we have an open attribute
return endSequenceDetected(scanner);
}
}
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();
}
}