/** * 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; }
public static String getAttributeName(XmlAttribute attribute) { if (attribute != null) { for (PsiElement child : attribute.getChildren()) { if (XmlHelper.isAttributeName(child)) { return child.getText(); } } } return null; }
public List<Element> getBlockElements(@NotNull Project project) { MagicentoProjectComponent magicento = MagicentoProjectComponent.getInstance(project); if (magicento != null) { File layoutFile = magicento.getCachedLayoutXml(this.getArea(), this.getPackage(), this.getTheme()); if (layoutFile != null && layoutFile.exists()) { Set<String> blocks = new LinkedHashSet<String>(); Set<String> factoriesAdded = new HashSet<String>(); String templatePath = this.getRelativePath(); String regex = "//block[@template='" + templatePath + "']"; List<Element> blocksElements = XmlHelper.findXpath(layoutFile, regex); if (blocksElements == null) { blocksElements = new ArrayList<Element>(); } // regex = "//action/*[.='"+templatePath+"']"; regex = "//*[action/*[.='" + templatePath + "']]"; List<Element> blocksElementsFromActions = XmlHelper.findXpath(layoutFile, regex); if (blocksElementsFromActions != null) { int n = blocksElementsFromActions.size(); for (int i = n - 1; i >= 0; i--) { Element blockElement = blocksElementsFromActions.get(i); if (blockElement.getName().equals("reference")) { String blockName = blockElement.getAttributeValue("name"); regex = "//block[@name='" + blockName + "']"; List<Element> referencedBlocks = XmlHelper.findXpath(layoutFile, regex); blocksElementsFromActions.remove(i); blocksElementsFromActions.addAll(referencedBlocks); } } } blocksElements.addAll(blocksElementsFromActions); return blocksElements; } } return null; }