use of com.intellij.struts2.dom.struts.StrutsRoot 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;
}
use of com.intellij.struts2.dom.struts.StrutsRoot in project intellij-plugins by JetBrains.
the class Struts2ModelInspection method checkFileElement.
/**
* Only inspect struts.xml files configured in fileset.
*
* @param strutsRootDomFileElement Root element of file to inspect.
* @param holder Holder.
*/
public void checkFileElement(final DomFileElement<StrutsRoot> strutsRootDomFileElement, final DomElementAnnotationHolder holder) {
final Module module = strutsRootDomFileElement.getModule();
if (module == null) {
return;
}
final XmlFile xmlFile = strutsRootDomFileElement.getFile();
final VirtualFile virtualFile = xmlFile.getVirtualFile();
final Set<StrutsFileSet> fileSets = StrutsManager.getInstance(xmlFile.getProject()).getAllConfigFileSets(module);
for (final StrutsFileSet strutsFileSet : fileSets) {
if (strutsFileSet.hasFile(virtualFile)) {
super.checkFileElement(strutsRootDomFileElement, holder);
break;
}
}
}
use of com.intellij.struts2.dom.struts.StrutsRoot in project intellij-plugins by JetBrains.
the class StrutsConstantManagerImpl method getConvertedValue.
@Override
@Nullable
public <T> T getConvertedValue(@NotNull final PsiElement context, @NotNull final StrutsConstantKey<T> strutsConstantKey) {
final PsiFile containingFile = context.getContainingFile();
final StrutsModel strutsModel = getStrutsModel(containingFile);
if (strutsModel == null) {
return null;
}
final String stringValue = getStringValue(containingFile, strutsModel, strutsConstantKey.getKey());
if (stringValue == null) {
return null;
}
final Converter<T> converter = findConverter(context, strutsConstantKey);
if (converter == null) {
//noinspection unchecked
return (T) stringValue;
}
final DomFileElement<StrutsRoot> first = strutsModel.getRoots().iterator().next();
final ConvertContext convertContext = ConvertContextFactory.createConvertContext(first);
//noinspection unchecked
return converter.fromString(stringValue, convertContext);
}
use of com.intellij.struts2.dom.struts.StrutsRoot in project intellij-plugins by JetBrains.
the class StrutsModelFactory method computeAllModels.
protected List<StrutsModel> computeAllModels(@NotNull final Module module) {
final PsiManager psiManager = PsiManager.getInstance(module.getProject());
final StrutsManager strutsManager = StrutsManager.getInstance(module.getProject());
final Set<StrutsFileSet> fileSets = strutsManager.getAllConfigFileSets(module);
final List<StrutsModel> models = new ArrayList<>(fileSets.size());
for (final StrutsFileSet set : fileSets) {
if (set.isRemoved()) {
continue;
}
final Set<XmlFile> files = new LinkedHashSet<>(set.getFiles().size());
for (final VirtualFilePointer filePointer : set.getFiles()) {
if (!filePointer.isValid())
continue;
final VirtualFile file = filePointer.getFile();
if (file == null) {
continue;
}
final PsiFile psiFile = psiManager.findFile(file);
if (psiFile instanceof XmlFile) {
final StrutsRoot strutsRootDom = getDom((XmlFile) psiFile);
if (strutsRootDom != null) {
files.add((XmlFile) psiFile);
// TODO addIncludes(files, strutsRootDom);
}
}
}
if (!files.isEmpty()) {
final DomFileElement<StrutsRoot> element = createMergedModelRoot(files);
final StrutsModel model;
if (element != null) {
model = new StrutsModelImpl(element, files);
models.add(model);
}
}
}
return models;
}
Aggregations