From 8437e0b4d3efefc93a2122814adb0476e7765eee Mon Sep 17 00:00:00 2001 From: Eugene Kudelevsky Date: Thu, 26 Aug 2010 19:53:41 +0400 Subject: [PATCH] WI-2883 do not visit included files, when generating dtd from xml document for completion/highlighting --- .../actions/GenerateDTDAction.java | 2 +- .../psi/impl/source/xml/XmlDocumentImpl.java | 2 +- .../src/com/intellij/xml/util/XmlUtil.java | 58 +++++++++++++------ 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/xml/impl/src/com/intellij/codeInsight/actions/GenerateDTDAction.java b/xml/impl/src/com/intellij/codeInsight/actions/GenerateDTDAction.java index 8f75e5a2318e..8ce1395c7b80 100644 --- a/xml/impl/src/com/intellij/codeInsight/actions/GenerateDTDAction.java +++ b/xml/impl/src/com/intellij/codeInsight/actions/GenerateDTDAction.java @@ -48,7 +48,7 @@ public class GenerateDTDAction extends BaseCodeInsightAction{ final XmlDocument document = ((XmlFile) file).getDocument(); if(document.getRootTag() != null){ buffer.append("\n"); XmlFile tempFile; try{ diff --git a/xml/impl/src/com/intellij/psi/impl/source/xml/XmlDocumentImpl.java b/xml/impl/src/com/intellij/psi/impl/source/xml/XmlDocumentImpl.java index 1b113a00ef23..6243d6312b4f 100644 --- a/xml/impl/src/com/intellij/psi/impl/source/xml/XmlDocumentImpl.java +++ b/xml/impl/src/com/intellij/psi/impl/source/xml/XmlDocumentImpl.java @@ -225,7 +225,7 @@ public class XmlDocumentImpl extends XmlElementImpl implements XmlDocument { } try { final PsiFile fileFromText = PsiFileFactory.getInstance(getProject()) - .createFileFromText(containingFile.getName() + ".dtd", XmlUtil.generateDocumentDTD(this)); + .createFileFromText(containingFile.getName() + ".dtd", XmlUtil.generateDocumentDTD(this, false)); if (fileFromText instanceof XmlFile) { return (XmlNSDescriptor)((XmlFile)fileFromText).getDocument().getMetaData(); } diff --git a/xml/impl/src/com/intellij/xml/util/XmlUtil.java b/xml/impl/src/com/intellij/xml/util/XmlUtil.java index 3b6f19448cd4..22fce1c42387 100644 --- a/xml/impl/src/com/intellij/xml/util/XmlUtil.java +++ b/xml/impl/src/com/intellij/xml/util/XmlUtil.java @@ -320,7 +320,16 @@ public class XmlUtil { final boolean deepFlag, final boolean wideFlag, final PsiFile baseFile) { - return new XmlElementProcessor(processor, baseFile).processXmlElements(element, deepFlag, wideFlag); + return processXmlElements(element, processor, deepFlag, wideFlag, baseFile, true); + } + + public static boolean processXmlElements(final XmlElement element, + final PsiElementProcessor processor, + final boolean deepFlag, + final boolean wideFlag, + final PsiFile baseFile, + boolean processIncludes) { + return new XmlElementProcessor(processor, baseFile).processXmlElements(element, deepFlag, wideFlag, processIncludes); } public static boolean processXmlElementChildren(final XmlElement element, final PsiElementProcessor processor, final boolean deepFlag) { @@ -328,7 +337,7 @@ public class XmlUtil { final boolean wideFlag = false; for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { - if (!p.processElement(child, deepFlag, wideFlag) && !wideFlag) return false; + if (!p.processElement(child, deepFlag, wideFlag, true) && !wideFlag) return false; } return true; @@ -557,7 +566,7 @@ public class XmlUtil { targetFile = _targetFile; } - private boolean processXmlElements(PsiElement element, boolean deepFlag, boolean wideFlag) { + private boolean processXmlElements(PsiElement element, boolean deepFlag, boolean wideFlag, boolean processIncludes) { if (deepFlag) if (!processor.execute(element)) return false; PsiElement startFrom = element.getFirstChild(); @@ -575,7 +584,7 @@ public class XmlUtil { //} while (newElement != null) { - if (!processElement(newElement, deepFlag, wideFlag)) return false; + if (!processElement(newElement, deepFlag, wideFlag, processIncludes)) return false; newElement = newElement.getNextSibling(); } @@ -586,13 +595,13 @@ public class XmlUtil { if (!xmlConditionalSection.isIncluded(targetFile)) return true; startFrom = xmlConditionalSection.getBodyStart(); } - else if (XmlIncludeHandler.isXInclude(element)) { + else if (processIncludes && XmlIncludeHandler.isXInclude(element)) { XmlTag tag = (XmlTag)element; if (!processXInclude(deepFlag, wideFlag, tag)) return false; } for (PsiElement child = startFrom; child != null; child = child.getNextSibling()) { - if (!processElement(child, deepFlag, wideFlag) && !wideFlag) return false; + if (!processElement(child, deepFlag, wideFlag, processIncludes) && !wideFlag) return false; } return true; @@ -610,7 +619,7 @@ public class XmlUtil { if (inclusion != null) { for (PsiElement psiElement : inclusion) { - if (!processElement(psiElement, deepFlag, wideFlag)) return false; + if (!processElement(psiElement, deepFlag, wideFlag, true)) return false; } } @@ -666,21 +675,21 @@ public class XmlUtil { return new XmlTag[]{rootTag}; } - private boolean processElement(PsiElement child, boolean deepFlag, boolean wideFlag) { + private boolean processElement(PsiElement child, boolean deepFlag, boolean wideFlag, boolean processIncludes) { if (deepFlag) { - if (!processXmlElements(child, true, wideFlag)) { + if (!processXmlElements(child, true, wideFlag, processIncludes)) { return false; } } else { if (child instanceof XmlEntityRef) { - if (!processXmlElements(child, false, wideFlag)) return false; + if (!processXmlElements(child, false, wideFlag, processIncludes)) return false; } else if (child instanceof XmlConditionalSection) { - if (!processXmlElements(child, false, wideFlag)) return false; + if (!processXmlElements(child, false, wideFlag, processIncludes)) return false; } - else if (XmlIncludeHandler.isXInclude(child)) { - if (!processXmlElements(child, false, wideFlag)) return false; + else if (processIncludes && XmlIncludeHandler.isXInclude(child)) { + if (!processXmlElements(child, false, wideFlag, processIncludes)) return false; } else if (!processor.execute(child)) return false; } @@ -984,7 +993,8 @@ public class XmlUtil { private static void computeTag(XmlTag tag, final Map> tagsMap, - final Map> attributesMap) { + final Map> attributesMap, + final boolean processIncludes) { if (tag == null) { return; } @@ -1042,7 +1052,17 @@ public class XmlUtil { attributesMap.put(tagName, list); final List tags = tagsMap.get(tagName) != null ? tagsMap.get(tagName) : new ArrayList(); tagsMap.put(tagName, tags); - tag.processElements(new FilterElementProcessor(XmlTagFilter.INSTANCE) { + PsiFile file = tag.isValid() ? tag.getContainingFile() : null; + processXmlElements(tag, new FilterElementProcessor(XmlTagFilter.INSTANCE) { + public void add(PsiElement element) { + XmlTag tag = (XmlTag)element; + if (!tags.contains(tag.getName())) { + tags.add(tag.getName()); + } + computeTag(tag, tagsMap, attributesMap, processIncludes); + } + }, false, false, file, processIncludes); + /*tag.processElements(new FilterElementProcessor(XmlTagFilter.INSTANCE) { public void add(PsiElement element) { XmlTag tag = (XmlTag)element; if (!tags.contains(tag.getName())) { @@ -1050,7 +1070,7 @@ public class XmlUtil { } computeTag(tag, tagsMap, attributesMap); } - }, tag); + }, tag);*/ } @Nullable @@ -1332,19 +1352,19 @@ public class XmlUtil { } } - public static String generateDocumentDTD(XmlDocument doc) { + public static String generateDocumentDTD(XmlDocument doc, boolean full) { final Map> tags = new LinkedHashMap>(); final Map> attributes = new LinkedHashMap>(); try { XmlEntityRefImpl.setNoEntityExpandOutOfDocument(doc, true); final XmlTag rootTag = doc.getRootTag(); - computeTag(rootTag, tags, attributes); + computeTag(rootTag, tags, attributes, full); // For supporting not welformed XML for (PsiElement element = rootTag != null ? rootTag.getNextSibling() : null; element != null; element = element.getNextSibling()) { if (element instanceof XmlTag) { - computeTag((XmlTag)element, tags, attributes); + computeTag((XmlTag)element, tags, attributes, full); } } }