private void reportOneTagProblem( final XmlTag tag, final String name, final String localizedMessage, final IntentionAction basicIntention, final HighlightDisplayKey key, final XmlEntitiesInspection inspection, final int type) { boolean htmlTag = false; if (tag instanceof HtmlTag) { htmlTag = true; if (isAdditionallyDeclared(inspection.getAdditionalEntries(type), name)) return; } final InspectionProfile profile = InspectionProjectProfileManager.getInstance(tag.getProject()).getInspectionProfile(); final IntentionAction intentionAction = inspection.getIntentionAction(name, type); if (htmlTag && profile.isToolEnabled(key, tag)) { addElementsForTagWithManyQuickFixes( tag, localizedMessage, isInjectedHtmlTagForWhichNoProblemsReporting((HtmlTag) tag) ? HighlightInfoType.INFORMATION : SeverityRegistrar.getInstance(tag.getProject()) .getHighlightInfoTypeBySeverity(profile.getErrorLevel(key, tag).getSeverity()), intentionAction, basicIntention); } else if (!htmlTag) { addElementsForTag(tag, localizedMessage, HighlightInfoType.ERROR, basicIntention); } }
private void doFix() throws IncorrectOperationException { final XmlTag tag = PsiTreeUtil.getParentOfType(myReference.getElement(), XmlTag.class); assert tag != null; final XmlTag defineTag = tag.createChildTag("define", ApplicationLoader.RNG_NAMESPACE, "\n \n", false); defineTag.setAttribute("name", myReference.getCanonicalText()); final RngGrammar grammar = ((DefinitionReference) myReference).getScope(); if (grammar == null) return; final XmlTag root = grammar.getXmlTag(); if (root == null) return; final XmlTag[] tags = root.getSubTags(); for (XmlTag xmlTag : tags) { if (PsiTreeUtil.isAncestor(xmlTag, tag, false)) { final XmlElementFactory ef = XmlElementFactory.getInstance(tag.getProject()); final XmlText text = ef.createDisplayText(" "); final PsiElement e = root.addAfter(text, xmlTag); root.addAfter(defineTag, e); return; } } root.add(defineTag); }
private CachedValue<TypeDescriptor> createAndPutTypesCachedValue( final XmlTag tag, final Pair<QNameKey, XmlTag> pair) { final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()) .createCachedValue( new CachedValueProvider<TypeDescriptor>() { public CachedValueProvider.Result<TypeDescriptor> compute() { final String name = tag.getAttributeValue("name"); if (name != null && pair.first != null && pair.first.first != null && !name.equals(XmlUtil.findLocalNameByQualifiedName(pair.first.first))) { myTypesMap.remove(pair); return new Result<TypeDescriptor>(null); } final ComplexTypeDescriptor complexTypeDescriptor = new ComplexTypeDescriptor(XmlNSDescriptorImpl.this, tag); return new Result<TypeDescriptor>(complexTypeDescriptor, tag); } }, false); myTypesMap.put(pair, value); return value; }
public void setText(String value) { try { XmlText text = null; if (StringUtil.isNotEmpty(value)) { final XmlText[] texts = getTextElements(); if (texts.length == 0) { text = (XmlText) myTag.add( XmlElementFactory.getInstance(myTag.getProject()).createDisplayText("x")); } else { text = texts[0]; } text.setValue(value); } if (myElements.length > 0) { for (final XmlTagChild child : myElements) { if (child != text) { child.delete(); } } } } catch (IncorrectOperationException e) { LOG.error(e); } }
private static void addAttributesBefore(XmlTag tag, List<Pair<String, String>> attr2value) { XmlAttribute firstAttribute = ArrayUtil.getFirstElement(tag.getAttributes()); XmlElementFactory factory = XmlElementFactory.getInstance(tag.getProject()); for (Pair<String, String> pair : attr2value) { XmlAttribute xmlAttribute = factory.createXmlAttribute(pair.first, ""); if (firstAttribute != null) { tag.addBefore(xmlAttribute, firstAttribute); } else { tag.add(xmlAttribute); } } }
private CachedValue<TypeDescriptor> createAndPutTypesCachedValueSimpleType( final XmlTag tag, final Pair<QNameKey, XmlTag> pair) { final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()) .createCachedValue( new CachedValueProvider<TypeDescriptor>() { public CachedValueProvider.Result<TypeDescriptor> compute() { final SimpleTypeDescriptor simpleTypeDescriptor = new SimpleTypeDescriptor(tag); return new Result<TypeDescriptor>(simpleTypeDescriptor, tag); } }, false); myTypesMap.put(pair, value); return value; }
private void checkRequiredAttributes( XmlTag tag, String name, XmlElementDescriptor elementDescriptor) { XmlAttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributesDescriptors(tag); Set<String> requiredAttributes = null; for (XmlAttributeDescriptor attribute : attributeDescriptors) { if (attribute != null && attribute.isRequired()) { if (requiredAttributes == null) { requiredAttributes = new HashSet<String>(); } requiredAttributes.add(attribute.getName(tag)); } } if (requiredAttributes != null) { for (final String attrName : requiredAttributes) { if (tag.getAttribute(attrName, "") == null && !XmlExtension.getExtension(tag.getContainingFile()) .isRequiredAttributeImplicitlyPresent(tag, attrName)) { final InsertRequiredAttributeFix insertRequiredAttributeIntention = new InsertRequiredAttributeFix(tag, attrName, null); final String localizedMessage = XmlErrorMessages.message("element.doesnt.have.required.attribute", name, attrName); final InspectionProfile profile = InspectionProjectProfileManager.getInstance(tag.getProject()).getInspectionProfile(); final LocalInspectionToolWrapper toolWrapper = (LocalInspectionToolWrapper) profile.getInspectionTool(RequiredAttributesInspection.SHORT_NAME, tag); if (toolWrapper != null) { RequiredAttributesInspection inspection = (RequiredAttributesInspection) toolWrapper.getTool(); reportOneTagProblem( tag, attrName, localizedMessage, insertRequiredAttributeIntention, HighlightDisplayKey.find(RequiredAttributesInspection.SHORT_NAME), inspection, XmlEntitiesInspection.NOT_REQUIRED_ATTRIBUTE); } } } } }
@Nullable public static <T extends DomElement> T findDomElement( @Nullable final PsiElement element, final Class<T> beanClass, boolean strict) { if (element == null) return null; XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, strict); DomElement domElement; while (tag != null) { domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag); if (domElement != null) { return domElement.getParentOfType(beanClass, false); } tag = tag.getParentTag(); } return null; }
private static void generateRaw(final @NotNull XmlTag newTag) { XmlElementDescriptor selected = newTag.getDescriptor(); if (selected == null) return; switch (selected.getContentType()) { case XmlElementDescriptor.CONTENT_TYPE_EMPTY: newTag.collapseIfEmpty(); ASTNode node = newTag.getNode(); assert node != null; ASTNode elementEnd = node.findChildByType(XmlTokenType.XML_EMPTY_ELEMENT_END); if (elementEnd == null) { LeafElement emptyTagEnd = Factory.createSingleLeafElement( XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, newTag.getManager()); node.addChild(emptyTagEnd); } break; case XmlElementDescriptor.CONTENT_TYPE_MIXED: newTag.getValue().setText(""); } for (XmlAttributeDescriptor descriptor : selected.getAttributesDescriptors(newTag)) { if (descriptor.isRequired()) { newTag.setAttribute(descriptor.getName(), ""); } } List<XmlElementDescriptor> tags = getRequiredSubTags(selected); for (XmlElementDescriptor descriptor : tags) { if (descriptor == null) { XmlTag tag = XmlElementFactory.getInstance(newTag.getProject()) .createTagFromText("<", newTag.getLanguage()); newTag.addSubTag(tag, false); } else { XmlTag subTag = newTag.addSubTag(createTag(newTag, descriptor), false); generateRaw(subTag); } } }
@Nullable public static LayoutViewElement getLayoutViewElement(@NotNull XmlTag tag) { final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag); return domElement instanceof LayoutViewElement ? (LayoutViewElement) domElement : null; }
private TypeDescriptor doFindIn( final XmlTag[] tags, final String name, final String namespace, final Pair<QNameKey, XmlTag> pair, final XmlTag rootTag, final Set<XmlTag> visited) { for (final XmlTag tag : tags) { if (equalsToSchemaName(tag, "complexType")) { if (name == null) { CachedValue<TypeDescriptor> value = createAndPutTypesCachedValue(tag, pair); return value.getValue(); } String nameAttribute = tag.getAttributeValue("name"); if (isSameName(name, namespace, nameAttribute)) { CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair); return cachedValue.getValue(); } } else if (equalsToSchemaName(tag, "simpleType")) { if (name == null) { CachedValue<TypeDescriptor> value = createAndPutTypesCachedValueSimpleType(tag, pair); return value.getValue(); } String nameAttribute = tag.getAttributeValue("name"); if (isSameName(name, namespace, nameAttribute)) { CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair); return cachedValue.getValue(); } } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace == null || !namespace.equals(getDefaultNamespace())))) { final String schemaLocation = tag.getAttributeValue("schemaLocation"); if (schemaLocation != null) { final XmlFile xmlFile = XmlUtil.findNamespaceByLocation(rootTag.getContainingFile(), schemaLocation); if (xmlFile != null) { final XmlDocument document = xmlFile.getDocument(); if (document != null) { final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()) .createCachedValue( new CachedValueProvider<TypeDescriptor>() { public Result<TypeDescriptor> compute() { final String currentName = tag.getAttributeValue("name"); if ((currentName != null && !currentName.equals( XmlUtil.findLocalNameByQualifiedName(name))) || !xmlFile.isValid() || xmlFile.getDocument() == null) { myTypesMap.remove(pair); return new Result<TypeDescriptor>(null); } final XmlDocument document = xmlFile.getDocument(); final XmlNSDescriptorImpl nsDescriptor = findNSDescriptor(tag, document); if (nsDescriptor == null) { myTypesMap.remove(pair); return new Result<TypeDescriptor>(null); } final XmlTag rTag = document.getRootTag(); final TypeDescriptor complexTypeDescriptor = nsDescriptor.findTypeDescriptorImpl( rTag, name, namespace, visited); return new Result<TypeDescriptor>(complexTypeDescriptor, rTag); } }, false); if (value.getValue() != null) { myTypesMap.put(pair, value); return value.getValue(); } } } } } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) { final XmlTag[] subTags = tag.getSubTags(); TypeDescriptor descriptor = doFindIn(subTags, name, namespace, pair, rootTag, visited); if (descriptor != null) return descriptor; final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag); if (nsDescriptor != null) { final XmlTag redefinedRootTag = ((XmlDocument) nsDescriptor.getDeclaration()).getRootTag(); descriptor = doFindIn( redefinedRootTag.getSubTags(), name, namespace, pair, redefinedRootTag, visited); if (descriptor != null) return descriptor; } } } return null; }
@Nullable public XmlElementDescriptor getElementDescriptor( String localName, String namespace, Set<XmlNSDescriptorImpl> visited, boolean reference) { if (visited.contains(this)) return null; final QNameKey pair = new QNameKey(namespace, localName); final CachedValue<XmlElementDescriptor> descriptor = myDescriptorsMap.get(pair); if (descriptor != null) { final XmlElementDescriptor value = descriptor.getValue(); if (value == null || value.getDeclaration().isValid()) return value; } final XmlTag rootTag = myTag; if (rootTag == null) return null; XmlTag[] tags = rootTag.getSubTags(); visited.add(this); for (final XmlTag tag : tags) { if (equalsToSchemaName(tag, ELEMENT_TAG_NAME)) { String name = tag.getAttributeValue("name"); if (name != null) { if (checkElementNameEquivalence(localName, namespace, name, tag)) { final CachedValue<XmlElementDescriptor> cachedValue = CachedValuesManager.getManager(tag.getProject()) .createCachedValue( new CachedValueProvider<XmlElementDescriptor>() { public Result<XmlElementDescriptor> compute() { final String name = tag.getAttributeValue("name"); if (name != null && !name.equals(pair.second)) { myDescriptorsMap.remove(pair); return new Result<XmlElementDescriptor>(null); } final XmlElementDescriptor xmlElementDescriptor = createElementDescriptor(tag); return new Result<XmlElementDescriptor>( xmlElementDescriptor, xmlElementDescriptor.getDependences()); } }, false); myDescriptorsMap.put(pair, cachedValue); return cachedValue.getValue(); } } } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (reference && equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace.equals(tag.getAttributeValue("namespace")) || namespace.length() == 0 && tag.getAttributeValue("namespace") == null))) { final XmlAttribute schemaLocation = tag.getAttribute("schemaLocation", null); if (schemaLocation != null) { final XmlFile xmlFile = XmlUtil.findNamespaceByLocation( rootTag.getContainingFile(), schemaLocation.getValue()); if (xmlFile != null) { final XmlDocument includedDocument = xmlFile.getDocument(); if (includedDocument != null) { final PsiMetaData data = includedDocument.getMetaData(); if (data instanceof XmlNSDescriptorImpl) { final XmlElementDescriptor elementDescriptor = ((XmlNSDescriptorImpl) data) .getElementDescriptor(localName, namespace, visited, reference); if (elementDescriptor != null) { // final CachedValue<XmlElementDescriptor> value = // includedDocument.getManager().getCachedValuesManager() // .createCachedValue(new CachedValueProvider<XmlElementDescriptor>() { // public Result<XmlElementDescriptor> compute() { // return new Result<XmlElementDescriptor>(elementDescriptor, // elementDescriptor.getDependences()); // } // }, false); // return value.getValue(); return elementDescriptor; } } } } } } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) { final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag); if (nsDescriptor != null) { final XmlElementDescriptor xmlElementDescriptor = nsDescriptor.getElementDescriptor(localName, namespace, visited, reference); if (xmlElementDescriptor != null) return xmlElementDescriptor; } } } return null; }
public void visitXmlTag(XmlTag tag) { final PsiFile file = tag.getContainingFile(); if (file.getFileType() != StdFileTypes.XML) { return; } if (!tag.getLocalName().equals("define")) { return; } if (!tag.getNamespace().equals(ApplicationLoader.RNG_NAMESPACE)) { return; } if (tag.getAttribute("combine") != null) { return; // ? } final XmlAttribute attr = tag.getAttribute("name"); if (attr == null) return; final XmlAttributeValue value = attr.getValueElement(); if (value == null) return; final String s = value.getValue(); if (s == null || s.length() == 0) { return; } final PsiElement parent = value.getParent(); if (!(parent instanceof XmlAttribute)) { return; } if (!"name".equals(((XmlAttribute) parent).getName())) { return; } final PsiElement grandParent = parent.getParent(); if (!(grandParent instanceof XmlTag)) { return; } final DomElement element = DomManager.getDomManager(tag.getProject()).getDomElement(tag); if (element == null) { return; } final RngGrammar rngGrammar = element.getParentOfType(RngGrammar.class, true); if (rngGrammar != null) { if (processUsages(tag, value, new LocalSearchScope(rngGrammar.getXmlTag()))) return; } else { if (processUsages(tag, value, new LocalSearchScope(file))) return; } final PsiElementProcessor.CollectElements<XmlFile> collector = new PsiElementProcessor.CollectElements<XmlFile>(); RelaxIncludeIndex.processBackwardDependencies((XmlFile) file, collector); if (processUsages(tag, value, new LocalSearchScope(collector.toArray()))) return; myHolder.registerProblem( value, "Unreferenced define", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new MyFix<XmlTag>(tag)); }