@NotNull
  private static XmlFileHeader calcXmlFileHeader(final XmlFile file) {

    if (file instanceof PsiFileEx
        && ((PsiFileEx) file).isContentsLoaded()
        && file.getNode().isParsed()) {
      return computeHeaderByPsi(file);
    }

    if (!XmlUtil.isStubBuilding() && file.getFileType() == XmlFileType.INSTANCE) {
      VirtualFile virtualFile = file.getVirtualFile();
      if (virtualFile instanceof VirtualFileWithId) {
        ObjectStubTree tree =
            StubTreeLoader.getInstance().readFromVFile(file.getProject(), virtualFile);
        if (tree != null) {
          Stub root = tree.getRoot();
          if (root instanceof FileStub) {
            return ((FileStub) root).getHeader();
          }
        }
      }
    }

    if (!file.isValid()) return XmlFileHeader.EMPTY;

    return NanoXmlUtil.parseHeader(file);
  }
        @NotNull
        public Map<Integer, Void> map(final FileContent inputData) {
          final Map<Integer, Void> map = new HashMap<Integer, Void>();

          NanoXmlUtil.parse(
              new StringReader(inputData.getContentAsText().toString()),
              new NanoXmlUtil.IXMLBuilderAdapter() {
                private boolean isFirstElement = true;

                public void startElement(
                    final String elemName,
                    final String nsPrefix,
                    final String nsURI,
                    final String systemID,
                    final int lineNr)
                    throws Exception {
                  if (isFirstElement) {
                    if (!"project".equalsIgnoreCase(elemName)) {
                      stop();
                    }
                    isFirstElement = false;
                  } else {
                    if ("import".equalsIgnoreCase(elemName)
                        || "include".equalsIgnoreCase(elemName)) {
                      map.put(ANT_FILES_WITH_IMPORTS_KEY, null);
                      stop();
                    }
                  }
                }

                public void addAttribute(
                    final String key,
                    final String nsPrefix,
                    final String nsURI,
                    final String value,
                    final String type)
                    throws Exception {
                  // if (myAttributes != null) {
                  //  myAttributes.add(key);
                  // }
                }

                public void elementAttributesProcessed(
                    final String name, final String nsPrefix, final String nsURI) throws Exception {
                  // if (myAttributes != null) {
                  //  if (!(myAttributes.contains("name") && myAttributes.contains("default"))) {
                  //    stop();
                  //  }
                  //  myAttributes = null;
                  // }
                }
              });
          return map;
        }
Пример #3
0
 public static boolean isTestngXML(final VirtualFile virtualFile) {
   if ("xml".equalsIgnoreCase(virtualFile.getExtension())
       && virtualFile.isInLocalFileSystem()
       && virtualFile.isValid()) {
     final String result = NanoXmlUtil.parseHeader(virtualFile).getRootTagLocalName();
     if (result != null && result.equals(SUITE_TAG_NAME)) {
       return true;
     }
   }
   return false;
 }
  @NotNull
  private static XmlFileHeader calcXmlFileHeader(final PsiFile file) {

    // if (file.getFileType() == XmlFileType.INSTANCE) {
    //  VirtualFile virtualFile = file.getVirtualFile();
    //  if (virtualFile instanceof VirtualFileWithId) {
    //    ObjectStubTree tree = StubTreeLoader.getInstance().readFromVFile(file.getProject(),
    // virtualFile);
    //    if (tree != null) {
    //      return ((FileStub)tree.getRoot()).getHeader();
    //    }
    //  }
    // }
    if (file instanceof XmlFile && file.getNode().isParsed()) {
      final XmlDocument document = ((XmlFile) file).getDocument();
      if (document != null) {
        String publicId = null;
        String systemId = null;
        final XmlProlog prolog = document.getProlog();
        if (prolog != null) {
          final XmlDoctype doctype = prolog.getDoctype();
          if (doctype != null) {
            publicId = doctype.getPublicId();
            systemId = doctype.getSystemId();
            if (systemId == null) {
              systemId = doctype.getDtdUri();
            }
          }
        }

        final XmlTag tag = document.getRootTag();
        if (tag != null) {
          String localName = tag.getLocalName();
          if (StringUtil.isNotEmpty(localName)) {
            if (tag.getPrevSibling() instanceof PsiErrorElement) {
              return XmlFileHeader.EMPTY;
            }

            String psiNs = tag.getNamespace();
            return new XmlFileHeader(
                localName,
                psiNs == XmlUtil.EMPTY_URI || Comparing.equal(psiNs, systemId) ? null : psiNs,
                publicId,
                systemId);
          }
        }
      }
      return XmlFileHeader.EMPTY;
    }

    if (!file.isValid()) return XmlFileHeader.EMPTY;
    return NanoXmlUtil.parseHeader(file);
  }
    @Nullable
    private static String getControllerClassName(String content) {
      if (!content.contains(JavaFxNamespaceDataProvider.JAVAFX_NAMESPACE)) {
        return null;
      }

      final String[] className = new String[] {null};

      class StopException extends RuntimeException {}

      try {
        NanoXmlUtil.parse(
            new StringReader(content),
            new NanoXmlUtil.IXMLBuilderAdapter() {
              private boolean myFxRootUsed = false;

              @Override
              public void addAttribute(
                  String key, String nsPrefix, String nsURI, String value, String type)
                  throws Exception {
                if (value != null
                    && (FxmlConstants.FX_CONTROLLER.equals(nsPrefix + ":" + key)
                        || FxmlConstants.TYPE.equals(key) && myFxRootUsed)) {
                  className[0] = value;
                }
              }

              @Override
              public void elementAttributesProcessed(String name, String nsPrefix, String nsURI)
                  throws Exception {
                throw new StopException();
              }

              @Override
              public void startElement(
                  String name, String nsPrefix, String nsURI, String systemID, int lineNr)
                  throws Exception {
                myFxRootUsed = FxmlConstants.FX_ROOT.equals(nsPrefix + ":" + name);
              }
            });
      } catch (StopException ignore) {
      }
      return className[0];
    }