Example usage for org.w3c.dom Element getAttributes

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

Introduction

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

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:com.amalto.core.storage.hibernate.MappingGenerator.java

private Element handleSimpleField(FieldMetadata field) {
    if (isDoingColumns) {
        Element column = document.createElement("column"); //$NON-NLS-1$
        Attr columnName = document.createAttribute("name"); //$NON-NLS-1$
        String columnNameValue = resolver.get(field, compositeKeyPrefix);
        columnName.setValue(columnNameValue);
        column.getAttributes().setNamedItem(columnName);
        // TMDM-9003: should add not-null="false" explicitly if not-null isn't "true" for many-to-many scenario 
        Attr notNull = document.createAttribute("not-null"); //$NON-NLS-1$
        if (generateConstrains && isColumnMandatory) {
            notNull.setValue(Boolean.TRUE.toString());
        } else {/*from   www  .j a  v  a2 s .  c o m*/
            notNull.setValue(Boolean.FALSE.toString());
        }
        column.getAttributes().setNamedItem(notNull);

        if (resolver.isIndexed(field)) { // Create indexes for fields that should be indexed.
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Creating index for field '" + field.getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }
            Attr indexName = document.createAttribute("index"); //$NON-NLS-1$
            setIndexName(field, columnNameValue, indexName);
            column.getAttributes().setNamedItem(indexName);
        }
        parentElement.appendChild(column);
        return column;
    }
    if (field.isKey()) {
        Element idElement;
        if (!compositeId) {
            idElement = document.createElement("id"); //$NON-NLS-1$
            if (Types.UUID.equals(field.getType().getName())
                    && ScatteredMappingCreator.GENERATED_ID.equals(field.getName())) {
                // <generator class="org.hibernate.id.UUIDGenerator"/>
                Element generator = document.createElement("generator"); //$NON-NLS-1$
                Attr generatorClass = document.createAttribute("class"); //$NON-NLS-1$
                generatorClass.setValue("org.hibernate.id.UUIDGenerator"); //$NON-NLS-1$
                generator.getAttributes().setNamedItem(generatorClass);
                idElement.appendChild(generator);
            }
        } else {
            idElement = document.createElement("key-property"); //$NON-NLS-1$
        }
        Attr idName = document.createAttribute("name"); //$NON-NLS-1$
        idName.setValue(field.getName());
        Attr columnName = document.createAttribute("column"); //$NON-NLS-1$
        columnName.setValue(resolver.get(field));

        idElement.getAttributes().setNamedItem(idName);
        idElement.getAttributes().setNamedItem(columnName);
        if (field.getType().getData(MetadataRepository.DATA_MAX_LENGTH) != null) {
            Attr length = document.createAttribute("length"); //$NON-NLS-1$
            String value = field.getType().<String>getData(MetadataRepository.DATA_MAX_LENGTH);
            length.setValue(value);
            idElement.getAttributes().setNamedItem(length);
        }
        return idElement;
    } else {
        if (!field.isMany()) {
            Element propertyElement = document.createElement("property"); //$NON-NLS-1$
            Attr propertyName = document.createAttribute("name"); //$NON-NLS-1$
            propertyName.setValue(field.getName());
            Element columnElement = document.createElement("column"); //$NON-NLS-1$
            Attr columnName = document.createAttribute("name"); //$NON-NLS-1$
            columnName.setValue(resolver.get(field));

            if (resolver.isIndexed(field)) { // Create indexes for fields that should be indexed.
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Creating index for field '" + field.getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                }
                Attr indexName = document.createAttribute("index"); //$NON-NLS-1$
                setIndexName(field, field.getName(), indexName);
                propertyElement.getAttributes().setNamedItem(indexName);
            } else {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("*Not* creating index for field '" + field.getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
            // Not null
            Attr notNull = document.createAttribute("not-null"); //$NON-NLS-1$
            if (generateConstrains && field.isMandatory()) {
                notNull.setValue(Boolean.TRUE.toString());
            } else {
                notNull.setValue(Boolean.FALSE.toString());
            }
            columnElement.getAttributes().setNamedItem(notNull);
            // default value
            addDefaultValueAttribute(field, columnElement);

            addFieldTypeAttribute(field, columnElement, propertyElement, dataSource.getDialectName());
            propertyElement.getAttributes().setNamedItem(propertyName);
            columnElement.getAttributes().setNamedItem(columnName);
            propertyElement.appendChild(columnElement);
            return propertyElement;
        } else {
            Element listElement = document.createElement("list"); //$NON-NLS-1$
            Attr name = document.createAttribute("name"); //$NON-NLS-1$
            name.setValue(field.getName());
            Attr tableName = document.createAttribute("table"); //$NON-NLS-1$
            tableName.setValue(resolver.getCollectionTable(field));
            listElement.getAttributes().setNamedItem(tableName);
            if (field.getContainingType().getKeyFields().size() == 1) {
                // lazy="extra"
                Attr lazyAttribute = document.createAttribute("lazy"); //$NON-NLS-1$
                lazyAttribute.setValue("extra"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(lazyAttribute);
                // fetch="join"
                Attr fetchAttribute = document.createAttribute("fetch"); //$NON-NLS-1$
                fetchAttribute.setValue("join"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(fetchAttribute);
                // inverse="true"
                Attr inverse = document.createAttribute("inverse"); //$NON-NLS-1$
                inverse.setValue("false"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(inverse);
            } else {
                /*
                 * Hibernate does not handle correctly reverse collection when main entity owns multiple keys.
                 */
                // lazy="false"
                Attr lazyAttribute = document.createAttribute("lazy"); //$NON-NLS-1$
                lazyAttribute.setValue("false"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(lazyAttribute);
                // In case containing type has > 1 key, switch to fetch="select" since Hibernate returns incorrect
                // results in case of fetch="join".
                Attr fetchAttribute = document.createAttribute("fetch"); //$NON-NLS-1$
                fetchAttribute.setValue("select"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(fetchAttribute);
                // batch-size="20"
                Attr batchSize = document.createAttribute("batch-size"); //$NON-NLS-1$
                batchSize.setValue("20"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(batchSize);
            }
            // cascade="delete"
            Attr cascade = document.createAttribute("cascade"); //$NON-NLS-1$
            cascade.setValue("lock, all-delete-orphan"); //$NON-NLS-1$
            listElement.getAttributes().setNamedItem(cascade);
            // Keys
            Element key = document.createElement("key"); //$NON-NLS-1$
            Collection<FieldMetadata> keyFields = field.getContainingType().getKeyFields();
            for (FieldMetadata keyField : keyFields) {
                Element column = document.createElement("column"); //$NON-NLS-1$
                Attr columnName = document.createAttribute("name"); //$NON-NLS-1$
                column.getAttributes().setNamedItem(columnName);
                columnName.setValue(resolver.get(keyField));
                key.appendChild(column);
            }
            // <element column="name" type="string"/>
            Element element = document.createElement("element"); //$NON-NLS-1$
            Element columnElement = document.createElement("column"); //$NON-NLS-1$
            Attr columnNameAttr = document.createAttribute("name"); //$NON-NLS-1$
            columnNameAttr.setValue("value"); //$NON-NLS-1$

            // default value
            addDefaultValueAttribute(field, columnElement);

            columnElement.getAttributes().setNamedItem(columnNameAttr);
            addFieldTypeAttribute(field, columnElement, element, dataSource.getDialectName());
            element.appendChild(columnElement);
            // Not null warning
            if (field.isMandatory()) {
                LOGGER.warn("Field '" + field.getName() //$NON-NLS-1$
                        + "' is mandatory and a collection. Constraint can not be expressed in database schema."); //$NON-NLS-1$
            }
            // <index column="pos" />
            Element index = document.createElement("list-index"); //$NON-NLS-1$
            Attr indexColumn = document.createAttribute("column"); //$NON-NLS-1$
            indexColumn.setValue("pos"); //$NON-NLS-1$
            index.getAttributes().setNamedItem(indexColumn);
            listElement.getAttributes().setNamedItem(name);
            listElement.appendChild(key);
            listElement.appendChild(index);
            listElement.appendChild(element);
            return listElement;
        }
    }
}

From source file:com.amalto.core.storage.hibernate.MappingGenerator.java

private void addDefaultValueAttribute(FieldMetadata field, Element columnElement) {
    // default value
    String defaultValueRule = field.getData(MetadataRepository.DEFAULT_VALUE_RULE);
    if (StringUtils.isNotBlank(defaultValueRule)) {
        Attr defaultValueAttr = document.createAttribute("default"); //$NON-NLS-1$
        defaultValueAttr.setValue(HibernateStorageUtils.convertedDefaultValue(dataSource.getDialectName(),
                defaultValueRule, "'"));
        columnElement.getAttributes().setNamedItem(defaultValueAttr);
    }//from   ww w.ja  va  2 s .  c  o  m
}

From source file:com.hp.hpl.inkml.InkMLDOMParser.java

/**
 * Method to bind Annotation element/*from   w w w .ja  va 2  s  . c o m*/
 * 
 * @param element the Annotation element
 * @return Annotation data object
 * @throws InkMLException
 */
protected Annotation getAnnotation(final Element element) throws InkMLException {
    final Annotation annotation = new Annotation();

    final NamedNodeMap attributesMap = element.getAttributes();
    final int length = attributesMap.getLength();
    for (int index = 0; index < length; index++) {
        final Attr attribute = (Attr) attributesMap.item(index);
        final String attributeName = attribute.getName();
        if ("type".equals(attributeName)) {
            annotation.setType(attribute.getValue());
        } else if ("encoding".equals(attributeName)) {
            annotation.setEncoding(attribute.getValue());
        } else {
            annotation.addToOtherAttributesMap(attributeName, attribute.getValue());
        }
    }
    final Node valueNode = element.getFirstChild();
    if (null != valueNode) {
        annotation.setAnnotationTextValue(valueNode.getNodeValue());
    }
    return annotation;
}

From source file:com.hp.hpl.inkml.InkMLDOMParser.java

/**
 * Method to bind AnnotationXML element/*from   w w w. ja  v a 2 s .  c  o  m*/
 * 
 * @param element the AnnotationXML element
 * @return AnnotationXML data object
 * @throws InkMLException
 */
protected AnnotationXML getAnnotationXML(final Element element) throws InkMLException {
    final AnnotationXML aXml = new AnnotationXML();
    final NamedNodeMap attributesMap = element.getAttributes();
    final int length = attributesMap.getLength();
    for (int index = 0; index < length; index++) {
        final Attr attribute = (Attr) attributesMap.item(index);
        final String attributeName = attribute.getName();
        if ("type".equals(attributeName)) {
            aXml.setType(attribute.getValue());
        } else if ("encoding".equals(attributeName)) {
            aXml.setEncoding(attribute.getValue());
        } else {
            aXml.addToOtherAttributesMap(attributeName, attribute.getValue());
        }
    }
    InkMLDOMParser.LOG.finest("annotationXML received: " + element.toString());
    final NodeList list = element.getChildNodes();
    final int nChildren = list.getLength();
    if (nChildren > 0) {
        for (int i = 0; i < nChildren; i++) {
            final Node node = list.item(i);
            if (!(node instanceof Element)) {
                continue;
            }
            final Element childElement = (Element) node;
            // get the tagName to use as Key in the valueMap
            final String tagName = childElement.getLocalName();
            // String key = this.parentXPath+"/"+tagName;
            final String value = childElement.getFirstChild().getNodeValue();
            // propertyElementsMap.put(key, childElement);
            // propertyElementsMap.put(key, value);
            aXml.addToPropertyElementsMap(tagName, value);
            InkMLDOMParser.LOG
                    .finer("The property with name = " + tagName + " is added to the propertyElementsMap.");
        }
    }
    return aXml;
}

From source file:com.amalto.core.storage.hibernate.MappingGenerator.java

private Element newManyToManyElement(boolean enforceDataBaseIntegrity, ReferenceFieldMetadata referencedField) {
    // <many-to-many column="bar_id" class="Bar"/>
    Element manyToMany = document.createElement("many-to-many"); //$NON-NLS-1$
    // If data model authorizes fk integrity override, don't enforce database FK integrity.
    if (enforceDataBaseIntegrity) {
        // Ensure default settings for Hibernate are set (in case they change).
        Attr notFound = document.createAttribute("not-found"); //$NON-NLS-1$
        notFound.setValue("exception"); //$NON-NLS-1$
        manyToMany.getAttributes().setNamedItem(notFound);
        // foreign-key="..."
        String fkConstraintName = resolver.getFkConstraintName(referencedField);
        if (!fkConstraintName.isEmpty()) {
            Attr foreignKeyConstraintName = document.createAttribute("foreign-key"); //$NON-NLS-1$
            foreignKeyConstraintName.setValue(fkConstraintName);
            manyToMany.getAttributes().setNamedItem(foreignKeyConstraintName);
        }//from ww w  . j  a v  a 2s.c o  m
    } else {
        // Disables all warning/errors from Hibernate.
        Attr integrity = document.createAttribute("unique"); //$NON-NLS-1$
        integrity.setValue("false"); //$NON-NLS-1$
        manyToMany.getAttributes().setNamedItem(integrity);

        Attr foreignKey = document.createAttribute("foreign-key"); //$NON-NLS-1$
        // Disables foreign key generation for DDL.
        foreignKey.setValue("none"); //$NON-NLS-1$
        manyToMany.getAttributes().setNamedItem(foreignKey);

        Attr notFound = document.createAttribute("not-found"); //$NON-NLS-1$
        notFound.setValue("ignore"); //$NON-NLS-1$
        manyToMany.getAttributes().setNamedItem(notFound);
    }
    Attr className = document.createAttribute("class"); //$NON-NLS-1$
    className.setValue(ClassCreator.getClassName(referencedField.getReferencedType().getName()));
    manyToMany.getAttributes().setNamedItem(className);
    isDoingColumns = true;
    this.parentElement = manyToMany;
    isColumnMandatory = referencedField.isMandatory() && generateConstrains;
    compositeKeyPrefix = referencedField.getName();
    {
        referencedField.getReferencedField().accept(this);
    }
    isDoingColumns = false;
    return manyToMany;
}

From source file:com.draagon.meta.loader.xml.XMLFileMetaDataLoader.java

/**
 * Parses actual element attributes and adds them as StringAttributes
 *///from w ww. jav a 2s. c o  m
protected void parseAttributes(MetaData md, Element el) {

    NamedNodeMap attrs = el.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {

        Node n = attrs.item(i);
        String attrName = n.getNodeName();
        if (!reservedAttributes.contains(attrName)) {

            String value = n.getNodeValue();
            md.addAttribute(new StringAttribute(attrName, value));
        }
    }
}

From source file:com.netspective.sparx.util.xml.XmlSource.java

public void inheritElement(Element srcElement, Element destElem, Set excludeElems, String inheritedFromNode) {
    NamedNodeMap inhAttrs = srcElement.getAttributes();
    for (int i = 0; i < inhAttrs.getLength(); i++) {
        Node attrNode = inhAttrs.item(i);
        final String nodeName = attrNode.getNodeName();
        if (!excludeElems.contains(nodeName) && destElem.getAttribute(nodeName).equals(""))
            destElem.setAttribute(nodeName, attrNode.getNodeValue());
    }/* w ww.  j ava  2 s  .  c  o  m*/

    DocumentFragment inheritFragment = xmlDoc.createDocumentFragment();
    NodeList inhChildren = srcElement.getChildNodes();
    for (int i = inhChildren.getLength() - 1; i >= 0; i--) {
        Node childNode = inhChildren.item(i);

        // only add if there isn't an attribute overriding this element
        final String nodeName = childNode.getNodeName();
        if (destElem.getAttribute(nodeName).length() == 0 && (!excludeElems.contains(nodeName))) {
            Node cloned = childNode.cloneNode(true);
            if (inheritedFromNode != null && cloned.getNodeType() == Node.ELEMENT_NODE)
                ((Element) cloned).setAttribute("_inherited-from", inheritedFromNode);
            inheritFragment.insertBefore(cloned, inheritFragment.getFirstChild());
        }
    }

    destElem.insertBefore(inheritFragment, destElem.getFirstChild());
}

From source file:com.nridge.core.base.io.xml.DocumentXML.java

private Document loadDocument(Element anElement) throws IOException {
    Node nodeItem;//from  w w w. j a v  a  2  s. c  o m
    Attr nodeAttr;
    Document document;
    Element nodeElement;
    String nodeName, nodeValue;

    String docName = anElement.getAttribute("name");
    String typeName = anElement.getAttribute("type");
    String docTitle = anElement.getAttribute("title");
    String schemaVersion = anElement.getAttribute("schemaVersion");
    if ((StringUtils.isNotEmpty(typeName)) && (StringUtils.isNotEmpty(schemaVersion)))
        document = new Document(typeName);
    else
        document = new Document("Unknown");
    if (StringUtils.isNotEmpty(docName))
        document.setName(docName);
    if (StringUtils.isNotEmpty(docTitle))
        document.setName(docTitle);

    NamedNodeMap namedNodeMap = anElement.getAttributes();
    int attrCount = namedNodeMap.getLength();
    for (int attrOffset = 0; attrOffset < attrCount; attrOffset++) {
        nodeAttr = (Attr) namedNodeMap.item(attrOffset);
        nodeName = nodeAttr.getNodeName();
        nodeValue = nodeAttr.getNodeValue();

        if (StringUtils.isNotEmpty(nodeValue)) {
            if ((StringUtils.equalsIgnoreCase(nodeName, "name"))
                    || (StringUtils.equalsIgnoreCase(nodeName, "type"))
                    || (StringUtils.equalsIgnoreCase(nodeName, "title"))
                    || (StringUtils.equalsIgnoreCase(nodeName, "schemaVersion")))
                continue;
            else
                document.addFeature(nodeName, nodeValue);
        }
    }

    NodeList nodeList = anElement.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        nodeItem = nodeList.item(i);

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

        nodeName = nodeItem.getNodeName();
        if (StringUtils.equalsIgnoreCase(nodeName, IO.XML_TABLE_NODE_NAME)) {
            nodeElement = (Element) nodeItem;
            DataTableXML dataTableXML = new DataTableXML();
            dataTableXML.load(nodeElement);
            document.setTable(dataTableXML.getTable());
        } else if (StringUtils.equalsIgnoreCase(nodeName, IO.XML_RELATED_NODE_NAME)) {
            nodeElement = (Element) nodeItem;
            loadRelated(document, nodeElement);
        } else if (StringUtils.equalsIgnoreCase(nodeName, IO.XML_ACL_NODE_NAME)) {
            nodeElement = (Element) nodeItem;
            loadACL(document, nodeElement);
        }
    }

    return document;
}

From source file:com.hp.hpl.inkml.InkMLDOMParser.java

/**
 * Method to bind InkSource.ActiveArea element
 * /*w ww.j  a va2 s  .c  om*/
 * @param element the InkSource.ActiveArea element
 * @param inkSrc the enclosing InkSource data object
 * @return InkSource.ActiveArea data object
 * @throws InkMLException
 */
protected InkSource.ActiveArea getActiveArea(final Element element, final InkSource inkSrc)
        throws InkMLException {
    final InkSource.ActiveArea activeArea = inkSrc.new ActiveArea();
    final NamedNodeMap attrMap = element.getAttributes();
    final int length = attrMap.getLength();
    for (int i = 0; i < length; i++) {
        final Node attr = attrMap.item(i);
        final String attrName = attr.getLocalName();
        if ("size".equals(attrName)) {
            activeArea.setSize(attr.getNodeValue());
        }
        if ("height".equals(attrName)) {
            activeArea.setHegiht(Double.valueOf(attr.getNodeValue()).doubleValue());
        }
        if ("width".equals(attrName)) {
            activeArea.setWidth(Double.valueOf(attr.getNodeValue()).doubleValue());
        }
        if ("units".equals(attrName)) {
            activeArea.setUnits(attr.getNodeValue());
        }
    }
    return activeArea;
}

From source file:com.hp.hpl.inkml.InkMLDOMParser.java

/**
 * Method to bind CanvasTransform element
 * // www.jav  a  2 s . c om
 * @param element the CanvasTransform element
 * @return CanvasTransform data object
 * @throws InkMLException
 */
protected CanvasTransform getCanvasTransform(final Element element) throws InkMLException {
    final CanvasTransform canvasTransform = new CanvasTransform();
    // Extract and set Attribute values
    final NamedNodeMap attrMap = element.getAttributes();
    final int length = attrMap.getLength();
    for (int i = 0; i < length; i++) {
        final Node attr = attrMap.item(i);
        canvasTransform.setAttribute(attr.getLocalName(), attr.getNodeValue());
    }

    final NodeList list = element.getElementsByTagName("mapping");
    final int nMappingChildren = list.getLength();
    if (nMappingChildren == 2) {
        canvasTransform.setForwardMapping(this.getMapping((Element) list.item(0)));
        canvasTransform.setReverseMapping(this.getMapping((Element) list.item(1)));
    } else if (nMappingChildren == 1) {
        canvasTransform.setForwardMapping(this.getMapping((Element) list.item(0)));
    }
    return canvasTransform;
}