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

The following examples show how to use com.intellij.psi.xml.XmlTag#setAttribute() . 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: AttrMergeUtil.java    From svgtoandroid with MIT License 6 votes vote down vote up
public static XmlTag mergeAttrs(XmlTag src, Map<String, String> attrs) {
    Logger.debug("MergeAttrs: " + src.getName() + ", " + attrs.toString());

    copyAttr(src, "id", attrs);
    copyAttr(src, "fill", attrs);
    copyAttr(src, "fill-rule", attrs);
    copyAttr(src, "stroke", attrs);
    copyAttr(src, "stroke-width", attrs);

    if (src.getAttribute("transform") != null) {
        if (attrs.get("transform") != null) {
            String translate = attrs.get("transform");
            src.setAttribute("transform", merge(translate, src.getAttribute("transform").getValue()));
        }
    } else {
        if (attrs.get("transform") != null) {
            src.setAttribute("transform", attrs.get("transform"));
        }
    }
    if (attrs.get("transform") != null) {
        Logger.debug("Transform attrs merged:" + src.getAttribute("transform").getValue());
    }
    return src;
}
 
Example 2
Source File: Transformer.java    From svgtoandroid with MIT License 5 votes vote down vote up
public void transforming(CallBack callBack) {
    svgParser = new SVGParser(svg, dpi);
    styleParser = new StyleParser(svgParser.getStyles());

    Logger.debug(svgParser.toString());

    XmlFile dist = getDistXml();
    XmlDocument document = dist.getDocument();
    if (document != null && document.getRootTag() != null) {
        XmlTag rootTag = document.getRootTag();

        //set attr to root tag
        try {
            rootTag.getAttribute("android:width").setValue(svgParser.getWidth());
            rootTag.getAttribute("android:height").setValue(svgParser.getHeight());
            rootTag.getAttribute("android:viewportWidth").setValue(svgParser.getViewportWidth());
            rootTag.getAttribute("android:viewportHeight").setValue(svgParser.getViewportHeight());

            if (svgParser.getAlpha().length() > 0) {
                rootTag.setAttribute("android:alpha", svgParser.getAlpha());
            }
        } catch (NullPointerException npe) {
            //do nothing, because those attr is exist certainly.
        }

        //generate group
        if (svgParser.hasGroupTag()) {
            for (XmlTag g : svgParser.getGroups()) {
                parseGroup(g, rootTag);
            }
        } else {
            Logger.warn("Root tag has no subTag named 'group'");
            parseShapeNode(svg.getRootTag(), rootTag, null);
        }
        CodeStyleManager.getInstance(project).reformat(dist, true);
        callBack.onComplete(dist);
        Logger.debug(dist.toString());
    }
}
 
Example 3
Source File: AttrMergeUtil.java    From svgtoandroid with MIT License 4 votes vote down vote up
private static void copyAttr(XmlTag target, String attrName, Map<String, String> attrs) {
    if (target.getAttribute(attrName) == null && attrs.get(attrName) != null) {
        target.setAttribute(attrName, attrs.get(attrName));
    }
}
 
Example 4
Source File: Transformer.java    From svgtoandroid with MIT License 4 votes vote down vote up
private void parseShapeNode(XmlTag srcTag, XmlTag distTag, Map<String, String> existedAttrs) {
    if (existedAttrs == null) {
        existedAttrs = new HashMap<String, String>();
    }
    List<XmlTag> childes = svgParser.getShapeTags(srcTag);

    List<XmlTag> useTags = svgParser.getUseTags(srcTag);
    Map<String, XmlTag> defsTags = svgParser.getAcceptedDefNodes();
    for (XmlTag useTag : useTags) {
        String href = useTag.getAttributeValue("xlink:href");
        XmlTag def = defsTags.get(href.replace("#", ""));
        if (def != null) {
            XmlTag clonedTag = (XmlTag) def.copy();
            for (XmlAttribute attribute : useTag.getAttributes()) {
                if (!"xlink:href".equalsIgnoreCase(attribute.getName())) {
                    clonedTag.setAttribute(attribute.getName(), attribute.getValue());
                }
            }
            childes.add(clonedTag);
        }
    }

    for (XmlTag child : childes) {
        XmlTag pathElement = distTag.createChildTag("path", distTag.getNamespace(), null, false);
        Map<String, String> myAttrs = new HashMap<String, String>();
        myAttrs.putAll(existedAttrs);
        myAttrs.putAll(svgParser.getChildAttrs(child));
        Logger.debug("Existed attrs: " + myAttrs);

        XmlAttribute id = child.getAttribute("class");
        String idValue = id == null ? null : id.getValue();
        if (idValue != null) {
            pathElement.setAttribute("android:name", idValue);
        }

        pathElement.setAttribute("android:fillColor", decideFillColor(srcTag, child));

        for (String svgElementAttribute : myAttrs.keySet()) {
            if (AttrMapper.getAttrName(svgElementAttribute) != null && AttrMapper.getAttrName(svgElementAttribute).contains("Color")) {
                pathElement.setAttribute(AttrMapper.getAttrName(svgElementAttribute), StdColorUtil.formatColor(myAttrs.get(svgElementAttribute)));
            } else if (AttrMapper.getAttrName(svgElementAttribute) != null) {
                if (AttrMapper.getAttrName(svgElementAttribute).equals("android:fillType")) {
                    String value = myAttrs.get(svgElementAttribute).toLowerCase();
                    String xmlValue = "nonZero";
                    if (value.equals("evenodd")) {
                        xmlValue = "evenOdd";
                    }
                    pathElement.setAttribute("android:fillType", xmlValue);
                } else {
                    pathElement.setAttribute(AttrMapper.getAttrName(svgElementAttribute), myAttrs.get(svgElementAttribute));
                }
            }
        }

        if (AttrMapper.isShapeName(child.getName())) {
            pathElement.setAttribute("android:pathData", SVGAttrParser.getPathData(child));
        }

        distTag.addSubTag(pathElement, false);
    }
}