Example usage for org.w3c.dom Element hasAttribute

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

Introduction

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

Prototype

public boolean hasAttribute(String name);

Source Link

Document

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

Usage

From source file:org.commonreality.sensors.xml2.XMLProcessor.java

public double getTime(Element frame) {
    double nextTime = 0;
    /*//ww  w  .  ja  v a 2s  .com
     * is time absolute or relative?
     */
    if (frame.hasAttribute("value"))
        nextTime = Double.valueOf(frame.getAttribute("value"));
    else if (frame.hasAttribute("relative"))
        nextTime = _sensor.getClock().getTime() + Double.valueOf(frame.getAttribute("relative"));
    else if (frame.hasAttribute("immediate"))
        nextTime = Double.NaN;

    return nextTime;
}

From source file:org.dhatim.xml.DomUtils.java

/**
 * Get attribute value, returning <code>null</code> if unset.
 * <p/>//from www.j av  a 2  s .c  o m
 * Some DOM implementations return an empty string for an unset
 * attribute.
 * @param element The DOM element.
 * @param attributeName The attribute to get.
 * @param namespaceURI Namespace URI of the required attribute, or null
 * to perform a non-namespaced get.
 * @return The attribute value, or <code>null</code> if unset.
 */
public static String getAttributeValue(Element element, String attributeName, String namespaceURI) {
    AssertArgument.isNotNull(element, "element");
    AssertArgument.isNotNullAndNotEmpty(attributeName, "attributeName");

    String attributeValue;

    if (namespaceURI == null) {
        attributeValue = element.getAttribute(attributeName);
    } else {
        attributeValue = element.getAttributeNS(namespaceURI, attributeName);
    }

    if (attributeValue.length() == 0 && !element.hasAttribute(attributeName)) {
        return null;
    }

    return attributeValue;
}

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

/**
 * Parse the bean definition itself, without regard to name or aliases. May return <code>null</code> if problems
 * occurred during the parse of the bean definition.
 *///from ww  w.j  av  a 2s .c  o m
private AbstractBeanDefinition parseBeanDefinitionElement(Element ele, String beanName,
        BeanDefinition containingBean) {

    this.parseState.push(new BeanEntry(beanName));

    String className = null;
    if (ele.hasAttribute(BeanDefinitionParserDelegate.CLASS_ATTRIBUTE)) {
        className = ele.getAttribute(BeanDefinitionParserDelegate.CLASS_ATTRIBUTE).trim();
    }

    try {
        AbstractBeanDefinition beanDefinition = BeanDefinitionReaderUtils.createBeanDefinition(null, className,
                parserContext.getReaderContext().getBeanClassLoader());

        // some early validation
        String activation = ele.getAttribute(LAZY_INIT_ATTR);
        String scope = ele.getAttribute(BeanDefinitionParserDelegate.SCOPE_ATTRIBUTE);

        if (EAGER_INIT_VALUE.equals(activation) && BeanDefinition.SCOPE_PROTOTYPE.equals(scope)) {
            error("Prototype beans cannot be eagerly activated", ele);
        }

        // add marker to indicate that the scope was present
        if (StringUtils.hasText(scope)) {
            beanDefinition.setAttribute(DECLARED_SCOPE, Boolean.TRUE);
        }

        // parse attributes
        parseAttributes(ele, beanName, beanDefinition);

        // inner beans get a predefined scope in RFC 124
        if (containingBean != null) {
            beanDefinition.setLazyInit(true);
            beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        }

        // parse description
        beanDefinition.setDescription(
                DomUtils.getChildElementValueByTagName(ele, BeanDefinitionParserDelegate.DESCRIPTION_ELEMENT));

        parseConstructorArgElements(ele, beanDefinition);
        parsePropertyElements(ele, beanDefinition);

        beanDefinition.setResource(parserContext.getReaderContext().getResource());
        beanDefinition.setSource(extractSource(ele));

        return beanDefinition;
    } catch (ClassNotFoundException ex) {
        error("Bean class [" + className + "] not found", ele, ex);
    } catch (NoClassDefFoundError err) {
        error("Class that bean class [" + className + "] depends on not found", ele, err);
    } catch (Throwable ex) {
        error("Unexpected failure during bean definition parsing", ele, ex);
    } finally {
        this.parseState.pop();
    }

    return null;
}

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

private Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {
    String elementName = (propertyName != null) ? "<property> element for property '" + propertyName + "'"
            : "<constructor-arg> element";

    // Should only have one child element: ref, value, list, etc.
    NodeList nl = ele.getChildNodes();
    Element subElement = null;//  ww  w.  j ava 2s .  c  o m
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element
                && !DomUtils.nodeNameEquals(node, BeanDefinitionParserDelegate.DESCRIPTION_ELEMENT)) {
            // Child element is what we're looking for.
            if (subElement != null) {
                error(elementName + " must not contain more than one sub-element", ele);
            } else {
                subElement = (Element) node;
            }
        }
    }

    boolean hasRefAttribute = ele.hasAttribute(BeanDefinitionParserDelegate.REF_ATTRIBUTE);
    boolean hasValueAttribute = ele.hasAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE);
    if ((hasRefAttribute && hasValueAttribute)
            || ((hasRefAttribute || hasValueAttribute) && subElement != null)) {
        error(elementName
                + " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element",
                ele);
    }

    if (hasRefAttribute) {
        String refName = ele.getAttribute(BeanDefinitionParserDelegate.REF_ATTRIBUTE);
        if (!StringUtils.hasText(refName)) {
            error(elementName + " contains empty 'ref' attribute", ele);
        }
        RuntimeBeanReference ref = new RuntimeBeanReference(refName);
        ref.setSource(parserContext.extractSource(ele));
        return ref;
    } else if (hasValueAttribute) {
        TypedStringValue valueHolder = new TypedStringValue(
                ele.getAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE));
        valueHolder.setSource(parserContext.extractSource(ele));
        return valueHolder;
    } else if (subElement != null) {
        return parsePropertySubElement(subElement, bd, null);
    } else {
        // Neither child element nor "ref" or "value" attribute found.
        error(elementName + " must specify a ref or value", ele);
        return null;
    }
}

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

/**
 * Parse a map element./*from   w w  w  . j  a  va  2  s  .  com*/
 */
public Map<?, ?> parseMapElement(Element mapEle, BeanDefinition bd) {
    String defaultKeyType = mapEle.getAttribute(BeanDefinitionParserDelegate.KEY_TYPE_ATTRIBUTE);
    String defaultValueType = mapEle.getAttribute(BeanDefinitionParserDelegate.VALUE_TYPE_ATTRIBUTE);

    List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle,
            BeanDefinitionParserDelegate.ENTRY_ELEMENT);
    ManagedMap<Object, Object> map = new ManagedMap<Object, Object>(entryEles.size());
    map.setSource(extractSource(mapEle));
    map.setKeyTypeName(defaultKeyType);
    map.setValueTypeName(defaultValueType);
    map.setMergeEnabled(parseMergeAttribute(mapEle));

    for (Element entryEle : entryEles) {
        // Should only have one value child element: ref, value, list, etc.
        // Optionally, there might be a key child element.
        NodeList entrySubNodes = entryEle.getChildNodes();
        Element keyEle = null;
        Element valueEle = null;
        for (int j = 0; j < entrySubNodes.getLength(); j++) {
            Node node = entrySubNodes.item(j);
            if (node instanceof Element) {
                Element candidateEle = (Element) node;
                if (DomUtils.nodeNameEquals(candidateEle, BeanDefinitionParserDelegate.KEY_ELEMENT)) {
                    if (keyEle != null) {
                        error("<entry> element is only allowed to contain one <key> sub-element", entryEle);
                    } else {
                        keyEle = candidateEle;
                    }
                } else {
                    // Child element is what we're looking for.
                    if (valueEle != null) {
                        error("<entry> element must not contain more than one value sub-element", entryEle);
                    } else {
                        valueEle = candidateEle;
                    }
                }
            }
        }

        // Extract key from attribute or sub-element.
        Object key = null;
        boolean hasKeyAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE);
        boolean hasKeyRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
        if ((hasKeyAttribute && hasKeyRefAttribute)
                || ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle);
        }
        if (hasKeyAttribute) {
            key = buildTypedStringValueForMap(entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE),
                    defaultKeyType, entryEle);
        } else if (hasKeyRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'key-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            key = ref;
        } else if (keyEle != null) {
            key = parseKeyElement(keyEle, bd, defaultKeyType);
        } else {
            error("<entry> element must specify a key", entryEle);
        }

        // Extract value from attribute or sub-element.
        Object value = null;
        boolean hasValueAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE);
        boolean hasValueRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
        if ((hasValueAttribute && hasValueRefAttribute)
                || ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
        }
        if (hasValueAttribute) {
            value = buildTypedStringValueForMap(
                    entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE), defaultValueType,
                    entryEle);
        } else if (hasValueRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'value-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            value = ref;
        } else if (valueEle != null) {
            value = parsePropertySubElement(valueEle, bd, defaultValueType);
        } else {
            error("<entry> element must specify a value", entryEle);
        }

        // Add final key and value to the Map.
        map.put(key, value);
    }

    return map;
}

From source file:org.eclipse.smila.search.datadictionary.messages.ddconfig.DFieldConfigCodec.java

/**
 * Decode standard values of a field config.
 * //from  ww  w.j a  v  a  2  s.  c  o m
 * @param dField
 *          Field config.
 * @param element
 *          Element.
 * @throws ConfigurationException
 *           Unable to decode field config.
 */
public static void decodeStandardValues(DFieldConfig dField, Element element) throws ConfigurationException {
    final Log log = LogFactory.getLog(DFieldConfigCodec.class);

    if (element.hasAttribute("Weight")) {
        dField.setWeight(new Integer(element.getAttribute("Weight")));
    }

    if (element.hasAttribute("FieldTemplate")) {
        dField.setFieldTemplate(element.getAttribute("FieldTemplate"));
    }
    if (element.hasAttribute("Constraint")) {
        dField.setConstraint(element.getAttribute("Constraint"));
    }

    final NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (!(nl.item(i) instanceof Element)) {
            continue;
        }
        final Element el = (Element) nl.item(i);

        if ("Transformer".equals(el.getLocalName())) {
            try {
                dField.setTransformer(DTransformerCodec.decode(el));
            } catch (final DSearchException e) {
                log.error("Unable to decode Transformer: " + e.getMessage(), e);
                throw new ConfigurationException("Unable to decode Transformer: " + e.getMessage());
            }
        } else if ("NodeTransformer".equals(el.getLocalName())) {
            try {
                dField.setNodeTransformer(DNodeTransformerCodec.decode(el));
            } catch (final DSearchException e) {
                log.error("Unable to decode NodeTransformer: " + e.getMessage(), e);
                throw new ConfigurationException("Unable to decode NodeTransformer: " + e.getMessage());
            }
        }
    }

}

From source file:org.eshark.xmlprog.cache.ClassCache.java

private void importClasses(Element xmlElement) {
    final String lKey = "key";
    NodeList entries = xmlElement.getChildNodes();
    int numEntries = entries.getLength();
    int start = numEntries > 0 && entries.item(0).getNodeName().equals("comment") ? 1 : 0;
    for (int i = start; i < numEntries; i++) {
        Element entry = (Element) entries.item(i);
        if (entry.hasAttribute(lKey)) {
            Node n = entry.getFirstChild();
            String val = (n == null) ? "" : n.getNodeValue();
            if (!isEmpty(val = val.trim())) {

                try {
                    mXmlMap.put(entry.getAttribute(lKey), getAppClass(val));
                } catch (XMLProgException XMLPE) {
                    XMLPE.printStackTrace();
                } catch (ClassNotFoundException CNFE) {
                    CNFE.printStackTrace();
                }/*from   w  w  w  .  j a va  2s  .  c  o m*/
                // For testing purpose
                // Please delete later
                System.out.println("CLASS KEY :: " + entry.getAttribute(lKey) + " VALUE :: " + val);
            }
        }
    }
}

From source file:org.exist.xquery.modules.httpclient.POSTFunction.java

private Part[] parseFields(Element fields) throws XPathException {
    NodeList nlField = fields.getElementsByTagNameNS(HTTPClientModule.NAMESPACE_URI, "field");

    Part[] parts = new Part[nlField.getLength()];

    for (int i = 0; i < nlField.getLength(); i++) {
        Element field = (Element) nlField.item(i);

        if (field.hasAttribute("type") && field.getAttribute("type").equals("file")) {
            try {

                String url = field.getAttribute("value");
                if (url.startsWith("xmldb:exist://")) {
                    parts[i] = new FilePart(field.getAttribute("name"), new DBFile(url.substring(13)));
                } else {
                    parts[i] = new FilePart(field.getAttribute("name"), new File(url));
                }//from w  w w  . ja  va2 s  . com

            } catch (FileNotFoundException e) {
                throw (new XPathException(e));
            }

        } else {
            parts[i] = new StringPart(field.getAttribute("name"), field.getAttribute("value"));
        }

    }

    return (parts);
}

From source file:org.exoplatform.services.jcr.ext.artifact.ArtifactManagingServiceImpl.java

/**
 * Get attribute value.//ww  w .  ja  v  a2s .  c  om
 * 
 * @param element
 *          The element to get attribute value
 * @param attr
 *          The attribute name
 * @return Value of attribute if present and null in other case
 */
private String getAttributeSmart(Element element, String attr) {
    return element.hasAttribute(attr) ? element.getAttribute(attr) : null;
}

From source file:org.exoplatform.services.jcr.ext.script.groovy.GroovyScript2RestLoader.java

/**
 * Get attribute value./*from  w  w w  .ja va2s . c om*/
 * 
 * @param element The element to get attribute value
 * @param attr The attribute name
 * @return Value of attribute if present and null in other case
 */
protected String getAttributeSmart(Element element, String attr) {
    return element.hasAttribute(attr) ? element.getAttribute(attr) : null;
}