Example usage for org.w3c.dom Element hasAttributeNS

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

Introduction

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

Prototype

public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Returns true when an attribute with a given local name and namespace URI is specified on this element or has a default value, false otherwise.

Usage

From source file:edu.internet2.middleware.shibboleth.common.config.attribute.resolver.dataConnector.StoredIDDataConnectorBeanDefinitionParser.java

/**
 * Builds a JDBC {@link javax.sql.DataSource} from an ApplicationManagedConnection configuration element.
 * //from ww w.j a v  a2 s.c  o  m
 * @param pluginId ID of this data connector
 * @param amc the application managed configuration element
 * 
 * @return the built data source
 */
protected DataSource buildApplicationManagedConnection(String pluginId, Element amc) {
    ComboPooledDataSource datasource = new ComboPooledDataSource();

    String driverClass = DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcDriver"));
    ClassLoader classLoader = this.getClass().getClassLoader();
    try {
        classLoader.loadClass(driverClass);
    } catch (ClassNotFoundException e) {
        log.error(
                "Unable to create relational database connector, JDBC driver can not be found on the classpath");
        throw new BeanCreationException(
                "Unable to create relational database connector, JDBC driver can not be found on the classpath");
    }

    try {
        datasource.setDriverClass(driverClass);
        datasource.setJdbcUrl(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcURL")));
        datasource.setUser(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcUserName")));
        datasource.setPassword(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcPassword")));

        if (amc.hasAttributeNS(null, "poolAcquireIncrement")) {
            datasource.setAcquireIncrement(Integer
                    .parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolAcquireIncrement"))));
        } else {
            datasource.setAcquireIncrement(3);
        }

        if (amc.hasAttributeNS(null, "poolAcquireRetryAttempts")) {
            datasource.setAcquireRetryAttempts(Integer
                    .parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolAcquireRetryAttempts"))));
        } else {
            datasource.setAcquireRetryAttempts(36);
        }

        if (amc.hasAttributeNS(null, "poolAcquireRetryDelay")) {
            datasource.setAcquireRetryDelay(Integer
                    .parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolAcquireRetryDelay"))));
        } else {
            datasource.setAcquireRetryDelay(5000);
        }

        if (amc.hasAttributeNS(null, "poolBreakAfterAcquireFailure")) {
            datasource.setBreakAfterAcquireFailure(XMLHelper
                    .getAttributeValueAsBoolean(amc.getAttributeNodeNS(null, "poolBreakAfterAcquireFailure")));
        } else {
            datasource.setBreakAfterAcquireFailure(true);
        }

        if (amc.hasAttributeNS(null, "poolMinSize")) {
            datasource.setMinPoolSize(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolMinSize"))));
        } else {
            datasource.setMinPoolSize(2);
        }

        if (amc.hasAttributeNS(null, "poolMaxSize")) {
            datasource.setMaxPoolSize(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolMaxSize"))));
        } else {
            datasource.setMaxPoolSize(50);
        }

        if (amc.hasAttributeNS(null, "poolMaxIdleTime")) {
            datasource.setMaxIdleTime(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolMaxIdleTime"))));
        } else {
            datasource.setMaxIdleTime(600);
        }

        if (amc.hasAttributeNS(null, "poolIdleTestPeriod")) {
            datasource.setIdleConnectionTestPeriod(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolIdleTestPeriod"))));
        } else {
            datasource.setIdleConnectionTestPeriod(180);
        }

        datasource.setMaxStatementsPerConnection(10);

        log.debug("Created application managed data source for data connector {}", pluginId);
        return datasource;
    } catch (PropertyVetoException e) {
        log.error("Unable to create data source for data connector {} with JDBC driver class {}", pluginId,
                driverClass);
        return null;
    }
}

From source file:edu.internet2.middleware.shibboleth.common.config.attribute.resolver.attributeDefinition.BaseAttributeDefinitionBeanDefinitionParser.java

/** {@inheritDoc} */
protected void doParse(String pluginId, Element pluginConfig, Map<QName, List<Element>> pluginConfigChildren,
        BeanDefinitionBuilder pluginBuilder, ParserContext parserContext) {

    String sourceAttributeId = pluginConfig.getAttributeNS(null, "sourceAttributeID");
    log.debug("Setting source attribute ID for attribute definition {} to: {}", pluginId, sourceAttributeId);
    pluginBuilder.addPropertyValue("sourceAttributeId", sourceAttributeId);

    List<Element> displayNames = pluginConfigChildren
            .get(new QName(AttributeResolverNamespaceHandler.NAMESPACE, "DisplayName"));
    if (displayNames != null) {
        log.debug("Setting {} display names for attribute definition {}", displayNames.size(), pluginId);
        pluginBuilder.addPropertyValue("displayNames", processLocalizedElement(displayNames));
    }//from www  . j a v a  2 s. c  o m

    List<Element> displayDescriptions = pluginConfigChildren
            .get(new QName(AttributeResolverNamespaceHandler.NAMESPACE, "DisplayDescription"));
    if (displayDescriptions != null) {
        log.debug("Setting {} display descriptions for attribute definition {}", displayDescriptions.size(),
                pluginId);
        pluginBuilder.addPropertyValue("displayDescriptions", processLocalizedElement(displayDescriptions));
    }

    boolean dependencyOnly = false;
    if (pluginConfig.hasAttributeNS(null, "dependencyOnly")) {
        dependencyOnly = XMLHelper
                .getAttributeValueAsBoolean(pluginConfig.getAttributeNodeNS(null, "dependencyOnly"));
    }
    if (log.isDebugEnabled()) {
        log.debug("Attribute definition {} produces attributes that are only dependencies: {}", pluginId,
                dependencyOnly);
    }
    pluginBuilder.addPropertyValue("dependencyOnly", dependencyOnly);

    pluginBuilder.addPropertyValue("attributeEncoders", SpringConfigurationUtils
            .parseInnerCustomElements(pluginConfigChildren.get(ATTRIBUTE_ENCODER_ELEMENT_NAME), parserContext));
}

From source file:edu.internet2.middleware.shibboleth.common.config.attribute.resolver.dataConnector.RDBMSDataConnectorBeanDefinitionParser.java

/**
 * Builds a JDBC {@link javax.sql.DataSource} from an ApplicationManagedConnection configuration element.
 * //from w w w  .  j av  a2  s.  c om
 * @param pluginId ID of this data connector
 * @param amc the application managed configuration element
 * 
 * @return the built data source
 */
protected DataSource buildApplicationManagedConnection(String pluginId, Element amc) {
    ComboPooledDataSource datasource = new ComboPooledDataSource();

    String driverClass = DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcDriver"));
    ClassLoader classLoader = this.getClass().getClassLoader();
    try {
        classLoader.loadClass(driverClass);
    } catch (ClassNotFoundException e) {
        log.error(
                "Unable to create relational database connector, JDBC driver can not be found on the classpath");
        throw new BeanCreationException(
                "Unable to create relational database connector, JDBC driver can not be found on the classpath");
    }

    try {
        datasource.setDriverClass(driverClass);
        datasource.setJdbcUrl(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcURL")));
        datasource.setUser(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcUserName")));
        datasource.setPassword(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "jdbcPassword")));

        if (amc.hasAttributeNS(null, "poolAcquireIncrement")) {
            datasource.setAcquireIncrement(Integer
                    .parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolAcquireIncrement"))));
        } else {
            datasource.setAcquireIncrement(3);
        }

        if (amc.hasAttributeNS(null, "poolAcquireRetryAttempts")) {
            datasource.setAcquireRetryAttempts(Integer
                    .parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolAcquireRetryAttempts"))));
        } else {
            datasource.setAcquireRetryAttempts(36);
        }

        if (amc.hasAttributeNS(null, "poolAcquireRetryDelay")) {
            datasource.setAcquireRetryDelay(Integer
                    .parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolAcquireRetryDelay"))));
        } else {
            datasource.setAcquireRetryDelay(5000);
        }

        if (amc.hasAttributeNS(null, "poolBreakAfterAcquireFailure")) {
            datasource.setBreakAfterAcquireFailure(XMLHelper
                    .getAttributeValueAsBoolean(amc.getAttributeNodeNS(null, "poolBreakAfterAcquireFailure")));
        } else {
            datasource.setBreakAfterAcquireFailure(true);
        }

        if (amc.hasAttributeNS(null, "poolMinSize")) {
            datasource.setMinPoolSize(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolMinSize"))));
        } else {
            datasource.setMinPoolSize(2);
        }

        if (amc.hasAttributeNS(null, "poolMaxSize")) {
            datasource.setMaxPoolSize(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolMaxSize"))));
        } else {
            datasource.setMaxPoolSize(50);
        }

        if (amc.hasAttributeNS(null, "poolMaxIdleTime")) {
            datasource.setMaxIdleTime(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolMaxIdleTime"))));
        } else {
            datasource.setMaxIdleTime(600);
        }

        if (amc.hasAttributeNS(null, "poolIdleTestPeriod")) {
            datasource.setIdleConnectionTestPeriod(
                    Integer.parseInt(DatatypeHelper.safeTrim(amc.getAttributeNS(null, "poolIdleTestPeriod"))));
        } else {
            datasource.setIdleConnectionTestPeriod(180);
        }

        log.debug("Created application managed data source for data connector {}", pluginId);
        return datasource;
    } catch (PropertyVetoException e) {
        log.error("Unable to create data source for data connector {} with JDBC driver class {}", pluginId,
                driverClass);
        return null;
    }
}

From source file:edu.internet2.middleware.shibboleth.common.config.attribute.resolver.dataConnector.LdapDataConnectorBeanDefinitionParser.java

/**
 * Process the pooling configuration for the LDAP data connector.
 * //w w w. j ava2 s . c o  m
 * @param pluginId ID of the LDAP plugin
 * @param pluginConfig LDAP plugin configuration element
 * @param pluginConfigChildren child elements of the plugin
 * @param pluginBuilder plugin builder
 * @param parserContext current parsing context
 */
protected void processPoolingConfig(String pluginId, Element pluginConfig,
        Map<QName, List<Element>> pluginConfigChildren, BeanDefinitionBuilder pluginBuilder,
        ParserContext parserContext) {

    List<Element> poolConfigElems = pluginConfigChildren
            .get(new QName(DataConnectorNamespaceHandler.NAMESPACE, "ConnectionPool"));
    if (poolConfigElems == null || poolConfigElems.size() == 0) {
        log.debug("Data connector {} is pooling connections: {}", pluginId, false);
        pluginBuilder.addPropertyValue("poolStrategy", new LdapPoolEmptyStrategy());
        return;
    }

    Element poolConfigElem = poolConfigElems.get(0);

    LdapPoolConfig ldapPoolConfig = new LdapPoolConfig();
    LdapPoolVTStrategy ldapPoolStrategy = new LdapPoolVTStrategy();
    ldapPoolStrategy.setLdapPoolConfig(ldapPoolConfig);
    log.debug("Data connector {} is pooling connections: {}", pluginId, true);
    pluginBuilder.addPropertyValue("poolStrategy", ldapPoolStrategy);

    int poolMinSize = 0;
    if (pluginConfig.hasAttributeNS(null, "poolInitialSize")) {
        poolMinSize = Integer.parseInt(pluginConfig.getAttributeNS(null, "poolInitialSize"));
        log.warn(
                "Data connector {} using deprecated attribute poolInitialSize on <DataConnector> use minPoolSize on child <PoolConfig> instead");
    } else if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "minPoolSize")) {
        poolMinSize = Integer.parseInt(poolConfigElem.getAttributeNS(null, "minPoolSize"));
    }
    log.debug("Data connector {} pool minimum connections: {}", pluginId, poolMinSize);
    ldapPoolConfig.setMinPoolSize(poolMinSize);

    int poolMaxSize = 3;
    if (pluginConfig.hasAttributeNS(null, "poolMaxIdleSize")) {
        poolMaxSize = Integer.parseInt(pluginConfig.getAttributeNS(null, "poolMaxIdleSize"));
        log.warn(
                "Data connector {} using deprecated attribute poolMaxIdleSize on <DataConnector> use maxPoolSize on child <PoolConfig> instead");
    } else if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "maxPoolSize")) {
        poolMaxSize = Integer.parseInt(poolConfigElem.getAttributeNS(null, "maxPoolSize"));
    }
    log.debug("Data connector {} pool maximum connections: {}", pluginId, poolMaxSize);
    ldapPoolConfig.setMaxPoolSize(poolMaxSize);

    boolean blockWhenEmpty = true;
    if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "blockWhenEmpty")) {
        blockWhenEmpty = XMLHelper
                .getAttributeValueAsBoolean(poolConfigElem.getAttributeNodeNS(null, "blockWhenEmpty"));
    }
    log.debug("Data connector {} pool block when empty: {}", pluginId, blockWhenEmpty);
    ldapPoolStrategy.setBlockWhenEmpty(blockWhenEmpty);

    int blockWaitTime = 0;
    if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "blockWaitTime")) {
        blockWaitTime = (int) SpringConfigurationUtils.parseDurationToMillis("blockWaitTime",
                poolConfigElem.getAttributeNS(null, "blockWaitTime"), 0);
    }
    if (blockWaitTime == 0) {
        log.debug("Data connector {} pool block wait time: indefintely", pluginId);
    } else {
        log.debug("Data connector {} pool block wait time: {}ms", pluginId, blockWaitTime);
    }
    ldapPoolStrategy.setBlockWaitTime(blockWaitTime);

    boolean poolValidatePeriodically = false;
    if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "validatePeriodically")) {
        poolValidatePeriodically = XMLHelper
                .getAttributeValueAsBoolean(poolConfigElem.getAttributeNodeNS(null, "validatePeriodically"));
    }
    log.debug("Data connector {} pool validate periodically: {}", pluginId, poolValidatePeriodically);
    ldapPoolConfig.setValidatePeriodically(poolValidatePeriodically);

    int poolValidateTimerPeriod = 1800000;
    if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "validateTimerPeriod")) {
        poolValidateTimerPeriod = (int) SpringConfigurationUtils.parseDurationToMillis("validateTimerPeriod",
                poolConfigElem.getAttributeNS(null, "validateTimerPeriod"), 0);
    }
    log.debug("Data connector {} pool validate timer period: {}ms", pluginId, poolValidateTimerPeriod);
    ldapPoolConfig.setValidateTimerPeriod(poolValidateTimerPeriod);

    String poolValidateDn = "";
    if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "validateDN")) {
        poolValidateDn = poolConfigElem.getAttributeNS(null, "validateDN");
    }
    String poolValidateFilter = "(objectClass=*)";
    if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "validateFilter")) {
        poolValidateFilter = poolConfigElem.getAttributeNS(null, "validateFilter");
    }
    LdapValidator poolValidator = new CompareLdapValidator(poolValidateDn,
            new SearchFilter(poolValidateFilter));
    log.debug("Data connector {} pool validation filter: {}", pluginId, poolValidateFilter);
    pluginBuilder.addPropertyValue("poolValidator", poolValidator);

    int poolExpirationTime = 600000;
    if (poolConfigElem != null && poolConfigElem.hasAttributeNS(null, "expirationTime")) {
        poolExpirationTime = (int) SpringConfigurationUtils.parseDurationToMillis("expirationTime",
                poolConfigElem.getAttributeNS(null, "expirationTime"), 0);
    }
    log.debug("Data connector {} pool expiration time: {}ms", pluginId, poolExpirationTime);
    ldapPoolConfig.setExpirationTime(poolExpirationTime);
}

From source file:edu.internet2.middleware.shibboleth.common.config.attribute.resolver.dataConnector.LdapDataConnectorBeanDefinitionParser.java

/**
 * Process the LDAP connection security configuration for the LDAP data connector.
 * /*w w  w  .j a  v a 2 s  . c o m*/
 * @param pluginId ID of the LDAP plugin
 * @param pluginConfig LDAP plugin configuration element
 * @param pluginConfigChildren child elements of the plugin
 * @param pluginBuilder plugin builder
 * @param parserContext current parsing context
 */
protected void processSecurityConfig(String pluginId, Element pluginConfig,
        Map<QName, List<Element>> pluginConfigChildren, BeanDefinitionBuilder pluginBuilder,
        ParserContext parserContext) {
    RuntimeBeanReference trustCredential = processCredential(
            pluginConfigChildren
                    .get(new QName(DataConnectorNamespaceHandler.NAMESPACE, "StartTLSTrustCredential")),
            parserContext);
    if (trustCredential != null) {
        log.debug("Data connector {} using provided SSL/TLS trust material", pluginId);
        pluginBuilder.addPropertyValue("trustCredential", trustCredential);
    }

    RuntimeBeanReference connectionCredential = processCredential(
            pluginConfigChildren.get(
                    new QName(DataConnectorNamespaceHandler.NAMESPACE, "StartTLSAuthenticationCredential")),
            parserContext);
    if (connectionCredential != null) {
        log.debug("Data connector {} using provided SSL/TLS client authentication material", pluginId);
        pluginBuilder.addPropertyValue("connectionCredential", connectionCredential);
    }

    boolean useStartTLS = false;
    if (pluginConfig.hasAttributeNS(null, "useStartTLS")) {
        useStartTLS = XMLHelper
                .getAttributeValueAsBoolean(pluginConfig.getAttributeNodeNS(null, "useStartTLS"));
    }
    log.debug("Data connector {} use startTLS: {}", pluginId, useStartTLS);
    pluginBuilder.addPropertyValue("useStartTLS", useStartTLS);
}

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   ww  w  .j  a  v  a  2 s .co 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  ww  w. ja  v a2s  . c o m

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

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 w w  .  j a  va  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;/*from   w ww .  j a  v  a 2s . c  om*/
    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.ja  v  a  2s  . com*/

    //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")));
    }

}