mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 06:05:01 +07:00
xsd2xml and xml2xsd functionality
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -162,3 +162,56 @@ html.add.table.column.after.action=Add a new column to the table after the curre
|
||||
html.add.table.column.before.action=Add a new column to the table before the current one
|
||||
xml.settings=Xml Settings
|
||||
xml.javadoc.enumeration.value.message=Enumeration value
|
||||
|
||||
#xsd2xml and xml2xsd
|
||||
url.is.empty.validation.problem=Url is empty
|
||||
invalid.number.validation.problem=Invalid number
|
||||
negative.number.validation.problem=Negative number
|
||||
result.schema.file.name.is.empty.validation.problem=Result schema file name is empty
|
||||
generate.schema.from.instance.document.dialog.title=Generate Schema From Instance Document
|
||||
instance.2.xsd.dialog.title=Instance To Schema
|
||||
|
||||
xml2xsd.generator.error=Instance 2 Schema generator error
|
||||
generate.schema.from.instance.xml.document.lvcslabel=Generate Schema From Instance Xml Document
|
||||
local.elements.global.complex.types.option.name=local elements/global complex types
|
||||
local.elements.types.option.name=local elements/types
|
||||
global.elements.local.types.option.name=global elements/local types
|
||||
select.xml.document.dialog.title=Select Xml Document
|
||||
no.class.in.selected.text.editor.error.message=No class in selected text editor
|
||||
generate.instance.document.from.schema.lvcslabel=Generate Instance Document From Schema
|
||||
schema.generator.process.title=Schema Generator
|
||||
instance.to.schema.generator.error=Instance to Schema Generator Error
|
||||
select.schema.document.dialog.title=Select Schema Document
|
||||
generate.instance.document.from.schema.dialog.title=Generate Instance Document From Schema
|
||||
xmlbeans.instance.document.name.tooltip=Instance document file name to be generated
|
||||
schema2instance.dialog.schema.path=Schema path:
|
||||
schema2instance.dialog.instance.document.name=Instance document name:
|
||||
schema2instance.dialog.element.name=Element name:
|
||||
schema2instance.dialog.enable.restriction.check=Enable restriction check
|
||||
schema2instance.dialog.enable.unique.check=Enable unique check
|
||||
instance2schema.dialog.detect.enumerations.limit=Detect enumerations limit:
|
||||
instance2schema.dialog.design.type=Design type:
|
||||
instance2schema.dialog.detect.simple.content.type=Detect simple content type:
|
||||
instance2schema.dialog.instance.document.path=Instance document path:
|
||||
any.dialog.status=Status
|
||||
schema2.instance.no.valid.root.element.name.validation.error=Root element name is not valid
|
||||
schema2.instance.output.file.name.is.empty.validation.problem=Output file name is not valid
|
||||
xmlbeans.url.tooltip=Path to schema or wsdl to be used for generation using XmlBeans
|
||||
xmlbeans.schema2instance.url.tooltip=Path to schema
|
||||
xmlbeans.instance2schema.url.tooltip=Path to instance document
|
||||
xmlbeans.rootelement.tooltip=Root element for instance document generation
|
||||
xmlbeans.particle.valid.tooltip=Enable particle valid (restriction) rule
|
||||
xmlbeans.unique.particle.tooltip=Enable unique particle rule
|
||||
xmlbeans.designtype.tooltip=XMLSchema design type
|
||||
xmlbeans.simplecontenttype.tooltip=Simple content types detection (leaf text)
|
||||
xmlbeans.enumerations.tooltip=Detection enumeration from following count
|
||||
webservice.status.tooltip=Status of current settings, input errors, etc
|
||||
xmlbeans.instance2schema.result.schema.name=Result schema file name
|
||||
browse.button.tooltip=Browse for local file
|
||||
instance.document.file.is.not.exist=Instance document file is not exist
|
||||
xml2xsd.generator.error.message=Can't generate schema from instance document. Check your xml document
|
||||
file.doesnt.exist=File {0} doesn't exist
|
||||
error=Error
|
||||
cant.rename.file=Can't rename {0} to {1}
|
||||
cant.delete.file=Can't delete file {0}
|
||||
select.xsd.schema.dialog.title=Select XSD Schema
|
||||
@@ -4,11 +4,23 @@
|
||||
<group id="ToolsXmlGroup">
|
||||
<action id="ValidateXml" class="com.intellij.xml.actions.ValidateXmlAction"/>
|
||||
<action id="GenerateDTD" class="com.intellij.codeInsight.actions.GenerateDTDAction"/>
|
||||
<action id="XSD2Document" class="com.intellij.xml.actions.xmlbeans.GenerateSchemaFromInstanceDocumentAction" text="Generate XSD schema from XML file"/>
|
||||
<action id="Document2XSD" class="com.intellij.xml.actions.xmlbeans.GenerateInstanceDocumentFromSchemaAction" text="Generate XML document from XSD schema"/>
|
||||
<separator/>
|
||||
|
||||
<add-to-group group-id="ToolsMenu" anchor="before" relative-to-action="ExternalToolsGroup"/>
|
||||
</group>
|
||||
|
||||
<group id="XmlGenerateToolsGroup">
|
||||
<separator/>
|
||||
<reference ref="GenerateDTD" />
|
||||
<reference ref="XSD2Document" />
|
||||
<reference ref="Document2XSD" />
|
||||
<separator/>
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
|
||||
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
|
||||
</group>
|
||||
|
||||
<reference ref="ValidateXml">
|
||||
<add-to-group group-id="EditorPopupMenu.Run" anchor="last"/>
|
||||
<add-to-group group-id="ProjectViewPopupMenuRefactoringGroup" anchor="last"/>
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class FileUtils {
|
||||
@NonNls
|
||||
public static final String CLASS_RESOURCE_STRING = "*?.class";
|
||||
@NonNls
|
||||
private static final String SOAP_ADDRESS = "soap:address";
|
||||
|
||||
private FileUtils() {
|
||||
}
|
||||
|
||||
public static File saveStreamContentAsFile(String fullFileName, InputStream stream) throws IOException {
|
||||
fullFileName = findFreeFileName(fullFileName);
|
||||
OutputStream ostream = new FileOutputStream(fullFileName);
|
||||
byte[] buf = new byte[8192];
|
||||
|
||||
while(true) {
|
||||
int read = stream.read(buf,0,buf.length);
|
||||
if (read == -1) break;
|
||||
ostream.write(buf,0,read);
|
||||
}
|
||||
ostream.flush();
|
||||
ostream.close();
|
||||
return new File(fullFileName);
|
||||
}
|
||||
|
||||
private static String findFreeFileName(String filename) {
|
||||
File f = new File(filename);
|
||||
if (! f.exists()) return filename;
|
||||
int dot = filename.lastIndexOf('.'); // we believe file has some ext. For instance, ".wsdl"
|
||||
String name = filename.substring(0, dot);
|
||||
String ext = filename.substring(dot);
|
||||
int num = 0;
|
||||
do {
|
||||
f = new File(name + ++num + ext);
|
||||
} while (f.exists());
|
||||
return name + num + ext;
|
||||
}
|
||||
|
||||
|
||||
public static void saveText(VirtualFile virtualFile, String text) throws IOException {
|
||||
OutputStream outputStream = virtualFile.getOutputStream(virtualFile);
|
||||
FileUtil.copy(new ByteArrayInputStream(text.getBytes(virtualFile.getCharset().name())), outputStream);
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
public static boolean copyFile(File in, File out) {
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(in);
|
||||
FileOutputStream fos = new FileOutputStream(out);
|
||||
byte[] buf = new byte[1024];
|
||||
int i;
|
||||
while ((i = fis.read(buf)) != -1) {
|
||||
fos.write(buf, 0, i);
|
||||
}
|
||||
fis.close();
|
||||
fos.close();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
import com.intellij.javaee.ExternalResourceManager;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.DataKeys;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.xml.XmlFile;
|
||||
import com.intellij.xml.XmlBundle;
|
||||
import gnu.trove.THashMap;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringBufferInputStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class GenerateInstanceDocumentFromSchemaAction extends AnAction {
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
final VirtualFile file = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
|
||||
final boolean enabled = isAcceptableFile(file);
|
||||
e.getPresentation().setEnabled(enabled);
|
||||
e.getPresentation().setVisible(enabled);
|
||||
}
|
||||
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
final Project project = DataKeys.PROJECT.getData(e.getDataContext());
|
||||
final VirtualFile file = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
|
||||
|
||||
final GenerateInstanceDocumentFromSchemaDialog dialog = new GenerateInstanceDocumentFromSchemaDialog(project, file);
|
||||
dialog.setOkAction(new Runnable() {
|
||||
public void run() {
|
||||
doAction(project, dialog);
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void doAction(final Project project, final GenerateInstanceDocumentFromSchemaDialog dialog) {
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
|
||||
@NonNls List<String> parameters = new LinkedList<String>();
|
||||
|
||||
final String url = dialog.getUrl().getText();
|
||||
final VirtualFile relativeFile = VfsUtil.findRelativeFile(ExternalResourceManager.getInstance().getResourceLocation(url), null);
|
||||
VirtualFile relativeFileDir;
|
||||
if (relativeFile == null) {
|
||||
Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
|
||||
return;
|
||||
} else {
|
||||
relativeFileDir = relativeFile.getParent();
|
||||
}
|
||||
if (relativeFileDir == null) {
|
||||
Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!dialog.enableRestrictionCheck()) {
|
||||
parameters.add("-nopvr");
|
||||
}
|
||||
|
||||
if (!dialog.enableUniquenessCheck()) {
|
||||
parameters.add("-noupa");
|
||||
}
|
||||
|
||||
|
||||
String pathToUse;
|
||||
|
||||
try {
|
||||
final File tempDir = File.createTempFile("xsd2inst","");
|
||||
tempDir.delete();
|
||||
tempDir.mkdir();
|
||||
|
||||
pathToUse = tempDir.getPath() + File.separatorChar + Xsd2InstanceUtils.processAndSaveAllSchemas(
|
||||
(XmlFile) PsiManager.getInstance(project).findFile(relativeFile),
|
||||
new THashMap<String, String>(),
|
||||
new Xsd2InstanceUtils.SchemaReferenceProcessor() {
|
||||
public void processSchema(String schemaFileName, String schemaContent) {
|
||||
try {
|
||||
final String fullFileName = tempDir.getPath() + File.separatorChar + schemaFileName;
|
||||
FileUtils.saveStreamContentAsFile(
|
||||
fullFileName,
|
||||
new StringBufferInputStream(schemaContent)
|
||||
);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
|
||||
parameters.add(pathToUse);
|
||||
|
||||
parameters.add("-name");
|
||||
parameters.add(dialog.getElementName());
|
||||
|
||||
String xml = Xsd2InstanceUtils.generate(parameters.toArray(new String[parameters.size()]));
|
||||
|
||||
|
||||
final VirtualFile baseDirForCreatedInstanceDocument1 = relativeFileDir;
|
||||
String xmlFileName = baseDirForCreatedInstanceDocument1.getPath() + File.separatorChar + dialog.getOutputFileName();
|
||||
FileOutputStream fileOutputStream = null;
|
||||
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(xmlFileName);
|
||||
fileOutputStream.write(xml.getBytes());
|
||||
fileOutputStream.close();
|
||||
fileOutputStream = null;
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
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) {
|
||||
return virtualFile != null && "xsd".equalsIgnoreCase(virtualFile.getExtension());
|
||||
}
|
||||
|
||||
public static boolean isAcceptableFile(VirtualFile file) {
|
||||
return isAcceptableFileForGenerateSchemaFromInstanceDocument(file);
|
||||
}
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.xml.actions.xmlbeans.GenerateInstanceDocumentFromSchemaDialog">
|
||||
<grid id="fece0" binding="panel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="1" left="1" bottom="1" right="1"/>
|
||||
<constraints>
|
||||
<xy x="282" y="178" width="526" height="157"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="fc23b" class="javax.swing.JLabel" binding="generateFromUrlText">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/XmlBundle" key="schema2instance.dialog.schema.path"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.schema2instance.url.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="bef0c" class="javax.swing.JLabel" binding="statusText">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/XmlBundle" key="any.dialog.status"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="webservice.status.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="6432a" class="javax.swing.JLabel" binding="status">
|
||||
<constraints>
|
||||
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="webservice.status.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="b2571" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="generateFromUrl">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.instance2schema.url.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="73cde" class="javax.swing.JLabel" binding="rootElementChooserText">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/XmlBundle" key="schema2instance.dialog.element.name"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.rootelement.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="d3b6f" class="javax.swing.JComboBox" binding="rootElementChooser">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<editable value="true"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.rootelement.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="a0090" class="javax.swing.JCheckBox" binding="enableRestrictionCheck">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<selected value="true"/>
|
||||
<text resource-bundle="messages/XmlBundle" key="schema2instance.dialog.enable.restriction.check"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.particle.valid.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3df45" class="javax.swing.JCheckBox" binding="enableUniqueCheck">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="2"/>
|
||||
<horizontalTextPosition value="4"/>
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<selected value="true"/>
|
||||
<text resource-bundle="messages/XmlBundle" key="schema2instance.dialog.enable.unique.check"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.unique.particle.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="9cfb7" class="javax.swing.JLabel" binding="outputFileNameText">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<labelFor value="aa173"/>
|
||||
<text resource-bundle="messages/XmlBundle" key="schema2instance.dialog.instance.document.name"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.instance.document.name.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="aa173" class="javax.swing.JTextField" binding="outputFileName">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.instance.document.name.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
|
||||
import com.intellij.javaee.ExternalResourceManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.xml.XmlFile;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.xml.XmlBundle;
|
||||
import com.intellij.xml.XmlElementDescriptor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class GenerateInstanceDocumentFromSchemaDialog extends DialogWrapper {
|
||||
private JPanel panel;
|
||||
private TextFieldWithBrowseButton generateFromUrl;
|
||||
private JLabel status;
|
||||
private JLabel statusText;
|
||||
private JLabel generateFromUrlText;
|
||||
private JComboBox rootElementChooser;
|
||||
private JLabel rootElementChooserText;
|
||||
private JCheckBox enableRestrictionCheck;
|
||||
private JCheckBox enableUniqueCheck;
|
||||
private JTextField outputFileName;
|
||||
private JLabel outputFileNameText;
|
||||
private String previousUri;
|
||||
private Runnable myOkAction;
|
||||
private Project myProject;
|
||||
|
||||
public GenerateInstanceDocumentFromSchemaDialog(Project project, VirtualFile file) {
|
||||
super(project, true);
|
||||
myProject = project;
|
||||
|
||||
UIUtils.configureBrowseButton(project, generateFromUrl, new String[] {"xsd"}, XmlBundle.message("select.xsd.schema.dialog.title"), false);
|
||||
|
||||
doInitFor(rootElementChooserText, rootElementChooser);
|
||||
doInitFor(generateFromUrlText, generateFromUrl.getTextField());
|
||||
doInitFor(outputFileNameText, outputFileName);
|
||||
generateFromUrl.setText(file.getPresentableUrl());
|
||||
updateFile();
|
||||
|
||||
setTitle(XmlBundle.message("generate.instance.document.from.schema.dialog.title"));
|
||||
|
||||
init();
|
||||
|
||||
outputFileName.setText(file.getName()+ ".xml");
|
||||
}
|
||||
|
||||
public void doInitFor(JLabel textComponent, JComponent component) {
|
||||
textComponent.setLabelFor(component);
|
||||
|
||||
if (component instanceof JTextField) {
|
||||
((JTextField)component).getDocument().addDocumentListener(new DocumentListener() {
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
}
|
||||
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
}
|
||||
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
}
|
||||
});
|
||||
} else if (component instanceof JComboBox) {
|
||||
JComboBox jComboBox = ((JComboBox) component);
|
||||
|
||||
jComboBox.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
validateData();
|
||||
}
|
||||
});
|
||||
|
||||
((JTextField)jComboBox.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener() {
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
updateFile();
|
||||
}
|
||||
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
updateFile();
|
||||
}
|
||||
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
updateFile();
|
||||
}
|
||||
});
|
||||
|
||||
if (jComboBox.isEditable()) {
|
||||
jComboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
|
||||
public void keyTyped(KeyEvent e) {
|
||||
validateData();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void validateData() {
|
||||
String msg = doValidateWithData();
|
||||
setOKActionEnabled(msg == null);
|
||||
status.setText(msg == null ? "" : msg);
|
||||
status.setForeground(Color.RED);
|
||||
}
|
||||
|
||||
public void configureComboBox(JComboBox combo, List<String> lastValues) { // without -editor.selectAll- no focus
|
||||
combo.setModel(new DefaultComboBoxModel(lastValues.toArray(new String[lastValues.size()])));
|
||||
}
|
||||
|
||||
private void updateFile() {
|
||||
String uri = generateFromUrl.getText();
|
||||
boolean hasPrevious = (previousUri != null && previousUri.equals(uri));
|
||||
final PsiFile psifile = findFile(uri);
|
||||
List<String> myRootValues;
|
||||
|
||||
if (psifile == null) {
|
||||
configureComboBox(rootElementChooser, Collections.EMPTY_LIST);
|
||||
return;
|
||||
}
|
||||
|
||||
final XmlTag rootTag = getRootTag(psifile);
|
||||
|
||||
if (rootTag == null) {
|
||||
configureComboBox(rootElementChooser, Collections.EMPTY_LIST);
|
||||
rootElementChooser.setSelectedIndex(-1);
|
||||
previousUri = uri;
|
||||
return;
|
||||
}
|
||||
|
||||
myRootValues = Xsd2InstanceUtils.addVariantsFromRootTag(rootTag);
|
||||
|
||||
Object selectedItem = rootElementChooser.getSelectedItem();
|
||||
configureComboBox(rootElementChooser, myRootValues);
|
||||
|
||||
if (hasPrevious) {
|
||||
rootElementChooser.setSelectedItem(selectedItem);
|
||||
} else {
|
||||
rootElementChooser.setSelectedIndex(myRootValues.size() > 0 ? 0:-1);
|
||||
}
|
||||
previousUri = uri;
|
||||
}
|
||||
|
||||
private XmlTag getRootTag(PsiFile psifile) {
|
||||
return ((XmlFile) psifile).getDocument().getRootTag();
|
||||
}
|
||||
|
||||
private PsiFile findFile(String uri) {
|
||||
final VirtualFile file = uri != null ? VfsUtil.findRelativeFile(ExternalResourceManager.getInstance().getResourceLocation(uri), null):null;
|
||||
return file != null ? PsiManager.getInstance(myProject).findFile(file):null;
|
||||
}
|
||||
|
||||
public String getOutputFileName() {
|
||||
return outputFileName.getText();
|
||||
}
|
||||
|
||||
public Boolean areCurrentParametersStillValid() {
|
||||
updateFile();
|
||||
return rootElementChooser.getSelectedItem() != null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected String doValidateWithData() {
|
||||
String rootElementName = getElementName();
|
||||
if (rootElementName == null || rootElementName.length() == 0) {
|
||||
return XmlBundle.message("schema2.instance.no.valid.root.element.name.validation.error");
|
||||
}
|
||||
|
||||
final PsiFile psiFile = findFile(getUrl().getText());
|
||||
if (psiFile instanceof XmlFile) {
|
||||
final XmlTag tag = getRootTag(psiFile);
|
||||
if (tag != null) {
|
||||
final XmlElementDescriptor descriptor = Xsd2InstanceUtils.getDescriptor(tag, rootElementName);
|
||||
|
||||
if (descriptor == null) {
|
||||
return XmlBundle.message("schema2.instance.no.valid.root.element.name.validation.error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final String fileName = getOutputFileName();
|
||||
if (fileName == null || fileName.length() == 0) {
|
||||
return XmlBundle.message("schema2.instance.output.file.name.is.empty.validation.problem");
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
protected boolean isAcceptableFile(VirtualFile virtualFile) {
|
||||
return GenerateInstanceDocumentFromSchemaAction.isAcceptableFileForGenerateSchemaFromInstanceDocument(virtualFile);
|
||||
}
|
||||
|
||||
|
||||
protected TextFieldWithBrowseButton getUrl() {
|
||||
return generateFromUrl;
|
||||
}
|
||||
|
||||
protected JLabel getUrlText() {
|
||||
return generateFromUrlText;
|
||||
}
|
||||
|
||||
protected JLabel getStatusTextField() {
|
||||
return statusText;
|
||||
}
|
||||
|
||||
protected JLabel getStatusField() {
|
||||
return status;
|
||||
}
|
||||
|
||||
protected JComponent createCenterPanel() {
|
||||
return panel;
|
||||
}
|
||||
|
||||
boolean enableUniquenessCheck() {
|
||||
return enableUniqueCheck.isSelected();
|
||||
}
|
||||
|
||||
boolean enableRestrictionCheck() {
|
||||
return enableRestrictionCheck.isSelected();
|
||||
}
|
||||
|
||||
String getElementName() {
|
||||
return (String)rootElementChooser.getSelectedItem();
|
||||
}
|
||||
|
||||
public void setOkAction(Runnable runnable) {
|
||||
myOkAction = runnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doOKAction() {
|
||||
super.doOKAction();
|
||||
if (myOkAction != null) {
|
||||
myOkAction.run();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
import com.intellij.javaee.ExternalResourceManager;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.DataKeys;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.xml.XmlBundle;
|
||||
import org.apache.xmlbeans.impl.inst2xsd.Inst2Xsd;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class GenerateSchemaFromInstanceDocumentAction extends AnAction {
|
||||
private static final Map<String, String> DESIGN_TYPES = new HashMap<String, String>();
|
||||
private static final Map<String, String> CONTENT_TYPES = new HashMap<String, String>();
|
||||
static {
|
||||
DESIGN_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.LOCAL_ELEMENTS_GLOBAL_COMPLEX_TYPES, "vb");
|
||||
DESIGN_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.LOCAL_ELEMENTS_TYPES, "ss");
|
||||
DESIGN_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.GLOBAL_ELEMENTS_LOCAL_TYPES, "rd");
|
||||
CONTENT_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.SMART_TYPE, "smart");
|
||||
CONTENT_TYPES.put(GenerateSchemaFromInstanceDocumentDialog.STRING_TYPE, "string");
|
||||
}
|
||||
|
||||
//private static final
|
||||
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
final VirtualFile file = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
|
||||
final boolean enabled = isAcceptableFile(file);
|
||||
e.getPresentation().setEnabled(enabled);
|
||||
e.getPresentation().setVisible(enabled);
|
||||
}
|
||||
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
final Project project = DataKeys.PROJECT.getData(e.getDataContext());
|
||||
final VirtualFile file = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
|
||||
|
||||
final GenerateSchemaFromInstanceDocumentDialog dialog = new GenerateSchemaFromInstanceDocumentDialog(project, file);
|
||||
dialog.setOkAction(new Runnable() {
|
||||
public void run() {
|
||||
doAction(project, dialog);
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private static void doAction(final Project project, final GenerateSchemaFromInstanceDocumentDialog dialog) {
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
|
||||
final String url = dialog.getUrl().getText();
|
||||
final VirtualFile relativeFile = VfsUtil.findRelativeFile(ExternalResourceManager.getInstance().getResourceLocation(url), null);
|
||||
VirtualFile relativeFileDir;
|
||||
if (relativeFile == null) {
|
||||
Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
|
||||
return;
|
||||
} else {
|
||||
relativeFileDir = relativeFile.getParent();
|
||||
}
|
||||
if (relativeFileDir == null) {
|
||||
Messages.showErrorDialog(project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
|
||||
return;
|
||||
}
|
||||
|
||||
@NonNls List<String> parameters = new LinkedList<String>();
|
||||
parameters.add("-design");
|
||||
parameters.add(DESIGN_TYPES.get(dialog.getDesignType()));
|
||||
|
||||
parameters.add("-simple-content-types");
|
||||
parameters.add(CONTENT_TYPES.get(dialog.getSimpleContentType()));
|
||||
|
||||
parameters.add("-enumerations");
|
||||
String enumLimit = dialog.getEnumerationsLimit();
|
||||
parameters.add("0".equals(enumLimit) ? "never" : enumLimit);
|
||||
|
||||
parameters.add("-outDir");
|
||||
final String dirPath = relativeFileDir.getPath();
|
||||
parameters.add(dirPath);
|
||||
|
||||
final File expectedSchemaFile = new File(dirPath + File.separator + relativeFile.getName() + "0.xsd");
|
||||
if (expectedSchemaFile.exists()) {
|
||||
if (!expectedSchemaFile.delete()) {
|
||||
Messages.showErrorDialog(project, XmlBundle.message("cant.delete.file", expectedSchemaFile.getPath()), XmlBundle.message("error"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
parameters.add("-outPrefix");
|
||||
parameters.add(relativeFile.getName());
|
||||
|
||||
parameters.add(url);
|
||||
File xsd = new File(dirPath + File.separator + dialog.getTargetSchemaName());
|
||||
final VirtualFile xsdFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xsd);
|
||||
if (xsdFile != null) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
xsdFile.delete(null);
|
||||
} catch (IOException e) {//
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Inst2Xsd.main(parameters.toArray(new String[parameters.size()]));
|
||||
if (expectedSchemaFile.exists()) {
|
||||
final boolean renamed = expectedSchemaFile.renameTo(xsd);
|
||||
if (! renamed) {
|
||||
Messages.showErrorDialog(project, XmlBundle.message("cant.rename.file", expectedSchemaFile.getPath(), xsd.getPath()), XmlBundle.message("error"));
|
||||
}
|
||||
}
|
||||
|
||||
VirtualFile xsdVFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xsd);
|
||||
if (xsdVFile != null) {
|
||||
FileEditorManager.getInstance(project).openFile(xsdVFile, true);
|
||||
} else {
|
||||
Messages.showErrorDialog(project, XmlBundle.message("xml2xsd.generator.error.message"), XmlBundle.message("xml2xsd.generator.error"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static boolean isAcceptableFile(VirtualFile file) {
|
||||
return file != null && "xml".equalsIgnoreCase(file.getExtension());
|
||||
}
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.xml.actions.xmlbeans.GenerateSchemaFromInstanceDocumentDialog">
|
||||
<grid id="fece0" binding="panel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="1" left="1" bottom="1" right="1"/>
|
||||
<constraints>
|
||||
<xy x="282" y="178" width="645" height="153"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="fc23b" class="javax.swing.JLabel" binding="generateFromUrlText">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/XmlBundle" key="instance2schema.dialog.instance.document.path"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.instance2schema.url.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="bef0c" class="javax.swing.JLabel" binding="statusText">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/XmlBundle" key="any.dialog.status"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="webservice.status.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="6432a" class="javax.swing.JLabel" binding="status">
|
||||
<constraints>
|
||||
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<foreground color="-65536"/>
|
||||
<text value=""/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="webservice.status.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="b2571" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="generateFromUrl">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.instance2schema.url.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="6ad20" class="javax.swing.JLabel" binding="designTypeText">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/XmlBundle" key="instance2schema.dialog.design.type"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.designtype.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="5c4f" class="javax.swing.JLabel" binding="detectSimpleContentTypesText">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text resource-bundle="messages/XmlBundle" key="instance2schema.dialog.detect.simple.content.type"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.simplecontenttype.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="cbbd3" class="javax.swing.JLabel" binding="detectEnumerationsLimitText">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<labelFor value="18828"/>
|
||||
<text resource-bundle="messages/XmlBundle" key="instance2schema.dialog.detect.enumerations.limit"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.enumerations.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="18828" class="javax.swing.JTextField" binding="detectEnumerationsLimit">
|
||||
<constraints>
|
||||
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.enumerations.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="78b1e" class="com.intellij.openapi.ui.ComboBox" binding="detectSimpleContentTypes">
|
||||
<constraints>
|
||||
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.simplecontenttype.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="f3bf0" class="com.intellij.openapi.ui.ComboBox" binding="designType">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.designtype.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="a7b02" class="javax.swing.JLabel" binding="resultSchemaFileNameText">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<labelFor value="6bb2c"/>
|
||||
<text value="Result schema file name:"/>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.instance2schema.result.schema.name"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="6bb2c" class="javax.swing.JTextField" binding="resultSchemaFileName">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<toolTipText resource-bundle="messages/XmlBundle" key="xmlbeans.enumerations.tooltip"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.ComboBox;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.xml.XmlBundle;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class GenerateSchemaFromInstanceDocumentDialog extends DialogWrapper {
|
||||
private JPanel panel;
|
||||
private TextFieldWithBrowseButton generateFromUrl;
|
||||
private JLabel status;
|
||||
private JLabel statusText;
|
||||
private JLabel generateFromUrlText;
|
||||
private JLabel designTypeText;
|
||||
private JTextField detectEnumerationsLimit;
|
||||
private ComboBox detectSimpleContentTypes;
|
||||
private ComboBox designType;
|
||||
private JLabel detectEnumerationsLimitText;
|
||||
private JLabel detectSimpleContentTypesText;
|
||||
private JLabel resultSchemaFileNameText;
|
||||
private JTextField resultSchemaFileName;
|
||||
|
||||
static final String LOCAL_ELEMENTS_GLOBAL_COMPLEX_TYPES = XmlBundle.message("local.elements.global.complex.types.option.name");
|
||||
static final String LOCAL_ELEMENTS_TYPES = XmlBundle.message("local.elements.types.option.name");
|
||||
static final String GLOBAL_ELEMENTS_LOCAL_TYPES = XmlBundle.message("global.elements.local.types.option.name");
|
||||
|
||||
private static List<String> designTypes = Arrays.asList(
|
||||
LOCAL_ELEMENTS_GLOBAL_COMPLEX_TYPES,
|
||||
LOCAL_ELEMENTS_TYPES,
|
||||
GLOBAL_ELEMENTS_LOCAL_TYPES
|
||||
);
|
||||
|
||||
static final String STRING_TYPE = "string";
|
||||
static final String SMART_TYPE = "smart";
|
||||
private static List<String> simpleContentTypes = Arrays.asList(STRING_TYPE, SMART_TYPE);
|
||||
private Runnable myOkAction;
|
||||
|
||||
public GenerateSchemaFromInstanceDocumentDialog(Project project, VirtualFile file) {
|
||||
super(project, true);
|
||||
|
||||
setTitle(XmlBundle.message("generate.schema.from.instance.document.dialog.title"));
|
||||
|
||||
doInitFor(designTypeText, designType);
|
||||
configureComboBox(designType,designTypes);
|
||||
|
||||
doInitFor(detectSimpleContentTypesText, detectSimpleContentTypes);
|
||||
configureComboBox(detectSimpleContentTypes, simpleContentTypes);
|
||||
|
||||
|
||||
doInitFor(detectEnumerationsLimitText, detectEnumerationsLimit);
|
||||
detectEnumerationsLimit.setText("10");
|
||||
|
||||
|
||||
UIUtils.configureBrowseButton(project, generateFromUrl, new String[] {"xml"}, XmlBundle.message("select.xml.document.dialog.title"), false);
|
||||
doInitFor(generateFromUrlText, generateFromUrl.getTextField());
|
||||
|
||||
doInitFor(resultSchemaFileNameText, resultSchemaFileName);
|
||||
|
||||
init();
|
||||
|
||||
generateFromUrl.setText(file.getPresentableUrl());
|
||||
resultSchemaFileName.setText(file.getName() + ".xsd");
|
||||
}
|
||||
|
||||
private void validateData() {
|
||||
String msg = doValidateWithData();
|
||||
setOKActionEnabled(msg == null);
|
||||
status.setText(msg == null ? "" : msg);
|
||||
status.setForeground(Color.RED);
|
||||
}
|
||||
|
||||
public static void configureComboBox(JComboBox combo, List<String> lastValues) {
|
||||
combo.setModel(new DefaultComboBoxModel(lastValues.toArray(new String[lastValues.size()])));
|
||||
if (combo.getItemCount() != 0) {
|
||||
combo.setSelectedIndex(0);
|
||||
combo.getEditor().selectAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void setFileUrl(String url) {
|
||||
generateFromUrl.setText(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doOKAction() {
|
||||
super.doOKAction();
|
||||
if (myOkAction != null) {
|
||||
myOkAction.run();
|
||||
}
|
||||
}
|
||||
|
||||
public void setOkAction(Runnable okAction) {
|
||||
myOkAction = okAction;
|
||||
}
|
||||
|
||||
public void doInitFor(JLabel textComponent, JComponent component) {
|
||||
textComponent.setLabelFor(component);
|
||||
|
||||
if (component instanceof JTextField) {
|
||||
((JTextField)component).getDocument().addDocumentListener(new DocumentListener() {
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
}
|
||||
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
}
|
||||
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
validateData();
|
||||
}
|
||||
});
|
||||
} else if (component instanceof JComboBox) {
|
||||
JComboBox jComboBox = ((JComboBox) component);
|
||||
|
||||
jComboBox.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
validateData();
|
||||
}
|
||||
});
|
||||
|
||||
if (jComboBox.isEditable()) {
|
||||
jComboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
|
||||
public void keyTyped(KeyEvent e) {
|
||||
validateData();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected TextFieldWithBrowseButton getUrl() {
|
||||
return generateFromUrl;
|
||||
}
|
||||
|
||||
protected JLabel getUrlText() {
|
||||
return generateFromUrlText;
|
||||
}
|
||||
|
||||
protected JLabel getStatusTextField() {
|
||||
return statusText;
|
||||
}
|
||||
|
||||
protected JLabel getStatusField() {
|
||||
return status;
|
||||
}
|
||||
|
||||
protected JComponent createCenterPanel() {
|
||||
return panel;
|
||||
}
|
||||
|
||||
String getDesignType() {
|
||||
return (String) designType.getSelectedItem();
|
||||
}
|
||||
|
||||
String getSimpleContentType() {
|
||||
return (String) detectSimpleContentTypes.getSelectedItem();
|
||||
}
|
||||
|
||||
String getEnumerationsLimit() {
|
||||
return detectEnumerationsLimit.getText();
|
||||
}
|
||||
|
||||
public String getTargetSchemaName() {
|
||||
return resultSchemaFileName.getText();
|
||||
}
|
||||
|
||||
protected String doValidateWithData() {
|
||||
if (! new File(generateFromUrl.getText()).exists()) {
|
||||
return XmlBundle.message("instance.document.file.is.not.exist");
|
||||
}
|
||||
|
||||
try {
|
||||
int i = Integer.parseInt(getEnumerationsLimit());
|
||||
if (i < 0) return XmlBundle.message("negative.number.validation.problem");
|
||||
} catch(NumberFormatException ex) {
|
||||
return XmlBundle.message("invalid.number.validation.problem");
|
||||
}
|
||||
|
||||
if (getTargetSchemaName() == null || getTargetSchemaName().length() == 0) {
|
||||
return XmlBundle.message("result.schema.file.name.is.empty.validation.problem");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
|
||||
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
|
||||
import com.intellij.openapi.fileChooser.FileChooserDialog;
|
||||
import com.intellij.openapi.fileChooser.FileChooserFactory;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.xml.XmlBundle;
|
||||
import com.intellij.javaee.ExternalResourceManager;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class UIUtils {
|
||||
private UIUtils() {}
|
||||
|
||||
public static void configureBrowseButton(final Project myProject,
|
||||
final TextFieldWithBrowseButton wsdlUrl,
|
||||
final String[] _extensions,
|
||||
final String selectFileDialogTitle,
|
||||
final boolean multipleFileSelection) {
|
||||
wsdlUrl.getButton().setToolTipText(XmlBundle.message("browse.button.tooltip"));
|
||||
wsdlUrl.getButton().addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
final FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false, false, multipleFileSelection) {
|
||||
private final List<String> extensions = Arrays.asList(_extensions);
|
||||
|
||||
public boolean isFileSelectable(VirtualFile virtualFile) {
|
||||
return extensions.contains(virtualFile.getExtension());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
|
||||
return super.isFileVisible(file, showHiddenFiles) && (file.isDirectory() || isFileSelectable(file));
|
||||
}
|
||||
};
|
||||
|
||||
fileChooserDescriptor.setTitle(selectFileDialogTitle);
|
||||
|
||||
final FileChooserDialog fileChooser = FileChooserFactory.getInstance().createFileChooser(
|
||||
fileChooserDescriptor,
|
||||
myProject
|
||||
);
|
||||
|
||||
VirtualFile initialFile = myProject.getBaseDir();
|
||||
String selectedItem = wsdlUrl.getTextField().getText();
|
||||
if (selectedItem != null && selectedItem.startsWith(LocalFileSystem.PROTOCOL_PREFIX)) {
|
||||
VirtualFile fileByPath = VfsUtil.findRelativeFile(ExternalResourceManager.getInstance().getResourceLocation(VfsUtil.fixURLforIDEA(selectedItem)), null);
|
||||
if (fileByPath != null) initialFile = fileByPath;
|
||||
}
|
||||
|
||||
final VirtualFile[] virtualFiles = fileChooser.choose(initialFile, myProject);
|
||||
if (virtualFiles.length == 1) {
|
||||
String url = fixIDEAUrl(virtualFiles[0].getUrl());
|
||||
wsdlUrl.setText(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static String fixIDEAUrl(String url) {
|
||||
return SystemInfo.isWindows ? VfsUtil.fixIDEAUrl(url) : url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package com.intellij.xml.actions.xmlbeans;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.XmlRecursiveElementVisitor;
|
||||
import com.intellij.psi.impl.source.tree.LeafPsiElement;
|
||||
import com.intellij.psi.meta.PsiMetaData;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.xml.XmlAttribute;
|
||||
import com.intellij.psi.xml.XmlDocument;
|
||||
import com.intellij.psi.xml.XmlFile;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.xml.XmlElementDescriptor;
|
||||
import com.intellij.xml.XmlNSDescriptor;
|
||||
import com.intellij.xml.impl.schema.XmlNSDescriptorImpl;
|
||||
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.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class Xsd2InstanceUtils {
|
||||
public static String generate(String[] args) {
|
||||
Set flags = new HashSet();
|
||||
Set opts = new HashSet();
|
||||
flags.add("h");
|
||||
flags.add("help");
|
||||
flags.add("usage");
|
||||
flags.add("license");
|
||||
flags.add("version");
|
||||
flags.add("dl");
|
||||
flags.add("noupa");
|
||||
flags.add("nopvr");
|
||||
flags.add("partial");
|
||||
opts.add("name");
|
||||
|
||||
CommandLine cl = new CommandLine(args, flags, opts);
|
||||
|
||||
String[] badOpts = cl.getBadOpts();
|
||||
if (badOpts.length > 0) {
|
||||
throw new IllegalArgumentException("Unrecognized option: " + badOpts[0]);
|
||||
|
||||
}
|
||||
|
||||
boolean dl = (cl.getOpt("dl") != null);
|
||||
boolean nopvr = (cl.getOpt("nopvr") != null);
|
||||
boolean noupa = (cl.getOpt("noupa") != null);
|
||||
|
||||
File[] schemaFiles = cl.filesEndingWith(".xsd");
|
||||
String rootName = cl.getOpt("name");
|
||||
|
||||
if (rootName == null) {
|
||||
throw new IllegalArgumentException("Required option \"-name\" must be present");
|
||||
}
|
||||
|
||||
// Process Schema files
|
||||
List sdocs = new ArrayList();
|
||||
for (int i = 0; i < schemaFiles.length; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
sdocs.add(XmlObject.Factory.parse(schemaFiles[i],
|
||||
(new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest()));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IllegalArgumentException("Can not load schema file: " + schemaFiles[i] + ": ");
|
||||
}
|
||||
}
|
||||
|
||||
XmlObject[] schemas = (XmlObject[]) sdocs.toArray(new XmlObject[sdocs.size()]);
|
||||
|
||||
SchemaTypeSystem sts = null;
|
||||
if (schemas.length > 0)
|
||||
{
|
||||
Collection errors = new ArrayList();
|
||||
XmlOptions compileOptions = new XmlOptions();
|
||||
if (dl)
|
||||
compileOptions.setCompileDownloadUrls();
|
||||
if (nopvr)
|
||||
compileOptions.setCompileNoPvrRule();
|
||||
if (noupa)
|
||||
compileOptions.setCompileNoUpaRule();
|
||||
|
||||
try {
|
||||
sts = XmlBeans.compileXsd(schemas, XmlBeans.getBuiltinTypeSystem(), compileOptions);
|
||||
} catch (XmlException e) {
|
||||
String out = "Schema compilation errors: ";
|
||||
for (Object error : errors) out += "\n" + error;
|
||||
throw new IllegalArgumentException(out);
|
||||
}
|
||||
}
|
||||
|
||||
if (sts == null)
|
||||
{
|
||||
throw new IllegalArgumentException("No Schemas to process.");
|
||||
}
|
||||
SchemaType[] globalElems = sts.documentTypes();
|
||||
SchemaType elem = null;
|
||||
for (int i = 0; i < globalElems.length; i++)
|
||||
{
|
||||
if (rootName.equals(globalElems[i].getDocumentElementName().getLocalPart()))
|
||||
{
|
||||
elem = globalElems[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (elem == null) {
|
||||
throw new IllegalArgumentException("Could not find a global element with name \"" + rootName + "\"");
|
||||
}
|
||||
|
||||
// Now generate it
|
||||
return SampleXmlUtil.createSampleForType(elem);
|
||||
}
|
||||
|
||||
public static XmlElementDescriptor getDescriptor(XmlTag tag, String elementName) {
|
||||
final PsiMetaData metaData = tag.getMetaData();
|
||||
|
||||
if (metaData instanceof XmlNSDescriptorImpl) {
|
||||
final XmlNSDescriptorImpl nsDescriptor = (XmlNSDescriptorImpl) metaData;
|
||||
return nsDescriptor.getElementDescriptor(elementName, nsDescriptor.getDefaultNamespace());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<String> addVariantsFromRootTag(XmlTag rootTag) {
|
||||
PsiMetaData metaData = rootTag.getMetaData();
|
||||
if (metaData instanceof XmlNSDescriptorImpl) {
|
||||
XmlNSDescriptorImpl nsDescriptor = (XmlNSDescriptorImpl) metaData;
|
||||
|
||||
List<String> elementDescriptors = new ArrayList<String>();
|
||||
XmlElementDescriptor[] rootElementsDescriptors = nsDescriptor.getRootElementsDescriptors(PsiTreeUtil.getParentOfType(rootTag, XmlDocument.class));
|
||||
for(XmlElementDescriptor e:rootElementsDescriptors) {
|
||||
elementDescriptors.add(e.getName());
|
||||
}
|
||||
|
||||
return elementDescriptors;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
public static String processAndSaveAllSchemas(@NotNull XmlFile file, @NotNull final Map<String, String> scannedToFileName,
|
||||
final @NotNull SchemaReferenceProcessor schemaReferenceProcessor) {
|
||||
final String fileName = file.getName();
|
||||
|
||||
String previous = scannedToFileName.get(fileName);
|
||||
|
||||
if (previous != null) return previous;
|
||||
|
||||
scannedToFileName.put(fileName, fileName);
|
||||
|
||||
final StringBuilder result = new StringBuilder();
|
||||
|
||||
file.acceptChildren(new XmlRecursiveElementVisitor() {
|
||||
@Override public void visitElement(PsiElement psiElement) {
|
||||
super.visitElement(psiElement);
|
||||
if (psiElement instanceof LeafPsiElement) {
|
||||
final String text = psiElement.getText();
|
||||
result.append(text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void visitXmlAttribute(XmlAttribute xmlAttribute) {
|
||||
boolean replaced = false;
|
||||
|
||||
if (xmlAttribute.isNamespaceDeclaration()) {
|
||||
replaced = true;
|
||||
final String value = xmlAttribute.getValue();
|
||||
result.append(xmlAttribute.getText()).append(" ");
|
||||
|
||||
if (!scannedToFileName.containsKey(value)) {
|
||||
final XmlNSDescriptor nsDescriptor = xmlAttribute.getParent().getNSDescriptor(value, true);
|
||||
|
||||
if (nsDescriptor != null) {
|
||||
processAndSaveAllSchemas(nsDescriptor.getDescriptorFile(), scannedToFileName, schemaReferenceProcessor);
|
||||
}
|
||||
}
|
||||
} else if ("schemaLocation".equals(xmlAttribute.getName())) {
|
||||
final PsiReference[] references = xmlAttribute.getValueElement().getReferences();
|
||||
|
||||
if (references.length > 0) {
|
||||
PsiElement psiElement = references[0].resolve();
|
||||
|
||||
if (psiElement instanceof XmlFile) {
|
||||
final String s = processAndSaveAllSchemas(((XmlFile) psiElement), scannedToFileName, schemaReferenceProcessor);
|
||||
if (s != null) {
|
||||
result.append(xmlAttribute.getName()).append("='").append(s).append('\'');
|
||||
replaced = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!replaced) result.append(xmlAttribute.getText());
|
||||
}
|
||||
});
|
||||
|
||||
schemaReferenceProcessor.processSchema(fileName, result.toString());
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public interface SchemaReferenceProcessor {
|
||||
void processSchema(String schemaFileName, String schemaContent);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,9 @@
|
||||
<orderEntry type="library" name="commons-collections" level="project" />
|
||||
<orderEntry type="module" module-name="platform-impl" />
|
||||
<orderEntry type="module" module-name="lang-impl" />
|
||||
<orderEntry type="module" module-name="openapi" />
|
||||
<orderEntry type="module" module-name="icons" />
|
||||
<orderEntry type="library" name="XmlBeans" level="project" />
|
||||
</component>
|
||||
<component name="copyright">
|
||||
<Base>
|
||||
|
||||
Reference in New Issue
Block a user