IAE fix (Show user an exception and return)
[fedora-idea.git] / xml / impl / src / com / intellij / xml / actions / xmlbeans / GenerateInstanceDocumentFromSchemaAction.java
blob56f7f93330ab2e68e17e51aabf3488e977b57880
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.ActionPlaces;
20 import com.intellij.openapi.actionSystem.AnAction;
21 import com.intellij.openapi.actionSystem.AnActionEvent;
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.util.Computable;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.openapi.vfs.LocalFileSystem;
31 import com.intellij.openapi.vfs.VfsUtil;
32 import com.intellij.openapi.vfs.VirtualFile;
33 import com.intellij.psi.PsiManager;
34 import com.intellij.psi.xml.XmlFile;
35 import com.intellij.util.ArrayUtil;
36 import com.intellij.xml.XmlBundle;
37 import gnu.trove.THashMap;
38 import org.jetbrains.annotations.NonNls;
39 import org.jetbrains.annotations.Nullable;
41 import java.io.File;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.io.StringBufferInputStream;
45 import java.util.LinkedList;
46 import java.util.List;
48 /**
49 * @author Konstantin Bulenkov
51 public class GenerateInstanceDocumentFromSchemaAction extends AnAction {
52 @Override
53 public void update(AnActionEvent e) {
54 final VirtualFile file = PlatformDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
55 final boolean enabled = isAcceptableFile(file);
56 e.getPresentation().setEnabled(enabled);
57 if (ActionPlaces.isPopupPlace(e.getPlace())) {
58 e.getPresentation().setVisible(enabled);
62 public void actionPerformed(AnActionEvent e) {
63 final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
64 final VirtualFile file = PlatformDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
66 final GenerateInstanceDocumentFromSchemaDialog dialog = new GenerateInstanceDocumentFromSchemaDialog(project, file);
67 dialog.setOkAction(new Runnable() {
68 public void run() {
69 doAction(project, dialog);
71 });
73 dialog.show();
76 private void doAction(final Project project, final GenerateInstanceDocumentFromSchemaDialog dialog) {
77 FileDocumentManager.getInstance().saveAllDocuments();
79 @NonNls List<String> parameters = new LinkedList<String>();
81 final String url = dialog.getUrl().getText();
82 final VirtualFile relativeFile = VfsUtil.findRelativeFile(ExternalResourceManager.getInstance().getResourceLocation(url), null);
83 VirtualFile relativeFileDir;
84 if (relativeFile == null) {
85 Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
86 return;
87 } else {
88 relativeFileDir = relativeFile.getParent();
90 if (relativeFileDir == null) {
91 Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
92 return;
95 if (!dialog.enableRestrictionCheck()) {
96 parameters.add("-nopvr");
99 if (!dialog.enableUniquenessCheck()) {
100 parameters.add("-noupa");
104 String pathToUse;
106 try {
107 final File tempDir = File.createTempFile("xsd2inst","");
108 tempDir.delete();
109 tempDir.mkdir();
111 pathToUse = tempDir.getPath() + File.separatorChar + Xsd2InstanceUtils.processAndSaveAllSchemas(
112 (XmlFile) PsiManager.getInstance(project).findFile(relativeFile),
113 new THashMap<String, String>(),
114 new Xsd2InstanceUtils.SchemaReferenceProcessor() {
115 public void processSchema(String schemaFileName, String schemaContent) {
116 try {
117 final String fullFileName = tempDir.getPath() + File.separatorChar + schemaFileName;
118 FileUtils.saveStreamContentAsFile(
119 fullFileName,
120 new StringBufferInputStream(schemaContent)
122 } catch (IOException e) {
123 throw new RuntimeException(e);
128 } catch (IOException e) {
129 return;
132 parameters.add(pathToUse);
134 parameters.add("-name");
135 parameters.add(dialog.getElementName());
137 String xml;
138 try {
139 xml = Xsd2InstanceUtils.generate(ArrayUtil.toStringArray(parameters));
140 } catch (IllegalArgumentException e) {
141 Messages.showErrorDialog(project, StringUtil.getMessage(e), XmlBundle.message("error"));
142 return;
147 final VirtualFile baseDirForCreatedInstanceDocument1 = relativeFileDir;
148 String xmlFileName = baseDirForCreatedInstanceDocument1.getPath() + File.separatorChar + dialog.getOutputFileName();
149 FileOutputStream fileOutputStream = null;
151 try {
152 fileOutputStream = new FileOutputStream(xmlFileName);
153 fileOutputStream.write(xml.getBytes());
154 fileOutputStream.close();
155 fileOutputStream = null;
157 catch (IOException e) {
158 e.printStackTrace();
161 final File xmlFile = new File(xmlFileName);
162 VirtualFile virtualFile = ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
163 @Nullable
164 public VirtualFile compute() {
165 return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xmlFile);
168 FileEditorManager.getInstance(project).openFile(virtualFile, true);
171 static boolean isAcceptableFileForGenerateSchemaFromInstanceDocument(VirtualFile virtualFile) {
172 return virtualFile != null && "xsd".equalsIgnoreCase(virtualFile.getExtension());
175 public static boolean isAcceptableFile(VirtualFile file) {
176 return isAcceptableFileForGenerateSchemaFromInstanceDocument(file);