mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[patch from Sascha.Weinreuter] IDEA-67266 Generate XML document from XSD fails with non-ASCII content
This commit is contained in:
+22
-18
@@ -39,10 +39,10 @@ import gnu.trove.THashMap;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringBufferInputStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -57,7 +57,7 @@ public class GenerateInstanceDocumentFromSchemaAction extends AnAction {
|
||||
e.getPresentation().setEnabled(enabled);
|
||||
if (ActionPlaces.isPopupPlace(e.getPlace())) {
|
||||
e.getPresentation().setVisible(enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
@@ -113,12 +113,12 @@ public class GenerateInstanceDocumentFromSchemaAction extends AnAction {
|
||||
(XmlFile) PsiManager.getInstance(project).findFile(relativeFile),
|
||||
new THashMap<String, String>(),
|
||||
new Xsd2InstanceUtils.SchemaReferenceProcessor() {
|
||||
public void processSchema(String schemaFileName, String schemaContent) {
|
||||
public void processSchema(String schemaFileName, byte[] schemaContent) {
|
||||
try {
|
||||
final String fullFileName = tempDir.getPath() + File.separatorChar + schemaFileName;
|
||||
FileUtils.saveStreamContentAsFile(
|
||||
fullFileName,
|
||||
new StringBufferInputStream(schemaContent)
|
||||
new ByteArrayInputStream(schemaContent)
|
||||
);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -147,26 +147,30 @@ public class GenerateInstanceDocumentFromSchemaAction extends AnAction {
|
||||
|
||||
final VirtualFile baseDirForCreatedInstanceDocument1 = relativeFileDir;
|
||||
String xmlFileName = baseDirForCreatedInstanceDocument1.getPath() + File.separatorChar + dialog.getOutputFileName();
|
||||
FileOutputStream fileOutputStream = null;
|
||||
|
||||
FileOutputStream fileOutputStream;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(xmlFileName);
|
||||
fileOutputStream.write(xml.getBytes());
|
||||
fileOutputStream.close();
|
||||
fileOutputStream = null;
|
||||
try {
|
||||
// the generated XML doesn't have any XML declaration -> utf-8
|
||||
fileOutputStream.write(xml.getBytes("utf-8"));
|
||||
}
|
||||
finally {
|
||||
fileOutputStream.close();
|
||||
}
|
||||
|
||||
final File xmlFile = new File(xmlFileName);
|
||||
VirtualFile virtualFile = ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
|
||||
@Nullable
|
||||
public VirtualFile compute() {
|
||||
return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xmlFile);
|
||||
}
|
||||
});
|
||||
FileEditorManager.getInstance(project).openFile(virtualFile, true);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Messages.showErrorDialog(project, "Could not save generated XML document: " + StringUtil.getMessage(e), XmlBundle.message("error"));
|
||||
}
|
||||
|
||||
final File xmlFile = new File(xmlFileName);
|
||||
VirtualFile virtualFile = ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
|
||||
@Nullable
|
||||
public VirtualFile compute() {
|
||||
return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xmlFile);
|
||||
}
|
||||
});
|
||||
FileEditorManager.getInstance(project).openFile(virtualFile, true);
|
||||
}
|
||||
|
||||
static boolean isAcceptableFileForGenerateSchemaFromInstanceDocument(VirtualFile virtualFile) {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.XmlRecursiveElementVisitor;
|
||||
@@ -28,12 +29,14 @@ import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.xml.XmlElementDescriptor;
|
||||
import com.intellij.xml.XmlNSDescriptor;
|
||||
import com.intellij.xml.impl.schema.XmlNSDescriptorImpl;
|
||||
import com.intellij.xml.util.XmlUtil;
|
||||
import org.apache.xmlbeans.*;
|
||||
import org.apache.xmlbeans.impl.tool.CommandLine;
|
||||
import org.apache.xmlbeans.impl.xsd2inst.SampleXmlUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@@ -85,7 +88,7 @@ public class Xsd2InstanceUtils {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IllegalArgumentException("Can not load schema file: " + schemaFiles[i] + ": ");
|
||||
throw new IllegalArgumentException("Can not load schema file: " + schemaFiles[i] + ": " + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +148,7 @@ public class Xsd2InstanceUtils {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static List<String> addVariantsFromRootTag(XmlTag rootTag) {
|
||||
PsiMetaData metaData = rootTag.getMetaData();
|
||||
if (metaData instanceof XmlNSDescriptorImpl) {
|
||||
@@ -217,11 +220,26 @@ public class Xsd2InstanceUtils {
|
||||
}
|
||||
});
|
||||
|
||||
schemaReferenceProcessor.processSchema(fileName, result.toString());
|
||||
final VirtualFile virtualFile = file.getVirtualFile();
|
||||
final String content = result.toString();
|
||||
|
||||
byte[] bytes;
|
||||
if (virtualFile != null) {
|
||||
bytes = content.getBytes(virtualFile.getCharset());
|
||||
} else {
|
||||
try {
|
||||
final String charsetName = XmlUtil.extractXmlEncodingFromProlog(content.getBytes());
|
||||
bytes = charsetName != null ? content.getBytes(charsetName) : content.getBytes();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
bytes = content.getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
schemaReferenceProcessor.processSchema(fileName, bytes);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public interface SchemaReferenceProcessor {
|
||||
void processSchema(String schemaFileName, String schemaContent);
|
||||
void processSchema(String schemaFileName, byte[] schemaContent);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user