List of usage examples for org.w3c.dom Element hasAttribute
public boolean hasAttribute(String name);
true
when an attribute with a given name is specified on this element or has a default value, false
otherwise. From source file:org.sakaiproject.citation.impl.BaseConfigurationService.java
protected void saveServletClientMappings(Document document) { Element clientElement = document.getElementById("saveciteClients"); if (clientElement == null) { NodeList mapNodes = document.getElementsByTagName("map"); if (mapNodes != null) { for (int i = 0; i < mapNodes.getLength(); i++) { Element mapElement = (Element) mapNodes.item(i); if (mapElement.hasAttribute("id") && mapElement.getAttribute("id").equals("saveciteClients")) { clientElement = mapElement; break; }/*from w w w.jav a 2 s . c om*/ } } } if (clientElement != null) { try { XStream xstream = new XStream(); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(clientElement), new StreamResult(buffer)); String str = buffer.toString(); // DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); // LSSerializer serializer = domImplLS.createLSSerializer(); // String str = serializer.writeToString(clientElement); this.saveciteClients = (Map<String, List<Map<String, String>>>) xstream.fromXML(str); } catch (Exception e) { m_log.warn("Exception trying to read saveciteClients from config XML", e); } } }
From source file:org.sakaiproject.content.impl.BaseContentService.java
/** * Replace the WT user id with the new qualified id * /*w w w .ja v a 2 s . com*/ * @param el * The XML element holding the perproties * @param useIdTrans * The HashMap to track old WT id to new CTools id */ protected void WTUserIdTrans(Element el, Map userIdTrans) { NodeList children4 = el.getChildNodes(); int length4 = children4.getLength(); for (int i4 = 0; i4 < length4; i4++) { Node child4 = children4.item(i4); if (child4.getNodeType() == Node.ELEMENT_NODE) { Element element4 = (Element) child4; if (element4.getTagName().equals("property")) { String creatorId = ""; String modifierId = ""; if (element4.hasAttribute("CHEF:creator")) { if ("BASE64".equalsIgnoreCase(element4.getAttribute("enc"))) { creatorId = Xml.decodeAttribute(element4, "CHEF:creator"); } else { creatorId = element4.getAttribute("CHEF:creator"); } String newCreatorId = (String) userIdTrans.get(creatorId); if (newCreatorId != null) { Xml.encodeAttribute(element4, "CHEF:creator", newCreatorId); element4.setAttribute("enc", "BASE64"); } } else if (element4.hasAttribute("CHEF:modifiedby")) { if ("BASE64".equalsIgnoreCase(element4.getAttribute("enc"))) { modifierId = Xml.decodeAttribute(element4, "CHEF:modifiedby"); } else { modifierId = element4.getAttribute("CHEF:modifiedby"); } String newModifierId = (String) userIdTrans.get(modifierId); if (newModifierId != null) { Xml.encodeAttribute(element4, "CHEF:creator", newModifierId); element4.setAttribute("enc", "BASE64"); } } } } } }
From source file:org.sdm.spa.WSWithComplexTypes.java
/** * Recursively create a SOAPBodyElement for a namespace based on an XML * Element./*from w ww. j av a 2 s . c o m*/ * * @param root * the XML Element containing the data * @param name * the name * @param te * the SOAP TypeEntry * @param nillable * whether it can be nil * @param parentNS * the parent XML namespace * @return the SOAPBodyElement */ private SOAPBodyElement _buildSOAPBody(Element root, String name, TypeEntry te, boolean nillable, String parentNS) throws SOAPException, IllegalActionException { SOAPBodyElement retval = new SOAPBodyElement(XMLUtils.StringToElement(parentNS, name, "")); if (root.hasAttribute("nil")) { // System.out.println("is nil"); if (!nillable) { throw new IllegalActionException(this, "Must supply " + name); } else { retval.setAttribute("xsi:nil", "true"); } } else if (!te.isSimpleType() && !te.isBaseType()) { // System.out.println("is complex"); Vector<?> parts = te.getContainedElements(); if (parts != null) { for (int i = 0; i < parts.size(); i++) { ElementDecl decl = (ElementDecl) parts.get(i); String declName = _getDeclName(decl); NodeList nl = root.getElementsByTagName(declName); if (nl.getLength() == 0) { System.out.println("WARNING: missing: " + name); } else { if (nl.getLength() > 1) { // xxx array? for (int j = 0; j < nl.getLength(); j++) { retval.addChildElement(_buildSOAPBody((Element) nl.item(j), declName, decl.getType(), decl.getNillable(), decl.getQName().getNamespaceURI())); } } else { // System.out.println("found " + nl.item(0)); retval.addChildElement(_buildSOAPBody((Element) nl.item(0), declName, decl.getType(), decl.getNillable(), decl.getQName().getNamespaceURI())); } } } } else if (te.isReferenced()) { TypeEntry newTe = te.getRefType(); return _buildSOAPBody(root, name, newTe, nillable, parentNS); } } else { retval.setValue(root.getChildNodes().item(0).getNodeValue()); _addTypeOrNamespace(retval, parentNS, te.getQName().getLocalPart()); } return retval; }
From source file:org.sonar.plugins.web.markup.rules.MarkupErrorCatalog.java
private void readErrorCatalog() { Document document = parseMessageCatalog("markup-errors.html"); NodeList nodeList = document.getElementsByTagName("dt"); // find errors with explanation for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); if (element.hasAttribute("id")) { MessageDefinition error = new MessageDefinition(); String id = element.getAttribute("id"); error.id = StringUtils.substringAfterLast(id, "-"); error.remark = element.getChildNodes().item(0).getNodeValue().trim(); errors.add(error);//from w w w. jav a2s .co m } } // find explanation for the first group of errors Map<String, String> explanations = findExplanations(document); for (MessageDefinition error : errors) { error.explanation = explanations.get(error.id); if (error.explanation == null) { LOG.error("Could not find explanation for " + error.id); } } // find errors without explanation nodeList = document.getElementsByTagName("li"); for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); if (element.hasAttribute("id")) { MessageDefinition error = new MessageDefinition(); String id = element.getAttribute("id"); error.id = StringUtils.substringAfterLast(id, "-"); error.remark = getGrandChildContent(element, "p").trim(); errors.add(error); } } }
From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java
/** * Populate the given DocumentDefaultsDefinition instance with the default lazy-init, * autowire, dependency check settings, init-method, destroy-method and merge settings. * Support nested 'beans' element use cases by falling back to <literal>parentDefaults</literal> * in case the defaults are not explicitly set locally. * @param defaults the defaults to populate * @param parentDefaults the parent BeanDefinitionParserDelegate (if any) defaults to fall back to * @param root the root element of the current bean definition document (or nested beans element) *//* w ww . j a v a 2s. c o m*/ protected void populateDefaults(DocumentDefaultsDefinition defaults, @Nullable DocumentDefaultsDefinition parentDefaults, Element root) { String lazyInit = root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE); if (DEFAULT_VALUE.equals(lazyInit)) { // Potentially inherited from outer <beans> sections, otherwise falling back to false. lazyInit = (parentDefaults != null ? parentDefaults.getLazyInit() : FALSE_VALUE); } defaults.setLazyInit(lazyInit); String merge = root.getAttribute(DEFAULT_MERGE_ATTRIBUTE); if (DEFAULT_VALUE.equals(merge)) { // Potentially inherited from outer <beans> sections, otherwise falling back to false. merge = (parentDefaults != null ? parentDefaults.getMerge() : FALSE_VALUE); } defaults.setMerge(merge); String autowire = root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE); if (DEFAULT_VALUE.equals(autowire)) { // Potentially inherited from outer <beans> sections, otherwise falling back to 'no'. autowire = (parentDefaults != null ? parentDefaults.getAutowire() : AUTOWIRE_NO_VALUE); } defaults.setAutowire(autowire); if (root.hasAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)) { defaults.setAutowireCandidates(root.getAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)); } else if (parentDefaults != null) { defaults.setAutowireCandidates(parentDefaults.getAutowireCandidates()); } if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) { defaults.setInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)); } else if (parentDefaults != null) { defaults.setInitMethod(parentDefaults.getInitMethod()); } if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) { defaults.setDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)); } else if (parentDefaults != null) { defaults.setDestroyMethod(parentDefaults.getDestroyMethod()); } defaults.setSource(this.readerContext.extractSource(root)); }
From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java
/** * Parse the bean definition itself, without regard to name or aliases. May return * {@code null} if problems occurred during the parsing of the bean definition. *///from w ww . j a va 2s . co m @Nullable public AbstractBeanDefinition parseBeanDefinitionElement(Element ele, String beanName, @Nullable BeanDefinition containingBean) { this.parseState.push(new BeanEntry(beanName)); String className = null; if (ele.hasAttribute(CLASS_ATTRIBUTE)) { className = ele.getAttribute(CLASS_ATTRIBUTE).trim(); } String parent = null; if (ele.hasAttribute(PARENT_ATTRIBUTE)) { parent = ele.getAttribute(PARENT_ATTRIBUTE); } try { AbstractBeanDefinition bd = createBeanDefinition(className, parent); parseBeanDefinitionAttributes(ele, beanName, containingBean, bd); bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT)); parseMetaElements(ele, bd); parseLookupOverrideSubElements(ele, bd.getMethodOverrides()); parseReplacedMethodSubElements(ele, bd.getMethodOverrides()); parseConstructorArgElements(ele, bd); parsePropertyElements(ele, bd); parseQualifierElements(ele, bd); bd.setResource(this.readerContext.getResource()); bd.setSource(extractSource(ele)); return bd; } 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.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java
/** * Apply the attributes of the given bean element to the given bean * definition. * @param ele bean declaration element/*from w w w.j a v a 2 s . co m*/ * @param beanName bean name * @param containingBean containing bean definition * @return a bean definition initialized according to the bean element attributes */ public AbstractBeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName, @Nullable BeanDefinition containingBean, AbstractBeanDefinition bd) { if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) { error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele); } else if (ele.hasAttribute(SCOPE_ATTRIBUTE)) { bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE)); } else if (containingBean != null) { // Take default from containing bean in case of an inner bean definition. bd.setScope(containingBean.getScope()); } if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) { bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE))); } String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE); if (DEFAULT_VALUE.equals(lazyInit)) { lazyInit = this.defaults.getLazyInit(); } bd.setLazyInit(TRUE_VALUE.equals(lazyInit)); String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE); bd.setAutowireMode(getAutowireMode(autowire)); if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) { String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE); bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS)); } String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE); if ("".equals(autowireCandidate) || DEFAULT_VALUE.equals(autowireCandidate)) { String candidatePattern = this.defaults.getAutowireCandidates(); if (candidatePattern != null) { String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern); bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName)); } } else { bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate)); } if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) { bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE))); } if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) { String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE); bd.setInitMethodName(initMethodName); } else if (this.defaults.getInitMethod() != null) { bd.setInitMethodName(this.defaults.getInitMethod()); bd.setEnforceInitMethod(false); } if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) { String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE); bd.setDestroyMethodName(destroyMethodName); } else if (this.defaults.getDestroyMethod() != null) { bd.setDestroyMethodName(this.defaults.getDestroyMethod()); bd.setEnforceDestroyMethod(false); } if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) { bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE)); } if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) { bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE)); } return bd; }
From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java
/** * Get the value of a property element. May be a list etc. * Also used for constructor arguments, "propertyName" being null in this case. */// w w w .j ava 2 s .c o m @Nullable public Object parsePropertyValue(Element ele, BeanDefinition bd, @Nullable 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; for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT) && !nodeNameEquals(node, META_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(REF_ATTRIBUTE); boolean hasValueAttribute = ele.hasAttribute(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(REF_ATTRIBUTE); if (!StringUtils.hasText(refName)) { error(elementName + " contains empty 'ref' attribute", ele); } RuntimeBeanReference ref = new RuntimeBeanReference(refName); ref.setSource(extractSource(ele)); return ref; } else if (hasValueAttribute) { TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE)); valueHolder.setSource(extractSource(ele)); return valueHolder; } else if (subElement != null) { return parsePropertySubElement(subElement, bd); } 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.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java
/** * Parse a map element.//from w w w. j ava 2s. c o m */ public Map<Object, Object> parseMapElement(Element mapEle, @Nullable BeanDefinition bd) { String defaultKeyType = mapEle.getAttribute(KEY_TYPE_ATTRIBUTE); String defaultValueType = mapEle.getAttribute(VALUE_TYPE_ATTRIBUTE); List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle, ENTRY_ELEMENT); ManagedMap<Object, Object> map = new ManagedMap<>(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 (nodeNameEquals(candidateEle, 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 (nodeNameEquals(candidateEle, DESCRIPTION_ELEMENT)) { // the element is a <description> -> ignore it } else 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(KEY_ATTRIBUTE); boolean hasKeyRefAttribute = entryEle.hasAttribute(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(KEY_ATTRIBUTE), defaultKeyType, entryEle); } else if (hasKeyRefAttribute) { String refName = entryEle.getAttribute(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(VALUE_ATTRIBUTE); boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE); boolean hasValueTypeAttribute = entryEle.hasAttribute(VALUE_TYPE_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 ((hasValueTypeAttribute && hasValueRefAttribute) || (hasValueTypeAttribute && !hasValueAttribute) || (hasValueTypeAttribute && valueEle != null)) { error("<entry> element is only allowed to contain a 'value-type' " + "attribute when it has a 'value' attribute", entryEle); } if (hasValueAttribute) { String valueType = entryEle.getAttribute(VALUE_TYPE_ATTRIBUTE); if (!StringUtils.hasText(valueType)) { valueType = defaultValueType; } value = buildTypedStringValueForMap(entryEle.getAttribute(VALUE_ATTRIBUTE), valueType, entryEle); } else if (hasValueRefAttribute) { String refName = entryEle.getAttribute(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.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java
/** * Initialize the default lazy-init, autowire and dependency check settings. * @see #setDefaultLazyInit/*from w ww . j av a 2s. c o m*/ * @see #setDefaultAutowire * @see #setDefaultDependencyCheck */ protected void initDefaults(Element root) { setDefaultLazyInit(root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE)); setDefaultAutowire(root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE)); setDefaultDependencyCheck(root.getAttribute(DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE)); if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) { setDefaultInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)); } if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) { setDefaultDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)); } }