List of usage examples for org.dom4j Element attributes
List<Attribute> attributes();
From source file:org.jcommon.com.util.config.ConfigLoader.java
License:Apache License
@SuppressWarnings("unchecked") private static String getTextFromElement(Element e) { Element element = e; if (element != null) { List<Attribute> l = element.attributes(); if (l != null && l.size() != 0) { String[] keys = new String[l.size()]; String[] values = new String[l.size()]; for (int i = 0; i < l.size(); i++) { Attribute a = l.get(i); keys[i] = a.getName();//w ww.jav a2 s.com values[i] = a.getValue(); } return JsonUtils.toJson(keys, values, false); } return element.getTextTrim(); } return null; }
From source file:org.localmatters.serializer.config.SerializationElementHandler.java
License:Apache License
/** * Handles the type of the element/*w w w . j av a2 s . com*/ * which is a special case) * @param element The element * @param attributes The map of attributes consumed for this element * @return The serialization for this element */ @SuppressWarnings("unchecked") protected Serialization handleType(Element element, Map<String, String> attributes) { AbstractSerialization serialization = null; String type = element.getName(); if (TYPE_REFERENCE.equalsIgnoreCase(type)) { serialization = handleReference(element, attributes); } else if (TYPE_COMPLEX.equalsIgnoreCase(type)) { String parent = element.attributeValue(ATTRIBUTE_PARENT); if (StringUtils.isNotBlank(parent)) { attributes.put(ATTRIBUTE_PARENT, parent); } serialization = handleComplex(element, attributes); } else if (TYPE_ATTRIBUTE.equalsIgnoreCase(type)) { serialization = handleAttribute(element, attributes); } else if (TYPE_NAMESPACE.equalsIgnoreCase(type)) { serialization = handleNamespace(element, attributes); } else if (TYPE_VALUE.equalsIgnoreCase(type)) { serialization = handleValue(element, attributes); } else if (TYPE_LIST.equalsIgnoreCase(type)) { serialization = handleList(element, attributes); } else if (TYPE_MAP.equalsIgnoreCase(type)) { serialization = handleMap(element, attributes); } else { throw new ConfigurationException(INVALID_TYPE_FORMAT, type, element.getPath()); } String displayEmpty = element.attributeValue(ATTRIBUTE_DISPLAY_EMPTY); if (StringUtils.isNotBlank(displayEmpty)) { attributes.put(ATTRIBUTE_DISPLAY_EMPTY, displayEmpty); serialization.setWriteEmpty(Boolean.valueOf(displayEmpty)); } // validates the number of attributes with the ones that have been // consumed to see if the element contains invalid attributes if (CollectionUtils.size(element.attributes()) != CollectionUtils.size(attributes)) { List<String> invalids = new ArrayList<String>(); for (Element attribute : (List<Element>) element.attributes()) { String attributeName = attribute.getName(); if (!attributes.containsKey(attributeName)) { invalids.add(attributeName); } } throw new ConfigurationException(INVALID_ATTRIBUTES_FORMAT, invalids, type, element.getPath()); } return serialization; }
From source file:org.lushlife.guicexml.internal.xml.ComponentXmlReader.java
License:Apache License
public static Map<String, PropertyValue> createAttributeMap(Element element, PropertyManagement xmlManagement) { Map<String, PropertyValue> attribute = new HashMap<String, PropertyValue>(); for (Object obj : element.attributes()) { Attribute attr = (Attribute) obj; if (attr.getName().equals("name")) { continue; }/*from w ww .j a v a 2 s .co m*/ if (attr.getName().equals("id")) { continue; } if (attr.getName().equals("qualifier")) { continue; } if (attr.getName().equals("class")) { continue; } if (attr.getName().equals("types")) { continue; } if (attr.getName().equals("scope")) { continue; } if (attr.getName().equals("startup")) { continue; } if (attr.getName().equals("constructor-parameter-types")) { continue; } if (attr.getName().equals("factory-method")) { continue; } if (attr.getName().equals("args")) { continue; } attribute.put(attr.getName(), xmlManagement.toPropertyValue(attr.getValue())); } for (Object obj : element.elements()) { Element ele = (Element) obj; if (ele.getName().equals("property")) { String stringValue = ele.attributeValue("value"); String k = ele.attributeValue("name"); PropertyValue v = (stringValue != null) ? xmlManagement.toPropertyValue(stringValue) : xmlManagement.toPropertyValue(ele); attribute.put(k, v); } else { String k = ele.getName(); PropertyValue v = xmlManagement.toPropertyValue(ele); attribute.put(k, v); } } return attribute; }
From source file:org.makumba.commons.tags.MakumbaTLDGenerator.java
License:Open Source License
/** * Replaces the content of a tag attribute using "specifiedIn" by the actual content * /*from w ww . j a v a2 s . co m*/ * @param processedTags * the hashmap of already processed tags * @param errorMsg * the error message appearing in case the referenced attribute can't be found * @param tagName * the name of the parent tag * @param attributeTagContent * the content of the attribute */ public static void replaceReferencedAttribute(HashMap<String, Element> processedTags, final String errorMsg, String tagName, Element attributeTagContent) { Element newTag = getReferencedAttributes(processedTags, errorMsg, tagName, attributeTagContent, true); attributeTagContent.setAttributes(newTag.attributes()); final List<Element> elements = getElementList(newTag); for (Element element : elements) { attributeTagContent.add((Element) element.clone()); } }
From source file:org.onecmdb.core.utils.xml.XmlParser.java
License:Open Source License
public void dumpElement(Element el, int level) { System.out.println(tab(level) + el.getName() + ":" + el.getData() + " {"); List attributes = el.attributes(); for (int i = 0; i < attributes.size(); i++) { Attribute a = (Attribute) attributes.get(i); System.out.println(tab(level) + " " + a.getName() + "=" + a.getData()); }//from w w w . j a va 2s.c o m List els = el.elements(); for (int i = 0; i < els.size(); i++) { dumpElement((Element) els.get(i), level + 1); } System.out.println(tab(level) + "}"); }
From source file:org.onecmdb.ui.gwt.desktop.server.service.content.adapter.GXTModelContentAdapter.java
License:Open Source License
public BaseModel updateModel(Element el, BaseModel parent) throws Exception { //System.out.println(el.getName()); String className = classMap.getProperty(el.getName()); if (className == null) { className = ConfigurationFactory.get("gxtadapter." + el.getName() + ".class"); if (className == null) { className = BaseModel.class.getName(); }//from www . j av a 2 s . c o m //throw new IllegalArgumentException(el.getName() + " has no class definition"); } Class clazz = Class.forName(className); Object object = clazz.newInstance(); if (!(object instanceof BaseModel)) { throw new IllegalArgumentException( el.getName() + " class " + className + " is not implementing BaseModel"); } BaseModel model = (BaseModel) object; if (parent == null) { parent = model; } model.set("tag", el.getName()); for (Attribute a : (List<Attribute>) el.attributes()) { updateModelValue(model, a.getName(), a.getText(), false); } for (Element e : (List<Element>) el.elements()) { boolean asList = false; Attribute a = e.attribute("asList"); if (a != null) { asList = true; } boolean simpleList = false; a = e.attribute("asSimpleList"); if (a != null) { asList = true; simpleList = true; } if (isSimpleElement(e) || simpleList) { updateModelValue(model, e.getName(), e.getTextTrim(), asList); } else { updateModelValue(model, e.getName(), updateModel(e, model), asList); } } return (model); }
From source file:org.opencms.setup.xml.CmsSetupXmlHelper.java
License:Open Source License
/** * Replaces a attibute's value in the given node addressed by the xPath.<p> * /*from w ww . j a v a2s . c o m*/ * @param document the document to replace the node attribute * @param xPath the xPath to the node * @param attribute the attribute to replace the value of * @param value the new value to set * * @return <code>true</code> if successful <code>false</code> otherwise */ public static boolean setAttribute(Document document, String xPath, String attribute, String value) { Node node = document.selectSingleNode(xPath); Element e = (Element) node; @SuppressWarnings("unchecked") List<Attribute> attributes = e.attributes(); for (Attribute a : attributes) { if (a.getName().equals(attribute)) { a.setValue(value); return true; } } return false; }
From source file:org.opencms.xml.CmsXmlContentDefinition.java
License:Open Source License
/** * Validates if a given element has exactly the required attributes set.<p> * /*from w w w . j a v a2 s . c o m*/ * @param element the element to validate * @param requiredAttributes the list of required attributes * @param optionalAttributes the list of optional attributes * * @throws CmsXmlException if the validation fails */ protected static void validateAttributesExists(Element element, String[] requiredAttributes, String[] optionalAttributes) throws CmsXmlException { if (element.attributeCount() < requiredAttributes.length) { throw new CmsXmlException( Messages.get().container(Messages.ERR_EL_ATTRIBUTE_TOOFEW_3, element.getUniquePath(), new Integer(requiredAttributes.length), new Integer(element.attributeCount()))); } if (element.attributeCount() > (requiredAttributes.length + optionalAttributes.length)) { throw new CmsXmlException(Messages.get().container(Messages.ERR_EL_ATTRIBUTE_TOOMANY_3, element.getUniquePath(), new Integer(requiredAttributes.length + optionalAttributes.length), new Integer(element.attributeCount()))); } for (int i = 0; i < requiredAttributes.length; i++) { String attributeName = requiredAttributes[i]; if (element.attribute(attributeName) == null) { throw new CmsXmlException(Messages.get().container(Messages.ERR_EL_MISSING_ATTRIBUTE_2, element.getUniquePath(), attributeName)); } } List<String> rA = Arrays.asList(requiredAttributes); List<String> oA = Arrays.asList(optionalAttributes); for (int i = 0; i < element.attributes().size(); i++) { String attributeName = element.attribute(i).getName(); if (!rA.contains(attributeName) && !oA.contains(attributeName)) { throw new CmsXmlException(Messages.get().container(Messages.ERR_EL_INVALID_ATTRIBUTE_2, element.getUniquePath(), attributeName)); } } }
From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java
License:Open Source License
/** * <p>/*w w w . j ava 2s.c o m*/ * This will handle printing out an <code>{@link Element}</code>, * its <code>{@link Attribute}</code>s, and its value. * </p> * * @param element <code>Element</code> to output. * @param out <code>Writer</code> to write to. * @param indent <code>int</code> level of indention. * @param namespaces <code>List</code> stack of Namespaces in scope. */ protected void printElement(Element element, Writer out, int indentLevel, TDOM4JNamespaceStack namespaces) throws IOException { List mixedContent = element.elements(); boolean empty = mixedContent.size() == 0; boolean stringOnly = !empty && mixedContent.size() == 1 && mixedContent.get(0) instanceof String; // Print beginning element tag /* maybe the doctype, xml declaration, and processing instructions should only break before and not after; then this check is unnecessary, or maybe the println should only come after and never before. Then the output always ends with a newline */ indent(out, indentLevel); // Print the beginning of the tag plus attributes and any // necessary namespace declarations out.write("<"); out.write(element.getQualifiedName()); int previouslyDeclaredNamespaces = namespaces.size(); Namespace ns = element.getNamespace(); // Add namespace decl only if it's not the XML namespace and it's // not the NO_NAMESPACE with the prefix "" not yet mapped // (we do output xmlns="" if the "" prefix was already used and we // need to reclaim it for the NO_NAMESPACE) if (ns != Namespace.XML_NAMESPACE && !(ns == Namespace.NO_NAMESPACE && namespaces.getURI("") == null)) { String prefix = ns.getPrefix(); String uri = namespaces.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace decl namespaces.push(ns); printNamespace(ns, out); } } // Print out additional namespace declarations List additionalNamespaces = element.additionalNamespaces(); if (additionalNamespaces != null) { for (int i = 0; i < additionalNamespaces.size(); i++) { Namespace additional = (Namespace) additionalNamespaces.get(i); String prefix = additional.getPrefix(); String uri = namespaces.getURI(prefix); if (!additional.getURI().equals(uri)) { namespaces.push(additional); printNamespace(additional, out); } } } printAttributes(element.attributes(), element, out, namespaces); // handle "" string same as empty if (stringOnly) { String elementText = trimText ? element.getTextTrim() : element.getText(); if (elementText == null || elementText.equals("")) { empty = true; } } if (empty) { // Simply close up if (!expandEmptyElements) { out.write(" />"); } else { out.write("></"); out.write(element.getQualifiedName()); out.write(">"); } maybePrintln(out); } else { // we know it's not null or empty from above out.write(">"); if (stringOnly) { // if string only, print content on same line as tags printElementContent(element, out, indentLevel, namespaces, mixedContent); } else { maybePrintln(out); printElementContent(element, out, indentLevel, namespaces, mixedContent); indent(out, indentLevel); } out.write("</"); out.write(element.getQualifiedName()); out.write(">"); maybePrintln(out); } // remove declared namespaces from stack while (namespaces.size() > previouslyDeclaredNamespaces) { namespaces.pop(); } }
From source file:org.orbeon.oxf.transformer.xupdate.statement.Utils.java
License:Open Source License
/** * Evaluate a sequence a statements, and insert the result of the * evaluation the given parent node at the given position. *///from w w w .jav a 2 s .c o m public static void insert(LocationData locationData, Node parent, int position, Object toInsert) { List nodesToInsert = xpathObjectToDOM4JList(locationData, toInsert); if (parent instanceof Element) Collections.reverse(nodesToInsert); for (Iterator j = nodesToInsert.iterator(); j.hasNext();) { Object object = j.next(); Node node = object instanceof String || object instanceof Number ? Dom4jUtils.createText(object.toString()) : (Node) ((Node) object).clone(); if (parent instanceof Element) { Element element = (Element) parent; if (node instanceof Attribute) { element.attributes().add(node); } else { element.content().add(position, node); } } else if (parent instanceof Attribute) { Attribute attribute = (Attribute) parent; attribute.setValue(attribute.getValue() + node.getText()); } else if (parent instanceof Document) { // Update a document element final Document document = (Document) parent; if (node instanceof Element) { if (document.getRootElement() != null) throw new ValidationException("Document already has a root element", locationData); document.setRootElement((Element) node); } else if (node instanceof ProcessingInstruction) { document.add(node); } else { throw new ValidationException( "Only an element or processing instruction can be at the root of a document", locationData); } } else { throw new ValidationException("Cannot insert into a node of type '" + parent.getClass() + "'", locationData); } } }