Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
MSG_UNKNOWN_CC_COMPONENT=The component library {0} does not contain component {1}
MSG_MISSING_REQUIRED_ATTRIBUTE=The required attribute {0} is missing
MSG_UNKNOWN_ATTRIBUTE=The attribute {0} is not defined in the component {1}
MSG_DUPLICATE_ATTRIBUTE=The attribute {0} is already declared on the component {1}

MSG_MISSING_LIBRARY=No library found for namespace {0}
MSG_UNDECLARED_COMPONENT=Undeclared component {0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.netbeans.modules.csl.api.Hint;
import org.netbeans.modules.csl.api.RuleContext;
import org.netbeans.modules.html.editor.api.gsf.HtmlErrorFilterContext;
Expand All @@ -37,7 +39,6 @@
import org.netbeans.modules.html.editor.lib.api.elements.OpenTag;
import org.netbeans.modules.parsing.api.Snapshot;
import org.netbeans.modules.web.jsf.editor.JsfUtils;
import static org.netbeans.modules.web.jsf.editor.hints.HintsProvider.ERROR_RULE_BADGING;
import org.netbeans.modules.web.jsfapi.api.Attribute;
import org.netbeans.modules.web.jsfapi.api.Library;
import org.netbeans.modules.web.jsfapi.api.LibraryComponent;
Expand Down Expand Up @@ -68,6 +69,7 @@ public List<Hint> compute(RuleContext context) {
// - whether the tag exists
// - if it has all the required attributes
// - if all used attributes are allowed
// - if there are duplicated attributes
private static List<Hint> checkCCCalls(final HtmlParserResult result) {
final List<Hint> hints = new ArrayList<>();
final Snapshot snapshot = result.getSnapshot();
Expand Down Expand Up @@ -125,7 +127,7 @@ public void visit(Element node) {
NbBundle.getMessage(HintsProvider.class, "MSG_UNKNOWN_CC_COMPONENT", lib.getDisplayName(), tagName),
snapshot.getSource().getFileObject(),
JsfUtils.createOffsetRange(snapshot, docText, node.from(), node.to()),
Collections.EMPTY_LIST, DEFAULT_ERROR_HINT_PRIORITY));
Collections.emptyList(), DEFAULT_ERROR_HINT_PRIORITY));

//put the hint to the close tag as well
CloseTag matchingCloseTag = openTag.matchingCloseTag();
Expand All @@ -134,56 +136,67 @@ public void visit(Element node) {
NbBundle.getMessage(HintsProvider.class, "MSG_UNKNOWN_CC_COMPONENT", lib.getDisplayName(), tagName),
snapshot.getSource().getFileObject(),
JsfUtils.createOffsetRange(snapshot, docText, matchingCloseTag.from(), matchingCloseTag.to()),
Collections.EMPTY_LIST, DEFAULT_ERROR_HINT_PRIORITY));
Collections.emptyList(), DEFAULT_ERROR_HINT_PRIORITY));
}

} else {
//check the component attributes
Tag tag = component.getTag();
if (tag != null) {
//Check wheter the tag has some non-generic (e.g. explicitly declared) attributes
if (!tag.hasNonGenenericAttributes()) {
//There aren't any declared attributes so we cannot do any attributes checks
//since facelets allows to not to declare the attributes in the descriptor, but
//use it in the facelets page. The engine then simply sets all the found
//attributes to the component without knowing if the component knows them or not.
return;
}
if (tag == null) {
//no tld, we cannot check much.
//btw, composite library w/o TLD simulates a TLD since can be reasonable parsed
return;
}
//Check wheter the tag has some non-generic (e.g. explicitly declared) attributes
if (!tag.hasNonGenenericAttributes()) {
//There aren't any declared attributes so we cannot do any attributes checks
//since facelets allows to not to declare the attributes in the descriptor, but
//use it in the facelets page. The engine then simply sets all the found
//attributes to the component without knowing if the component knows them or not.
return;
}

//1. check required attributes
Collection<Attribute> attrs = tag.getAttributes();
for (Attribute attr : attrs) {
if (attr.isRequired() && attr.getDefaultValue() == null) {
if (openTag.getAttribute(attr.getName()) == null) {
//missing required attribute
Hint hint = new Hint(ERROR_RULE_BADGING,
NbBundle.getMessage(HintsProvider.class, "MSG_MISSING_REQUIRED_ATTRIBUTE", attr.getName()),
snapshot.getSource().getFileObject(),
JsfUtils.createOffsetRange(snapshot, docText, node.from(), node.to()),
Collections.EMPTY_LIST, DEFAULT_ERROR_HINT_PRIORITY);
hints.add(hint);
}
}
//1. check required attributes
Collection<Attribute> attrs = tag.getAttributes();
for (Attribute attr : attrs) {
if (attr.isRequired() && attr.getDefaultValue() == null
&& openTag.getAttribute(attr.getName()) == null) {
//missing required attribute
Hint hint = new Hint(ERROR_RULE_BADGING,
NbBundle.getMessage(HintsProvider.class, "MSG_MISSING_REQUIRED_ATTRIBUTE", attr.getName()),
snapshot.getSource().getFileObject(),
JsfUtils.createOffsetRange(snapshot, docText, node.from(), node.to()),
Collections.emptyList(), DEFAULT_ERROR_HINT_PRIORITY);
hints.add(hint);
}
}

//2. check for unknown attributes
for (org.netbeans.modules.html.editor.lib.api.elements.Attribute nodeAttr : openTag.attributes()) {
//do not check attributes with a namespace
String nodeAttrName = nodeAttr.name().toString();
if (nodeAttr.namespacePrefix() == null && tag.getAttribute(nodeAttrName) == null && !"xmlns".equals(nodeAttrName.toLowerCase(Locale.ENGLISH))) {
//2. check for unknown attributes
//3. check for duplicate attributes
Set<String> seenAttributes = new HashSet<>();
for (org.netbeans.modules.html.editor.lib.api.elements.Attribute nodeAttr : openTag.attributes()) {
String nodeAttrName = nodeAttr.name().toString();
//do not check attributes with a namespace
if (nodeAttr.namespacePrefix() == null) {
if (tag.getAttribute(nodeAttrName) == null && !"xmlns".equalsIgnoreCase(nodeAttrName)) {
//unknown attribute
Hint hint = new Hint(ERROR_RULE_BADGING,
NbBundle.getMessage(HintsProvider.class, "MSG_UNKNOWN_ATTRIBUTE", nodeAttr.name(), tag.getName()),
snapshot.getSource().getFileObject(),
JsfUtils.createOffsetRange(snapshot, docText, nodeAttr.from(), nodeAttr.to()),
Collections.EMPTY_LIST, DEFAULT_ERROR_HINT_PRIORITY);
Collections.emptyList(), DEFAULT_ERROR_HINT_PRIORITY);
hints.add(hint);
}
}

} else {
//no tld, we cannot check much.
//btw, composite library w/o TLD simulates a TLD since can be reasonable parsed
if (!seenAttributes.add(nodeAttrName)) {
//duplicate attribute, flag only the repeated occurrence(s)
Hint hint = new Hint(ERROR_RULE_BADGING,
NbBundle.getMessage(HintsProvider.class, "MSG_DUPLICATE_ATTRIBUTE", nodeAttrName, tag.getName()),
snapshot.getSource().getFileObject(),
JsfUtils.createOffsetRange(snapshot, docText, nodeAttr.from(), nodeAttr.to()),
Collections.emptyList(), DEFAULT_ERROR_HINT_PRIORITY);
hints.add(hint);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>

</h:head>
<h:body>
<h:message/>

<h:message for="for"/>
</h:body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
<h:head>

</h:head>
<h:body>
<h:inputText id="id1" id="repeatedId"/>
<h:outputText pt:attr="xxx" pt:attr="yyy"/>
</h:body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>

</h:head>
<h:body>
<h:inputText id="id1" unknownAttribute="true"/>
</h:body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>

</h:head>
<h:body>
<h:unknownComponent>
</h:unknownComponent>

<h:inputText/>

<h:anotherUnknownComponent/>
</h:body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.netbeans.modules.web.jsf.editor.hints;

import java.util.List;

import org.netbeans.modules.csl.api.Hint;
import org.netbeans.modules.csl.api.HintSeverity;
import org.netbeans.modules.csl.api.RuleContext;
import org.netbeans.modules.web.jsf.editor.TestBaseForTestProject;
import org.netbeans.modules.web.jsfapi.api.Library;
import org.openide.util.NbBundle;

public class ComponentUsagesCheckerTest extends TestBaseForTestProject {

public ComponentUsagesCheckerTest(String name) {
super(name);
}

private List<Hint> computeHints(String relFilePath) throws Exception {
ParseResultInfo info = parse(relFilePath);
assertNotNull(info);
assertNotNull(info.result);
ComponentUsagesChecker checker = new ComponentUsagesChecker();
RuleContext context = new RuleContext();
context.parserResult = info.result;
return checker.compute(context);
}

public void testMissingRequiredAttribute() throws Exception {
List<Hint> hints = computeHints("testWebProject/web/hints/missing_attribute.xhtml");
assertNotNull(hints);
assertEquals(1, hints.size());
Hint hint = hints.get(0);
String expectedMessage = NbBundle.getMessage(HintsProvider.class, "MSG_MISSING_REQUIRED_ATTRIBUTE", "for");
assertEquals(expectedMessage, hint.getDescription());
assertEquals(HintSeverity.ERROR, hint.getRule().getDefaultSeverity());
assertNotNull(hint.getRange());
}

public void testRepeatedAttribute() throws Exception {
List<Hint> hints = computeHints("testWebProject/web/hints/repeated_attribute.xhtml");
assertNotNull(hints);
assertEquals(2, hints.size());
Hint hint1 = hints.get(0); // inputText -> id
String expectedMessage1 = NbBundle.getMessage(HintsProvider.class, "MSG_DUPLICATE_ATTRIBUTE", "id", "inputText");
assertEquals(expectedMessage1, hint1.getDescription());
assertEquals(HintSeverity.ERROR, hint1.getRule().getDefaultSeverity());
assertNotNull(hint1.getRange());
Hint hint2 = hints.get(1); // outputText -> pt:attr (prefixed)
String expectedMessage2 = NbBundle.getMessage(HintsProvider.class, "MSG_DUPLICATE_ATTRIBUTE", "pt:attr", "outputText");
assertEquals(expectedMessage2, hint2.getDescription());
assertEquals(HintSeverity.ERROR, hint2.getRule().getDefaultSeverity());
assertNotNull(hint2.getRange());
}

public void testUnknownAttribute() throws Exception {
List<Hint> hints = computeHints("testWebProject/web/hints/unknown_attribute.xhtml");
assertNotNull(hints);
assertEquals(1, hints.size());
Hint hint = hints.get(0);
String expectedMessage = NbBundle.getMessage(HintsProvider.class, "MSG_UNKNOWN_ATTRIBUTE", "unknownAttribute", "inputText");
assertEquals(expectedMessage, hint.getDescription());
assertEquals(HintSeverity.ERROR, hint.getRule().getDefaultSeverity());
assertNotNull(hint.getRange());
}

public void testUnknownComponent() throws Exception {
List<Hint> hints = computeHints("testWebProject/web/hints/unknown_component.xhtml");
assertNotNull(hints);
assertEquals(3, hints.size());
Library lib = getJsfSupportImpl().getLibrary("http://java.sun.com/jsf/html");
assertNotNull(lib);
String expectedUnknownComp = NbBundle.getMessage(HintsProvider.class, "MSG_UNKNOWN_CC_COMPONENT", lib.getDisplayName(), "unknownComponent");
assertEquals(expectedUnknownComp, hints.get(0).getDescription()); // h:unknownComponent starting tag
assertEquals(expectedUnknownComp, hints.get(1).getDescription()); // h:unknownComponent closing tag
String expectedAnotherUnknownComp = NbBundle.getMessage(HintsProvider.class, "MSG_UNKNOWN_CC_COMPONENT", lib.getDisplayName(), "anotherUnknownComponent");
assertEquals(expectedAnotherUnknownComp, hints.get(2).getDescription()); // <h:anotherUnknownComponent/>
hints.forEach(hint -> {
assertEquals(HintSeverity.ERROR, hint.getRule().getDefaultSeverity());
assertNotNull(hint.getRange());
});
}

}
Loading