use of com.intellij.util.xml.DomElement in project intellij-plugins by JetBrains.
the class TilesOgnlInjector method getLanguagesToInject.
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
final PsiFile containingFile = context.getContainingFile();
if (!JamCommonUtil.isPlainXmlFile(containingFile)) {
return;
}
assert context instanceof XmlAttributeValue;
if (!((XmlAttributeValue) context).getValue().startsWith(OGNL_PREFIX)) {
return;
}
PsiElement parent = context.getParent();
if (parent instanceof XmlAttribute) {
String name = ((XmlAttribute) parent).getLocalName();
if ("expression".equals(name) || "templateExpression".equals(name)) {
DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement((XmlTag) parent.getParent());
if (domElement instanceof Put || domElement instanceof Add || domElement instanceof Definition) {
final TextRange attributeTextRange = ElementManipulators.getValueTextRange(context);
final TextRange ognlTextRange = TextRange.from(attributeTextRange.getStartOffset() + OGNL_PREFIX.length(), attributeTextRange.getLength() - OGNL_PREFIX.length());
registrar.startInjecting(OgnlLanguage.INSTANCE).addPlace(OgnlLanguage.EXPRESSION_PREFIX, OgnlLanguage.EXPRESSION_SUFFIX, (PsiLanguageInjectionHost) context, ognlTextRange).doneInjecting();
}
}
}
}
use of com.intellij.util.xml.DomElement in project android by JetBrains.
the class ActivityLocatorUtils method getQualifiedName.
@Nullable
public static String getQualifiedName(@NotNull ActivityAlias alias) {
ApplicationManager.getApplication().assertReadAccessAllowed();
String name = alias.getName().getStringValue();
if (name == null) {
return null;
}
int dotIndex = name.indexOf('.');
if (dotIndex > 0) {
// fully qualified
return name;
}
// attempt to retrieve the package name from the manifest in which this alias was defined
String pkg = null;
DomElement parent = alias.getParent();
if (parent instanceof Application) {
parent = parent.getParent();
if (parent instanceof Manifest) {
Manifest manifest = (Manifest) parent;
pkg = manifest.getPackage().getStringValue();
}
}
// if we have a package name, prepend that to the activity alias
return pkg == null ? name : pkg + (dotIndex == -1 ? "." : "") + name;
}
use of com.intellij.util.xml.DomElement in project android by JetBrains.
the class RtlSupportProcessor method getLayoutRefactoringForTag.
private List<UsageInfo> getLayoutRefactoringForTag(@NotNull XmlTag tag, boolean createV17, int minSdk) {
final DomElement domElement = DomManager.getDomManager(myProject).getDomElement(tag);
if (!(domElement instanceof LayoutViewElement)) {
return Collections.emptyList();
}
final List<UsageInfo> result = new ArrayList<UsageInfo>();
final XmlAttribute[] attributes = tag.getAttributes();
for (XmlAttribute attributeToMirror : attributes) {
final String localName = attributeToMirror.getLocalName();
final String namespacePrefix = attributeToMirror.getNamespacePrefix();
final String mirroredLocalName = ourMapMirroredAttributeName.get(localName);
// Check if this is a RTL attribute to mirror or if it is a Gravity attribute
if (mirroredLocalName != null) {
// Mirror only attributes that has not been mirrored before
final XmlAttribute attributeMirrored = tag.getAttribute(namespacePrefix + ":" + mirroredLocalName);
if (attributeMirrored == null) {
final int startOffset = 0;
final int endOffset = attributeToMirror.getTextLength();
RtlRefactoringUsageInfo usageInfoForAttribute = new RtlRefactoringUsageInfo(attributeToMirror, startOffset, endOffset);
usageInfoForAttribute.setType(LAYOUT_FILE_ATTRIBUTE);
usageInfoForAttribute.setCreateV17(createV17);
usageInfoForAttribute.setAndroidManifestMinSdkVersion(minSdk);
result.add(usageInfoForAttribute);
}
} else if (localName.equals(ATTR_GRAVITY) || localName.equals(ATTR_LAYOUT_GRAVITY)) {
final String value = attributeToMirror.getValue();
if (value != null && (value.contains(GRAVITY_VALUE_LEFT) || value.contains(GRAVITY_VALUE_RIGHT))) {
final int startOffset = 0;
final int endOffset = attributeToMirror.getTextLength();
RtlRefactoringUsageInfo usageInfoForAttribute = new RtlRefactoringUsageInfo(attributeToMirror, startOffset, endOffset);
usageInfoForAttribute.setType(LAYOUT_FILE_ATTRIBUTE);
usageInfoForAttribute.setCreateV17(createV17);
result.add(usageInfoForAttribute);
}
}
}
return result;
}
use of com.intellij.util.xml.DomElement in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method getResourceName.
/** Looks up the resource name for the given refactored element. Uses the same
* instanceof chain checkups as is done in {@link #canProcessElement} */
@Nullable
private static String getResourceName(PsiElement originalElement) {
PsiElement element = LazyValueResourceElementWrapper.computeLazyElement(originalElement);
if (element == null) {
return null;
}
if (element instanceof PsiFile) {
PsiFile file = (PsiFile) element;
LocalResourceManager manager = LocalResourceManager.getInstance(element);
if (manager != null) {
String type = manager.getFileResourceType(file);
if (type != null) {
String name = file.getName();
return AndroidCommonUtils.getResourceName(type, name);
}
}
return LintUtils.getBaseName(file.getName());
} else if (element instanceof PsiField) {
PsiField field = (PsiField) element;
return field.getName();
} else if (element instanceof XmlAttributeValue) {
if (AndroidResourceUtil.isIdDeclaration((XmlAttributeValue) element)) {
return AndroidResourceUtil.getResourceNameByReferenceText(((XmlAttributeValue) element).getValue());
}
XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
if (tag != null) {
DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
if (domElement instanceof ResourceElement) {
return ((ResourceElement) domElement).getName().getValue();
}
}
}
return null;
}
use of com.intellij.util.xml.DomElement in project android by JetBrains.
the class AndroidRenameHandler method isPackageAttributeInManifest.
static boolean isPackageAttributeInManifest(@NotNull Project project, @Nullable PsiElement element) {
if (element == null) {
return false;
}
final PsiFile psiFile = element.getContainingFile();
if (!(psiFile instanceof XmlFile)) {
return false;
}
final AndroidFacet facet = AndroidFacet.getInstance(psiFile);
if (facet == null) {
return false;
}
final VirtualFile vFile = psiFile.getVirtualFile();
if (vFile == null || !vFile.equals(AndroidRootUtil.getPrimaryManifestFile(facet))) {
return false;
}
if (!(element instanceof XmlAttributeValue)) {
return false;
}
final PsiElement parent = element.getParent();
if (!(parent instanceof XmlAttribute)) {
return false;
}
final GenericAttributeValue attrValue = DomManager.getDomManager(project).getDomElement((XmlAttribute) parent);
if (attrValue == null) {
return false;
}
final DomElement parentDomElement = attrValue.getParent();
return parentDomElement instanceof Manifest && attrValue.equals(((Manifest) parentDomElement).getPackage());
}
Aggregations