Example usage for org.w3c.dom Element getAttributeNS

List of usage examples for org.w3c.dom Element getAttributeNS

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttributeNS.

Prototype

public String getAttributeNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Retrieves an attribute value by local name and namespace URI.

Usage

From source file:com.android.tools.lint.checks.AndroidAutoDetector.java

private void checkAutoMetadataTag(Element element) {
    String name = element.getAttributeNS(ANDROID_URI, ATTR_NAME);

    if (CAR_APPLICATION_METADATA_NAME.equals(name)) {
        String autoFileName = element.getAttributeNS(ANDROID_URI, ATTR_RESOURCE);

        if (autoFileName != null && autoFileName.startsWith("@xml/")) { //$NON-NLS-1$
            // Store the fact that we need to check all the auto issues.
            mDoAutomotiveAppCheck = true;
            mAutomotiveResourceFileName = autoFileName.substring("@xml/".length()) + DOT_XML; //$NON-NLS-1$
        }/*from ww w . ja v  a  2s  .c  o m*/
    }
}

From source file:com.android.tools.lint.checks.AndroidAutoDetector.java

private void checkServiceForBrowserServiceIntentFilter(Element element) {
    if (TAG_SERVICE.equals(element.getTagName()) && !mMediaIntentFilterFound) {

        for (Element child : LintUtils.getChildren(element)) {
            String tagName = child.getTagName();
            if (TAG_INTENT_FILTER.equals(tagName)) {
                for (Element filterChild : LintUtils.getChildren(child)) {
                    if (NODE_ACTION.equals(filterChild.getTagName())) {
                        String actionValue = filterChild.getAttributeNS(ANDROID_URI, ATTR_NAME);
                        if (ACTION_MEDIA_BROWSER_SERVICE.equals(actionValue)) {
                            mMediaIntentFilterFound = true;
                            return;
                        }/*from  w  w w.j av  a 2  s.co  m*/
                    }
                }
            }
        }
    }
}

From source file:com.android.tools.lint.checks.AndroidAutoDetector.java

private void checkForMediaSearchIntentFilter(Element element) {
    if (!mMediaSearchIntentFilterFound) {

        for (Element filterChild : LintUtils.getChildren(element)) {
            if (NODE_ACTION.equals(filterChild.getTagName())) {
                String actionValue = filterChild.getAttributeNS(ANDROID_URI, ATTR_NAME);
                if (ACTION_MEDIA_PLAY_FROM_SEARCH.equals(actionValue)) {
                    mMediaSearchIntentFilterFound = true;
                    break;
                }//w ww  .j ava 2s .co  m
            }
        }
    }
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private String get(Element el, String att, String defaultValue) {
    if (el.getOwnerDocument() == inputDOM && !el.hasAttributeNS(NS_ANDROID, att)) {
        return defaultValue;
    } else if (el.getOwnerDocument() != inputDOM && !el.hasAttribute(att)) {
        return defaultValue;
    }/*from w ww  .j av a2 s .  c o m*/

    String val = el.getOwnerDocument() == inputDOM ? el.getAttributeNS(NS_ANDROID, att) : el.getAttribute(att);
    if (val == null) {
        return defaultValue;
    }
    return val;
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private String getNS(Element el, String att, String defaultValue) {
    if (!el.hasAttributeNS(NS_ANDROID, att)) {
        return defaultValue;
    }//from   w  w w. j  a v a  2  s  .  co  m

    String val = el.getAttributeNS(NS_ANDROID, att);
    if (val == null) {
        return defaultValue;
    }
    return val;
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private int get(Element el, String att, int defaultValue) {
    String val = el.getOwnerDocument() == inputDOM ? el.getAttributeNS(NS_ANDROID, att) : el.getAttribute(att);
    if (val == null) {
        return defaultValue;
    }//ww w .  jav  a 2  s  . c om
    int out = defaultValue;
    try {
        out = Integer.parseInt(val);
    } catch (Exception ex) {
    }
    return out;
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private double get(Element el, String att, double defaultValue) {
    String val = el.getOwnerDocument() == inputDOM ? el.getAttributeNS(NS_ANDROID, att) : el.getAttribute(att);
    if (val == null) {
        return defaultValue;
    }//from w w  w . ja va  2s  .c  om
    double out = defaultValue;
    try {
        out = Double.parseDouble(val);
    } catch (Exception ex) {
    }
    return out;
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private void convertTextView(Element inputSrcElement, Element out) {
    out.removeAttribute("layout");
    if ("true".equals(inputSrcElement.getAttributeNS(NS_ANDROID, "editable"))) {
        if ("true".equals(inputSrcElement.getAttributeNS(NS_ANDROID, "singleLine"))) {
            out.setAttribute("type", "TextField");
        } else {//from w  ww . ja v a 2 s  .  c  o m
            out.setAttribute("type", "TextArea");
        }
    } else {
        if ("true".equals(inputSrcElement.getAttributeNS(NS_ANDROID, "singleLine"))) {
            out.setAttribute("type", "Label");
        } else {
            out.setAttribute("type", "SpanLabel");
        }
    }
    if (inputSrcElement.hasAttributeNS(NS_ANDROID, "text")) {
        out.setAttribute("text", parseText(inputSrcElement.getAttributeNS(NS_ANDROID, "text")));
        if (out.getAttribute("type").equals("SpanLabel") && out.getAttribute("text").length() < 30) {

            out.setAttribute("type", "Label");
        }
    }

}

From source file:com.codename1.android.AndroidLayoutImporter.java

private void applyStyles(Element inputSrcElement, Element out) {
    String type = out.getAttribute("type");
    String id = "Android" + type + (styleIndex++);
    String selId = id + ".sel";
    String unselId = id;/* ww w .  jav  a  2s  . c o m*/
    String pressedId = id + ".press";
    String disabledId = id + ".dis";

    outputResources.setThemeProperty(themeName, unselId + ".derive", type);
    outputResources.setThemeProperty(themeName, selId + "#derive", type + ".sel");
    outputResources.setThemeProperty(themeName, pressedId + "#derive", type + ".press");
    outputResources.setThemeProperty(themeName, disabledId + "#derive", type + ".dis");
    out.setAttribute("uiid", id);

    // If there is a width/height specified, we will use 3 piece borders to 
    // force preserved space.
    if ((getNS(inputSrcElement, "width", null) != null || getNS(inputSrcElement, "height", null) != null
            || getNS(inputSrcElement, "minHeight", null) != null
            || getNS(inputSrcElement, "minWidth", null) != null)
            && getNS(inputSrcElement, "background", null) == null) {
        // Removing this for now, because using border images just to ensure width is
        // crazy heavy and really shouldn't be necessary... will find a better way.
        /*
        String minWidth = getNS(inputSrcElement, "minWidth", getNS(inputSrcElement, "width", "5dp"));
        String minHeight = getNS(inputSrcElement, "minHeight", getNS(inputSrcElement, "height", "5dp"));
        System.out.println("Creating border with width "+minWidth+" and height "+minHeight+" for id "+id);
        Border border = Border.createHorizonalImageBorder(createBlankImage(minWidth, minHeight), createBlankImage("1dp", minHeight), createBlankImage("1dp", minHeight));
                
        setAllStyles(id, "border", border);
            */
    }

    if (getNS(inputSrcElement, "paddingTop", null) != null
            || getNS(inputSrcElement, "paddingBottom", null) != null
            || getNS(inputSrcElement, "paddingLeft", null) != null
            || getNS(inputSrcElement, "paddingRight", null) != null) {
        String padding = parseNumber(getNS(inputSrcElement, "paddingTop", "0")).value + ","
                + parseNumber(getNS(inputSrcElement, "paddingBottom", "0")).value + ","
                + parseNumber(getNS(inputSrcElement, "paddingLeft", "0")).value + ","
                + parseNumber(getNS(inputSrcElement, "paddingRight", "0")).value;

        byte[] paddingUnits = new byte[] { parseNumber(get(inputSrcElement, "paddingTop", "0")).unit,
                parseNumber(getNS(inputSrcElement, "paddingRight", "0")).unit,
                parseNumber(getNS(inputSrcElement, "paddingBottom", "0")).unit,
                parseNumber(getNS(inputSrcElement, "paddingLeft", "0")).unit };
        //System.out.println("Setting padding: "+padding);
        setAllStyles(id, "padding", padding);
        setAllStyles(id, "padUnit", paddingUnits);
    }

    if (getNS(inputSrcElement, "marginTop", null) != null
            || getNS(inputSrcElement, "marginBottom", null) != null
            || getNS(inputSrcElement, "marginLeft", null) != null
            || getNS(inputSrcElement, "marginRight", null) != null) {
        String margin = parseNumber(get(inputSrcElement, "marginTop", "0")).value + ","
                + parseNumber(getNS(inputSrcElement, "marginBottom", "0")).value + ","
                + parseNumber(getNS(inputSrcElement, "marginLeft", "0")).value + ","
                + parseNumber(getNS(inputSrcElement, "marginRight", "0")).value;

        byte[] marginUnits = new byte[] { parseNumber(get(inputSrcElement, "marginTop", "0")).unit,
                parseNumber(getNS(inputSrcElement, "marginRight", "0")).unit,
                parseNumber(getNS(inputSrcElement, "marginBottom", "0")).unit,
                parseNumber(getNS(inputSrcElement, "marginLeft", "0")).unit };
        //System.out.println("Setting margin "+margin);
        setAllStyles(id, "margin", margin);
        setAllStyles(id, "marUnit", marginUnits);
    }

    if (get(inputSrcElement, "background", null) != null) {
        // We have a custom background
        String backgroundStr = getNS(inputSrcElement, "background", null);
        if (backgroundStr.startsWith("@drawable/")) {
            try {
                backgroundStr = backgroundStr.substring(backgroundStr.indexOf("/") + 1);
                List<Element> items = getSelectorElementsForDrawable(backgroundStr);

                // 

                if (items != null) {
                    String defaultBackground = null;
                    String pressedBackground = null;
                    String disabledBackground = null;
                    String selectedBackground = null;
                    for (Element item : items) {

                        if (item.hasAttributeNS(NS_ANDROID, "drawable")) {
                            String itemDrawableStr = item.getAttributeNS(NS_ANDROID, "drawable");
                            if (itemDrawableStr.startsWith("@drawable/")) {
                                itemDrawableStr = itemDrawableStr.substring(itemDrawableStr.indexOf("/") + 1);
                            }
                            File itemDrawable = findDrawableResource(itemDrawableStr);
                            if (itemDrawable == null || (!itemDrawable.getName().endsWith(".png")
                                    && !itemDrawable.getName().endsWith(".jpg"))) {
                                // Let's not support nested xml drawables just yet...
                                // we'll skip this
                                continue;
                            }
                            if (!outputResources.containsResource(itemDrawable.getName())) {
                                // If the resource file hasn't imported the image yet, we won't set it here
                                continue;
                            }

                            if (item.hasAttributeNS(NS_ANDROID, "state_pressed")
                                    && "true".equals(item.getAttributeNS(NS_ANDROID, "state_pressed"))) {
                                pressedBackground = itemDrawable.getName();
                                //outputResources.setThemeProperty(themeName, pressedId+"#border", createImageBorder(itemDrawable.getName()));
                            } else if (item.hasAttributeNS(NS_ANDROID, "state_enabled")
                                    && "false".equals(item.getAttributeNS(NS_ANDROID, "state_enabled"))) {
                                //outputResources.setThemeProperty(themeName, disabledId+"#border", createImageBorder(itemDrawable.getName()));
                                disabledBackground = itemDrawable.getName();
                            } else if (item.hasAttributeNS(NS_ANDROID, "state_focused")
                                    && "true".equals(item.getAttributeNS(NS_ANDROID, "state_focused"))) {
                                selectedBackground = itemDrawable.getName();
                            } else {
                                defaultBackground = itemDrawable.getName();
                                //outputResources.setThemeProperty(themeName, id+".border", createImageBorder(itemDrawable.getName()));
                            }
                        }
                    }

                    if (defaultBackground != null) {
                        if (pressedBackground == null)
                            pressedBackground = defaultBackground;
                        if (selectedBackground == null)
                            selectedBackground = defaultBackground;
                        if (disabledBackground == null)
                            disabledBackground = defaultBackground;
                    }

                    if (defaultBackground != null) {
                        outputResources.setThemeProperty(themeName, id + ".border",
                                createImageBorder(defaultBackground));
                    }
                    if (pressedBackground != null) {
                        outputResources.setThemeProperty(themeName, pressedId + "#border",
                                createImageBorder(pressedBackground));
                    }
                    if (selectedBackground != null) {
                        outputResources.setThemeProperty(themeName, selId + "#border",
                                createImageBorder(pressedBackground));
                    }
                    if (disabledBackground != null) {
                        outputResources.setThemeProperty(themeName, disabledId + "#border",
                                createImageBorder(disabledBackground));
                    }
                }
            } catch (SAXException ex) {
                Logger.getLogger(AndroidLayoutImporter.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(AndroidLayoutImporter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    if (false && (get(inputSrcElement, "drawable", null) != null
            || get(inputSrcElement, "drawableTop", null) != null
            || get(inputSrcElement, "drawableRight", null) != null
            || get(inputSrcElement, "drawableBottom", null) != null
            || get(inputSrcElement, "drawableLeft", null) != null)) {
        String iconDrawableStr = null;
        int iconPosition = -1;
        if (inputSrcElement.hasAttributeNS(NS_ANDROID, "drawableLeft")) {
            iconDrawableStr = get(inputSrcElement, "drawableLeft", null);
            iconPosition = Component.LEFT;
        } else if (get(inputSrcElement, "drawableRight", null) != null) {
            iconDrawableStr = get(inputSrcElement, "drawableRight", null);
            iconPosition = Component.RIGHT;
        } else if (get(inputSrcElement, "drawableTop", null) != null) {
            iconDrawableStr = get(inputSrcElement, "drawableTop", null);
            iconPosition = Component.TOP;
        } else if (get(inputSrcElement, "drawableBottom", null) != null) {
            iconDrawableStr = get(inputSrcElement, "drawableBottom", null);
            iconPosition = Component.BOTTOM;
        }

        if (iconPosition >= 0) {
            switch (iconPosition) {
            case Component.TOP:
                out.setAttribute("textPosition", String.valueOf(Component.BOTTOM));
                break;
            case Component.BOTTOM:
                out.setAttribute("textPosition", String.valueOf(Component.TOP));
                break;
            case Component.LEFT:
                out.setAttribute("textPosition", String.valueOf(Component.RIGHT));
                break;
            case Component.RIGHT:
                out.setAttribute("textPosition", String.valueOf(Component.LEFT));
                break;
            }
            String backgroundStr = iconDrawableStr;
            if (backgroundStr.startsWith("@drawable/")) {
                try {
                    backgroundStr = backgroundStr.substring(backgroundStr.indexOf("/") + 1);
                    List<Element> items = getSelectorElementsForDrawable(backgroundStr);
                    if (items != null) {
                        for (Element item : items) {

                            if (item.hasAttributeNS(NS_ANDROID, "drawable")) {
                                String itemDrawableStr = item.getAttributeNS(NS_ANDROID, "drawable");
                                if (itemDrawableStr.startsWith("@drawable/")) {
                                    itemDrawableStr = itemDrawableStr
                                            .substring(itemDrawableStr.indexOf("/") + 1);
                                }
                                File itemDrawable = findDrawableResource(itemDrawableStr);
                                if (itemDrawable == null || (!itemDrawable.getName().endsWith(".png")
                                        && !itemDrawable.getName().endsWith(".jpg"))) {
                                    // Let's not support nested xml drawables just yet...
                                    // we'll skip this
                                    continue;
                                }
                                if (!outputResources.containsResource(itemDrawable.getName())) {
                                    // If the resource file hasn't imported the image yet, we won't set it here
                                    continue;
                                }
                                if (item.hasAttributeNS(NS_ANDROID, "state_pressed")
                                        && "true".equals(item.getAttributeNS(NS_ANDROID, "state_pressed"))) {
                                    out.setAttribute("pressedIcon", itemDrawableStr);
                                } else if (item.hasAttributeNS(NS_ANDROID, "state_enabled")
                                        && "false".equals(item.getAttributeNS(NS_ANDROID, "state_enabled"))) {

                                    out.setAttribute("disabledIcon", itemDrawableStr);
                                } else {
                                    out.setAttribute("icon", itemDrawableStr);
                                }
                            }
                        }
                    }
                } catch (SAXException ex) {
                    Logger.getLogger(AndroidLayoutImporter.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(AndroidLayoutImporter.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    // Try to find an existing style that we can merge
    Hashtable<String, Object> theme = outputResources.getTheme(themeName);

    String matchingId = findMatchingId(id, theme);
    if (matchingId != null) {
        //System.out.println("Found UIID with identical styles to "+id+".  Removing "+id+" and just using "+matchingId);
        // There is already a UIID that is identical to this one, so let's just
        // use that.
        out.setAttribute("uiid", matchingId);

        Set<String> keysToRemove = new HashSet<String>();
        String prefix = id + ".";
        for (String key : theme.keySet()) {
            if (key.startsWith(prefix)) {
                keysToRemove.add(key);
            }
        }
        for (String key : keysToRemove) {
            theme.remove(key);
        }
        outputResources.setTheme(themeName, theme);
    }
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private void convertButton(Element inputSrcElement, Element out) {
    out.removeAttribute("layout");
    out.setAttribute("type", "Button");
    //System.out.println("Converting button "+inputSrcElement);
    int attlen = inputSrcElement.getAttributes().getLength();
    for (int i = 0; i < attlen; i++) {
        Node n = inputSrcElement.getAttributes().item(i);
        //System.out.println("Namespace is "+n.getNamespaceURI());
        //System.out.println("Node "+i+"="+n);
    }// w  w  w . j av a 2s .c om

    //System.out.println("Text is "+inputSrcElement.getAttributeNS(NS_ANDROID, "text"));
    if (inputSrcElement.hasAttributeNS(NS_ANDROID, "text")) {
        out.setAttribute("text", parseText(inputSrcElement.getAttributeNS(NS_ANDROID, "text")));
    }

}