use of org.sonar.plugins.web.node.TagNode in project sonar-web by SonarSource.
the class ElementTokenizer method parseToken.
private void parseToken(Node node) {
TagNode element = (TagNode) node;
CodeReader codeReader = new CodeReader(node.getCode());
ParseMode mode = ParseMode.BEFORE_NODE_NAME;
for (int ch = codeReader.peek(); ch != -1; ch = codeReader.peek()) {
// handle white space
if (Character.isWhitespace(ch)) {
codeReader.pop();
continue;
}
// handle special characters
switch(ch) {
case '=':
mode = ParseMode.BEFORE_ATTRIBUTE_VALUE;
codeReader.pop();
continue;
case '<':
nestedTag(element, codeReader, mode);
continue;
case '>':
case '/':
case '%':
case '@':
codeReader.pop();
continue;
default:
break;
}
mode = parseToken(mode, codeReader, element);
}
}
use of org.sonar.plugins.web.node.TagNode in project sonar-web by SonarSource.
the class ElementTokenizer method parseNestedTag.
/**
* Parse a nested tag with PageLexer.
* The nested tag is added as an attribute to its parent element.
*/
private static void parseNestedTag(CodeReader codeReader, TagNode element) {
PageLexer nestedPageLexer = new PageLexer();
List<Node> nodeList = nestedPageLexer.nestedParse(codeReader);
// add the nested tags as attribute.
for (Node node : nodeList) {
element.getAttributes().add(new Attribute(node.getCode()));
}
}
use of org.sonar.plugins.web.node.TagNode in project sonar-web by SonarSource.
the class PageLexer method createNodeHierarchy.
/**
* Scan the nodes and build the hierarchy of parent and child nodes.
*/
private static void createNodeHierarchy(List<Node> nodeList) {
TagNode current = null;
for (Node node : nodeList) {
if (node.getNodeType() == NodeType.TAG) {
TagNode element = (TagNode) node;
// start element
if (!element.isEndElement()) {
element.setParent(current);
current = element;
}
// end element
if ((element.isEndElement() || element.hasEnd()) && current != null) {
current = current.getParent();
}
}
}
}
use of org.sonar.plugins.web.node.TagNode in project sonar-web by SonarSource.
the class PageLexerTest method testLexer.
@Test
public void testLexer() throws FileNotFoundException {
String fileName = "src/test/resources/src/main/webapp/create-salesorder.xhtml";
PageLexer lexer = new PageLexer();
List<Node> nodeList = lexer.parse(new FileReader(fileName));
assertTrue(nodeList.size() > 50);
// check tagnodes
for (Node node : nodeList) {
if (node instanceof TagNode) {
assertTrue(node.getCode().startsWith("<"));
assertTrue(node.getCode().endsWith(">"));
}
}
showHierarchy(nodeList);
// check hierarchy
for (Node node : nodeList) {
if (node instanceof TagNode) {
TagNode tagNode = (TagNode) node;
if (!tagNode.isEndElement()) {
if (tagNode.equalsElementName("define")) {
assertTrue("Tag should have children: " + tagNode.getCode(), tagNode.getChildren().size() > 0);
} else if (tagNode.equalsElementName("outputText")) {
assertThat(tagNode.getChildren().size()).isEqualTo(0);
}
}
}
}
}
use of org.sonar.plugins.web.node.TagNode in project sonar-web by SonarSource.
the class PageLexerTest method testNestedTagInAttribute.
@Test
public void testNestedTagInAttribute() {
String fragment = "<td id=\"typeCellHeader\"<c:if test='${param.typeNormalOrError == \"error\"}'>" + "style=\"display:none;\"</c:if>>Type" + "</td>";
StringReader reader = new StringReader(fragment);
PageLexer lexer = new PageLexer();
List<Node> nodeList = lexer.parse(reader);
assertEquals(3, nodeList.size());
assertTrue(nodeList.get(0) instanceof TagNode);
assertTrue(nodeList.get(1) instanceof TextNode);
assertTrue(nodeList.get(2) instanceof TagNode);
TagNode tagNode = (TagNode) nodeList.get(0);
assertEquals(4, tagNode.getAttributes().size());
// the embedded tags are added as attributes
assertThat(tagNode.getAttributes().get(1).getValue()).isEmpty();
assertThat(tagNode.getAttributes().get(3).getValue()).isEmpty();
}
Aggregations