validation and fetching urls inside xsi:schemaLocation and xsi:noNamespaceSchemaLocation

This commit is contained in:
Maxim Mossienko
2005-03-14 20:52:07 +03:00
parent 089e4de7a7
commit ae1bdf0131
2 changed files with 67 additions and 7 deletions
@@ -347,12 +347,13 @@ public class XmlHighlightVisitor extends PsiElementVisitor implements Validator.
if (attribute.isNamespaceDeclaration()) {
checkNamespaceAttribute(attribute);
return;
}
if (attribute.getName().endsWith("Location")) {
} else if (attribute.getName().endsWith("Location")) {
final String namespace = attribute.getNamespace();
// TODO[ik]: check schema attributes
if (namespace.equals(XmlUtil.XML_SCHEMA_INSTANCE_URI)) return;
if (namespace.equals(XmlUtil.XML_SCHEMA_INSTANCE_URI)) {
checkSchemaLocationAttribute(attribute);
return;
}
}
final XmlElementDescriptor elementDescriptor = tag.getDescriptor();
@@ -569,11 +570,45 @@ public class XmlHighlightVisitor extends PsiElementVisitor implements Validator.
return;
}
// check if the namespace is defined
final XmlAttributeValue element = attribute.getValueElement();
if(element == null) return;
final int start = element.getTextRange().getStartOffset() + 1;
int end = element.getTextRange().getEndOffset() - 1;
reportURIProblem(start,end);
}
private void checkSchemaLocationAttribute(XmlAttribute attribute) {
if(attribute.getValueElement() == null) return;
String location = attribute.getValue();
if (attribute.getLocalName().equals("noNamespaceSchemaLocation")) {
if(XmlUtil.findXmlFile(attribute.getContainingFile(),location) == null) {
final int start = attribute.getValueElement().getTextOffset();
reportURIProblem(start,start + location.length());
}
} else if (attribute.getLocalName().equals("schemaLocation")) {
StringTokenizer tokenizer = new StringTokenizer(location);
XmlFile file = null;
while(tokenizer.hasMoreElements()) {
tokenizer.nextToken(); // skip namespace
if (!tokenizer.hasMoreElements()) return;
String url = tokenizer.nextToken();
if (file == null) {
file = (XmlFile)attribute.getContainingFile();
}
if(XmlUtil.findXmlFile(file,url) == null) {
final int start = attribute.getValueElement().getTextOffset() + location.indexOf(url);
reportURIProblem(start,start+url.length());
}
}
}
}
private void reportURIProblem(int start, int end) { // report the problem
if (start > end) {
end = start;
}
@@ -29,6 +29,7 @@ import java.net.URLConnection;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.lang.reflect.InvocationTargetException;
/**
@@ -66,13 +67,37 @@ public class FetchExtResourceAction extends BaseIntentionAction {
}
final XmlAttribute attribute = PsiTreeUtil.getParentOfType(currentElement, XmlAttribute.class);
if (attribute != null && attribute.isNamespaceDeclaration()) {
if(attribute == null) return null;
if (attribute.isNamespaceDeclaration()) {
final String uri = attribute.getValue();
final PsiElement parent = attribute.getParent();
if (uri != null && parent instanceof XmlTag && ((XmlTag)parent).getNSDescriptor(uri, true) == null) {
return uri;
}
} else if (attribute.getNamespace().equals(XmlUtil.XML_SCHEMA_INSTANCE_URI)) {
final String location = attribute.getValue();
if (attribute.getLocalName().equals("noNamespaceSchemaLocation")) {
if (XmlUtil.findXmlFile(file,location) == null) return location;
} else if (attribute.getLocalName().equals("schemaLocation")) {
StringTokenizer tokenizer = new StringTokenizer(location);
int offsetInAttr = offset - attribute.getValueElement().getTextOffset();
while(tokenizer.hasMoreElements()) {
tokenizer.nextToken(); // skip namespace
if (!tokenizer.hasMoreElements()) return null;
String url = tokenizer.nextToken();
int index = location.indexOf(url);
if (index <= offsetInAttr && index + url.length() >= offsetInAttr ) {
return url;
}
}
}
}
return null;
}