update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / xml / actions / xmlbeans / GenerateSchemaFromInstanceDocumentAction.java
blobe4055797f72a71a58774ef32030ad85958934978
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.xml.actions.xmlbeans;
18 import com.intellij.javaee.ExternalResourceManager;
19 import com.intellij.openapi.actionSystem.AnAction;
20 import com.intellij.openapi.actionSystem.AnActionEvent;
21 import com.intellij.openapi.actionSystem.ActionPlaces;
22 import com.intellij.openapi.actionSystem.PlatformDataKeys;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.fileEditor.FileDocumentManager;
25 import com.intellij.openapi.fileEditor.FileEditorManager;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.ui.Messages;
28 import com.intellij.openapi.vfs.LocalFileSystem;
29 import com.intellij.openapi.vfs.VfsUtil;
30 import com.intellij.openapi.vfs.VirtualFile;
31 import com.intellij.xml.XmlBundle;
32 import org.apache.xmlbeans.impl.inst2xsd.Inst2Xsd;
33 import org.jetbrains.annotations.NonNls;
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.HashMap;
42 /**
43 * @author Konstantin Bulenkov
45 public class GenerateSchemaFromInstanceDocumentAction extends AnAction {
46 private static final Map<String, String> DESIGN_TYPES = new HashMap<String, String>();
47 private static final Map<String, String> CONTENT_TYPES = new HashMap<String, String>();
48 static {
49 DESIGN_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.LOCAL_ELEMENTS_GLOBAL_COMPLEX_TYPES, "vb");
50 DESIGN_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.LOCAL_ELEMENTS_TYPES, "ss");
51 DESIGN_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.GLOBAL_ELEMENTS_LOCAL_TYPES, "rd");
52 CONTENT_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.SMART_TYPE, "smart");
53 CONTENT_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.STRING_TYPE, "string");
56 //private static final
58 @Override
59 public void update(AnActionEvent e) {
60 final VirtualFile file = PlatformDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
61 final boolean enabled = isAcceptableFile(file);
62 e.getPresentation().setEnabled(enabled);
63 if (ActionPlaces.isPopupPlace(e.getPlace())) {
64 e.getPresentation().setVisible(enabled);
68 public void actionPerformed(AnActionEvent e) {
69 final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
70 final VirtualFile file = PlatformDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
72 final GenerateSchemaFromInstanceDocumentDialog dialog = new GenerateSchemaFromInstanceDocumentDialog(project, file);
73 dialog.setOkAction(new Runnable() {
74 public void run() {
75 doAction(project, dialog);
77 });
79 dialog.show();
82 private static void doAction(final Project project, final GenerateSchemaFromInstanceDocumentDialog dialog) {
83 FileDocumentManager.getInstance().saveAllDocuments();
85 final String url = dialog.getUrl().getText();
86 final VirtualFile relativeFile = VfsUtil.findRelativeFile(ExternalResourceManager.getInstance().getResourceLocation(url), null);
87 VirtualFile relativeFileDir;
88 if (relativeFile == null) {
89 Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
90 return;
91 } else {
92 relativeFileDir = relativeFile.getParent();
94 if (relativeFileDir == null) {
95 Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
96 return;
99 @NonNls List<String> parameters = new LinkedList<String>();
100 parameters.add("-design");
101 parameters.add(DESIGN_TYPES.get(dialog.getDesignType()));
103 parameters.add("-simple-content-types");
104 parameters.add(CONTENT_TYPES.get(dialog.getSimpleContentType()));
106 parameters.add("-enumerations");
107 String enumLimit = dialog.getEnumerationsLimit();
108 parameters.add("0".equals(enumLimit) ? "never" : enumLimit);
110 parameters.add("-outDir");
111 final String dirPath = relativeFileDir.getPath();
112 parameters.add(dirPath);
114 final File expectedSchemaFile = new File(dirPath + File.separator + relativeFile.getName() + "0.xsd");
115 if (expectedSchemaFile.exists()) {
116 if (!expectedSchemaFile.delete()) {
117 Messages.showErrorDialog(project, XmlBundle.message("cant.delete.file", expectedSchemaFile.getPath()), XmlBundle.message("error"));
118 return;
122 parameters.add("-outPrefix");
123 parameters.add(relativeFile.getName());
125 parameters.add(url);
126 File xsd = new File(dirPath + File.separator + dialog.getTargetSchemaName());
127 final VirtualFile xsdFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xsd);
128 if (xsdFile != null) {
129 ApplicationManager.getApplication().runWriteAction(new Runnable() {
130 public void run() {
131 try {
132 xsdFile.delete(null);
133 } catch (IOException e) {//
139 Inst2Xsd.main(parameters.toArray(new String[parameters.size()]));
140 if (expectedSchemaFile.exists()) {
141 final boolean renamed = expectedSchemaFile.renameTo(xsd);
142 if (! renamed) {
143 Messages.showErrorDialog(project, XmlBundle.message("cant.rename.file", expectedSchemaFile.getPath(), xsd.getPath()), XmlBundle.message("error"));
147 VirtualFile xsdVFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xsd);
148 if (xsdVFile != null) {
149 FileEditorManager.getInstance(project).openFile(xsdVFile, true);
150 } else {
151 Messages.showErrorDialog(project, XmlBundle.message("xml2xsd.generator.error.message"), XmlBundle.message("xml2xsd.generator.error"));
156 public static boolean isAcceptableFile(VirtualFile file) {
157 return file != null && "xml".equalsIgnoreCase(file.getExtension());