From ae1bdf013131eb3d5426ce198a9f6fe0fdf79aed Mon Sep 17 00:00:00 2001 From: Maxim Mossienko Date: Mon, 14 Mar 2005 20:52:07 +0300 Subject: [PATCH] validation and fetching urls inside xsi:schemaLocation and xsi:noNamespaceSchemaLocation --- .../impl/analysis/XmlHighlightVisitor.java | 47 ++++++++++++++++--- .../impl/quickfix/FetchExtResourceAction.java | 27 ++++++++++- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/source/com/intellij/codeInsight/daemon/impl/analysis/XmlHighlightVisitor.java b/source/com/intellij/codeInsight/daemon/impl/analysis/XmlHighlightVisitor.java index 6474fc189460..076528d0d47f 100644 --- a/source/com/intellij/codeInsight/daemon/impl/analysis/XmlHighlightVisitor.java +++ b/source/com/intellij/codeInsight/daemon/impl/analysis/XmlHighlightVisitor.java @@ -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; } diff --git a/source/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java b/source/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java index 2362973ed63a..5c939455b290 100644 --- a/source/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java +++ b/source/com/intellij/codeInsight/daemon/impl/quickfix/FetchExtResourceAction.java @@ -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; }