use of com.intellij.util.xml.DomElement in project android by JetBrains.
the class AndroidInlineStyleReferenceAction method doRefactorForTags.
@Override
protected void doRefactorForTags(@NotNull Project project, @NotNull final XmlTag[] tags) {
assert tags.length == 1;
final XmlTag tag = tags[0];
final PsiFile file = tag.getContainingFile();
if (file == null) {
return;
}
final StyleUsageData usageData = AndroidInlineUtil.getStyleUsageData(tag);
if (usageData == null) {
return;
}
final AndroidResourceReferenceBase reference = usageData.getReference();
final String title = AndroidBundle.message("android.inline.style.title");
final PsiElement[] styleElements = reference.computeTargetElements();
if (styleElements.length == 0) {
AndroidUtils.reportError(project, "Cannot find style by reference '" + reference.getValue() + "'", title);
return;
}
if (styleElements.length > 1) {
AndroidUtils.reportError(project, RefactoringBundle.getCannotRefactorMessage("Unambiguous style reference."), title);
return;
}
final PsiElement styleElement = styleElements[0];
final XmlTag styleTag = PsiTreeUtil.getParentOfType(styleElement, XmlTag.class);
final DomElement domElement = styleTag != null ? DomManager.getDomManager(project).getDomElement(styleTag) : null;
if (!(domElement instanceof Style)) {
AndroidUtils.reportError(project, "Cannot find style by reference '" + reference.getValue() + "'", title);
return;
}
final Style style = (Style) domElement;
String styleName = style.getName().getStringValue();
if (styleName == null) {
AndroidUtils.reportError(project, RefactoringBundle.getCannotRefactorMessage("Style name is not specified."), title);
return;
}
AndroidInlineUtil.doInlineStyleDeclaration(project, new AndroidInlineUtil.MyStyleData(styleName, style, styleElement), usageData, new ProjectBasedErrorReporter(project), myTestConfig);
}
use of com.intellij.util.xml.DomElement in project android by JetBrains.
the class AndroidInlineUtil method getStyleUsageData.
@Nullable
static StyleUsageData getStyleUsageData(@NotNull XmlTag tag) {
final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
if (domElement instanceof LayoutViewElement) {
final GenericAttributeValue<ResourceValue> styleAttribute = ((LayoutViewElement) domElement).getStyle();
final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(styleAttribute, true);
if (reference != null) {
return new ViewStyleUsageData(tag, styleAttribute, reference);
}
} else if (domElement instanceof Style) {
final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(((Style) domElement).getParentStyle(), true);
if (reference != null) {
return new ParentStyleUsageData((Style) domElement, reference);
}
}
return null;
}
use of com.intellij.util.xml.DomElement in project android by JetBrains.
the class AndroidFindStyleApplicationsProcessor method isPossibleApplicationOfStyle.
private boolean isPossibleApplicationOfStyle(XmlTag candidate) {
final DomElement domCandidate = DomManager.getDomManager(myProject).getDomElement(candidate);
if (!(domCandidate instanceof LayoutViewElement)) {
return false;
}
final LayoutViewElement candidateView = (LayoutViewElement) domCandidate;
final Map<Pair<String, String>, String> attrsInCandidateMap = new HashMap<Pair<String, String>, String>();
final List<XmlAttribute> attrsInCandidate = AndroidExtractStyleAction.getExtractableAttributes(candidate);
if (attrsInCandidate.size() < myAttrMap.size()) {
return false;
}
for (XmlAttribute attribute : attrsInCandidate) {
final String attrValue = attribute.getValue();
if (attrValue != null) {
attrsInCandidateMap.put(Pair.create(attribute.getNamespace(), attribute.getLocalName()), attrValue);
}
}
for (Map.Entry<AndroidAttributeInfo, String> entry : myAttrMap.entrySet()) {
final String ns = entry.getKey().getNamespace();
final String name = entry.getKey().getName();
final String value = entry.getValue();
final String valueInCandidate = attrsInCandidateMap.get(Pair.create(ns, name));
if (valueInCandidate == null || !valueInCandidate.equals(value)) {
return false;
}
}
if (candidateView.getStyle().getStringValue() != null) {
if (myParentStyleNameAttrValue == null) {
return false;
}
final PsiElement styleNameAttrValueForTag = getStyleNameAttrValueForTag(candidateView);
if (styleNameAttrValueForTag == null || !myParentStyleNameAttrValue.equals(styleNameAttrValueForTag)) {
return false;
}
} else if (myParentStyleNameAttrValue != null) {
return false;
}
return true;
}
use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.
the class ExtensionPointLocator method isRegisteredExtension.
public static boolean isRegisteredExtension(@NotNull PsiClass psiClass) {
String name = psiClass.getQualifiedName();
if (name == null)
return false;
Project project = psiClass.getProject();
GlobalSearchScope scope = getCandidatesScope(project);
return !PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {
@Override
public boolean process(PsiFile file, int startOffset, int endOffset) {
PsiElement at = file.findElementAt(startOffset);
String tokenText = at instanceof XmlToken ? at.getText() : null;
if (!StringUtil.equals(name, tokenText))
return true;
XmlTag tag = PsiTreeUtil.getParentOfType(at, XmlTag.class);
if (tag == null)
return true;
DomElement dom = DomUtil.getDomElement(tag);
return !(dom instanceof Extension && ((Extension) dom).getExtensionPoint() != null);
}
}, scope);
}
use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.
the class AppEngineFacet method getRootElement.
//todo[nik] copied from JamCommonUtil
@Nullable
private static <T> T getRootElement(final PsiFile file, final Class<T> domClass, final Module module) {
if (!(file instanceof XmlFile))
return null;
final DomManager domManager = DomManager.getDomManager(file.getProject());
final DomFileElement<DomElement> element = domManager.getFileElement((XmlFile) file, DomElement.class);
if (element == null)
return null;
final DomElement root = element.getRootElement();
if (!ReflectionUtil.isAssignable(domClass, root.getClass()))
return null;
return (T) root;
}
Aggregations