update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / codeInsight / actions / GenerateDTDAction.java
blob8f75e5a2318ed4e9c8bc603fae3a585a8964c0f5
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.actions;
18 import com.intellij.codeInsight.CodeInsightActionHandler;
19 import com.intellij.openapi.actionSystem.*;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.psi.*;
24 import com.intellij.psi.util.PsiTreeUtil;
25 import com.intellij.psi.xml.XmlDocument;
26 import com.intellij.psi.xml.XmlFile;
27 import com.intellij.psi.xml.XmlProcessingInstruction;
28 import com.intellij.psi.xml.XmlProlog;
29 import com.intellij.util.IncorrectOperationException;
30 import com.intellij.xml.util.XmlUtil;
31 import org.jetbrains.annotations.NonNls;
32 import org.jetbrains.annotations.NotNull;
34 /**
35 * Created by IntelliJ IDEA.
36 * User: ik
37 * Date: 22.05.2003
38 * Time: 13:46:54
39 * To change this template use Options | File Templates.
41 public class GenerateDTDAction extends BaseCodeInsightAction{
42 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.actions.GenerateDTDAction");
43 protected CodeInsightActionHandler getHandler(){
44 return new CodeInsightActionHandler(){
45 public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file){
46 if(file instanceof XmlFile && file.getVirtualFile() != null && file.getVirtualFile().isWritable()){
47 final @NonNls StringBuffer buffer = new StringBuffer();
48 final XmlDocument document = ((XmlFile) file).getDocument();
49 if(document.getRootTag() != null){
50 buffer.append("<!DOCTYPE " + document.getRootTag().getName() + " [\n");
51 buffer.append(XmlUtil.generateDocumentDTD(document));
52 buffer.append("]>\n");
53 XmlFile tempFile;
54 try{
55 final XmlProlog prolog = document.getProlog();
56 final PsiElement childOfType = PsiTreeUtil.getChildOfType(prolog, XmlProcessingInstruction.class);
57 if (childOfType != null) {
58 final String text = childOfType.getText();
59 buffer.insert(0,text);
60 final PsiElement nextSibling = childOfType.getNextSibling();
61 if (nextSibling instanceof PsiWhiteSpace) {
62 buffer.insert(text.length(),nextSibling.getText());
65 tempFile = (XmlFile)PsiFileFactory.getInstance(file.getProject()).createFileFromText("dummy.xml", buffer.toString());
66 prolog.replace(tempFile.getDocument().getProlog());
68 catch(IncorrectOperationException e){
69 LOG.error(e);
75 public boolean startInWriteAction(){
76 return true;
81 public void update(AnActionEvent event) {
82 super.update(event);
84 final DataContext dataContext = event.getDataContext();
85 final Presentation presentation = event.getPresentation();
86 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
87 Project project = PlatformDataKeys.PROJECT.getData(dataContext);
89 final boolean enabled;
90 if (editor != null && project != null) {
91 PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
92 enabled = file instanceof XmlFile;
94 else {
95 enabled = false;
98 presentation.setEnabled(enabled);
99 if (ActionPlaces.isPopupPlace(event.getPlace())) {
100 presentation.setVisible(enabled);
104 protected boolean isValidForFile(Project project, Editor editor, PsiFile file){
105 return file instanceof XmlFile;