use of com.intellij.psi.xml.XmlDocument in project intellij-plugins by JetBrains.
the class FlexSchemaHandler method getUniquePrefix.
static String getUniquePrefix(final String namespace, final XmlFile xmlFile) {
String prefix = getDefaultPrefix(namespace);
final XmlDocument document = xmlFile.getDocument();
final XmlTag tag = document == null ? null : document.getRootTag();
final String[] knownPrefixes = getKnownPrefixes(tag);
if (ArrayUtil.contains(prefix, knownPrefixes)) {
for (int i = 2; ; i++) {
final String newPrefix = prefix + i;
if (!ArrayUtil.contains(newPrefix, knownPrefixes)) {
prefix = newPrefix;
break;
}
}
}
return prefix;
}
use of com.intellij.psi.xml.XmlDocument in project intellij-plugins by JetBrains.
the class ActionScriptSmartCompletionContributor method getEventsMap.
public static Map<String, String> getEventsMap(JSClass clazzToProcess) {
if (clazzToProcess == null)
return Collections.emptyMap();
final Map<String, String> eventsMap = new THashMap<>();
class EventsDataCollector extends ResolveProcessor implements ActionScriptResolveUtil.MetaDataProcessor {
public EventsDataCollector() {
super(null);
setToProcessHierarchy(true);
setToProcessMembers(false);
setTypeContext(true);
setLocalResolve(true);
}
@Override
public boolean process(@NotNull final JSAttribute jsAttribute) {
if ("Event".equals(jsAttribute.getName())) {
final JSAttributeNameValuePair eventAttr = jsAttribute.getValueByName("name");
JSAttributeNameValuePair typeAttr = jsAttribute.getValueByName("type");
if (eventAttr != null && typeAttr != null) {
final String simpleValue = eventAttr.getSimpleValue();
if (simpleValue != null) {
eventsMap.put(simpleValue, typeAttr.getSimpleValue());
}
}
}
return true;
}
@Override
public boolean handleOtherElement(final PsiElement el, final PsiElement context, final Ref<PsiElement> continuePassElement) {
return true;
}
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if (element instanceof JSClass) {
ActionScriptResolveUtil.processMetaAttributesForClass(element, this, true);
}
return true;
}
}
final EventsDataCollector eventsDataCollector = new EventsDataCollector();
if (clazzToProcess instanceof XmlBackedJSClassImpl) {
XmlFile file = (XmlFile) clazzToProcess.getParent().getContainingFile();
if (file != null && JavaScriptSupportLoader.isFlexMxmFile(file)) {
final XmlDocument xmlDocument = file.getDocument();
final XmlTag rootTag = xmlDocument == null ? null : xmlDocument.getRootTag();
final XmlTag[] tags = rootTag == null ? XmlTag.EMPTY : MxmlJSClass.findLanguageSubTags(rootTag, FlexPredefinedTagNames.METADATA);
JSResolveUtil.JSInjectedFilesVisitor injectedFilesVisitor = new JSResolveUtil.JSInjectedFilesVisitor() {
@Override
protected void process(JSFile file) {
for (PsiElement element : file.getChildren()) {
if (element instanceof JSAttributeList) {
ActionScriptResolveUtil.processAttributeList(eventsDataCollector, null, (JSAttributeList) element, true, true);
}
}
}
};
for (XmlTag tag : tags) {
JSResolveUtil.processInjectedFileForTag(tag, injectedFilesVisitor);
}
}
}
clazzToProcess.processDeclarations(eventsDataCollector, ResolveState.initial(), clazzToProcess, clazzToProcess);
return eventsMap;
}
use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class ExternalAnnotationsManagerImpl method annotateExternally.
private void annotateExternally(@NotNull final PsiModifierListOwner listOwner, @NotNull final String annotationFQName, @Nullable final XmlFile xmlFile, @NotNull final PsiFile codeUsageFile, @Nullable final PsiNameValuePair[] values) {
if (xmlFile == null) {
notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
return;
}
try {
final XmlDocument document = xmlFile.getDocument();
if (document != null) {
final XmlTag rootTag = document.getRootTag();
final String externalName = getExternalName(listOwner, false);
if (externalName == null) {
LOG.info("member without external name: " + listOwner);
}
if (rootTag != null && externalName != null) {
XmlTag anchor = null;
for (XmlTag item : rootTag.getSubTags()) {
int compare = Comparing.compare(externalName, StringUtil.unescapeXml(item.getAttributeValue("name")));
if (compare == 0) {
anchor = null;
for (XmlTag annotation : item.getSubTags()) {
compare = Comparing.compare(annotationFQName, annotation.getAttributeValue("name"));
if (compare == 0) {
annotation.delete();
break;
}
anchor = annotation;
}
XmlTag newTag = XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(createAnnotationTag(annotationFQName, values));
item.addAfter(newTag, anchor);
commitChanges(xmlFile);
notifyAfterAnnotationChanging(listOwner, annotationFQName, true);
return;
}
if (compare < 0)
break;
anchor = item;
}
@NonNls String text = "<item name=\'" + StringUtil.escapeXml(externalName) + "\'>\n";
text += createAnnotationTag(annotationFQName, values);
text += "</item>";
rootTag.addAfter(XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(text), anchor);
commitChanges(xmlFile);
notifyAfterAnnotationChanging(listOwner, annotationFQName, true);
return;
}
}
notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
} catch (IncorrectOperationException e) {
LOG.error(e);
notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
} finally {
dropCache();
if (codeUsageFile.getVirtualFile().isInLocalFileSystem()) {
UndoUtil.markPsiFileForUndo(codeUsageFile);
}
}
}
use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class ExternalAnnotationsManagerImpl method processExistingExternalAnnotations.
private boolean processExistingExternalAnnotations(@NotNull final PsiModifierListOwner listOwner, @NotNull final String annotationFQN, @NotNull final Processor<XmlTag> annotationTagProcessor) {
try {
final List<XmlFile> files = findExternalAnnotationsXmlFiles(listOwner);
if (files == null) {
notifyAfterAnnotationChanging(listOwner, annotationFQN, false);
return false;
}
boolean processedAnything = false;
for (final XmlFile file : files) {
if (!file.isValid()) {
continue;
}
if (ReadonlyStatusHandler.getInstance(myPsiManager.getProject()).ensureFilesWritable(file.getVirtualFile()).hasReadonlyFiles()) {
continue;
}
final XmlDocument document = file.getDocument();
if (document == null) {
continue;
}
final XmlTag rootTag = document.getRootTag();
if (rootTag == null) {
continue;
}
final String externalName = getExternalName(listOwner, false);
final List<XmlTag> tagsToProcess = new ArrayList<>();
for (XmlTag tag : rootTag.getSubTags()) {
String className = StringUtil.unescapeXml(tag.getAttributeValue("name"));
if (!Comparing.strEqual(className, externalName)) {
continue;
}
for (XmlTag annotationTag : tag.getSubTags()) {
if (!Comparing.strEqual(annotationTag.getAttributeValue("name"), annotationFQN)) {
continue;
}
tagsToProcess.add(annotationTag);
processedAnything = true;
}
}
if (tagsToProcess.isEmpty()) {
continue;
}
WriteCommandAction.runWriteCommandAction(myPsiManager.getProject(), ExternalAnnotationsManagerImpl.class.getName(), null, () -> {
PsiDocumentManager.getInstance(myPsiManager.getProject()).commitAllDocuments();
try {
for (XmlTag annotationTag : tagsToProcess) {
annotationTagProcessor.process(annotationTag);
}
commitChanges(file);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
}
notifyAfterAnnotationChanging(listOwner, annotationFQN, processedAnything);
return processedAnything;
} finally {
dropCache();
}
}
use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class AntDomFileDescription method isAntFile.
public static boolean isAntFile(final XmlFile xmlFile) {
final XmlDocument document = xmlFile.getDocument();
if (document != null) {
final XmlTag tag = document.getRootTag();
final VirtualFile vFile = xmlFile.getOriginalFile().getVirtualFile();
if (tag != null && ROOT_TAG_NAME.equals(tag.getName()) && tag.getContext() instanceof XmlDocument) {
if (tag.getAttributeValue("name") != null && tag.getAttributeValue("default") != null && vFile != null && ForcedAntFileAttribute.mayBeAntFile(vFile)) {
return true;
}
}
if (vFile != null && ForcedAntFileAttribute.isAntFile(vFile)) {
return true;
}
}
return false;
}
Aggregations