Example #1
0
  /**
   * TODO: this method should be removed when find by xpath is ready, and currently works fine only
   * with one attribute
   *
   * @return
   */
  @NotNull
  public static List<XmlTag> findTagInFile(
      @NotNull XmlFile xmlFile, @NotNull String nodeName, Map<String, String> attributes) {
    List<XmlTag> xmlTags = new ArrayList<XmlTag>();
    // XmlTag rootTag = xmlFile.getRootTag();
    // xmlFile.getDocument().getManager();
    String xmlText = xmlFile.getText();

    // final XPathSupport support = XPathSupport.getInstance();

    String regex = "<" + nodeName + "[\\s>]+.*?";
    if (attributes != null) {
      for (Map.Entry<String, String> entry : attributes.entrySet()) {
        String attrName = entry.getKey();
        String attrValue = entry.getValue();
        regex += attrName + "\\s*" + "=\\s*\"" + attrValue + "\"\\s*";
      }
    }
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(xmlText);
    List<Integer> positions = new ArrayList<Integer>();
    while (m.find()) {
      positions.add(m.start());
    }
    for (Integer position : positions) {
      PsiElement psiElement = xmlFile.findElementAt(position);
      XmlTag xmlTag = XmlHelper.getParentOfType(psiElement, XmlTag.class, false);
      if (xmlTag != null) {
        xmlTags.add(xmlTag);
      }
    }

    return xmlTags;
  }
  private Map<ProblemDescriptor, HighlightDisplayLevel> runXmlFileSchemaValidation(
      @NotNull XmlFile xmlFile) {
    final AnnotationHolderImpl holder = new AnnotationHolderImpl(new AnnotationSession(xmlFile));

    final List<ExternalAnnotator> annotators =
        ExternalLanguageAnnotators.allForFile(StdLanguages.XML, xmlFile);
    for (ExternalAnnotator<?, ?> annotator : annotators) {
      processAnnotator(xmlFile, holder, annotator);
    }

    if (!holder.hasAnnotations()) return Collections.emptyMap();

    Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap =
        new LinkedHashMap<ProblemDescriptor, HighlightDisplayLevel>();
    for (final Annotation annotation : holder) {
      final HighlightInfo info = HighlightInfo.fromAnnotation(annotation);
      if (info.getSeverity() == HighlightSeverity.INFORMATION) continue;

      final PsiElement startElement = xmlFile.findElementAt(info.startOffset);
      final PsiElement endElement =
          info.startOffset == info.endOffset
              ? startElement
              : xmlFile.findElementAt(info.endOffset - 1);
      if (startElement == null || endElement == null) continue;

      final ProblemDescriptor descriptor =
          myInspectionManager.createProblemDescriptor(
              startElement,
              endElement,
              info.getDescription(),
              ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
              false);
      final HighlightDisplayLevel level =
          info.getSeverity() == HighlightSeverity.ERROR
              ? HighlightDisplayLevel.ERROR
              : HighlightDisplayLevel.WARNING;
      problemsMap.put(descriptor, level);
    }
    return problemsMap;
  }