Java Code Examples for com.intellij.psi.xml.XmlTag#findFirstSubTag()

The following examples show how to use com.intellij.psi.xml.XmlTag#findFirstSubTag() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultViewHelpersProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
private @NotNull
String extractDocumentation(XmlTag attributeTag) {
    StringBuilder attributeDocumentation = new StringBuilder();

    XmlTag attributeAnnotation = attributeTag.findFirstSubTag("xsd:annotation");
    if (attributeAnnotation != null) {
        XmlTag attributeDoc = attributeAnnotation.findFirstSubTag("xsd:documentation");
        if (attributeDoc != null) {
            for (XmlText textElement : attributeDoc.getValue().getTextElements()) {
                attributeDocumentation.append(textElement.getValue());
            }
        }
    }

    return attributeDocumentation.toString();
}
 
Example 2
Source File: LatteXmlFileDataFactory.java    From intellij-latte with MIT License 5 votes vote down vote up
@Nullable
public static LatteXmlFileData parse(XmlFile file) {
    XmlDocument document = file.getDocument();
    if (document == null || document.getRootTag() == null) {
        return null;
    }

    XmlTag configuration = document.getRootTag();
    if (configuration == null) {
        return null;
    }

    LatteXmlFileData.VendorResult vendor = getVendor(document);
    if (vendor == null) {
        return null;
    }

    LatteXmlFileData data = new LatteXmlFileData(vendor);
    XmlTag tags = configuration.findFirstSubTag("tags");
    if (tags != null) {
        loadTags(tags, data);
    }
    XmlTag filters = configuration.findFirstSubTag("filters");
    if (filters != null) {
        loadFilters(filters, data);
    }
    XmlTag variables = configuration.findFirstSubTag("variables");
    if (variables != null) {
        loadVariables(variables, data);
    }
    XmlTag functions = configuration.findFirstSubTag("functions");
    if (functions != null) {
        loadFunctions(functions, data);
    }
    return data;
}
 
Example 3
Source File: LatteXmlFileDataFactory.java    From intellij-latte with MIT License 5 votes vote down vote up
private static List<LatteArgumentSettings> getArguments(XmlTag tag) {
    XmlTag arguments = tag.findFirstSubTag("arguments");
    if (arguments == null) {
        return Collections.emptyList();
    }

    List<LatteArgumentSettings> out = new ArrayList<>();
    for (XmlTag argument : arguments.findSubTags("argument")) {
        XmlAttribute name = argument.getAttribute("name");
        XmlAttribute typesString = argument.getAttribute("types");
        if (name == null || typesString == null) {
            continue;
        }

        LatteArgumentSettings.Type[] types = LatteArgumentSettings.getTypes(typesString.getValue());
        if (types == null) {
            continue;
        }

        String validType = getTextValue(argument, "validType");
        LatteArgumentSettings instance = new LatteArgumentSettings(
                name.getValue(),
                types,
                validType.length() == 0 ? "mixed" : validType,
                isTrue(argument, "required"),
                isTrue(argument, "repeatable")
        );
        out.add(instance);
    }
    return out;
}
 
Example 4
Source File: DefaultViewHelpersProvider.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
@Override
public void visitXmlTag(XmlTag tag) {
    if (!tag.getName().equals("xsd:element")) {
        super.visitXmlTag(tag);

        return;
    }

    XmlAttribute nameAttribute = tag.getAttribute("name");
    if (nameAttribute == null || nameAttribute.getValue() == null) {
        super.visitXmlTag(tag);

        return;
    }

    ViewHelper viewHelper = new ViewHelper(nameAttribute.getValue());
    viewHelper.setDocumentation(extractDocumentation(tag));

    XmlTag complexType = tag.findFirstSubTag("xsd:complexType");
    if (complexType != null) {
        XmlTag[] attributeTags = complexType.findSubTags("xsd:attribute");
        for (XmlTag attributeTag : attributeTags) {
            String argumentName = attributeTag.getAttributeValue("name");
            if (argumentName == null) {
                continue;
            }

            ViewHelperArgument argument = new ViewHelperArgument(argumentName);

            argument.setDocumentation(extractDocumentation(attributeTag));

            String attributeType = attributeTag.getAttributeValue("php:type");
            if (attributeType == null) {
                argument.setType("mixed");
            } else {
                argument.setType(attributeType);
            }

            String requiredAttribute = attributeTag.getAttributeValue("use");
            if (requiredAttribute != null && requiredAttribute.equals("required")) {
                argument.setRequired(true);
            }

            viewHelper.addArgument(argumentName, argument);
        }
    }

    viewHelpers.put(nameAttribute.getValue(), viewHelper);

    super.visitXmlTag(tag);
}
 
Example 5
Source File: ConfigUtil.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
public static void visitNamespaceConfigurations(@NotNull Project project, @NotNull String namespace, @NotNull Consumer<Pair<String, XmlTag>> pairConsumer) {
    for(PhpClass phpClass: PhpIndex.getInstance(project).getAllSubclasses(ShopwareFQDN.PLUGIN_BOOTSTRAP)) {
        if(!namespace.equalsIgnoreCase(phpClass.getName())) {
            continue;
        }

        PsiDirectory parent = phpClass.getContainingFile().getParent();
        if(parent == null) {
            continue;
        }

        VirtualFile resources = VfsUtil.findRelativeFile(parent.getVirtualFile(), "Resources", "config.xml");
        if(resources == null) {
            continue;
        }

        PsiFile file = PsiManager.getInstance(project).findFile(resources);
        if(!(file instanceof XmlFile)) {
            continue;
        }

        XmlTag rootTag = ((XmlFile) file).getRootTag();
        if(rootTag == null) {
            continue;
        }

        XmlTag elements = rootTag.findFirstSubTag("elements");
        if(elements == null) {
            continue;
        }

        for (XmlTag element : elements.findSubTags("element")) {
            XmlTag xmlTag = element.findFirstSubTag("name");
            if(xmlTag != null) {
                String text = xmlTag.getValue().getText();
                if(StringUtils.isNotBlank(text)) {
                    pairConsumer.accept(Pair.create(text, xmlTag));
                }
            }
        }
    }
}
 
Example 6
Source File: EntityHelper.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@NotNull
public static String getOrmClass(@NotNull PsiFile psiFile, @NotNull String className) {

    // force global namespace not need to search for class
    if(className.startsWith("\\")) {
        return className;
    }

    String entityName = null;

    // espend\Doctrine\ModelBundle\Entity\Bike:
    // ...
    // targetEntity: Foo
    if(psiFile instanceof YAMLFile) {
        YAMLDocument yamlDocument = PsiTreeUtil.getChildOfType(psiFile, YAMLDocument.class);
        if(yamlDocument != null) {
            YAMLKeyValue entityKeyValue = PsiTreeUtil.getChildOfType(yamlDocument, YAMLKeyValue.class);
            if(entityKeyValue != null) {
                entityName = entityKeyValue.getKeyText();
            }
        }
    } else if(psiFile instanceof XmlFile) {

        XmlTag rootTag = ((XmlFile) psiFile).getRootTag();
        if(rootTag != null) {
            XmlTag entity = rootTag.findFirstSubTag("entity");
            if(entity != null) {
                String name = entity.getAttributeValue("name");
                if(org.apache.commons.lang.StringUtils.isBlank(name)) {
                    entityName = name;
                }
            }
        }
    }

    if(entityName == null) {
        return className;
    }

    // trim class name
    int lastBackSlash = entityName.lastIndexOf("\\");
    if(lastBackSlash > 0) {
        String fqnClass = entityName.substring(0, lastBackSlash + 1) + className;
        if(PhpElementsUtil.getClass(psiFile.getProject(), fqnClass) != null) {
            return fqnClass;
        }
    }

    return className;
}
 
Example 7
Source File: ProfilerUtil.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * ["foo/foo.html.twig": 1]
 *
 * <tr>
 *  <td>@Twig/Exception/traces_text.html.twig</td>
 *  <td class="font-normal">1</td>
 * </tr>
 */
public static Map<String, Integer> getRenderedElementTwigTemplates(@NotNull Project project, @NotNull String html) {
    HtmlFileImpl htmlFile = (HtmlFileImpl) PsiFileFactory.getInstance(project).createFileFromText(HTMLLanguage.INSTANCE, html);

    final XmlTag[] xmlTag = new XmlTag[1];
    PsiTreeUtil.processElements(htmlFile, psiElement -> {
        if(!(psiElement instanceof XmlTag) || !"h2".equals(((XmlTag) psiElement).getName())) {
            return true;
        }

        XmlTagValue keyTag = ((XmlTag) psiElement).getValue();
        String contents = StringUtils.trim(keyTag.getText());
        if(!"Rendered Templates".equalsIgnoreCase(contents)) {
            return true;
        }

        xmlTag[0] = (XmlTag) psiElement;

        return true;
    });

    if(xmlTag[0] == null) {
        return Collections.emptyMap();
    }

    XmlTag tableTag = PsiTreeUtil.getNextSiblingOfType(xmlTag[0], XmlTag.class);
    if(tableTag == null || !"table".equals(tableTag.getName())) {
        return Collections.emptyMap();
    }

    XmlTag tbody = tableTag.findFirstSubTag("tbody");
    if(tbody == null) {
        return Collections.emptyMap();
    }

    Map<String, Integer> templates = new HashMap<>();

    for (XmlTag tag : PsiTreeUtil.getChildrenOfTypeAsList(tbody, XmlTag.class)) {
        if(!"tr".equals(tag.getName())) {
            continue;
        }

        XmlTag[] tds = tag.findSubTags("td");
        if(tds.length < 2) {
            continue;
        }

        String template = stripHtmlTags(StringUtils.trim(tds[0].getValue().getText()));
        if(StringUtils.isBlank(template)) {
            continue;
        }

        Integer count;
        try {
            count = Integer.valueOf(stripHtmlTags(StringUtils.trim(tds[1].getValue().getText())));
        } catch (NumberFormatException e) {
            count = 0;
        }

        templates.put(template, count);
    }

    return templates;
}