예제 #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;
  }
 @Test
 public void removeAllElements() throws Exception {
   List<ParserFeature> features = Collections.emptyList();
   document =
       XmlHelper.parse(
           getClass()
               .getResourceAsStream(
                   "/com/google/code/configprocessor/data/xml-target-config-2.xml"),
           features);
   RemoveAction action =
       new RemoveAction("/root/property[@attribute='value3']", NodeSetPolicy.ALL);
   XmlActionProcessingAdvisor advisor =
       new XmlRemoveActionProcessingAdvisor(
           action, expressionResolver, namespaceContext, Collections.<ParserFeature>emptyList());
   String expected =
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
           + LINE_SEPARATOR
           + "<root>"
           + LINE_SEPARATOR
           + " <property5>"
           + LINE_SEPARATOR
           + "  <nested1 a=\"1\"/>"
           + LINE_SEPARATOR
           + " </property5>"
           + LINE_SEPARATOR
           + "</root>"
           + LINE_SEPARATOR;
   executeTest(advisor, expected);
 }
예제 #3
0
 public static String getAttributeName(XmlAttribute attribute) {
   if (attribute != null) {
     for (PsiElement child : attribute.getChildren()) {
       if (XmlHelper.isAttributeName(child)) {
         return child.getText();
       }
     }
   }
   return null;
 }