public @Nullable PsiFile resolve(final @Nullable String baseSystemId, final String _systemId) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("enter: resolveEntity(baseSystemId='" + baseSystemId + "' systemId='" + _systemId + "," + this + "')");
    }

    if (_systemId == null) return null;
    if (myStopOnUnDeclaredResource &&
        ExternalResourceManagerEx.getInstanceEx().isIgnoredResource(_systemId)) {
      throw new IgnoredResourceException();
    }

    final int length = XmlUtil.getPrefixLength(_systemId);
    final String systemId = _systemId.substring(length);

    final Computable<PsiFile> action = () -> {

      PsiFile baseFile = null;
      if (baseSystemId != null) {
        baseFile = getBaseFile(baseSystemId);
      }
      if (baseFile == null) {
        baseFile = myFile;
      }

      String version = null;
      String tagName = null;
      if (baseFile == myFile) {
        XmlTag rootTag = myFile.getRootTag();
        if (rootTag != null) {
          tagName = rootTag.getLocalName();
          version = rootTag.getAttributeValue("version");
        }
      }
      String resource = ((ExternalResourceManagerEx)ExternalResourceManager.getInstance()).getUserResource(myProject, systemId, version);
      if (resource != null) {
        XmlFile file = XmlUtil.findXmlFile(myFile, resource);
        if (file != null) return file;
      }

      PsiFile byLocation = resolveByLocation(myFile, systemId);
      if (byLocation != null) return byLocation;

      PsiFile psiFile = ExternalResourceManager.getInstance().getResourceLocation(systemId, baseFile, version);
      if (psiFile == null) {
        psiFile = XmlUtil.findXmlFile(baseFile, systemId);
      }
      // autodetection
      if (psiFile == null) {
        psiFile = XmlNamespaceIndex.guessSchema(systemId, tagName, version, null, myFile);
        if (psiFile == null) {
          psiFile = XmlNamespaceIndex.guessDtd(systemId, myFile);
        }
      }

      if (psiFile == null && baseSystemId != null) {
        String fullUrl = baseSystemId.substring( 0, baseSystemId.lastIndexOf('/') + 1 ) + systemId;
        psiFile = XmlUtil.findXmlFile(baseFile,fullUrl);
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("before relative file checking:"+psiFile+","+systemId+","+ baseSystemId+")");
      }
      if (psiFile == null && baseSystemId == null) { // entity file
        File workingFile = new File("");
        String workingDir = workingFile.getAbsoluteFile().getAbsolutePath().replace(File.separatorChar, '/') + "/";

        String relativePath = StringUtil.replace(systemId, workingDir, "");

        if (relativePath.equals(systemId)) {
          // on Windows systemId consisting of idea install path could become encoded DOS short name (e.g. idea%7f1.504)
          // I am not aware how to get such name from 'workingDir' so let just pickup filename from there
          relativePath = systemId.substring(systemId.lastIndexOf('/') + 1);
        }

        String res = myExternalResourcesMap.get(relativePath);
        if (res != null) {
          VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(res);
          if (file != null) {
            psiFile = PsiManager.getInstance(myProject).findFile(file);
            if (psiFile != null) return psiFile;
          }
        }

        if (LOG.isDebugEnabled()) {
          LOG.debug("next to relative file checking:"+relativePath+","+myExternalResourcesMap.size()+")");
        }

        for(String path:getResourcePaths()) {
          if (LOG.isDebugEnabled()) {
            LOG.debug("Finding file by url:" + path);
          }
          VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(path);
          if (file == null) continue;
          if (LOG.isDebugEnabled()) {
            LOG.debug("Finding "+relativePath+" relative to:"+file.getPath());
          }
          final VirtualFile relativeFile = UriUtil.findRelativeFile(relativePath, file);
          if (LOG.isDebugEnabled()) {
            LOG.debug("Found "+(relativeFile != null ? relativeFile.getPath():"null"));
          }

          if (relativeFile != null) {
            psiFile = PsiManager.getInstance(myProject).findFile(relativeFile);
            if (psiFile != null) break;
          }
        }
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("resolveEntity: psiFile='" + (psiFile != null ? psiFile.getVirtualFile() : null) + "'");
      }
      return psiFile;
    };

    final PsiFile psiFile = ApplicationManager.getApplication().runReadAction(action);
    if (psiFile != null) {
      final VirtualFile file = psiFile.getVirtualFile();
      if (file != null) {
        final String url = file.getUrl();
        if (LOG.isDebugEnabled()) {
          LOG.debug("Adding external resource ref:" + systemId + "," + url + "," + this);
        }
        myExternalResourcesMap.put(systemId,url);
      }
    }
    return psiFile;
  }
public @Nullable PsiFile resolve(final @Nullable String baseSystemId, final String _systemId)