use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class HTMLElementContentValidator method validateContent.
private void validateContent(Element parent, Node child) {
if (child == null)
return;
CMElementDeclaration ed = CMUtil.getDeclaration(parent);
if (ed == null || ed.getContentType() == CMElementDeclaration.ANY)
return;
List[] extendedContent = new List[1];
while (child != null) {
// perform actual validation
validateNode(parent, child, ed, extendedContent);
child = child.getNextSibling();
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class HTMLAttributeValidator method validate.
/**
*/
public void validate(IndexedRegion node) {
Element target = (Element) node;
if (CMUtil.isForeign(target))
return;
CMElementDeclaration edec = CMUtil.getDeclaration(target);
if (edec == null) {
NamedNodeMap attrs = target.getAttributes();
// unknown tag - go to validators from extension point
for (int i = 0; i < attrs.getLength(); i++) {
Attr a = (Attr) attrs.item(i);
final String attrName = a.getName().toLowerCase(Locale.US);
// Check for user-defined exclusions
if (shouldValidateAttributeName(target, attrName)) {
validateWithExtension(target, a, attrName);
}
}
} else {
CMNamedNodeMap declarations = edec.getAttributes();
List modelQueryNodes = null;
NamedNodeMap attrs = target.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
int rgnType = REGION_NAME;
int state = ErrorState.NONE_ERROR;
Attr a = (Attr) attrs.item(i);
// D203637; If the target attr has prefix, the validator should
// not
// warn about it. That is, just ignore. It is able to check
// whether
// an attr has prefix or not by calling XMLAttr#isGlobalAttr().
// When a attr has prefix (not global), it returns false.
boolean isXMLAttr = a instanceof IDOMAttr;
if (isXMLAttr) {
IDOMAttr xmlattr = (IDOMAttr) a;
if (!xmlattr.isGlobalAttr() || xmlattr.getNameRegion() instanceof ITextRegionContainer)
// skip futher validation and begin next loop.
continue;
}
CMAttributeDeclaration adec = (CMAttributeDeclaration) declarations.getNamedItem(a.getName());
final String attrName = a.getName().toLowerCase(Locale.US);
/* Check the modelquery if nothing is declared by the element declaration */
if (adec == null) {
if (modelQueryNodes == null)
modelQueryNodes = ModelQueryUtil.getModelQuery(target.getOwnerDocument()).getAvailableContent((Element) node, edec, ModelQuery.INCLUDE_ATTRIBUTES);
for (int k = 0; k < modelQueryNodes.size(); k++) {
CMNode cmnode = (CMNode) modelQueryNodes.get(k);
if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION && cmnode.getNodeName().toLowerCase(Locale.US).equals(attrName)) {
adec = (CMAttributeDeclaration) cmnode;
break;
}
}
}
if (adec == null) {
if ((attrName.startsWith(ATTR_NAME_DATA) && attrName.length() > ATTR_NAME_DATA_LENGTH) || (attrName.startsWith(ATTR_NAME_USER_AGENT_FEATURE) && attrName.length() > ATTR_NAME_USER_AGENT_FEATURE_LENGTH) || (attrName.startsWith(ATTR_NAME_WAI_ARIA) && attrName.length() > ATTR_NAME_WAI_ARIA_LENGTH)) {
if (isHTML5(target))
continue;
}
// Check for user-defined exclusions
if (!shouldValidateAttributeName(target, attrName))
continue;
// No attr declaration was found. That is, the attr name is
// undefined.
// but not regard it as undefined name if it includes nested
// region
// Then look into extension point for external validator
validateWithExtension(target, a, attrName);
} else {
// At 1st, the name should be checked.
if (CMUtil.isObsolete(adec)) {
state = ErrorState.OBSOLETE_ATTR_NAME_ERROR;
}
if (CMUtil.isHTML(edec) && (!CMUtil.isXHTML(edec))) {
// specifically.
if (CMUtil.isBooleanAttr(adec) && ((IDOMAttr) a).hasNameOnly())
// OK, keep going. No more check is needed
continue;
// against this attr.
} else {
// If the target is other than pure HTML (JSP or XHTML),
// the name
// must be checked exactly (ie in case sensitive way).
String actual = a.getName();
String desired = adec.getAttrName();
if (!actual.equals(desired)) {
// case mismatch
rgnType = REGION_NAME;
state = ErrorState.MISMATCHED_ERROR;
}
}
// Then, the value must be checked.
if (state == ErrorState.NONE_ERROR) {
// Need more check.
// Now, the value should be checked, if the type is ENUM.
CMDataType attrType = adec.getAttrType();
if (a instanceof IDOMAttr) {
final ITextRegion region = ((IDOMAttr) a).getEqualRegion();
if (region == null) {
rgnType = REGION_NAME;
state = ErrorState.MISSING_ATTR_VALUE_EQUALS_ERROR;
}
}
String actualValue = a.getValue();
if (attrType.getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED) {
// Check FIXED value.
String validValue = attrType.getImpliedValue();
if (!actualValue.equals(validValue)) {
rgnType = REGION_VALUE;
state = ErrorState.UNDEFINED_VALUE_ERROR;
}
} else if (CMDataType.URI.equals(attrType.getDataTypeName())) {
if (actualValue.indexOf('#') < 0 && actualValue.indexOf(":/") < 0 && !actualValue.toLowerCase(Locale.ENGLISH).startsWith(JAVASCRIPT_PREFIX) && CMUtil.isHTML(edec)) {
// $NON-NLS-1$ //$NON-NLS-2$
IStructuredDocumentRegion start = ((IDOMNode) node).getStartStructuredDocumentRegion();
// roundabout start tag check
if (start != null && start.getFirstRegion().getTextLength() == 1) {
// only check when we have a way to set dependencies
Collection dependencies = (Collection) ((IDOMNode) ((IDOMNode) node).getOwnerDocument()).getUserData(HTMLValidationAdapterFactory.DEPENDENCIES);
if (dependencies != null) {
IPath basePath = new Path(((IDOMNode) node).getModel().getBaseLocation());
if (basePath.segmentCount() > 1) {
IPath path = ModuleCoreSupport.resolve(basePath, actualValue);
IResource found = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
if (found != null) {
dependencies.add(found);
}
}
}
}
}
} else if (CMDataType.ENUM.equals(attrType.getDataTypeName())) {
/*
* Check current value is valid among a known list.
* There may be enumerated values provided even when
* the datatype is not ENUM, but we'll only validate
* against that list if the type matches.
*/
String[] enumeratedValues = attrType.getEnumeratedValues();
// several candidates are found.
boolean found = false;
for (int j = 0; j < enumeratedValues.length; j++) {
// At 1st, compare ignoring case.
if (actualValue.equalsIgnoreCase(enumeratedValues[j])) {
found = true;
if (CMUtil.isCaseSensitive(edec) && (!actualValue.equals(enumeratedValues[j]))) {
rgnType = REGION_VALUE;
state = ErrorState.MISMATCHED_VALUE_ERROR;
}
// exit the loop.
break;
}
}
if (!found) {
// retrieve and check extended values (retrieval can call extensions, which may take longer)
String[] modelQueryExtensionValues = ModelQueryUtil.getModelQuery(target.getOwnerDocument()).getPossibleDataTypeValues((Element) node, adec);
// copied loop from above
for (int j = 0; j < modelQueryExtensionValues.length; j++) {
// At 1st, compare ignoring case.
if (actualValue.equalsIgnoreCase(modelQueryExtensionValues[j])) {
found = true;
if (CMUtil.isCaseSensitive(edec) && (!actualValue.equals(modelQueryExtensionValues[j]))) {
rgnType = REGION_VALUE;
state = ErrorState.MISMATCHED_VALUE_ERROR;
}
// exit the loop.
break;
}
}
// includes nested region.
if (!hasNestedRegion(((IDOMNode) a).getValueRegion())) {
rgnType = REGION_VALUE;
state = ErrorState.UNDEFINED_VALUE_ERROR;
}
}
}
}
// <<D210422
if (state == ErrorState.NONE_ERROR) {
// Need more check.
if (isXMLAttr) {
String source = ((IDOMAttr) a).getValueRegionText();
if (source != null) {
char firstChar = source.charAt(0);
char lastChar = source.charAt(source.length() - 1);
boolean unclosedAttr = false;
if (isQuote(firstChar) || isQuote(lastChar)) {
if (lastChar != firstChar) {
unclosedAttr = true;
}
} else {
if (CMUtil.isXHTML(edec)) {
unclosedAttr = true;
}
}
if (unclosedAttr) {
rgnType = REGION_VALUE;
state = ErrorState.UNCLOSED_ATTR_VALUE;
}
}
}
}
// D210422
}
if (state != ErrorState.NONE_ERROR) {
Segment seg = getErrorSegment((IDOMNode) a, rgnType);
if (seg != null)
reporter.report(new ErrorInfoImpl(state, seg, a));
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class HMQUtil method getAncestorDeclarations.
private static Collection getAncestorDeclarations(Element target) {
Vector ancestors = new Vector();
Document doc = target.getOwnerDocument();
ModelQuery query = ModelQueryUtil.getModelQuery(doc);
CMElementDeclaration decl = query.getCMElementDeclaration(target);
ancestors.add(decl);
Element parent = getParent(target);
while (parent != null) {
decl = query.getCMElementDeclaration(parent);
if (decl != null)
ancestors.add(decl);
parent = getParent(parent);
}
return ancestors;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class HTMLFormatter method canInsertBreakAfter.
/**
*/
protected boolean canInsertBreakAfter(Node node) {
if (node == null)
return false;
Node parent = node.getParentNode();
if (parent == null)
return false;
Node next = node.getNextSibling();
// special exception if this node is a non-HTML tag (like JSP
// elements)
// BUG188093 - only preserve whitespace for jsp (not custom) tags
String prefix = node.getPrefix();
if (prefix != null && JSP.equals(prefix)) {
boolean canInsertBreakAfter = false;
// if a whitespace does not exist after it, do not add one
if (next != null && next.getNodeType() == Node.TEXT_NODE) {
String theText = ((Text) next).getData();
if (theText != null && theText.length() > 0) {
char theChar = theText.charAt(0);
canInsertBreakAfter = Character.isWhitespace(theChar);
}
}
// continue processing)
if (!canInsertBreakAfter)
return false;
}
// BUG188093 - only preserve whitespace for jsp (not custom) tags
if (next != null) {
prefix = next.getPrefix();
if (prefix != null && JSP.equals(prefix)) {
boolean canInsertBreakAfterPrevious = false;
// if a whitespace does not exist before it, do not add one
if (node.getNodeType() == Node.TEXT_NODE) {
String theText = ((Text) node).getData();
if (theText != null && theText.length() > 0) {
char theChar = theText.charAt(theText.length() - 1);
canInsertBreakAfterPrevious = Character.isWhitespace(theChar);
}
}
// continue processing)
if (!canInsertBreakAfterPrevious)
return false;
}
}
if (parent.getNodeType() == Node.DOCUMENT_NODE) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
// do not insert break after unclosed tag
if (!((IDOMElement) node).isClosed())
return false;
}
return true;
} else if (parent.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) parent;
// do not insert break before missing end tag
if (next == null && element.getEndStructuredDocumentRegion() == null)
return false;
// elements
if (element.getPrefix() != null)
return true;
CMElementDeclaration decl = getElementDeclaration(element);
if (decl != null) {
if (decl.getContentType() == CMElementDeclaration.ELEMENT)
return true;
// causes all closing tags to wrap to a new line
boolean allowsText = decl.getContentType() == CMElementDeclaration.MIXED || decl.getContentType() == CMElementDeclaration.PCDATA;
if (allowsNewlineAfter(allowsText, node, element, decl))
return true;
String tagName = element.getTagName();
// special for direct children under BODY
if (tagName != null && tagName.equalsIgnoreCase(BODY_NAME))
return true;
}
}
if (node.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) node;
CMElementDeclaration decl = getElementDeclaration(element);
if (canInsertBreakAfter(decl)) {
// spcial for BR
return canFormatChild(parent);
}
}
if (next != null && next.getNodeType() == Node.ELEMENT_NODE) {
CMElementDeclaration decl = getElementDeclaration((Element) next);
if (canInsertBreakBefore(decl))
return true;
}
return false;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class HTMLFormatter method canFormatChild.
/**
*/
protected boolean canFormatChild(Node node) {
while (node != null) {
if (node.getNodeType() != Node.ELEMENT_NODE)
return true;
CMElementDeclaration decl = getElementDeclaration((Element) node);
if (decl != null) {
if (decl.getContentType() == CMElementDeclaration.CDATA)
return false;
if (decl.supports(HTMLCMProperties.SHOULD_KEEP_SPACE)) {
boolean shouldKeepSpace = ((Boolean) decl.getProperty(HTMLCMProperties.SHOULD_KEEP_SPACE)).booleanValue();
if (shouldKeepSpace)
return false;
}
}
node = node.getParentNode();
}
return false;
}
Aggregations