Search in sources :

Example 6 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-plugins by JetBrains.

the class StrutsConstantManagerImpl method getStringValue.

/**
   * Returns the plain String value for the given constant.
   *
   * @param context     Current context.
   * @param strutsModel StrutsModel.
   * @param name        Name of constant.
   * @return {@code null} if no value could be resolved.
   */
@Nullable
private static String getStringValue(@NotNull final PsiFile context, @NotNull final StrutsModel strutsModel, @NotNull @NonNls final String name) {
    final Project project = context.getProject();
    final Module module = ModuleUtilCore.findModuleForPsiElement(context);
    assert module != null : context;
    // collect all properties with matching key
    final List<IProperty> properties = PropertiesImplUtil.findPropertiesByKey(project, name);
    String value = null;
    // 1. default.properties from struts2-core.jar
    final IProperty strutsDefaultProperty = ContainerUtil.find(properties, property -> {
        final VirtualFile virtualFile = property.getPropertiesFile().getVirtualFile();
        return virtualFile != null && virtualFile.getFileSystem() instanceof JarFileSystem && StringUtil.endsWith(virtualFile.getPath(), STRUTS_DEFAULT_PROPERTIES) && ModuleUtilCore.moduleContainsFile(module, virtualFile, true);
    });
    if (strutsDefaultProperty != null) {
        value = strutsDefaultProperty.getValue();
    }
    // 2. <constant> from StrutsModel
    final Condition<Constant> constantNameCondition = constant -> Comparing.equal(constant.getName().getStringValue(), name);
    final List<DomFileElement<StrutsRoot>> domFileElements = new ArrayList<>();
    collectStrutsXmls(domFileElements, strutsModel, "struts-default.xml", true);
    collectStrutsXmls(domFileElements, strutsModel, "struts-plugin.xml", true);
    collectStrutsXmls(domFileElements, strutsModel, "struts.xml", false);
    for (final DomFileElement<StrutsRoot> domFileElement : domFileElements) {
        final Constant constant = ContainerUtil.find(domFileElement.getRootElement().getConstants(), constantNameCondition);
        final String strutsXmlValue = constant != null ? constant.getValue().getStringValue() : null;
        if (strutsXmlValue != null) {
            value = strutsXmlValue;
        }
    }
    // 3. struts.properties in current module
    final IProperty strutsProperty = ContainerUtil.find(properties, property -> {
        final VirtualFile virtualFile = property.getPropertiesFile().getVirtualFile();
        return virtualFile != null && Comparing.equal(virtualFile.getName(), STRUTS_PROPERTIES_FILENAME) && ModuleUtilCore.moduleContainsFile(module, virtualFile, false);
    });
    if (strutsProperty != null) {
        value = strutsProperty.getValue();
    }
    // 4. web.xml
    final WebFacet webFacet = WebUtil.getWebFacet(context);
    if (webFacet == null) {
        // should not happen in real projects..
        return value;
    }
    final WebApp webApp = webFacet.getRoot();
    if (webApp == null) {
        // no web.xml
        return value;
    }
    final Filter filter = ContainerUtil.find(webApp.getFilters(), WEB_XML_STRUTS_FILTER_CONDITION);
    if (filter != null) {
        final ParamValue initParam = ContainerUtil.find(filter.getInitParams(), (Condition<ParamValue>) paramValue -> Comparing.equal(paramValue.getParamName().getStringValue(), name));
        if (initParam != null) {
            value = initParam.getParamValue().getStringValue();
        }
    }
    return value;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WebFacet(com.intellij.javaee.web.facet.WebFacet) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) ArrayList(java.util.ArrayList) FilteringProcessor(com.intellij.util.FilteringProcessor) PsiClass(com.intellij.psi.PsiClass) StrutsRoot(com.intellij.struts2.dom.struts.StrutsRoot) Comparing(com.intellij.openapi.util.Comparing) PsiElement(com.intellij.psi.PsiElement) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) WebUtil(com.intellij.javaee.web.WebUtil) Module(com.intellij.openapi.module.Module) StrutsConstants(com.intellij.struts2.StrutsConstants) CommonProcessors(com.intellij.util.CommonProcessors) Converter(com.intellij.util.xml.Converter) Extensions(com.intellij.openapi.extensions.Extensions) Constant(com.intellij.struts2.dom.struts.constant.Constant) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) DomFileElement(com.intellij.util.xml.DomFileElement) ConvertContextFactory(com.intellij.util.xml.impl.ConvertContextFactory) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PropertiesImplUtil(com.intellij.lang.properties.PropertiesImplUtil) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) WebApp(com.intellij.javaee.model.xml.web.WebApp) ParamValue(com.intellij.javaee.model.xml.ParamValue) ConvertContext(com.intellij.util.xml.ConvertContext) NotNull(org.jetbrains.annotations.NotNull) Filter(com.intellij.javaee.model.xml.web.Filter) Collections(java.util.Collections) Condition(com.intellij.openapi.util.Condition) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) Constant(com.intellij.struts2.dom.struts.constant.Constant) ArrayList(java.util.ArrayList) DomFileElement(com.intellij.util.xml.DomFileElement) ParamValue(com.intellij.javaee.model.xml.ParamValue) Project(com.intellij.openapi.project.Project) IProperty(com.intellij.lang.properties.IProperty) Filter(com.intellij.javaee.model.xml.web.Filter) StrutsRoot(com.intellij.struts2.dom.struts.StrutsRoot) Module(com.intellij.openapi.module.Module) WebFacet(com.intellij.javaee.web.facet.WebFacet) WebApp(com.intellij.javaee.model.xml.web.WebApp) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-community by JetBrains.

the class InspectionDescriptionInfo method doFindExtension.

@Nullable
private static Extension doFindExtension(Module module, PsiClass psiClass) {
    // Try search in narrow scopes first
    Project project = module.getProject();
    Set<DomFileElement<IdeaPlugin>> processed = new HashSet<>();
    for (GlobalSearchScope scope : DescriptionCheckerUtil.searchScopes(module)) {
        List<DomFileElement<IdeaPlugin>> origElements = DomService.getInstance().getFileElements(IdeaPlugin.class, project, scope);
        origElements.removeAll(processed);
        List<DomFileElement<IdeaPlugin>> elements = PluginDescriptorChooser.findAppropriateIntelliJModule(module.getName(), origElements);
        Query<PsiReference> query = ReferencesSearch.search(psiClass, new LocalSearchScope(elements.stream().map(DomFileElement::getFile).toArray(PsiElement[]::new)));
        Ref<Extension> result = Ref.create(null);
        query.forEach(ref -> {
            PsiElement element = ref.getElement();
            if (element instanceof XmlAttributeValue) {
                PsiElement parent = element.getParent();
                if (parent instanceof XmlAttribute && "implementationClass".equals(((XmlAttribute) parent).getName())) {
                    DomElement domElement = DomUtil.getDomElement(parent.getParent());
                    if (domElement instanceof Extension) {
                        Extension extension = (Extension) domElement;
                        ExtensionPoint extensionPoint = extension.getExtensionPoint();
                        if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), InspectionEP.class.getName())) {
                            result.set(extension);
                            return false;
                        }
                    }
                }
            }
            return true;
        });
        Extension extension = result.get();
        if (extension != null)
            return extension;
        processed.addAll(origElements);
    }
    return null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ExtensionPoint(org.jetbrains.idea.devkit.dom.ExtensionPoint) DomFileElement(com.intellij.util.xml.DomFileElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Extension(org.jetbrains.idea.devkit.dom.Extension) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-community by JetBrains.

the class CachedMultipleDomModelFactory method computeCombinedModel.

@Nullable
protected M computeCombinedModel(@NotNull Scope scope) {
    final List<M> models = getAllModels(scope);
    switch(models.size()) {
        case 0:
            return null;
        case 1:
            return models.get(0);
    }
    final Set<XmlFile> configFiles = new LinkedHashSet<>();
    final LinkedHashSet<DomFileElement<T>> list = new LinkedHashSet<>(models.size());
    for (M model : models) {
        final Set<XmlFile> files = model.getConfigFiles();
        for (XmlFile file : files) {
            ContainerUtil.addIfNotNull(list, getDomRoot(file));
        }
        configFiles.addAll(files);
    }
    final DomFileElement<T> mergedModel = getModelMerger().mergeModels(DomFileElement.class, list);
    final M firstModel = models.get(0);
    return createCombinedModel(configFiles, mergedModel, firstModel, scope);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) DomFileElement(com.intellij.util.xml.DomFileElement) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-community by JetBrains.

the class DeleteDomElement method update.

@Override
public void update(AnActionEvent e, DomModelTreeView treeView) {
    final SimpleNode selectedNode = treeView.getTree().getSelectedNode();
    if (selectedNode instanceof DomFileElementNode) {
        e.getPresentation().setVisible(false);
        return;
    }
    boolean enabled = false;
    if (selectedNode instanceof BaseDomElementNode) {
        final DomElement domElement = ((BaseDomElementNode) selectedNode).getDomElement();
        if (domElement.isValid() && DomUtil.hasXml(domElement) && !(domElement.getParent() instanceof DomFileElement)) {
            enabled = true;
        }
    }
    e.getPresentation().setEnabled(enabled);
    if (enabled) {
        e.getPresentation().setText(getPresentationText(selectedNode, ApplicationBundle.message("action.remove")));
    } else {
        e.getPresentation().setText(ApplicationBundle.message("action.remove"));
    }
    e.getPresentation().setIcon(AllIcons.General.Remove);
}
Also used : BaseDomElementNode(com.intellij.util.xml.tree.BaseDomElementNode) DomElement(com.intellij.util.xml.DomElement) DomFileElementNode(com.intellij.util.xml.tree.DomFileElementNode) DomFileElement(com.intellij.util.xml.DomFileElement) SimpleNode(com.intellij.ui.treeStructure.SimpleNode)

Example 10 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-plugins by JetBrains.

the class ValidatorManagerImpl method findValidationFilesFor.

@NotNull
@Override
public List<XmlFile> findValidationFilesFor(@NotNull final PsiClass clazz) {
    final PsiFile psiFile = clazz.getContainingFile().getOriginalFile();
    final PsiDirectory containingDirectory = psiFile.getContainingDirectory();
    if (containingDirectory == null) {
        return Collections.emptyList();
    }
    final PsiPackage containingPackage = JavaDirectoryService.getInstance().getPackage(containingDirectory);
    if (containingPackage == null) {
        return Collections.emptyList();
    }
    final PackageScope searchScope = new PackageScope(containingPackage, false, true);
    final List<DomFileElement<Validators>> validationRoots = DomService.getInstance().getFileElements(Validators.class, clazz.getProject(), searchScope);
    final List<DomFileElement<Validators>> filtered = ContainerUtil.filter(validationRoots, validatorDomFileElement -> {
        final String fileName = validatorDomFileElement.getFile().getName();
        return StringUtil.startsWith(fileName, clazz.getName());
    });
    return ContainerUtil.map(filtered, validatorsDomFileElement -> validatorsDomFileElement.getFile());
}
Also used : PackageScope(com.intellij.psi.search.PackageScope) DomFileElement(com.intellij.util.xml.DomFileElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DomFileElement (com.intellij.util.xml.DomFileElement)10 NotNull (org.jetbrains.annotations.NotNull)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 Nullable (org.jetbrains.annotations.Nullable)4 Comparing (com.intellij.openapi.util.Comparing)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 XmlFile (com.intellij.psi.xml.XmlFile)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 Collections (java.util.Collections)3 List (java.util.List)3 ParamValue (com.intellij.javaee.model.xml.ParamValue)2 WebApp (com.intellij.javaee.model.xml.web.WebApp)2 WebFacet (com.intellij.javaee.web.facet.WebFacet)2 ModuleGrouper (com.intellij.openapi.module.ModuleGrouper)2 ModuleUtilCore (com.intellij.openapi.module.ModuleUtilCore)2 Condition (com.intellij.openapi.util.Condition)2 PsiClass (com.intellij.psi.PsiClass)2