<commit_before>package com.intellij.codeInsight.actions;

import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.util.IncorrectOperationException;
import com.intellij.xml.util.XmlUtil;
import org.jetbrains.annotations.NonNls;

/**
 * Created by IntelliJ IDEA.
 * User: ik
 * Date: 22.05.2003
 * Time: 13:46:54
 * To change this template use Options | File Templates.
 */
public class GenerateDTDAction extends BaseCodeInsightAction{
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.actions.GenerateDTDAction");
  protected CodeInsightActionHandler getHandler(){
    return new CodeInsightActionHandler(){
      public void invoke(Project project, Editor editor, PsiFile file){
        if(file instanceof XmlFile && file.getVirtualFile() != null && file.getVirtualFile().isWritable()){
          final @NonNls StringBuffer buffer = new StringBuffer();
          final XmlDocument document = ((XmlFile) file).getDocument();
          if(document.getRootTag() != null){
            buffer.append("<!DOCTYPE " + document.getRootTag().getName() + " [\n");
            buffer.append(XmlUtil.generateDocumentDTD(document));
            buffer.append("]>\n");
            XmlFile tempFile = null;
            try{
              tempFile = (XmlFile) file.getManager().getElementFactory().createFileFromText("dummy.xml", buffer.toString());
              document.getProlog().replace(tempFile.getDocument().getProlog());
            }
            catch(IncorrectOperationException e){
              LOG.error(e);
            }
          }
        }
      }

      public boolean startInWriteAction(){
        return true;
      }
    };
  }

  public void update(AnActionEvent event) {
    super.update(event);<commit_msg>Generate DTD from XML blows away first line of XML file (IDEADEV-3387)<commit_after>package com.intellij.codeInsight.actions;

import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.*;
import com.intellij.util.IncorrectOperationException;
import com.intellij.xml.util.XmlUtil;
import org.jetbrains.annotations.NonNls;

/**
 * Created by IntelliJ IDEA.
 * User: ik
 * Date: 22.05.2003
 * Time: 13:46:54
 * To change this template use Options | File Templates.
 */
public class GenerateDTDAction extends BaseCodeInsightAction{
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.actions.GenerateDTDAction");
  protected CodeInsightActionHandler getHandler(){
    return new CodeInsightActionHandler(){
      public void invoke(Project project, Editor editor, PsiFile file){
        if(file instanceof XmlFile && file.getVirtualFile() != null && file.getVirtualFile().isWritable()){
          final @NonNls StringBuffer buffer = new StringBuffer();
          final XmlDocument document = ((XmlFile) file).getDocument();
          if(document.getRootTag() != null){
            buffer.append("<!DOCTYPE " + document.getRootTag().getName() + " [\n");
            buffer.append(XmlUtil.generateDocumentDTD(document));
            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) file.getManager().getElementFactory().createFileFromText("dummy.xml", buffer.toString());
              prolog.replace(tempFile.getDocument().getProlog());
            }
            catch(IncorrectOperationException e){
              LOG.error(e);
            }
          }
        }
      }

      public boolean startInWriteAction(){
        return true;
      }
    };
  }

  public void update(AnActionEvent event) {
    super.update(event);
{'fraction_non_alphanumeric': 0.11038712150249137, 'fraction_numerical': 0.006324262169413568, 'mean_word_length': 3.291940789473684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '534851', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1469, 'n_words': 281}