All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openxma.hbmfilemerge.XmlElement Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
package org.openxma.hbmfilemerge;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * Describes the name and the identification-attributes of an hibernate mapping xml element and provides some xpath
 * functionality
 */
enum XmlElement {
    CLASS("class", XPathToHbmNode.HIBERNATE_MAPPING), MANY_TO_ONE("many-to-one", XPathToHbmNode.CLASS,
            XmlElementAttribute.NAME, XmlElementAttribute.ENTITY_NAME), ONE_TO_ONE("one-to-one", XPathToHbmNode.CLASS,
            XmlElementAttribute.CLASS, XmlElementAttribute.ENTITY_NAME), ONE_TO_MANY("one-to-many",
            XPathToHbmNode.CLASS, XmlElementAttribute.CLASS, XmlElementAttribute.ENTITY_NAME), MAP("map",
            XPathToHbmNode.CLASS, XmlElementAttribute.CLASS, XmlElementAttribute.ENTITY_NAME), SET("set",
            XPathToHbmNode.CLASS, XmlElementAttribute.NAME), LIST("list", XPathToHbmNode.CLASS,
            XmlElementAttribute.NAME), BAG("bag", XPathToHbmNode.CLASS, XmlElementAttribute.NAME), IDBAG("idbag",
            XPathToHbmNode.CLASS, XmlElementAttribute.NAME), ARRAY("array", XPathToHbmNode.CLASS,
            XmlElementAttribute.NAME), PRIMITIVE_ARRAY("primitive-array", XPathToHbmNode.CLASS,
            XmlElementAttribute.NAME), PROPERTY("property", XPathToHbmNode.CLASS, XmlElementAttribute.NAME), ID("id",
            XPathToHbmNode.CLASS), COMPONENT("component", XPathToHbmNode.CLASS, XmlElementAttribute.NAME), QUERY(
            "query", XPathToHbmNode.HIBERNATE_MAPPING, XmlElementAttribute.NAME), FILTER_DEF("filter-def",
            XPathToHbmNode.HIBERNATE_MAPPING, XmlElementAttribute.NAME), FILTER("filter", XPathToHbmNode.CLASS,
            XmlElementAttribute.NAME);

    /**
     * the name of the hibernate xml element
     */
    private final String name;

    /**
     * the xml attribute(s) of this hibernate xml element, which makes the element identifiable
     */
    private XmlElementAttribute[] idAttributes = null;

    /**
     * if true this xml element appears only once within a hibernate class mapping. Therefore no
     * idAttributes are in place.
     */
    private boolean isSelfIdentified;

    private XPathToHbmNode xpathToParentXmlNode;

    /**
     * the XPathExpression for this hibernate xml element representation
     */
    private XPathExpression xpathExpr;

    private XmlElement(String name, XPathToHbmNode xpathToParentXmlNode, XmlElementAttribute... idAttributes) {
        this.name = name;
        this.xpathToParentXmlNode = xpathToParentXmlNode;
        this.idAttributes = idAttributes;
        this.isSelfIdentified = (null == this.idAttributes || this.idAttributes.length == 0);
        init();
    }

    private void init() {
        try {
            xpathExpr = HbmFileMergeConstants.XPATH.compile(new StringBuffer().append(xpathToParentXmlNode).append("/")
                    .append(name).toString());
        } catch (XPathExpressionException e) {
            throw new RuntimeException("could not compile xpath expression for assoc node", e);
        }

    }

    public XPathExpression getRelevantGenHbmNodeXPathExpression(XmlElementAttributeDto props)
            throws XPathExpressionException {
        return HbmFileMergeConstants.XPATH.compile(new StringBuffer().append(xpathToParentXmlNode).append("/")
                .append(name).append("[@").append(props.getRelevantIdAttr()).append("='")
                .append(props.getRelevantIdText()).append("']").toString());
    }

    public XmlElementAttributeDto getIdAttrValueFromFragmentIdAttrNode(Node fragmentNode) {
        if (null == fragmentNode || isSelfIdentified)
            return null;

        for (int i = 0; i <= idAttributes.length; i++) {
            XmlElementAttribute idAttr = idAttributes[i];
            String attrName = idAttr.getName();
            Node idAttrNode = fragmentNode.getAttributes().getNamedItem(attrName);

            if (null == idAttrNode)
                continue;

            return new XmlElementAttributeDto(idAttrNode.getNodeValue(), attrName);
        }

        return null;
    }

    public Node getParentNode(Document doc) throws XPathExpressionException {
        if (null == doc)
            return null;

        return (Node) HbmFileMergeConstants.XPATH.compile(xpathToParentXmlNode.toString()).evaluate(doc,
                XPathConstants.NODE);
    }

    public XPathExpression getXpathExpr() {
        return xpathExpr;
    }

    public boolean isSelfIdentified() {
        return isSelfIdentified;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy