Example usage for org.w3c.dom Element getAttribute

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

Introduction

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

Prototype

public String getAttribute(String name);

Source Link

Document

Retrieves an attribute value by name.

Usage

From source file:com.dangdang.ddframe.rdb.sharding.spring.namespace.parser.ShardingJdbcStrategyBeanDefinition.java

static AbstractBeanDefinition getBeanDefinitionByElement(final Element element) {
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(StrategyConfig.class);
    factory.addPropertyValue("shardingColumns",
            element.getAttribute(ShardingJdbcStrategyBeanDefinitionParserTag.SHARDING_COLUMNS_ATTRIBUTE));
    factory.addPropertyValue("algorithmClassName",
            element.getAttribute(ShardingJdbcStrategyBeanDefinitionParserTag.ALGORITHM_CLASS_ATTRIBUTE));
    factory.addPropertyValue("algorithmExpression",
            element.getAttribute(ShardingJdbcStrategyBeanDefinitionParserTag.ALGORITHM_EXPRESSION_ATTRIBUTE));
    return factory.getBeanDefinition();
}

From source file:com.microsoft.tfs.core.clients.build.internal.utils.XamlHelper.java

public static String updateProperties(final String originalXaml, final Properties properties) {
    final ArrayList keys = new ArrayList(properties.keySet());

    final Document document = DOMCreateUtils.parseString(originalXaml);
    final Element root = document.getDocumentElement();

    // first update any properties that we already have
    final NodeList nodes = root.getElementsByTagName("x:String"); //$NON-NLS-1$
    for (int i = 0; i < nodes.getLength(); i++) {
        final Element element = (Element) nodes.item(i);
        final String key = element.getAttribute("x:Key"); //$NON-NLS-1$
        element.getFirstChild().getNodeValue();

        if (properties.containsKey(key)) {
            keys.remove(key);//from   www . ja  va 2 s  . co m
            element.getFirstChild().setNodeValue(properties.getProperty(key));
        }
    }

    // now add any new properties to the xaml
    for (final Iterator it = keys.iterator(); it.hasNext();) {
        final String key = (String) it.next();
        final Element element = DOMUtils.appendChild(root, "x:String"); //$NON-NLS-1$
        element.setAttributeNS(XAML_NAMESPACE, "x:Key", key); //$NON-NLS-1$
        element.setAttributeNS(XML_NAMESPACE, "xml:space", "preserve"); //$NON-NLS-1$ //$NON-NLS-2$
        DOMUtils.appendText(element, properties.getProperty(key));
    }

    return DOMSerializeUtils.toString(root, DOMSerializeUtils.INDENT).trim();
}

From source file:Main.java

public static Hashtable<String, Element> getChildHash(Element elem, String elementName, String attrName) {
    if (elem == null)
        return null;
    NodeList nl = elem.getChildNodes();
    if (nl == null)
        return null;
    Hashtable<String, Element> retlist = new Hashtable<String, Element>(100);
    for (int n = 0; n < nl.getLength(); n++) {
        Node child = nl.item(n);/* w  ww . ja va 2s  .  c  om*/
        if (child instanceof Element) {
            Element element = (Element) child;
            if (!elementName.equals(element.getTagName()))
                continue;
            String keyValue = element.getAttribute(attrName);
            if (keyValue == null)
                continue;
            retlist.put(keyValue, element);
        }
    }
    if (retlist.size() == 0)
        return null;
    return retlist;
}

From source file:org.jdal.beans.BeanDefinitionUtils.java

/**
 * Add property value to {@link BeanDefinitionBuilder} if needed, following the naming {@link Conventions}
 * @param bdb BeanDefintionBuilder to operate on.
 * @param element Element holding the attribute
 * @param attributeName the attribute name
 *///from w ww  .  j a v a 2  s . c  o m
public static void addPropertyValueIfNeeded(BeanDefinitionBuilder bdb, Element element, String attributeName) {
    if (element.hasAttribute(attributeName))
        bdb.addPropertyValue(Conventions.attributeNameToPropertyName(attributeName),
                element.getAttribute(attributeName));
}

From source file:com.glaf.activiti.tasklistener.factory.TaskListenerTypes.java

static Map<String, Class<?>> initializeTaskListenerTypes() {
    Map<String, Class<?>> types = new java.util.HashMap<String, Class<?>>();
    String resource = SystemProperties.getString("activiti.taskListeners");
    if (StringUtils.isEmpty(resource)) {
        resource = DEFAULT_CONFIG;//from   www .  j a  va 2  s. com
    }
    if (StringUtils.isNotEmpty(resource)) {
        InputStream taskTypesStream = PropertiesUtils.getInputStream(resource);
        Element listenersElement = XmlUtils.parseXmlInputStream(taskTypesStream).getDocumentElement();
        Iterator<?> nodeTypeIterator = XmlUtils.elementIterator(listenersElement, "taskListeners");
        while (nodeTypeIterator.hasNext()) {
            Element nodeTypeElement = (Element) nodeTypeIterator.next();
            String elementTag = nodeTypeElement.getAttribute("element");
            String className = nodeTypeElement.getAttribute("class");
            try {
                Class<?> clazz = com.glaf.core.util.ClassUtils.loadClass(className);
                types.put(elementTag, clazz);
            } catch (Exception ex) {
                ex.printStackTrace();
                logger.error("node '" + elementTag + "' will not be available. class '" + className
                        + "' couldn't be loaded");
            }
        }
    }

    String ext_resource = CustomProperties.getString("activiti.taskListeners");
    if (StringUtils.isNotEmpty(ext_resource)) {
        InputStream typesStream = PropertiesUtils.getInputStream(resource);
        Element listenersElement = XmlUtils.parseXmlInputStream(typesStream).getDocumentElement();
        Iterator<?> nodeTypeIterator = XmlUtils.elementIterator(listenersElement, "taskListener");
        while (nodeTypeIterator.hasNext()) {
            Element nodeTypeElement = (Element) nodeTypeIterator.next();
            String elementTag = nodeTypeElement.getAttribute("element");
            String className = nodeTypeElement.getAttribute("class");
            try {
                Class<?> clazz = com.glaf.core.util.ClassUtils.loadClass(className);
                types.put(elementTag, clazz);
            } catch (Exception ex) {
                ex.printStackTrace();
                logger.error("node '" + elementTag + "' will not be available. class '" + className
                        + "' couldn't be loaded");
            }
        }
    }

    return types;
}

From source file:Main.java

public static <T> T loadObject(Element e, Class<T> type) throws InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
    if (e != null) {
        String c = e.getAttribute("type");
        Class<T> clazz = (Class) Class.forName(c);
        Constructor<T> con = clazz.getConstructor((Class[]) null);
        return con.newInstance();
    }/*  w  w w  .  ja  va 2 s  . c o m*/
    return null;
}

From source file:Main.java

/**
 * Returns an array of strings representing values for provided attribute
 * prefixed by 2 levels of parents, extracted from a provided list of nodes
 * (e.g.: parenetNodeAtrValue.childNodeAtrValue.childNodeAtrValue)
 * @param atrName/*from w ww  . j av  a2s  .co m*/
 * @param nodes
 * @return
 */
private static ArrayList<String> getValuesWithParentForAttribute(String atrName, NodeList nodes) {
    ArrayList<String> filteredNodesV = new ArrayList<String>();
    for (int j = 0; j < nodes.getLength(); j++) {
        Element childE = (Element) nodes.item(j);
        if (childE.hasAttribute(atrName)) {
            filteredNodesV.add(getXsdParentsAtrValue(childE, atrName) + childE.getAttribute(atrName));
        }
    }
    return filteredNodesV;
}

From source file:Main.java

public static List<Element> findElementsByAttribute(Element node, String tagName, String attrName,
        List<String> attrValues) {
    List<Element> result = new LinkedList<Element>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return result;
    }//from  w w w  .ja  va  2s.com
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Element element = checkIfElement(nodeList.item(i), tagName);
        if (element != null) {
            for (String value : attrValues) {
                if (element.getAttribute(attrName).equals(value)) {
                    result.add(element);
                    break;
                }
            }
        }
    }
    return result;
}

From source file:org.xacml4j.spring.pip.ResolverRegistryDefinitionParser.java

private static BeanDefinitionBuilder parseResolvers(Element element) {
    BeanDefinitionBuilder component = BeanDefinitionBuilder
            .rootBeanDefinition(ResolverRegistrationFactoryBean.class);
    String policyId = element.getAttribute("policyId");
    if (StringUtils.hasText(policyId)) {
        component.addPropertyValue("policyId", policyId);
    }//from   ww w.ja  v  a2  s. c o m
    String ref = element.getAttribute("ref");
    if (StringUtils.hasText(ref)) {
        component.addPropertyReference("resolver", ref);
    }
    return component;
}

From source file:Main.java

/**
 * Looks through all child elements of the specified root (recursively)
 * and returns the first element that corresponds to all parameters.
 *
 * @param root the Element where the search should begin
 * @param tagName the name of the node we're looking for
 * @param keyAttributeName the name of an attribute that the node has to
 * have//from  www.j  a v a 2  s.  co m
 * @param keyAttributeValue the value that attribute must have
 * @return the Element in the tree under root that matches the specified
 * parameters.
 * @throws NullPointerException if any of the arguments is null.
 */
public static Element locateElement(Element root, String tagName, String keyAttributeName,
        String keyAttributeValue) {
    NodeList nodes = root.getChildNodes();
    int len = nodes.getLength();

    for (int i = 0; i < len; i++) {
        Node node = nodes.item(i);

        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        Element element = (Element) node;

        // is this the node we're looking for?
        if (node.getNodeName().equals(tagName)) {
            String attr = element.getAttribute(keyAttributeName);

            if ((attr != null) && attr.equals(keyAttributeValue))
                return element;
        }

        //look inside.
        Element child = locateElement(element, tagName, keyAttributeName, keyAttributeValue);

        if (child != null)
            return child;
    }
    return null;
}