Search in sources :

Example 1 with WebApp

use of com.intellij.javaee.model.xml.web.WebApp in project intellij-plugins by JetBrains.

the class Struts2TilesModelProvider method computeModels.

@NotNull
public Collection<TilesModel> computeModels(@NotNull final Module module) {
    final Project project = module.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final GlobalSearchScope moduleScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, false);
    // struts2-tiles plugin must be available
    final PsiClass strutsTilesListenerClass = facade.findClass(STRUTS_TILES_LISTENER_CLASS, moduleScope);
    if (strutsTilesListenerClass == null) {
        return Collections.emptyList();
    }
    final PsiClass tilesListenerClass = facade.findClass(TilesConstants.TILES_LISTENER, moduleScope);
    final StrutsPluginDomFactory<TilesDefinitions, TilesModel> factory = StrutsProjectComponent.getInstance(project).getTilesFactory();
    final Set<TilesModel> struts2TilesModels = new HashSet<>();
    final Consumer<Set<XmlFile>> consumer = definitions -> {
        final DomFileElement<TilesDefinitions> domFileElement = factory.createMergedModelRoot(definitions);
        if (domFileElement != null) {
            struts2TilesModels.add(new TilesModelImpl(definitions, domFileElement, STRUTS2_TILES_MODEL));
        }
    };
    final WebDirectoryUtil webDirectoryUtil = WebDirectoryUtil.getWebDirectoryUtil(project);
    final Collection<WebFacet> webFacets = WebFacet.getInstances(module);
    for (final WebFacet webFacet : webFacets) {
        final WebApp webApp = webFacet.getRoot();
        if (webApp == null) {
            continue;
        }
        // determine configured tiles config files
        @NonNls final Set<String> tilesConfigNames = findConfiguredTilesPaths(webApp);
        // no configured paths? use default
        if (tilesConfigNames.isEmpty()) {
            tilesConfigNames.add(DEFAULT_TILES_XML);
        }
        // resolve to XmlFile
        final Set<XmlFile> tilesFileSet = new HashSet<>();
        for (final String tilesPath : tilesConfigNames) {
            final PsiElement tilesXmlFile = webDirectoryUtil.findFileByPath(tilesPath, webFacet);
            if (tilesXmlFile instanceof XmlFile) {
                tilesFileSet.add((XmlFile) tilesXmlFile);
            }
        }
        final List<Listener> listenerList = webApp.getListeners();
        for (final Listener listener : listenerList) {
            final PsiClass listenerClass = listener.getListenerClass().getValue();
            if (strutsTilesListenerClass.equals(listenerClass) || Comparing.equal(tilesListenerClass, listenerClass)) {
                consumer.consume(tilesFileSet);
            }
        }
    }
    return struts2TilesModels;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) WebFacet(com.intellij.javaee.web.facet.WebFacet) XmlFile(com.intellij.psi.xml.XmlFile) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) HashSet(com.intellij.util.containers.HashSet) NonNls(org.jetbrains.annotations.NonNls) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) StrutsProjectComponent(com.intellij.struts.StrutsProjectComponent) PsiClass(com.intellij.psi.PsiClass) Comparing(com.intellij.openapi.util.Comparing) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) TilesModelProvider(com.intellij.struts.TilesModelProvider) WebDirectoryUtil(com.intellij.psi.impl.source.jsp.WebDirectoryUtil) TilesDefinitions(com.intellij.struts.dom.tiles.TilesDefinitions) TilesModel(com.intellij.struts.TilesModel) TilesConstants(com.intellij.tiles.TilesConstants) StringUtil(com.intellij.openapi.util.text.StringUtil) TilesModelImpl(com.intellij.struts.psi.TilesModelImpl) Collection(java.util.Collection) DomFileElement(com.intellij.util.xml.DomFileElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) List(java.util.List) WebApp(com.intellij.javaee.model.xml.web.WebApp) ParamValue(com.intellij.javaee.model.xml.ParamValue) Listener(com.intellij.javaee.model.xml.Listener) NotNull(org.jetbrains.annotations.NotNull) StrutsPluginDomFactory(com.intellij.struts.StrutsPluginDomFactory) Collections(java.util.Collections) Consumer(com.intellij.util.Consumer) Condition(com.intellij.openapi.util.Condition) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) Set(java.util.Set) Listener(com.intellij.javaee.model.xml.Listener) TilesModelImpl(com.intellij.struts.psi.TilesModelImpl) DomFileElement(com.intellij.util.xml.DomFileElement) WebDirectoryUtil(com.intellij.psi.impl.source.jsp.WebDirectoryUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) NonNls(org.jetbrains.annotations.NonNls) XmlFile(com.intellij.psi.xml.XmlFile) PsiClass(com.intellij.psi.PsiClass) TilesDefinitions(com.intellij.struts.dom.tiles.TilesDefinitions) TilesModel(com.intellij.struts.TilesModel) Project(com.intellij.openapi.project.Project) WebFacet(com.intellij.javaee.web.facet.WebFacet) WebApp(com.intellij.javaee.model.xml.web.WebApp) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with WebApp

use of com.intellij.javaee.model.xml.web.WebApp 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 3 with WebApp

use of com.intellij.javaee.model.xml.web.WebApp in project intellij-plugins by JetBrains.

the class StrutsFrameworkSupportProvider method onFacetCreated.

@Override
protected void onFacetCreated(final StrutsFacet strutsFacet, final ModifiableRootModel modifiableRootModel, final FrameworkVersion version) {
    final Module module = strutsFacet.getModule();
    StartupManager.getInstance(module.getProject()).runWhenProjectIsInitialized(() -> DumbService.getInstance(module.getProject()).runWhenSmart(() -> {
        final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
        if (sourceRoots.length <= 0) {
            return;
        }
        final PsiDirectory directory = PsiManager.getInstance(module.getProject()).findDirectory(sourceRoots[0]);
        if (directory == null || directory.findFile(StrutsConstants.STRUTS_XML_DEFAULT_FILENAME) != null) {
            return;
        }
        final StrutsFileTemplateProvider templateProvider = new StrutsFileTemplateProvider(module);
        final FileTemplate strutsXmlTemplate = templateProvider.determineFileTemplate(directory.getProject());
        try {
            final StrutsFacetConfiguration strutsFacetConfiguration = strutsFacet.getConfiguration();
            // create empty struts.xml & fileset with all found struts-*.xml files (struts2.jar, plugins)
            final PsiElement psiElement = FileTemplateUtil.createFromTemplate(strutsXmlTemplate, StrutsConstants.STRUTS_XML_DEFAULT_FILENAME, null, directory);
            final Set<StrutsFileSet> empty = Collections.emptySet();
            final StrutsFileSet fileSet = new StrutsFileSet(StrutsFileSet.getUniqueId(empty), StrutsFileSet.getUniqueName("Default File Set", empty), strutsFacetConfiguration);
            fileSet.addFile(((XmlFile) psiElement).getVirtualFile());
            final StrutsConfigsSearcher searcher = new StrutsConfigsSearcher(module);
            DumbService.getInstance(module.getProject()).runWhenSmart(() -> searcher.search());
            final MultiMap<VirtualFile, PsiFile> jarConfigFiles = searcher.getJars();
            for (final VirtualFile virtualFile : jarConfigFiles.keySet()) {
                final Collection<PsiFile> psiFiles = jarConfigFiles.get(virtualFile);
                for (final PsiFile psiFile : psiFiles) {
                    fileSet.addFile(psiFile.getVirtualFile());
                }
            }
            strutsFacetConfiguration.getFileSets().add(fileSet);
            // create filter & mapping in web.xml (if present)
            new WriteCommandAction.Simple(modifiableRootModel.getProject()) {

                protected void run() throws Throwable {
                    final WebFacet webFacet = strutsFacet.getWebFacet();
                    final ConfigFile configFile = webFacet.getWebXmlDescriptor();
                    if (configFile == null)
                        return;
                    final XmlFile webXmlFile = configFile.getXmlFile();
                    final WebApp webApp = JamCommonUtil.getRootElement(webXmlFile, WebApp.class, null);
                    if (webApp == null)
                        return;
                    if (!FileModificationService.getInstance().prepareFileForWrite(webXmlFile))
                        return;
                    final Filter strutsFilter = webApp.addFilter();
                    strutsFilter.getFilterName().setStringValue("struts2");
                    @NonNls final String filterClass = templateProvider.is21orNewer() ? StrutsConstants.STRUTS_2_1_FILTER_CLASS : StrutsConstants.STRUTS_2_0_FILTER_CLASS;
                    strutsFilter.getFilterClass().setStringValue(filterClass);
                    final FilterMapping filterMapping = webApp.addFilterMapping();
                    filterMapping.getFilterName().setValue(strutsFilter);
                    filterMapping.addUrlPattern().setStringValue("/*");
                }
            }.execute();
            final NotificationListener showFacetSettingsListener = new NotificationListener() {

                public void hyperlinkUpdate(@NotNull final Notification notification, @NotNull final HyperlinkEvent event) {
                    if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                        notification.expire();
                        ModulesConfigurator.showFacetSettingsDialog(strutsFacet, null);
                    }
                }
            };
            Notifications.Bus.notify(new Notification("Struts 2", "Struts 2 Setup", "Struts 2 Facet has been created, please check <a href=\"more\">created fileset</a>", NotificationType.INFORMATION, showFacetSettingsListener), module.getProject());
        } catch (Exception e) {
            LOG.error("error creating struts.xml from template", e);
        }
    }));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet) HyperlinkEvent(javax.swing.event.HyperlinkEvent) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FilterMapping(com.intellij.javaee.model.xml.web.FilterMapping) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet) Notification(com.intellij.notification.Notification) StrutsFileTemplateProvider(com.intellij.struts2.StrutsFileTemplateProvider) MultiMap(com.intellij.util.containers.MultiMap) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) NonNls(org.jetbrains.annotations.NonNls) XmlFile(com.intellij.psi.xml.XmlFile) ConfigFile(com.intellij.util.descriptors.ConfigFile) StrutsConfigsSearcher(com.intellij.struts2.facet.ui.StrutsConfigsSearcher) Filter(com.intellij.javaee.model.xml.web.Filter) Collection(java.util.Collection) Module(com.intellij.openapi.module.Module) WebFacet(com.intellij.javaee.web.facet.WebFacet) WebApp(com.intellij.javaee.model.xml.web.WebApp) NotificationListener(com.intellij.notification.NotificationListener)

Aggregations

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