示例#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;
  }
示例#2
0
 public static String getAttributeName(XmlAttribute attribute) {
   if (attribute != null) {
     for (PsiElement child : attribute.getChildren()) {
       if (XmlHelper.isAttributeName(child)) {
         return child.getText();
       }
     }
   }
   return null;
 }