com.intellij.psi.xml.XmlAttribute Java Examples

The following examples show how to use com.intellij.psi.xml.XmlAttribute. 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: CommonUtil.java    From svgtoandroid with MIT License 6 votes vote down vote up
public static void dumpAttrs(String tag, List<XmlAttribute> attrs) {

        if (!Logger.loggable(Logger.DEBUG)) {
            return;
        }

        if (attrs == null) {
            return;
        }

        String ret = "[";
        for (XmlAttribute attr : attrs) {
            ret += attr.getName() + ":" + attr.getValue() + ",";
        }
        if (ret.lastIndexOf(",") > 0) {
            ret = ret.substring(0, ret.lastIndexOf(","));
        }
        ret = ret + "]";
        Logger.debug(tag + ": " + ret);
    }
 
Example #2
Source File: FormUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static Set<String> getTags(XmlTag serviceTag) {

        Set<String> tags = new HashSet<>();

        for(XmlTag serviceSubTag: serviceTag.getSubTags()) {
            if("tag".equals(serviceSubTag.getName())) {
                XmlAttribute attribute = serviceSubTag.getAttribute("name");
                if(attribute != null) {
                    String tagName = attribute.getValue();
                    if(tagName != null && StringUtils.isNotBlank(tagName)) {
                        tags.add(tagName);
                    }
                }

            }
        }

        return tags;
    }
 
Example #3
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
/**
 * Get the "for IN" variable identifier as separated string
 * <p>
 * {% for car in "cars" %}
 * {% for car in "cars"|length %}
 * {% for car in "cars.test" %}
 */
@NotNull
public static Collection<String> getForTagIdentifierAsString(XmlTag forTag) {
    XmlAttribute each = forTag.getAttribute("each");
    if (each == null || each.getValueElement() == null) {
        return ContainerUtil.emptyList();
    }

    PsiElement fluidElement = FluidUtil.retrieveFluidElementAtPosition(each.getValueElement());
    if (fluidElement == null) {
        return Collections.emptyList();
    }

    PsiElement deepestFirst = PsiTreeUtil.getDeepestFirst(fluidElement);

    return FluidTypeResolver.formatPsiTypeName(deepestFirst);
}
 
Example #4
Source File: RouteXmlReferenceContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public ResolveResult[] multiResolve(boolean b) {
    PsiElement element = getElement();
    PsiElement parent = element.getParent();

    String text = null;
    if(parent instanceof XmlText) {
        // <route><default key="_controller">Fo<caret>o\Bar</default></route>
        text = parent.getText();
    } else if(parent instanceof XmlAttribute) {
        // <route controller=""/>
        text = ((XmlAttribute) parent).getValue();
    }

    if(text == null || StringUtils.isBlank(text)) {
        return new ResolveResult[0];
    }

    return PsiElementResolveResult.createResults(
        RouteHelper.getMethodsOnControllerShortcut(getElement().getProject(), text)
    );
}
 
Example #5
Source File: FluidInjector.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    if (context.getLanguage() == XMLLanguage.INSTANCE) return;
    final Project project = context.getProject();
    if (!FluidIndexUtil.hasFluid(project)) return;

    final PsiElement parent = context.getParent();
    if (context instanceof XmlAttributeValueImpl && parent instanceof XmlAttribute) {
        final int length = context.getTextLength();
        final String name = ((XmlAttribute) parent).getName();

        if (isInjectableAttribute(project, length, name)) {
            registrar
                .startInjecting(FluidLanguage.INSTANCE)
                .addPlace(null, null, (PsiLanguageInjectionHost) context, ElementManipulators.getValueTextRange(context))
                .doneInjecting();
        }
    }
}
 
Example #6
Source File: ControllerMethodInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public String getRouteName() {

    XmlTag defaultTag = PsiTreeUtil.getParentOfType(this.psiElement, XmlTag.class);
    if(defaultTag != null) {
        XmlTag routeTag = PsiTreeUtil.getParentOfType(defaultTag, XmlTag.class);
        if(routeTag != null) {
            XmlAttribute id = routeTag.getAttribute("id");
            if(id != null) {
                return id.getValue();
            }
        }
    }

    return null;
}
 
Example #7
Source File: HtmlTemplateLineUtil.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
/**
 * 判断是否是资源文件
 *
 * @param bindingElement
 * @return
 */
public static boolean isRes(PsiElement bindingElement) {
    if (bindingElement instanceof XmlToken) {
        XmlToken xmlToken = (XmlToken) bindingElement;
        List<String> layouts = BeetlHtmlLineUtil.showBeetlLayout(xmlToken);
        List<String> includes = BeetlHtmlLineUtil.showBeetlInclude(xmlToken);
        return layouts.size() > 0 || includes.size() > 0;
    }
    if (!(bindingElement instanceof XmlAttribute)) {
        return false;
    }
    XmlAttribute attribute = (XmlAttribute) bindingElement;
    String name = Strings.nullToEmpty(attribute.getName());
    String path = Strings.nullToEmpty(attribute.getValue());
    int sp = path.indexOf("?");
    if (sp > -1) {
        path = path.substring(0, sp);
    }
    return resTag.contains(name) && path.startsWith("${") && endWithRes(path);
}
 
Example #8
Source File: AndroidUtils.java    From idea-android-studio-plugin with GNU General Public License v2.0 6 votes vote down vote up
@NotNull
public static List<AndroidView> getIDsFromXML(@NotNull PsiFile f) {
    final ArrayList<AndroidView> ret = new ArrayList<AndroidView>();
    f.accept(new XmlRecursiveElementVisitor() {
        @Override
        public void visitElement(final PsiElement element) {
            super.visitElement(element);
            if (element instanceof XmlTag) {
                XmlTag t = (XmlTag) element;
                XmlAttribute id = t.getAttribute("android:id", null);
                if (id == null) {
                    return;
                }
                final String val = id.getValue();
                if (val == null) {
                    return;
                }
                ret.add(new AndroidView(val, t.getName(), id));

            }

        }
    });

    return ret;
}
 
Example #9
Source File: Sqls2XmlNavigationHandler.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
private PsiClass findJavaPsiClass(PsiElement psiElement) {
    XmlTag parentOfType = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
    if (parentOfType.getName().equals("Sqls")) {
        XmlAttribute aClass = parentOfType.getAttribute("class");
        if (Objects.nonNull(aClass)) {
            String value = aClass.getValue();
            String[] split = value.split("\\.");
            Collection<PsiClass> roleBizImpl = JavaShortClassNameIndex.getInstance().get(split[split.length - 1], psiElement.getProject(), GlobalSearchScope.projectScope(psiElement.getProject()));
            for (PsiClass psiClass : roleBizImpl) {
                if (psiClass.getQualifiedName().equals(value)) {
                    return psiClass;
                }
            }
        }
    }
    return null;
}
 
Example #10
Source File: FlowInPlaceRenamer.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
private static Template buildTemplate(@NotNull XmlAttribute attr, List<XmlTag> refs) {
        //XmlFile containingFile = (XmlFile)attr.getContainingFile();
        PsiElement commonParent = PsiTreeUtil.findCommonParent(refs);
        TemplateBuilderImpl builder = new TemplateBuilderImpl(attr);

        XmlAttributeValue attrValue = attr.getValueElement();
        PsiElement valuePsi = attrValue.getFirstChild().getNextSibling();

        String flowNameValue = new String(attrValue.getValue());
        builder.replaceElement(valuePsi,"PrimaryVariable", new TextExpression(flowNameValue), true);
/*

        for (XmlTag ref : refs) {
            if (ref.getContainingFile().equals(attr.getContainingFile())) {
                XmlAttribute nextAttr = ref.getAttribute(MuleConfigConstants.NAME_ATTRIBUTE);
                XmlAttributeValue nextValue = nextAttr.getValueElement();
                PsiElement nextValuePsi = nextValue.getFirstChild().getNextSibling();
                builder.replaceElement(nextValuePsi, "OtherVariable", "PrimaryVariable",false);
            }
        }
*/

        return builder.buildInlineTemplate();
    }
 
Example #11
Source File: LatteXmlFileDataFactory.java    From intellij-latte with MIT License 6 votes vote down vote up
private static void loadFilters(XmlTag customFilters, LatteXmlFileData data) {
    for (XmlTag filterData : customFilters.findSubTags("filter")) {
        XmlAttribute name = filterData.getAttribute("name");
        if (name == null) {
            continue;
        }

        LatteFilterSettings filter = new LatteFilterSettings(
                name.getValue(),
                getTextValue(filterData, "description"),
                getTextValue(filterData, "arguments"),
                getTextValue(filterData, "insertColons")
        );
        data.addFilter(filter);
    }
}
 
Example #12
Source File: LatteXmlFileDataFactory.java    From intellij-latte with MIT License 6 votes vote down vote up
private static void loadTags(XmlTag customTags, LatteXmlFileData data) {
    for (XmlTag tag : customTags.findSubTags("tag")) {
        XmlAttribute name = tag.getAttribute("name");
        XmlAttribute type = tag.getAttribute("type");
        if (name == null || type == null || !LatteTagSettings.isValidType(type.getValue())) {
            continue;
        }

        LatteTagSettings macro = new LatteTagSettings(
                name.getValue(),
                LatteTagSettings.Type.valueOf(type.getValue()),
                isTrue(tag, "allowedFilters"),
                getTextValue(tag, "arguments"),
                isTrue(tag, "multiLine"),
                getTextValue(tag, "deprecatedMessage").trim(),
                getArguments(tag)
        );

        data.addTag(macro);
    }
}
 
Example #13
Source File: LatteXmlFileDataFactory.java    From intellij-latte with MIT License 6 votes vote down vote up
private static void loadFunctions(XmlTag customFunctions, LatteXmlFileData data) {
    for (XmlTag filter : customFunctions.findSubTags("function")) {
        XmlAttribute name = filter.getAttribute("name");
        if (name == null) {
            continue;
        }

        String returnType = getTextValue(filter, "returnType");
        LatteFunctionSettings instance = new LatteFunctionSettings(
                name.getValue(),
                returnType.length() == 0 ? "mixed" : returnType,
                getTextValue(filter, "arguments"),
                getTextValue(filter, "description")
        );
        data.addFunction(instance);
    }
}
 
Example #14
Source File: ContainerSettingDeprecatedInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    return new PsiElementVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if(element instanceof XmlAttribute) {
                registerXmlAttributeProblem(holder, (XmlAttribute) element);
            } else if(element instanceof YAMLKeyValue) {
                registerYmlRoutePatternProblem(holder, (YAMLKeyValue) element);
            }

            super.visitElement(element);
        }
    };
}
 
Example #15
Source File: MuleSchemaUtils.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
public static void insertSchemaLocationLookup(XmlFile xmlFile, String namespace, String locationLookup) {
    final XmlTag rootTag = xmlFile.getRootTag();
    if (rootTag == null)
        return;
    final XmlAttribute attribute = rootTag.getAttribute("xsi:schemaLocation", HTTP_WWW_W3_ORG_2001_XMLSCHEMA);
    if (attribute != null) {
        final String value = attribute.getValue();
        attribute.setValue(value + "\n\t\t\t" + namespace + " " + locationLookup);
    } else {
        final XmlElementFactory elementFactory = XmlElementFactory.getInstance(xmlFile.getProject());
        final XmlAttribute schemaLocation = elementFactory.createXmlAttribute("xsi:schemaLocation", XmlUtil.XML_SCHEMA_INSTANCE_URI);
        schemaLocation.setValue(namespace + " " + locationLookup);
        rootTag.add(schemaLocation);
    }

}
 
Example #16
Source File: Transformer.java    From svgtoandroid with MIT License 6 votes vote down vote up
private String decideFillColor(XmlTag group, XmlTag self) {
    String result = Configuration.getDefaultTint();

    XmlAttribute groupFill;
    if ((groupFill = group.getAttribute("fill")) != null) {
        result = StdColorUtil.formatColor(groupFill.getValue());
    }

    XmlAttribute selfFill;
    if ((selfFill = self.getAttribute("fill")) != null) {
        result = StdColorUtil.formatColor(selfFill.getValue());
    }

    XmlAttribute id = self.getAttribute("class");
    String colorFromStyle;
    if (id != null && id.getValue() != null && (colorFromStyle = styleParser.getFillColor(id.getValue())) != null) {
        result = colorFromStyle;
    }

    return result;
}
 
Example #17
Source File: XmlCamelIdeaUtils.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isPlaceForEndpointUri(PsiElement location) {
    XmlFile file = PsiTreeUtil.getParentOfType(location, XmlFile.class);
    if (file == null || file.getRootTag() == null || !isAcceptedNamespace(file.getRootTag().getNamespace())) {
        return false;
    }
    XmlAttributeValue value = PsiTreeUtil.getParentOfType(location, XmlAttributeValue.class, false);
    if (value == null) {
        return false;
    }
    XmlAttribute attr = PsiTreeUtil.getParentOfType(location, XmlAttribute.class);
    if (attr == null) {
        return false;
    }
    return attr.getLocalName().equals("uri") && isInsideCamelRoute(location, false);
}
 
Example #18
Source File: RouteHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PsiElement getXmlRouteNameTarget(@NotNull XmlFile psiFile,@NotNull String routeName) {

    XmlDocumentImpl document = PsiTreeUtil.getChildOfType(psiFile, XmlDocumentImpl.class);
    if(document == null) {
        return null;
    }

    for(XmlTag xmlTag: PsiTreeUtil.getChildrenOfTypeAsList(psiFile.getFirstChild(), XmlTag.class)) {
        if(xmlTag.getName().equals("routes")) {
            for(XmlTag routeTag: xmlTag.getSubTags()) {
                if(routeTag.getName().equals("route")) {
                    XmlAttribute xmlAttribute = routeTag.getAttribute("id");
                    if(xmlAttribute != null) {
                        String attrValue = xmlAttribute.getValue();
                        if(routeName.equals(attrValue)) {
                            return xmlAttribute;
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example #19
Source File: SVGParser.java    From svgtoandroid with MIT License 6 votes vote down vote up
public Map<String, String> getChildAttrs(XmlTag tag) {
    Map<String, String> styles = new HashMap<String, String>();
    XmlAttribute attrs[] = tag.getAttributes();
    for (XmlAttribute attr : attrs) {
        if (attr.getName().equals("style")) {
            String stylesValue = tag.getAttributeValue("style").replaceAll("\\s*", "");
            String[] list = stylesValue.split(";");
            for (String s : list) {
                String[] item = s.split(":");
                if (item[0].equals("fill")) {
                    item[1] = StdColorUtil.formatColor(item[1]);
                }
                styles.put(item[0], item[1]);
            }
        } else {
            if (attr.getName().equals("fill")) {
                styles.put(attr.getName(), StdColorUtil.formatColor(tag.getAttributeValue(attr.getName())));
            } else {
                styles.put(attr.getName(), tag.getAttributeValue(attr.getName()));
            }
        }
    }
    Logger.debug(tag.getName() + styles.toString());
    return styles;
}
 
Example #20
Source File: SVGParser.java    From svgtoandroid with MIT License 6 votes vote down vote up
public XmlTag trim(XmlTag rootTag, List<XmlAttribute> attr) {
    Logger.debug("Current tag: " + rootTag.getName());
    CommonUtil.dumpAttrs("current attr", Arrays.asList(rootTag.getAttributes()));
    CommonUtil.dumpAttrs("parent attr", attr);
    if (attr == null) {
        attr = new ArrayList<XmlAttribute>();
        attr.addAll(Arrays.asList(rootTag.getAttributes()));
    }
    XmlTag[] subTags = rootTag.getSubTags();
    List<XmlAttribute> attrs = new ArrayList<XmlAttribute>();
    if (subTags.length == 1 && subTags[0].getName().equals("g")) {
        Logger.debug("Tag" + rootTag + " has only a subTag and the tag is 'g'");
        Collections.addAll(attrs, subTags[0].getAttributes());
        attrs.addAll(attr);
        rootTag = trim(subTags[0], attrs);
    } else if (subTags.length > 0 && AttrMapper.isShapeName(subTags[0].getName())) {
        Logger.debug(rootTag.getSubTags()[0].getName());
        Logger.debug("Tag" + rootTag + " is correct tag.");
        for (XmlAttribute attribute : attr) {
            Logger.debug(attribute.getName() + ":" + attribute.getValue());
        }
        return AttrMergeUtil.mergeAttrs((XmlTag) rootTag.copy(), reduceAttrs(attr));
    }
    return rootTag;
}
 
Example #21
Source File: LatteXmlFileDataFactory.java    From intellij-latte with MIT License 5 votes vote down vote up
private static void loadVariables(XmlTag customVariables, LatteXmlFileData data) {
    for (XmlTag filter : customVariables.findSubTags("variable")) {
        XmlAttribute name = filter.getAttribute("name");
        if (name == null ) {
            continue;
        }

        String varType = getTextValue(filter, "type");
        LatteVariableSettings variable = new LatteVariableSettings(
                name.getValue(),
                varType.length() == 0 ? "mixed" : varType
        );
        data.addVariable(variable);
    }
}
 
Example #22
Source File: RouteSettingDeprecatedInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void registerRoutePatternProblem(@NotNull ProblemsHolder holder, @NotNull XmlAttribute xmlAttribute) {
    if(!"pattern".equals(xmlAttribute.getName())) {
        return;
    }

    XmlTag xmlTagRoute = PsiElementAssertUtil.getParentOfTypeWithNameOrNull(xmlAttribute, XmlTag.class, "route");
    if(xmlTagRoute != null && xmlAttribute.getFirstChild() != null) {
        holder.registerProblem(xmlAttribute.getFirstChild(), "Pattern is deprecated; use path instead", ProblemHighlightType.LIKE_DEPRECATED);
    }
}
 
Example #23
Source File: HtmlNSNamespaceProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
    if (attribute.getName().startsWith("xmlns:") && attribute.getValue() != null) {
        if (attribute.getValue().startsWith(fluidNamespaceURIPrefix)) {
            namespaces.add(new FluidNamespace(attribute.getLocalName(), attribute.getValue().substring(fluidNamespaceURIPrefix.length())));
        }
    }

    super.visitXmlAttribute(attribute);
}
 
Example #24
Source File: RTDocumentationProvider.java    From react-templates-plugin with MIT License 5 votes vote down vote up
@Override
public String generateDoc(PsiElement element, PsiElement originalElement) {
    if (element instanceof XmlAttribute) {
        XmlAttribute xmlAttr = (XmlAttribute) element;
        String name = xmlAttr.getName();
        if (name.startsWith("rt-")) {
            return getDoc(name);
        }
    }
    return "documentation is not available yet";
}
 
Example #25
Source File: RTDocumentationProvider.java    From react-templates-plugin with MIT License 5 votes vote down vote up
@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
    if (element instanceof XmlAttribute) {
        XmlAttribute xmlAttr = (XmlAttribute) element;
        if (xmlAttr.getName().startsWith("rt-")) {
            return "<strong>" + xmlAttr.getName() + "</strong> documentation is not available yet";
        }
    }
    return null;
}
 
Example #26
Source File: RTDocumentationProvider.java    From react-templates-plugin with MIT License 5 votes vote down vote up
@Override
public List<String> getUrlFor(PsiElement element, PsiElement originalElement) {
    if (element instanceof XmlAttribute) {
        if (((XmlAttribute) element).getName().startsWith("rt-")) {
            return Collections.singletonList("http://www.github.com/wix/react-templates");
        }
    }
    return null;
}
 
Example #27
Source File: RTDocumentationProvider.java    From react-templates-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PsiElement getCustomDocumentationElement(@NotNull Editor editor,
                                                @NotNull PsiFile file,
                                                @Nullable PsiElement element) {
    final IElementType elementType = element != null ? element.getNode().getElementType() : null;
    if (elementType == XmlTokenType.XML_NAME || elementType == XmlTokenType.XML_TAG_NAME) {
        return PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
    }
    if (elementType == XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN) {
        return PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
    }
    return null;
}
 
Example #28
Source File: RTAttributes.java    From react-templates-plugin with MIT License 5 votes vote down vote up
public static boolean isRTJSExpressionAttribute(XmlAttribute parent) {
        final String attributeName = DirectiveUtil.normalizeAttributeName(parent.getName());
        return isJSAttribute(attributeName);
//        final JSOffsetBasedImplicitElement directive = AngularIndexUtil.resolve(parent.getProject(), AngularDirectivesDocIndex.INDEX_ID, attributeName);
//        if (directive != null) {
//            final String restrict = directive.getTypeString();
//            final String param = restrict.split(";", -1)[2];
//            return param.endsWith("expression") || param.startsWith("string");
//        }
//        return false;
    }
 
Example #29
Source File: BeanReferenceProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected PsiReference[] getAttributeReferences(@NotNull XmlAttribute attribute, @NotNull XmlAttributeValue value,
                                                ProcessingContext context) {
    String beanId = attribute.getValue();
    if (beanId != null && beanId.length() > 0) {
        if ("ref".equals(attribute.getLocalName())) {
            return new PsiReference[] {new BeanReference(value, beanId)};
        } else if ("id".equals(attribute.getLocalName())) {
            return new PsiReference[] {new BeanSelfReference(value, beanId)};
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
 
Example #30
Source File: RTAttributes.java    From react-templates-plugin with MIT License 5 votes vote down vote up
public static boolean isRTJSExpressionAttribute(XmlAttribute parent) {
        final String attributeName = DirectiveUtil.normalizeAttributeName(parent.getName());
        return isJSAttribute(attributeName);
//        final JSOffsetBasedImplicitElement directive = AngularIndexUtil.resolve(parent.getProject(), AngularDirectivesDocIndex.INDEX_ID, attributeName);
//        if (directive != null) {
//            final String restrict = directive.getTypeString();
//            final String param = restrict.split(";", -1)[2];
//            return param.endsWith("expression") || param.startsWith("string");
//        }
//        return false;
    }