org.sonar.plugins.web.lex.TextTokenizer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-web-plugin Show documentation
Show all versions of sonar-web-plugin Show documentation
Enables scanning of HTML, and JSP/JSF files.
/*
* SonarSource :: Web :: Sonar Plugin
* Copyright (c) 2010-2016 SonarSource SA and Matthijs Galesloot
* [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sonar.plugins.web.lex;
import java.util.List;
import org.sonar.channel.CodeReader;
import org.sonar.channel.EndMatcher;
import org.sonar.plugins.web.node.Node;
import org.sonar.plugins.web.node.NodeType;
import org.sonar.plugins.web.node.TagNode;
import org.sonar.plugins.web.node.TextNode;
/**
* Tokenizer for content.
*
* @author Matthijs Galesloot
* @since 1.0
*
* TODO - handle CDATA
*/
class TextTokenizer extends AbstractTokenizer> {
private static final class EndTokenMatcher implements EndMatcher {
@Override
public boolean match(int endFlag) {
return endFlag == '<';
}
}
private final EndMatcher endTokenMatcher = new EndTokenMatcher();
public TextTokenizer() {
super("", "");
}
/**
* Checks for the end of a script block
*/
private static class EndScriptMatcher implements EndMatcher {
private final CodeReader codeReader;
private static final String END_SCRIPT = "";
public EndScriptMatcher(CodeReader codeReader) {
this.codeReader = codeReader;
}
@Override
public boolean match(int endFlag) {
// return true on end of file
if (endFlag == (char) -1) {
return true;
}
// check for end script
return (char) endFlag == '<' && END_SCRIPT.equalsIgnoreCase(new String(codeReader.peek(END_SCRIPT.length())));
}
}
@Override
public boolean consume(CodeReader codeReader, List nodeList) {
Node node = createNode();
setStartPosition(codeReader, node);
StringBuilder stringBuilder = new StringBuilder();
if (inScript(nodeList)) {
codeReader.popTo(new EndScriptMatcher(codeReader), stringBuilder);
} else {
codeReader.popTo(endTokenMatcher, stringBuilder);
}
node.setCode(stringBuilder.toString());
setEndPosition(codeReader, node);
nodeList.add(node);
return true;
}
private static boolean inScript(List nodeList) {
if (!nodeList.isEmpty()) {
Node node = nodeList.get(nodeList.size() - 1);
if (node.getNodeType() == NodeType.TAG) {
TagNode tag = (TagNode) node;
return !tag.isEndElement() && "script".equalsIgnoreCase(tag.getNodeName());
}
}
return false;
}
@Override
Node createNode() {
return new TextNode();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy