private void doFix() throws IncorrectOperationException { final XmlTag tag = PsiTreeUtil.getParentOfType(myReference.getElement(), XmlTag.class); assert tag != null; final XmlTag defineTag = tag.createChildTag("define", ApplicationLoader.RNG_NAMESPACE, "\n \n", false); defineTag.setAttribute("name", myReference.getCanonicalText()); final RngGrammar grammar = ((DefinitionReference) myReference).getScope(); if (grammar == null) return; final XmlTag root = grammar.getXmlTag(); if (root == null) return; final XmlTag[] tags = root.getSubTags(); for (XmlTag xmlTag : tags) { if (PsiTreeUtil.isAncestor(xmlTag, tag, false)) { final XmlElementFactory ef = XmlElementFactory.getInstance(tag.getProject()); final XmlText text = ef.createDisplayText(" "); final PsiElement e = root.addAfter(text, xmlTag); root.addAfter(defineTag, e); return; } } root.add(defineTag); }
private static void fillFromSchema(PsiFile file, ElementNames names) { if (!(file instanceof XmlFile)) return; final XmlFile f = (XmlFile) file; final XmlDocument d = f.getDocument(); if (d == null) return; final XmlTag rootTag = d.getRootTag(); if (rootTag == null) return; //noinspection unchecked names.dependencies.add(new NSDeclTracker(rootTag)); try { final Map<String, String> namespaceDeclarations = rootTag.getLocalNamespaceDeclarations(); final Collection<String> prefixes = namespaceDeclarations.keySet(); //noinspection unchecked final Set<XmlElementDescriptor> history = new THashSet<XmlElementDescriptor>(); final XmlElementFactory ef = XmlElementFactory.getInstance(file.getProject()); int noSchemaNamespaces = 0; for (String prefix : prefixes) { final String namespace = namespaceDeclarations.get(prefix); if (isIgnoredNamespace(prefix, namespace)) continue; final XmlTag tag = ef.createTagFromText("<dummy-tag xmlns='" + namespace + "' />", XMLLanguage.INSTANCE); final XmlDocument document = PsiTreeUtil.getParentOfType(tag, XmlDocument.class); final XmlNSDescriptor rootDescriptor = tag.getNSDescriptor(tag.getNamespace(), true); if (rootDescriptor == null || (rootDescriptor instanceof XmlNSDescriptorImpl && ((XmlNSDescriptorImpl) rootDescriptor).getTag() == null) || !rootDescriptor.getDeclaration().isPhysical()) { final QName any = QNameUtil.createAnyLocalName(namespace); names.elementNames.add(any); names.attributeNames.add(any); noSchemaNamespaces++; continue; } //noinspection unchecked names.dependencies.add(rootDescriptor.getDescriptorFile()); final XmlElementDescriptor[] e = rootDescriptor.getRootElementsDescriptors(document); for (XmlElementDescriptor descriptor : e) { processElementDescriptors(descriptor, tag, names, history); } } names.validateNames = names.elementNames.size() > noSchemaNamespaces; // final QName any = QNameUtil.createAnyLocalName(""); // names.elementNames.add(any); // names.attributeNames.add(any); } catch (IncorrectOperationException e) { Logger.getInstance(XsltContextProvider.class.getName()).error(e); } }
private static void addAttributesBefore(XmlTag tag, List<Pair<String, String>> attr2value) { XmlAttribute firstAttribute = ArrayUtil.getFirstElement(tag.getAttributes()); XmlElementFactory factory = XmlElementFactory.getInstance(tag.getProject()); for (Pair<String, String> pair : attr2value) { XmlAttribute xmlAttribute = factory.createXmlAttribute(pair.first, ""); if (firstAttribute != null) { tag.addBefore(xmlAttribute, firstAttribute); } else { tag.add(xmlAttribute); } } }
public void setText(String value) { try { XmlText text = null; if (StringUtil.isNotEmpty(value)) { final XmlText[] texts = getTextElements(); if (texts.length == 0) { text = (XmlText) myTag.add( XmlElementFactory.getInstance(myTag.getProject()).createDisplayText("x")); } else { text = texts[0]; } text.setValue(value); } if (myElements.length > 0) { for (final XmlTagChild child : myElements) { if (child != text) { child.delete(); } } } } catch (IncorrectOperationException e) { LOG.error(e); } }
private static void inlineMultiTags( XmlTag includeTag, XmlTag includeTagParent, XmlTag mergeTag, Project project) throws AndroidRefactoringErrorException { final Map<String, String> namespacesFromParent = includeTagParent.getLocalNamespaceDeclarations(); final Map<String, String> namespacesToAddToParent = new HashMap<String, String>(); final Map<String, String> namespacesToAddToEachTag = new HashMap<String, String>(); for (Map.Entry<String, String> entry : mergeTag.getLocalNamespaceDeclarations().entrySet()) { final String prefix = entry.getKey(); final String namespace = entry.getValue(); final String declaredNamespace = namespacesFromParent.get(prefix); if (declaredNamespace != null && !declaredNamespace.equals(namespace)) { namespacesToAddToEachTag.put(prefix, namespace); } else { namespacesToAddToParent.put(prefix, namespace); } } final XmlTag mergeTagCopy = (XmlTag) mergeTag.copy(); final XmlElementFactory xmlElementFactory = XmlElementFactory.getInstance(project); for (XmlTag subtag : mergeTagCopy.getSubTags()) { final XmlAttribute[] attributes = subtag.getAttributes(); final XmlAttribute firstAttribute = attributes.length > 0 ? attributes[0] : null; for (Map.Entry<String, String> entry : namespacesToAddToEachTag.entrySet()) { final String prefix = entry.getKey(); final String namespace = entry.getValue(); if (!subtag.getLocalNamespaceDeclarations().containsKey(prefix)) { final XmlAttribute xmlnsAttr = xmlElementFactory.createXmlAttribute("xmlns:" + prefix, namespace); if (firstAttribute != null) { subtag.addBefore(xmlnsAttr, firstAttribute); } else { subtag.add(xmlnsAttr); } } } } replaceByTagContent(project, includeTag, mergeTagCopy); addNamespaceAttributes(includeTagParent, namespacesToAddToParent, project); }
private void annotateExternally( @NotNull PsiModifierListOwner listOwner, @NotNull String annotationFQName, @Nullable final XmlFile xmlFile, @NotNull PsiFile codeUsageFile, PsiNameValuePair[] values) { if (xmlFile == null) return; try { final XmlDocument document = xmlFile.getDocument(); if (document != null) { final XmlTag rootTag = document.getRootTag(); final String externalName = getExternalName(listOwner, false); if (rootTag != null) { for (XmlTag tag : rootTag.getSubTags()) { if (Comparing.strEqual( StringUtil.unescapeXml(tag.getAttributeValue("name")), externalName)) { for (XmlTag annTag : tag.getSubTags()) { if (Comparing.strEqual(annTag.getAttributeValue("name"), annotationFQName)) { annTag.delete(); break; } } tag.add( XmlElementFactory.getInstance(myPsiManager.getProject()) .createTagFromText(createAnnotationTag(annotationFQName, values))); return; } } @NonNls String text = "<item name=\'" + StringUtil.escapeXml(externalName) + "\'>\n"; text += createAnnotationTag(annotationFQName, values); text += "</item>"; rootTag.add( XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(text)); } } } catch (IncorrectOperationException e) { LOG.error(e); } finally { dropCache(); if (codeUsageFile.getVirtualFile().isInLocalFileSystem()) { UndoUtil.markPsiFileForUndo(codeUsageFile); } } }
private static void addNamespaceAttributes( XmlTag tag, Map<String, String> namespaces, Project project) { final XmlAttribute[] parentAttributes = tag.getAttributes(); final XmlAttribute firstParentAttribute = parentAttributes.length > 0 ? parentAttributes[0] : null; final XmlElementFactory factory = XmlElementFactory.getInstance(project); for (Map.Entry<String, String> entry : namespaces.entrySet()) { final String prefix = entry.getKey(); final String namespace = entry.getValue(); if (!namespace.equals(tag.getNamespaceByPrefix(prefix))) { final XmlAttribute xmlnsAttr = factory.createXmlAttribute("xmlns:" + prefix, namespace); if (firstParentAttribute != null) { tag.addBefore(xmlnsAttr, firstParentAttribute); } else { tag.add(xmlnsAttr); } } } }
private ASTNode expandTag() throws IncorrectOperationException { ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(this); if (endTagStart == null) { final XmlTagImpl tagFromText = (XmlTagImpl) XmlElementFactory.getInstance(getProject()) .createTagFromText("<" + getName() + "></" + getName() + ">"); final ASTNode startTagStart = XmlChildRole.START_TAG_END_FINDER.findChild(tagFromText); endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(tagFromText); final LeafElement emptyTagEnd = (LeafElement) XmlChildRole.EMPTY_TAG_END_FINDER.findChild(this); if (emptyTagEnd != null) removeChild(emptyTagEnd); addChildren(startTagStart, null, null); } return endTagStart; }
public XmlAttribute setAttribute(String qname, String value) throws IncorrectOperationException { final XmlAttribute attribute = getAttribute(qname); if (attribute != null) { if (value == null) { deleteChildInternal(attribute.getNode()); return null; } attribute.setValue(value); return attribute; } else if (value == null) { return null; } else { PsiElement xmlAttribute = add(XmlElementFactory.getInstance(getProject()).createXmlAttribute(qname, value)); while (!(xmlAttribute instanceof XmlAttribute)) xmlAttribute = xmlAttribute.getNextSibling(); return (XmlAttribute) xmlAttribute; } }
@Override public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { if (!FileModificationService.getInstance().preparePsiElementsForWrite(element)) return; final XmlAttribute attr = (XmlAttribute) element.getParent(); final String name = attr.getName(); final XmlAttributeDescriptor descriptor = attr.getDescriptor(); LOG.assertTrue(descriptor != null); String value = attr.getValue(); final PsiElement declaration = descriptor.getDeclaration(); if (declaration instanceof PsiField) { final PsiType fieldType = ((PsiField) declaration).getType(); final PsiType itemType = JavaGenericsUtil.getCollectionItemType(fieldType, declaration.getResolveScope()); if (itemType != null) { final String typeNode = itemType.getPresentableText(); JavaFxPsiUtil.insertImportWhenNeeded( (XmlFile) attr.getContainingFile(), typeNode, itemType.getCanonicalText()); final String[] vals = value.split(","); value = StringUtil.join( vals, new Function<String, String>() { @Override public String fun(String s) { return "<" + typeNode + " " + FxmlConstants.FX_VALUE + "=\"" + s.trim() + "\"/>"; } }, "\n"); } } final XmlTag childTag = XmlElementFactory.getInstance(project) .createTagFromText("<" + name + ">" + value + "</" + name + ">"); attr.getParent().add(childTag); attr.delete(); }
public static void chooseColor( JComponent editorComponent, PsiElement element, String caption, boolean startInWriteAction) { final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false); if (literal == null) return; final String text = StringUtil.unquoteString(literal.getValue()); Color oldColor; try { oldColor = Color.decode(text); } catch (NumberFormatException e) { oldColor = JBColor.GRAY; } Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true); if (color == null) return; if (!Comparing.equal(color, oldColor)) { if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return; final String newText = "#" + ColorUtil.toHex(color); final PsiManager manager = literal.getManager(); final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createXmlAttribute("name", newText); final Runnable replaceRunnable = new Runnable() { @Override public void run() { final XmlAttributeValue valueElement = newAttribute.getValueElement(); assert valueElement != null; literal.replace(valueElement); } }; if (startInWriteAction) { new WriteCommandAction(element.getProject(), caption) { @Override protected void run(@NotNull Result result) throws Throwable { replaceRunnable.run(); } }.execute(); } else { replaceRunnable.run(); } } }
private static void generateRaw(final @NotNull XmlTag newTag) { XmlElementDescriptor selected = newTag.getDescriptor(); if (selected == null) return; switch (selected.getContentType()) { case XmlElementDescriptor.CONTENT_TYPE_EMPTY: newTag.collapseIfEmpty(); ASTNode node = newTag.getNode(); assert node != null; ASTNode elementEnd = node.findChildByType(XmlTokenType.XML_EMPTY_ELEMENT_END); if (elementEnd == null) { LeafElement emptyTagEnd = Factory.createSingleLeafElement( XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, newTag.getManager()); node.addChild(emptyTagEnd); } break; case XmlElementDescriptor.CONTENT_TYPE_MIXED: newTag.getValue().setText(""); } for (XmlAttributeDescriptor descriptor : selected.getAttributesDescriptors(newTag)) { if (descriptor.isRequired()) { newTag.setAttribute(descriptor.getName(), ""); } } List<XmlElementDescriptor> tags = getRequiredSubTags(selected); for (XmlElementDescriptor descriptor : tags) { if (descriptor == null) { XmlTag tag = XmlElementFactory.getInstance(newTag.getProject()) .createTagFromText("<", newTag.getLanguage()); newTag.addSubTag(tag, false); } else { XmlTag subTag = newTag.addSubTag(createTag(newTag, descriptor), false); generateRaw(subTag); } } }