public void registerPageLanguage(final Project project, final XmlFile containingFile, final String languageName) { new WriteCommandAction.Simple(project, getFamilyName()) { @Override protected void run() { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", StdFileTypes.XML, "<?language " + languageName + "?>"); final XmlDocument document = dummyFile.getDocument(); if (document != null) { final XmlProlog prolog = document.getProlog(); final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); LOG.assertTrue(instructions.size() == 1); final XmlDocument xmlDocument = containingFile.getDocument(); if (xmlDocument != null) { final XmlProlog xmlProlog = xmlDocument.getProlog(); if (xmlProlog != null) { final PsiElement element = xmlProlog.addBefore(instructions.iterator().next(), xmlProlog.getFirstChild()); xmlProlog.addAfter(PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\n"), element); } } } } }.execute(); }
private void registerPageLanguage(final Project project, final XmlFile containingFile, final String languageName) { new WriteCommandAction.Simple(project, getFamilyName()) { @Override protected void run() { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", StdFileTypes.XML, "<?language " + languageName + "?>"); final XmlDocument document = dummyFile.getDocument(); if (document != null) { final XmlProlog prolog = document.getProlog(); final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); LOG.assertTrue(instructions.size() == 1); final XmlDocument xmlDocument = containingFile.getDocument(); if (xmlDocument != null) { final XmlProlog xmlProlog = xmlDocument.getProlog(); if (xmlProlog != null) { final PsiElement element = xmlProlog.addBefore(instructions.iterator().next(), xmlProlog.getFirstChild()); xmlProlog.addAfter(PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\n"), element); } } } } }.execute(); }
private void registerPageLanguage(final Project project, final XmlFile containingFile, final String languageName) { new WriteCommandAction.Simple(project, getFamilyName()) { @Override protected void run() { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", XmlFileType.INSTANCE, "<?language " + languageName + "?>"); final XmlDocument document = dummyFile.getDocument(); if (document != null) { final XmlProlog prolog = document.getProlog(); final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); LOG.assertTrue(instructions.size() == 1); final XmlDocument xmlDocument = containingFile.getDocument(); if (xmlDocument != null) { final XmlProlog xmlProlog = xmlDocument.getProlog(); if (xmlProlog != null) { final PsiElement element = xmlProlog.addBefore(instructions.iterator().next(), xmlProlog.getFirstChild()); xmlProlog.addAfter(PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\n"), element); } } } } }.execute(); }
private static List<String> parseInstructions(XmlFile file, String instructionName) { List<String> definedImports = new ArrayList<String>(); XmlDocument document = file.getDocument(); if (document != null) { XmlProlog prolog = document.getProlog(); final Collection<XmlProcessingInstruction> instructions = new ArrayList<XmlProcessingInstruction>(PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class)); for (final XmlProcessingInstruction instruction : instructions) { final String instructionTarget = getInstructionTarget(instructionName, instruction); if (instructionTarget != null) { definedImports.add(instructionTarget); } } } return definedImports; }
public static void insertImportWhenNeeded(XmlFile xmlFile, String shortName, String qualifiedName) { if (shortName != null && findPsiClass(shortName, xmlFile.getRootTag()) == null) { final XmlDocument document = xmlFile.getDocument(); if (document != null) { final XmlProcessingInstruction processingInstruction = createSingleImportInstruction(qualifiedName, xmlFile.getProject()); final XmlProlog prolog = document.getProlog(); if (prolog != null) { prolog.add(processingInstruction); } else { document.addBefore(processingInstruction, document.getRootTag()); } PostprocessReformattingAspect.getInstance(xmlFile.getProject()).doPostponedFormatting(xmlFile.getViewProvider()); } } }
private static boolean is23(final ConvertContext context) { final DomElement element = DomUtil.getFileElement(context.getInvocationElement()).getRootElement(); final XmlTag tag = element.getXmlTag(); if (tag != null && "web-app".equals(element.getXmlElementName()) && tag.getAttribute("version") == null) { XmlDocument document = (XmlDocument)tag.getParent(); final XmlProlog prolog = document.getProlog(); if (prolog != null) { final XmlDoctype doctype = prolog.getDoctype(); if (doctype != null) { final String uri = doctype.getDtdUri(); if (uri != null && uri.contains("2_3")) return true; } } return false; } return true; }
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { final XmlTag anchor = getAnchor(element); if (anchor == null) return; PsiElement prevSibling = anchor.getPrevSibling(); while (prevSibling instanceof PsiWhiteSpace || prevSibling instanceof XmlText) { prevSibling = prevSibling.getPrevSibling(); } if (prevSibling instanceof XmlProlog) { prevSibling = prevSibling.getLastChild(); if (prevSibling != null && !(prevSibling instanceof XmlComment)) { prevSibling = PsiTreeUtil.getPrevSiblingOfType(prevSibling, XmlComment.class); } } if (prevSibling instanceof XmlComment) { final XmlComment comment = (XmlComment)prevSibling; final String text = XmlUtil.getCommentText(comment); if (text != null && InspectionUtil.SUPPRESSION_PATTERN.matcher(text).matches()) { final String s = text.trim() + ", " + myToolId; final XmlComment newComment = createComment(project, s); CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(comment.replace(newComment)); } else { addNoinspectionComment(project, anchor); } } else { addNoinspectionComment(project, anchor); } }
private void addNoinspectionComment(Project project, XmlTag anchor) throws IncorrectOperationException { final XmlComment newComment = createComment(project, "noinspection " + myToolId); PsiElement parent = anchor.getParentTag(); if (parent == null) { parent = PsiTreeUtil.getPrevSiblingOfType(anchor, XmlProlog.class); if (parent != null) { CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(parent.add(newComment)); } } else { CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(parent.addBefore(newComment, anchor)); } }
@NotNull protected CodeInsightActionHandler getHandler(){ return new CodeInsightActionHandler(){ public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { final XmlDocument document = findSuitableXmlDocument(file); if (document != null) { final @NonNls StringBuffer buffer = new StringBuffer(); buffer.append("<!DOCTYPE " + document.getRootTag().getName() + " [\n"); buffer.append(XmlUtil.generateDocumentDTD(document, true)); buffer.append("]>\n"); XmlFile tempFile; try{ final XmlProlog prolog = document.getProlog(); final PsiElement childOfType = PsiTreeUtil.getChildOfType(prolog, XmlProcessingInstruction.class); if (childOfType != null) { final String text = childOfType.getText(); buffer.insert(0,text); final PsiElement nextSibling = childOfType.getNextSibling(); if (nextSibling instanceof PsiWhiteSpace) { buffer.insert(text.length(),nextSibling.getText()); } } tempFile = (XmlFile)PsiFileFactory.getInstance(file.getProject()).createFileFromText("dummy.xml", buffer.toString()); prolog.replace(tempFile.getDocument().getProlog()); } catch (IncorrectOperationException e) { LOG.error(e); } } } public boolean startInWriteAction(){ return true; } }; }
private boolean hasDtdDeclaration() { XmlDocument document = myFile.getDocument(); if (document == null) return false; XmlProlog prolog = document.getProlog(); if (prolog == null) return false; XmlDoctype doctype = prolog.getDoctype(); if (doctype == null) return false; return true; }
public static boolean hasNonHtml5Doctype(XmlElement context) { XmlDocument doc = PsiTreeUtil.getParentOfType(context, XmlDocument.class); if(doc == null) { return false; } XmlProlog prolog = doc.getProlog(); XmlDoctype doctype = prolog != null ? prolog.getDoctype() : null; return doctype != null && !isHtml5Doctype(doctype); }
@Override @NotNull public XmlElementDescriptor[] getRootElementsDescriptors(@Nullable final XmlDocument document) { // Suggest more appropriate variant if DOCTYPE <element_name> exists final XmlProlog prolog = document != null ? document.getProlog() : null; if(prolog != null) { final XmlDoctype doctype = prolog.getDoctype(); if(doctype != null) { final XmlElement element = doctype.getNameElement(); if(element != null) { final XmlElementDescriptor descriptor = getElementDescriptor(element.getText()); if(descriptor != null) { return new XmlElementDescriptor[]{descriptor}; } } } } return getElements(); }
@NotNull public Collection<StructureViewTreeElement> getChildrenBase() { final XmlDocument document = getElement().getDocument(); List<XmlTag> rootTags = new ArrayList<XmlTag>(); if (document != null) { for (PsiElement element : document.getChildren()) if (element instanceof XmlTag) rootTags.add((XmlTag)element); } Collection<StructureViewTreeElement> structureViewTreeElements = AbstractXmlTagTreeElement.getStructureViewTreeElements(rootTags.toArray(new XmlTag[rootTags.size()])); Collection<StructureViewTreeElement> dtdStructureViewTreeElements = null; final XmlProlog prolog = document != null ? document.getProlog():null; if (prolog != null) { final XmlDoctype doctype = prolog.getDoctype(); if (doctype != null) { final XmlMarkupDecl xmlMarkupDecl = doctype.getMarkupDecl(); if (xmlMarkupDecl != null) { dtdStructureViewTreeElements = DtdFileTreeElement.collectElements(xmlMarkupDecl); } } } if (dtdStructureViewTreeElements != null) { final ArrayList<StructureViewTreeElement> result = new ArrayList<StructureViewTreeElement>( dtdStructureViewTreeElements.size() + structureViewTreeElements.size() ); result.addAll(dtdStructureViewTreeElements); result.addAll(structureViewTreeElements); structureViewTreeElements = result; } return structureViewTreeElements; }
@Override protected PsiElement getPreviousSibling(PsiElement element) { if(element == null) { return null; } PsiElement res = element.getPrevSibling(); if(res != null) { if(res instanceof XmlProlog) { XmlProlog prolog = (XmlProlog) res; if(prolog.getChildren().length > 0) { res = prolog.getLastChild(); } else { res = prolog.getPrevSibling(); } } } else { if(element.getParent() instanceof XmlProlog) { res = element.getParent().getPrevSibling(); } } return res; }
@Override protected PsiElement getNextSibling(PsiElement element) { if(element == null) { return null; } PsiElement res = element instanceof XmlProlog ? element : element.getNextSibling(); if(res != null) { if(res instanceof XmlProlog) { XmlProlog prolog = (XmlProlog) res; if(prolog.getChildren().length > 0) { res = prolog.getFirstChild(); } else { res = prolog.getNextSibling(); } } } else { if(element.getParent() instanceof XmlProlog) { res = element.getParent().getNextSibling(); } } return res; }
@Override public XmlProlog getProlog() { return null; }
public XmlProlog getProlog() { return null; }
@NotNull @Override public Runnable processFile(final PsiFile file) { VirtualFile vFile = file.getVirtualFile(); if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow)vFile).getDelegate(); final Project project = file.getProject(); if (vFile == null || !ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(vFile)) { return EmptyRunnable.INSTANCE; } final List<Pair<String, Boolean>> names = new ArrayList<Pair<String, Boolean>>(); collectNamesToImport(names, (XmlFile)file); Collections.sort(names, new Comparator<Pair<String, Boolean>>() { @Override public int compare(Pair<String, Boolean> o1, Pair<String, Boolean> o2) { return StringUtil.compare(o1.first, o2.first, true); } }); final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project); final List<Pair<String, Boolean>> sortedNames = ImportHelper.sortItemsAccordingToSettings(names, settings); final HashSet<String> onDemand = new HashSet<String>(); ImportHelper.collectOnDemandImports(sortedNames, onDemand, settings); final Set<String> imported = new HashSet<String>(); final List<String> imports = new ArrayList<String>(); for (Pair<String, Boolean> pair : sortedNames) { final String qName = pair.first; final String packageName = StringUtil.getPackageName(qName); if (imported.contains(packageName) || imported.contains(qName)) { continue; } if (onDemand.contains(packageName)) { imported.add(packageName); imports.add("<?import " + packageName + ".*?>"); } else { imported.add(qName); imports.add("<?import " + qName + "?>"); } } final PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject()); final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", XmlFileType.INSTANCE, StringUtil.join(imports, "\n")); final XmlDocument document = dummyFile.getDocument(); final XmlProlog newImportList = document.getProlog(); if (newImportList == null) return EmptyRunnable.getInstance(); return new Runnable() { @Override public void run() { final XmlDocument xmlDocument = ((XmlFile)file).getDocument(); final XmlProlog prolog = xmlDocument.getProlog(); if (prolog != null) { final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); for (final XmlProcessingInstruction instruction : instructions) { final ASTNode node = instruction.getNode(); final ASTNode nameNode = node.findChildByType(XmlTokenType.XML_NAME); if (nameNode != null && nameNode.getText().equals("import")) { instruction.delete(); } } prolog.add(newImportList); } else { document.addBefore(newImportList, document.getRootTag()); } } }; }
@Override public void visitXmlProlog(XmlProlog prolog) {}
public static boolean isHtml5Document(XmlDocument doc) { if(doc == null) { return false; } XmlProlog prolog = doc.getProlog(); XmlDoctype doctype = prolog != null ? prolog.getDoctype() : null; if(!isHtmlTagContainingFile(doc)) { return false; } final PsiFile htmlFile = doc.getContainingFile(); final String htmlFileFullName; if(htmlFile != null) { final VirtualFile vFile = htmlFile.getVirtualFile(); if(vFile != null) { htmlFileFullName = vFile.getPath(); } else { htmlFileFullName = htmlFile.getName(); } } else { htmlFileFullName = "unknown"; } if(doctype == null) { LOG.debug("DOCTYPE for " + htmlFileFullName + " is null"); return Html5SchemaProvider.getHtml5SchemaLocation().equals(ExternalResourceManagerEx.getInstanceEx().getDefaultHtmlDoctype(doc.getProject())); } final boolean html5Doctype = isHtml5Doctype(doctype); final String doctypeDescription = "text: " + doctype.getText() + ", dtdUri: " + doctype.getDtdUri() + ", publicId: " + doctype.getPublicId() + ", markupDecl: " + doctype.getMarkupDecl(); LOG.debug("DOCTYPE for " + htmlFileFullName + "; " + doctypeDescription + "; HTML5: " + html5Doctype); return html5Doctype; }
@Override public boolean isAcceptable(Object element, PsiElement context) { if(element instanceof XmlTag) { final XmlTag psiElement = (XmlTag) element; if(!psiElement.isValid()) { return false; } final String ns = psiElement.getNamespace(); if(isNamespaceAcceptable(ns)) { return true; } final PsiFile psiFile = psiElement.getContainingFile(); if(psiFile instanceof XmlFile) { // We use file references for as dtd namespace // But we should also check PUBLIC ID for namespace XmlDocument document = ((XmlFile) psiFile).getDocument(); if(document == null) { return false; } final XmlProlog prolog = document.getProlog(); if(prolog != null) { final XmlDoctype doctype = prolog.getDoctype(); if(doctype != null) { final String publicId = doctype.getPublicId(); if(publicId != null) { if(isNamespaceAcceptable(publicId)) { return true; } } } } } } else if(element instanceof XmlDocument) { return isAcceptable(((XmlDocument) element).getRootTag(), context); } return false; }