Example usage for org.dom4j Attribute getValue

List of usage examples for org.dom4j Attribute getValue

Introduction

In this page you can find the example usage for org.dom4j Attribute getValue.

Prototype

String getValue();

Source Link

Document

Returns the value of the attribute.

Usage

From source file:org.onesocialweb.openfire.handler.pep.IQPEPHandler.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//w w  w .  jav  a 2 s  .co  m
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    final Element childElement = packet.getChildElement();
    final List<Element> pubsubElements = childElement.elements();

    if (pubsubElements != null && pubsubElements.size() > 0) {
        Element actionElement = pubsubElements.get(0);
        Attribute node = actionElement.attribute("node");
        PEPNodeHandler handler = getHandler(node.getValue());
        if (handler != null) {
            return handler.handleIQ(packet);
        }
    }

    return XMPPServer.getInstance().getIQPEPHandler().handleIQ(packet);
}

From source file:org.onosproject.yang.serializers.utils.SerializersUtil.java

License:Apache License

/**
 * Converts XML atrtibutes into annotated node info.
 *
 * @param element XML element/*from  w w  w .  j  a  va  2s .  c o m*/
 * @param id      resource id of an element
 * @return annotated node info
 */
public static AnnotatedNodeInfo convertXmlAttributesToAnnotations(Element element, ResourceId id) {
    Iterator iter = element.attributeIterator();
    if (!iter.hasNext()) {
        // element does not have any attributes
        return null;
    }
    AnnotatedNodeInfo.Builder builder = DefaultAnnotatedNodeInfo.builder();
    builder = builder.resourceId(id);
    while (iter.hasNext()) {
        Attribute attr = (Attribute) iter.next();
        DefaultAnnotation annotation = new DefaultAnnotation(attr.getQualifiedName(), attr.getValue());
        builder = builder.addAnnotation(annotation);
    }
    return builder.build();
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecListener.java

License:Apache License

@Override
public void enterXmlElement(Element element, XmlNodeType nodeType, Element rootElement) {
    if (element.equals(rootElement)) {
        return;//from w  ww . jav a2s  .  c o m
    }

    YdtContextOperationType opType = null;

    for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
        Attribute attr = (Attribute) iter.next();
        if (attr.getName().equals(OPERATION)) {
            opType = YdtContextOperationType.valueOf(attr.getValue().toUpperCase());
        }
    }

    String nameSpace = null;
    if (element.getNamespace() != null) {
        nameSpace = element.getNamespace().getURI();
    }

    /*
     * When new module has to be added, and if curnode has reference of
     * previous module, then we need to traverse back to parent(logical root
     * node).
     */
    if (ydtExtBuilder.getRootNode() == ydtExtBuilder.getCurNode().getParent() && prevNodeNamespace != null
            && !prevNodeNamespace.equals(nameSpace)) {
        ydtExtBuilder.traverseToParent();
    }

    if (nodeType == OBJECT_NODE && element.content() == null || element.content().isEmpty()) {
        nodeType = TEXT_NODE;
    }
    if (nodeType == OBJECT_NODE) {
        if (ydtExtBuilder != null) {
            if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) {
                ydtExtBuilder.addChild(null, nameSpace, opType);
            }
            ydtExtBuilder.addChild(element.getName(), nameSpace, opType);
        }
    } else if (nodeType == TEXT_NODE) {

        if (ydtExtBuilder != null) {
            if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) {
                ydtExtBuilder.addChild(null, nameSpace, opType);
            }
            ydtExtBuilder.addLeaf(element.getName(), nameSpace, element.getText());
        }
    }

    if (nameSpace != null) {
        prevNodeNamespace = nameSpace;
    }
}

From source file:org.openadaptor.auxil.convertor.map.Dom4JDocumentMapFacade.java

License:Open Source License

private Object modify(Node node, Attribute attribute) {
    Object old = null;/*from w  w w  . j  a  va 2s.  c om*/
    if (node instanceof Element) { //Set or replace value of attribute on Element
        Element element = (Element) node;
        String attrName = attribute.getName();
        Attribute existing = element.attribute(attrName);
        if (existing != null) {
            element.remove(existing);
            old = existing;
        }
        element.add(attribute);
    } else if (node instanceof Attribute) { //Modify the value of the existing attribute.
        Attribute existing = (Attribute) node;
        old = DocumentHelper.createAttribute(null, existing.getName(), existing.getValue());
        existing.setValue(attribute.getValue());
    } else {
        throw new RecordException(
                "Target of put(x,Attribute) must be empty or an existing Element or Attribute. Got: " + node);
    }
    return old;
}

From source file:org.openadaptor.auxil.convertor.map.Dom4JDocumentMapFacade.java

License:Open Source License

private Object modify(Node node, String value) {
    Object old = null;//  ww  w  .  j a va  2 s .co  m
    if (node instanceof Element) { //Set the text of the element
        Element element = (Element) node;
        old = element.getText();
        element.setText(value);
    } else if (node instanceof Attribute) {//Test the value of the attribute
        Attribute attribute = (Attribute) node;
        old = attribute.getValue();
        attribute.setValue(value);
    } else if (node instanceof Text) {
        Text text = (Text) node;
        old = node.getText();
        text.setText(value);
    }
    return old;
}

From source file:org.opencms.importexport.A_CmsImport.java

License:Open Source License

/**
 * Reads all properties below a specified parent element from the <code>manifest.xml</code>.<p>
 * /*  w w w.j  av a2 s  .  c o m*/
 * @param parentElement the current file node
 * @param ignoredPropertyKeys a list of properties to be ignored
 * 
 * @return a list with all properties
 */
protected List readPropertiesFromManifest(Element parentElement, List ignoredPropertyKeys) {

    // all imported Cms property objects are collected in map first forfaster access
    Map properties = new HashMap();
    CmsProperty property = null;
    List propertyElements = parentElement
            .selectNodes("./" + A_CmsImport.N_PROPERTIES + "/" + A_CmsImport.N_PROPERTY);
    Element propertyElement = null;
    String key = null, value = null;
    Attribute attrib = null;

    // iterate over all property elements
    for (int i = 0, n = propertyElements.size(); i < n; i++) {
        propertyElement = (Element) propertyElements.get(i);
        key = getChildElementTextValue(propertyElement, A_CmsImport.N_NAME);

        if ((key == null) || ignoredPropertyKeys.contains(key)) {
            // continue if the current property (key) should be ignored or is null
            continue;
        }

        // all Cms properties are collected in a map keyed by their property keys
        property = (CmsProperty) properties.get(key);
        if (property == null) {
            property = new CmsProperty();
            property.setName(key);
            property.setAutoCreatePropertyDefinition(true);
            properties.put(key, property);
        }

        value = getChildElementTextValue(propertyElement, A_CmsImport.N_VALUE);
        if (value == null) {
            value = "";
        }

        attrib = propertyElement.attribute(A_CmsImport.N_PROPERTY_ATTRIB_TYPE);
        if ((attrib != null) && attrib.getValue().equals(A_CmsImport.N_PROPERTY_ATTRIB_TYPE_SHARED)) {
            // it is a shared/resource property value
            property.setResourceValue(value);
        } else {
            // it is an individual/structure value
            property.setStructureValue(value);
        }
    }

    return new ArrayList(properties.values());
}

From source file:org.opencms.relations.CmsLink.java

License:Open Source License

/**
 * Reconstructs a link object from the given XML node.<p>
 * // w ww  .  ja  v  a  2 s  . c o m
 * @param element the XML node containing the link information
 */
public CmsLink(Element element) {

    m_element = element;
    Attribute attrName = element.attribute(ATTRIBUTE_NAME);
    if (attrName != null) {
        m_name = attrName.getValue();
    } else {
        m_name = DEFAULT_NAME;
    }
    Attribute attrType = element.attribute(ATTRIBUTE_TYPE);
    if (attrType != null) {
        m_type = CmsRelationType.valueOfXml(attrType.getValue());
    } else {
        m_type = DEFAULT_TYPE;
    }
    Attribute attrInternal = element.attribute(ATTRIBUTE_INTERNAL);
    if (attrInternal != null) {
        m_internal = Boolean.valueOf(attrInternal.getValue()).booleanValue();
    } else {
        m_internal = true;
    }

    Element uuid = element.element(NODE_UUID);
    Element target = element.element(NODE_TARGET);
    Element anchor = element.element(NODE_ANCHOR);
    Element query = element.element(NODE_QUERY);

    m_structureId = (uuid != null) ? new CmsUUID(uuid.getText()) : null;
    m_target = (target != null) ? target.getText() : null;
    m_anchor = (anchor != null) ? anchor.getText() : null;
    setQuery((query != null) ? query.getText() : null);

    // update the uri from the components
    setUri();
}

From source file:org.opencms.xml.A_CmsXmlDocument.java

License:Open Source License

/**
 * Corrects the structure of this XML document.<p>
 * /* www  .  j  av  a 2 s. c o  m*/
 * @param cms the current OpenCms user context
 * 
 * @return the file that contains the corrected XML structure
 * 
 * @throws CmsXmlException if something goes wrong
 */
public CmsFile correctXmlStructure(CmsObject cms) throws CmsXmlException {

    // apply XSD schema translation
    Attribute schema = m_document.getRootElement()
            .attribute(I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION);
    if (schema != null) {
        String schemaLocation = schema.getValue();
        String translatedSchema = OpenCms.getResourceManager().getXsdTranslator()
                .translateResource(schemaLocation);
        if (!schemaLocation.equals(translatedSchema)) {
            schema.setValue(translatedSchema);
        }
    }

    // iterate over all locales
    Iterator<Locale> i = m_locales.iterator();
    while (i.hasNext()) {
        Locale locale = i.next();
        List<String> names = getNames(locale);
        List<I_CmsXmlContentValue> validValues = new ArrayList<I_CmsXmlContentValue>();

        // iterate over all nodes per language
        Iterator<String> j = names.iterator();
        while (j.hasNext()) {

            // this step is required for values that need a processing of their content
            // an example for this is the HTML value that does link replacement                
            String name = j.next();
            I_CmsXmlContentValue value = getValue(name, locale);
            if (value.isSimpleType()) {
                String content = value.getStringValue(cms);
                value.setStringValue(cms, content);
            }

            // save valid elements for later check
            validValues.add(value);
        }

        if (isAutoCorrectionEnabled()) {
            // full correction of XML

            List<Element> roots = new ArrayList<Element>();
            List<CmsXmlContentDefinition> rootCds = new ArrayList<CmsXmlContentDefinition>();
            List<Element> validElements = new ArrayList<Element>();

            // gather all XML content definitions and their parent nodes                                
            Iterator<I_CmsXmlContentValue> it = validValues.iterator();
            while (it.hasNext()) {
                // collect all root elements, also for the nested content definitions
                I_CmsXmlContentValue value = it.next();
                Element element = value.getElement();
                validElements.add(element);
                if (element.supportsParent()) {
                    // get the parent XML node
                    Element root = element.getParent();
                    if ((root != null) && !roots.contains(root)) {
                        // this is a parent node we do not have already in our storage
                        CmsXmlContentDefinition rcd = value.getContentDefinition();
                        if (rcd != null) {
                            // this value has a valid XML content definition
                            roots.add(root);
                            rootCds.add(rcd);
                        } else {
                            // no valid content definition for the XML value
                            throw new CmsXmlException(
                                    Messages.get().container(Messages.ERR_CORRECT_NO_CONTENT_DEF_3,
                                            value.getName(), value.getTypeName(), value.getPath()));
                        }
                    }
                }
            }

            for (int le = 0; le < roots.size(); le++) {
                // iterate all XML content root nodes and correct each XML subtree

                Element root = roots.get(le);
                CmsXmlContentDefinition cd = rootCds.get(le);

                // step 1: first sort the nodes according to the schema, this takes care of re-ordered elements
                List<List<Element>> nodeLists = new ArrayList<List<Element>>();
                for (I_CmsXmlSchemaType type : cd.getTypeSequence()) {
                    List<Element> elements = CmsXmlGenericWrapper.elements(root, type.getName());
                    int maxOccures = cd.getChoiceMaxOccurs() > 0 ? cd.getChoiceMaxOccurs()
                            : type.getMaxOccurs();
                    if (elements.size() > maxOccures) {
                        // to many nodes of this type appear according to the current schema definition
                        for (int lo = (elements.size() - 1); lo >= type.getMaxOccurs(); lo--) {
                            elements.remove(lo);
                        }
                    }
                    nodeLists.add(elements);
                }

                // step 2: clear the list of nodes (this will remove all invalid nodes)
                List<Element> nodeList = CmsXmlGenericWrapper.elements(root);
                nodeList.clear();
                Iterator<List<Element>> in = nodeLists.iterator();
                while (in.hasNext()) {
                    // now add all valid nodes in the right order
                    List<Element> elements = in.next();
                    nodeList.addAll(elements);
                }

                // step 3: now append the missing elements according to the XML content definition
                cd.addDefaultXml(cms, this, root, locale);
            }
        }
    }

    // write the modified XML back to the VFS file 
    if (m_file != null) {
        // make sure the file object is available
        m_file.setContents(marshal());
    }
    return m_file;
}

From source file:org.opencms.xml.CmsXmlContentDefinition.java

License:Open Source License

/**
 * Validates if a given attribute exists at the given element with an (optional) specified value.<p>
 * //from w w  w  .j  a va2  s. c  o m
 * If the required value is not <code>null</code>, the attribute must have exactly this 
 * value set.<p> 
 * 
 * If no value is required, some simple validation is performed on the attribute value,
 * like a check that the value does not have leading or trailing white spaces.<p>
 * 
 * @param element the element to validate
 * @param attributeName the attribute to check for
 * @param requiredValue the required value of the attribute, or <code>null</code> if any value is allowed
 * 
 * @return the value of the attribute
 * 
 * @throws CmsXmlException if the element does not have the required attribute set, or if the validation fails
 */
protected static String validateAttribute(Element element, String attributeName, String requiredValue)
        throws CmsXmlException {

    Attribute attribute = element.attribute(attributeName);
    if (attribute == null) {
        throw new CmsXmlException(Messages.get().container(Messages.ERR_EL_MISSING_ATTRIBUTE_2,
                element.getUniquePath(), attributeName));
    }
    String value = attribute.getValue();

    if (requiredValue == null) {
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(value) || !value.equals(value.trim())) {
            throw new CmsXmlException(Messages.get().container(Messages.ERR_EL_BAD_ATTRIBUTE_WS_3,
                    element.getUniquePath(), attributeName, value));
        }
    } else {
        if (!requiredValue.equals(value)) {
            throw new CmsXmlException(Messages.get().container(Messages.ERR_EL_BAD_ATTRIBUTE_VALUE_4,
                    new Object[] { element.getUniquePath(), attributeName, requiredValue, value }));
        }
    }
    return value;
}

From source file:org.opencms.xml.page.CmsXmlPage.java

License:Open Source License

/**
 * Checks if the element of a page object is enabled.<p>
 * // w  w w . j av  a 2s  .  c  o m
 * @param name the name of the element
 * @param locale the locale of the element
 * @return true if the element exists and is not disabled
 */
@Override
public boolean isEnabled(String name, Locale locale) {

    CmsXmlHtmlValue value = (CmsXmlHtmlValue) getValue(name, locale);

    if (value != null) {
        Element element = value.getElement();
        Attribute enabled = element.attribute(ATTRIBUTE_ENABLED);
        return ((enabled == null) || Boolean.valueOf(enabled.getValue()).booleanValue());
    }

    return false;
}