private static void collectDependencies(
      @Nullable XmlTag myTag, @NotNull XmlFile myFile, @NotNull Set<PsiFile> visited) {
    if (visited.contains(myFile)) return;
    visited.add(myFile);

    if (myTag == null) return;
    XmlTag[] tags = myTag.getSubTags();

    for (final XmlTag tag : tags) {
      if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || equalsToSchemaName(tag, IMPORT_TAG_NAME)) {
        final XmlAttribute schemaLocation = tag.getAttribute("schemaLocation", null);
        if (schemaLocation != null) {
          final XmlFile xmlFile =
              XmlUtil.findNamespaceByLocation(myFile, schemaLocation.getValue());
          addDependency(xmlFile, visited);
        }
      } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) {
        myRedefinedDescriptorsInProcessing.set(visited);
        try {
          final XmlFile file = getRedefinedElementDescriptorFile(tag);
          addDependency(file, visited);
        } finally {
          myRedefinedDescriptorsInProcessing.set(null);
        }
      }
    }

    final String schemaLocationDeclaration =
        myTag.getAttributeValue("schemaLocation", XmlUtil.XML_SCHEMA_INSTANCE_URI);
    if (schemaLocationDeclaration != null) {
      final StringTokenizer tokenizer = new StringTokenizer(schemaLocationDeclaration);

      while (tokenizer.hasMoreTokens()) {
        final String uri = tokenizer.nextToken();

        if (tokenizer.hasMoreTokens()) {
          PsiFile resourceLocation =
              ExternalResourceManager.getInstance()
                  .getResourceLocation(tokenizer.nextToken(), myFile, null);
          if (resourceLocation == null && uri != null)
            resourceLocation =
                ExternalResourceManager.getInstance().getResourceLocation(uri, myFile, null);

          if (resourceLocation instanceof XmlFile)
            addDependency((XmlFile) resourceLocation, visited);
        }
      }
    }
  }
Пример #2
0
 @Nullable
 private XmlFile retrieveFile(final String fileLocation, String version) {
   final String targetNs = XmlUtil.getTargetSchemaNsFromTag(this);
   if (fileLocation.equals(targetNs)) {
     return null;
   } else {
     final XmlFile file = XmlUtil.getContainingFile(this);
     final PsiFile psiFile =
         ExternalResourceManager.getInstance().getResourceLocation(fileLocation, file, version);
     return psiFile instanceof XmlFile ? (XmlFile) psiFile : null;
   }
 }
 private static VirtualFile findFileByPath(
     final String resPath, @Nullable final String dtdUrl, ProgressIndicator indicator) {
   final Ref<VirtualFile> ref = new Ref<>();
   ApplicationManager.getApplication()
       .invokeAndWait(
           () ->
               ApplicationManager.getApplication()
                   .runWriteAction(
                       () -> {
                         ref.set(
                             LocalFileSystem.getInstance()
                                 .refreshAndFindFileByPath(
                                     resPath.replace(File.separatorChar, '/')));
                         if (dtdUrl != null) {
                           ExternalResourceManager.getInstance().addResource(dtdUrl, resPath);
                         }
                       }),
           indicator.getModalityState());
   return ref.get();
 }
  private static void doAction(
      final Project project, final GenerateSchemaFromInstanceDocumentDialog dialog) {
    FileDocumentManager.getInstance().saveAllDocuments();

    final String url = dialog.getUrl().getText();
    final VirtualFile relativeFile =
        VfsUtilCore.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(
              () -> {
                try {
                  xsdFile.delete(null);
                } catch (IOException e) { //
                }
              });
    }

    Inst2Xsd.main(ArrayUtil.toStringArray(parameters));
    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"));
    }
  }