Example usage for org.w3c.dom Element insertBefore

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

Introduction

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

Prototype

public Node insertBefore(Node newChild, Node refChild) throws DOMException;

Source Link

Document

Inserts the node newChild before the existing child node refChild.

Usage

From source file:org.openestate.io.immoxml.ImmoXmlDocument.java

@Override
public void setDocumentVersion(ImmoXmlVersion version) {
    try {/*from  w w w  . j  av a2s.  c  om*/
        Document doc = this.getDocument();

        String currentVersion = StringUtils
                .trimToEmpty(XmlUtils.newXPath("/io:immoxml/io:uebertragung/@version", doc).stringValueOf(doc));
        String[] ver = StringUtils.split(currentVersion, "/", 2);

        Element node = (Element) XmlUtils.newXPath("/io:immoxml/io:uebertragung", doc).selectSingleNode(doc);
        if (node == null) {
            Element parentNode = (Element) XmlUtils.newXPath("/io:immoxml", doc).selectSingleNode(doc);
            if (parentNode == null) {
                LOGGER.warn("Can't find an <immoxml> element in the document!");
                return;
            }
            node = doc.createElement("uebertragung");
            parentNode.insertBefore(node, parentNode.getFirstChild());
        }

        String newVersion = version.toReadableVersion();
        if (ver.length > 1)
            newVersion += "/" + ver[1];
        node.setAttribute("version", newVersion);
    } catch (JaxenException ex) {
        LOGGER.error("Can't evaluate XPath expression!");
        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
    }
}

From source file:org.openestate.io.kyero.KyeroDocument.java

@Override
public void setDocumentVersion(KyeroVersion version) {
    try {//from w  w w. j a  v  a  2 s . c o m
        Document doc = this.getDocument();

        Element node = (Element) XmlUtils.newXPath("/io:root/io:kyero/io:feed_version", doc)
                .selectSingleNode(doc);
        if (node == null) {
            Element parentNode = (Element) XmlUtils.newXPath("/io:root/io:kyero", doc).selectSingleNode(doc);
            if (parentNode == null) {
                Element grandParentNode = (Element) XmlUtils.newXPath("/io:root", doc).selectSingleNode(doc);
                if (grandParentNode == null) {
                    LOGGER.warn("Can't find a <root> element in the document!");
                    return;
                }
                parentNode = doc.createElement("kyero");
                grandParentNode.insertBefore(parentNode, grandParentNode.getFirstChild());
            }
            node = doc.createElement("feed_version");
            parentNode.insertBefore(node, parentNode.getFirstChild());
        }

        String newVersion = version.toXmlVersion();
        node.setTextContent(newVersion);
    } catch (JaxenException ex) {
        LOGGER.error("Can't evaluate XPath expression!");
        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
    }
}

From source file:org.openestate.io.openimmo.OpenImmoFeedbackDocument.java

@Override
public void setDocumentVersion(OpenImmoVersion version) {
    try {/*from  w ww .j a  v a 2s.c om*/
        Document doc = this.getDocument();

        String currentVersion = StringUtils.trimToEmpty(
                XmlUtils.newXPath("/io:openimmo/io:uebertragung/@version", doc).stringValueOf(doc));
        String[] ver = StringUtils.split(currentVersion, "/", 2);

        Element node = (Element) XmlUtils.newXPath("/io:openimmo_feedback/io:version", doc)
                .selectSingleNode(doc);

        // versions older then 1.2.4 do not support the <version> element
        if (OpenImmoVersion.V1_2_4.isNewerThen(version)) {
            if (node != null) {
                Element root = XmlUtils.getRootElement(doc);
                root.removeChild(node);
            }
            return;
        }

        if (node == null) {
            Element parentNode = (Element) XmlUtils.newXPath("/io:openimmo_feedback", doc)
                    .selectSingleNode(doc);
            if (parentNode == null) {
                LOGGER.warn("Can't find an <openimmo_feedback> element in the document!");
                return;
            }
            node = doc.createElement("version");
            parentNode.insertBefore(node, parentNode.getFirstChild());
        }

        String newVersion = version.toReadableVersion();
        if (ver.length > 1)
            newVersion += "/" + ver[1];
        node.setTextContent(newVersion);
    } catch (JaxenException ex) {
        LOGGER.error("Can't evaluate XPath expression!");
        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
    }
}

From source file:org.openestate.io.openimmo.OpenImmoTransferDocument.java

@Override
public void setDocumentVersion(OpenImmoVersion version) {
    try {/*from   ww  w . jav  a 2s  . c  o  m*/
        Document doc = this.getDocument();

        String currentVersion = StringUtils.trimToEmpty(
                XmlUtils.newXPath("/io:openimmo/io:uebertragung/@version", doc).stringValueOf(doc));
        String[] ver = StringUtils.split(currentVersion, "/", 2);

        Element node = (Element) XmlUtils.newXPath("/io:openimmo/io:uebertragung", doc).selectSingleNode(doc);
        if (node == null) {
            Element parentNode = (Element) XmlUtils.newXPath("/io:openimmo", doc).selectSingleNode(doc);
            if (parentNode == null) {
                LOGGER.warn("Can't find an <openimmo> element in the document!");
                return;
            }
            node = doc.createElement("uebertragung");
            parentNode.insertBefore(node, parentNode.getFirstChild());
        }

        String newVersion = version.toReadableVersion();
        if (ver.length > 1)
            newVersion += "/" + ver[1];
        node.setAttribute("version", newVersion);
    } catch (JaxenException ex) {
        LOGGER.error("Can't evaluate XPath expression!");
        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
    }
}

From source file:org.pentaho.metadata.util.XmiParser.java

public Document toXmiDocument(Domain domain) {
    if (domain == null) {
        logger.error(Messages.getErrorString("XmiParser.ERROR_0001_DOMAIN_NULL")); //$NON-NLS-1$
        return null;
    }// www  .  ja  va  2 s.  c o  m

    Document doc;
    try {
        // create an XML document
        DocumentBuilderFactory dbf = XmiParser.createSecureDocBuilderFactory();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.newDocument();
        Element xmiElement = doc.createElement("XMI"); //$NON-NLS-1$
        xmiElement.setAttribute("xmlns:CWM", "org.omg.xmi.namespace.CWM"); //$NON-NLS-1$ //$NON-NLS-2$
        xmiElement.setAttribute("xmlns:CWMMDB", "org.omg.xmi.namespace.CWMMDB"); //$NON-NLS-1$ //$NON-NLS-2$
        xmiElement.setAttribute("xmlns:CWMOLAP", "org.omg.xmi.namespace.CWMOLAP"); //$NON-NLS-1$ //$NON-NLS-2$
        xmiElement.setAttribute("xmlns:CWMRDB", "org.omg.xmi.namespace.CWMRDB"); //$NON-NLS-1$ //$NON-NLS-2$
        xmiElement.setAttribute("xmlns:CWMTFM", "org.omg.xmi.namespace.CWMTFM"); //$NON-NLS-1$ //$NON-NLS-2$
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); //$NON-NLS-1$
        xmiElement.setAttribute("timestamp", sdf.format(date)); //$NON-NLS-1$
        xmiElement.setAttribute("xmi.version", "1.2"); //$NON-NLS-1$ //$NON-NLS-2$
        doc.appendChild(xmiElement);
        Element xmiHeader = doc.createElement("XMI.header"); //$NON-NLS-1$
        xmiElement.appendChild(xmiHeader);
        Element xmiDocumentation = doc.createElement("XMI.documentation"); //$NON-NLS-1$
        xmiHeader.appendChild(xmiDocumentation);
        addTextElement(doc, xmiDocumentation, "XMI.exporter", "Pentaho XMI Generator"); //$NON-NLS-1$ //$NON-NLS-2$
        addTextElement(doc, xmiDocumentation, "XMI.exporterVersion", "1.0"); //$NON-NLS-1$ //$NON-NLS-2$

        Element xmiContent = doc.createElement("XMI.content"); //$NON-NLS-1$
        xmiElement.appendChild(xmiContent);

        // first add concepts
        List<Element> allDescriptions = new ArrayList<Element>();

        IdGen idGen = new IdGen();
        for (Concept concept : domain.getConcepts()) {
            /*
             * <CWM:Class isAbstract="false" name="Date" xmi.id="a1"> <CWM:ModelElement.taggedValue> <CWM:TaggedValue
             * tag="CONCEPT_PARENT_NAME" value="Base" xmi.id="a2"/> </CWM:ModelElement.taggedValue> </CWM:Class>
             */
            Element cwmClass = doc.createElement("CWM:Class"); //$NON-NLS-1$
            cwmClass.setAttribute("isAbstract", "false"); //$NON-NLS-1$ //$NON-NLS-2$
            cwmClass.setAttribute("name", concept.getId()); //$NON-NLS-1$
            String idStr = idGen.getNextId();

            createDescriptions(doc, concept, "CWM:Class", idStr, allDescriptions, idGen); //$NON-NLS-1$

            cwmClass.setAttribute("xmi.id", idStr); //$NON-NLS-1$

            if (concept.getParentConcept() != null) {
                Element modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                modelElement.appendChild(createTaggedValue(doc, "CONCEPT_PARENT_NAME", //$NON-NLS-1$
                        concept.getParentConcept().getId(), idGen.getNextId()));
                cwmClass.appendChild(modelElement);
            }

            xmiContent.appendChild(cwmClass);
        }

        // Description

        Element beforeDesc = null;

        // Event Support

        Element eventModelElement = null;
        for (String key : domain.getChildProperties().keySet()) {
            if (key.startsWith("LEGACY_EVENT_")) { //$NON-NLS-1$
                // if any keys event, create a model element
                if (eventModelElement == null) {
                    eventModelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                }
                String shortkey = key.substring("LEGACY_EVENT_".length()); //$NON-NLS-1$
                eventModelElement.appendChild(createTaggedValue(doc, shortkey,
                        (String) domain.getChildProperties().get(key), idGen.getNextId()));
            }
        }
        // only add cwm:event if one or more keys exist
        if (eventModelElement != null) {
            Element event = doc.createElement("CWM:Event"); //$NON-NLS-1$
            event.setAttribute("xmi.id", idGen.getNextId()); //$NON-NLS-1$
            event.setAttribute("name", "SECURITY_SERVICE"); //$NON-NLS-1$ //$NON-NLS-2$
            event.appendChild(eventModelElement);
            xmiContent.appendChild(event);
        }

        // Parameter / Locale info
        int val = 1;
        for (LocaleType localeType : domain.getLocales()) {
            Element cwmParameter = doc.createElement("CWM:Parameter"); //$NON-NLS-1$
            if (beforeDesc == null) {
                beforeDesc = cwmParameter;
            }
            cwmParameter.setAttribute("name", localeType.getCode()); //$NON-NLS-1$
            cwmParameter.setAttribute("xmi.id", idGen.getNextId()); //$NON-NLS-1$
            Element modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
            modelElement.appendChild(createTaggedValue(doc, "LOCALE_IS_DEFAULT", "" + ((val == 1) ? "Y" : "N"),
                    idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            modelElement.appendChild(createTaggedValue(doc, "LOCALE_ORDER", "" + val++, idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$
            modelElement.appendChild(createTaggedValue(doc, "LOCALE_DESCRIPTION", localeType.getDescription(), //$NON-NLS-1$
                    idGen.getNextId()));
            cwmParameter.appendChild(modelElement);
            xmiContent.appendChild(cwmParameter);
        }

        // CWMOLAP:Schema elements get converted here
        generateOlapXmi(domain, doc, idGen, xmiContent);

        // CWMRDB:Catalog: Data Source objects
        for (IPhysicalModel model : domain.getPhysicalModels()) {
            if (model.getId().equals("__MISSING_PARENT_PHYSICAL_MODEL__")) { //$NON-NLS-1$
                continue;
            }

            if (model instanceof SqlPhysicalModel) {
                SqlPhysicalModel sqlModel = (SqlPhysicalModel) model;
                SqlDataSource datasource = sqlModel.getDatasource();
                Element catalog = doc.createElement("CWMRDB:Catalog"); //$NON-NLS-1$
                catalog.setAttribute("name", model.getId()); //$NON-NLS-1$
                catalog.setAttribute("xmi.id", idGen.getNextId()); //$NON-NLS-1$
                Element modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$

                modelElement.appendChild(createTaggedValue(doc, "DATABASE_TYPE", datasource.getDialectType(), //$NON-NLS-1$
                        idGen.getNextId()));
                modelElement.appendChild(createTaggedValue(doc, "DATABASE_ACCESS", //$NON-NLS-1$
                        datasource.getType().toString(), idGen.getNextId()));
                modelElement.appendChild(createTaggedValue(doc, "DATABASE_DATABASE", //$NON-NLS-1$
                        datasource.getDatabaseName(), idGen.getNextId()));
                modelElement.appendChild(
                        createTaggedValue(doc, "DATABASE_SERVER", datasource.getHostname(), idGen.getNextId())); //$NON-NLS-1$
                modelElement.appendChild(
                        createTaggedValue(doc, "DATABASE_PORT", datasource.getPort(), idGen.getNextId())); //$NON-NLS-1$
                modelElement.appendChild(createTaggedValue(doc, "DATABASE_USERNAME", datasource.getUsername(), //$NON-NLS-1$
                        idGen.getNextId()));
                modelElement.appendChild(createTaggedValue(doc, "DATABASE_PASSWORD", datasource.getPassword(), //$NON-NLS-1$
                        idGen.getNextId()));
                if (!StringUtils.isEmpty(datasource.getServername())) {
                    modelElement.appendChild(createTaggedValue(doc, "DATABASE_SERVER_INSTANCE", //$NON-NLS-1$
                            datasource.getServername(), idGen.getNextId()));
                }

                for (String attribute : datasource.getAttributes().keySet()) {
                    modelElement
                            .appendChild(createTaggedValue(doc, CWM.TAG_DATABASE_ATTRIBUTE_PREFIX + attribute,
                                    datasource.getAttributes().get(attribute), idGen.getNextId()));
                }

                catalog.appendChild(modelElement);
                xmiContent.appendChild(catalog);
            } else {
                // we do not support CSV to XMI yet
                logger.warn(Messages.getErrorString("XmiParser.ERROR_0003_PHYSICAL_MODEL_NOT_SUPPORTED", //$NON-NLS-1$
                        model.getClass().getName()));
            }
        }

        // CWMRDB:Table: physicalTables

        for (IPhysicalModel model : domain.getPhysicalModels()) {
            if (model instanceof SqlPhysicalModel) {
                SqlPhysicalModel sqlModel = (SqlPhysicalModel) model;
                for (SqlPhysicalTable table : sqlModel.getPhysicalTables()) {
                    Element cwmRdbTable = doc.createElement("CWMRDB:Table"); //$NON-NLS-1$
                    cwmRdbTable.setAttribute("isAbstract", "false"); //$NON-NLS-1$ //$NON-NLS-2$
                    cwmRdbTable.setAttribute("isSystem", "false"); //$NON-NLS-1$ //$NON-NLS-2$
                    cwmRdbTable.setAttribute("isTemporary", "false"); //$NON-NLS-1$ //$NON-NLS-2$
                    cwmRdbTable.setAttribute("name", table.getId()); //$NON-NLS-1$
                    String idstr = idGen.getNextId();
                    cwmRdbTable.setAttribute("xmi.id", idstr); //$NON-NLS-1$
                    createDescriptions(doc, table, "CWMRDB:Table", idstr, allDescriptions, idGen); //$NON-NLS-1$

                    Element modelElement = null;
                    if (!model.getId().equals("__MISSING_PARENT_PHYSICAL_MODEL__")) { //$NON-NLS-1$
                        modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                        modelElement.appendChild(createTaggedValue(doc, "TABLE_TARGET_DATABASE_NAME", //$NON-NLS-1$
                                model.getId(), idGen.getNextId()));
                    }
                    if (table.getParentConcept() != null) {
                        if (modelElement == null) {
                            modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                        }
                        modelElement.appendChild(createTaggedValue(doc, "CONCEPT_PARENT_NAME", //$NON-NLS-1$
                                table.getParentConcept().getId(), idGen.getNextId()));
                    }
                    if (modelElement != null) {
                        cwmRdbTable.appendChild(modelElement);
                    }

                    Element ownedElement = doc.createElement("CWM:Namespace.ownedElement"); //$NON-NLS-1$
                    for (IPhysicalColumn column : table.getPhysicalColumns()) {
                        SqlPhysicalColumn sqlColumn = (SqlPhysicalColumn) column;
                        Element rdbColumn = doc.createElement("CWMRDB:Column"); //$NON-NLS-1$
                        rdbColumn.setAttribute("name", sqlColumn.getId()); //$NON-NLS-1$
                        idstr = idGen.getNextId();
                        rdbColumn.setAttribute("xmi.id", idstr); //$NON-NLS-1$
                        createDescriptions(doc, column, "CWMRDB:Column", idstr, allDescriptions, idGen); //$NON-NLS-1$
                        if (sqlColumn.getParentConcept() != null) {
                            modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                            modelElement.appendChild(createTaggedValue(doc, "CONCEPT_PARENT_NAME", //$NON-NLS-1$
                                    sqlColumn.getParentConcept().getId(), idGen.getNextId()));
                            rdbColumn.appendChild(modelElement);
                        }
                        ownedElement.appendChild(rdbColumn);
                    }
                    cwmRdbTable.appendChild(ownedElement);
                    xmiContent.appendChild(cwmRdbTable);
                }
            }
        }

        // CWMMDB:Schema: logical categories

        for (LogicalModel model : domain.getLogicalModels()) {
            Element mdbSchema = doc.createElement("CWMMDB:Schema"); //$NON-NLS-1$
            mdbSchema.setAttribute("name", model.getId()); //$NON-NLS-1$
            String idstr = idGen.getNextId();
            mdbSchema.setAttribute("xmi.id", idstr); //$NON-NLS-1$
            createDescriptions(doc, model, "CWMMDB:Schema", idstr, allDescriptions, idGen); //$NON-NLS-1$

            // Serialize all calculated members across cubes into a single XML string and store as a description
            @SuppressWarnings("unchecked")
            List<OlapCube> cubes = (List<OlapCube>) model.getProperty("olap_cubes");
            if (cubes != null) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("<cubes>");
                for (OlapCube cube : cubes) {
                    if (cube.getOlapCalculatedMembers() != null && cube.getOlapCalculatedMembers().size() > 0) {
                        buffer.append("<cube>");
                        buffer.append(XMLHandler.addTagValue("name", cube.getName()));
                        buffer.append(OlapUtil.toXmlCalculatedMembers(cube.getOlapCalculatedMembers()));
                        buffer.append("</cube>");
                    }
                }
                buffer.append("</cubes>");
                createDescription(doc, buffer.toString(), LogicalModel.PROPERTY_OLAP_CALCULATED_MEMBERS,
                        "String", null, idGen, "CWMMDB:Schema", idstr, allDescriptions);
            }

            Element ownedElement = doc.createElement("CWM:Namespace.ownedElement"); //$NON-NLS-1$
            mdbSchema.appendChild(ownedElement);
            for (Category category : model.getCategories()) {
                Element extent = doc.createElement("CWM:Extent"); //$NON-NLS-1$
                extent.setAttribute("name", category.getId()); //$NON-NLS-1$
                idstr = idGen.getNextId();
                extent.setAttribute("xmi.id", idstr); //$NON-NLS-1$
                createDescriptions(doc, category, "CWM:Extent", idstr, allDescriptions, idGen); //$NON-NLS-1$
                Element modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                modelElement
                        .appendChild(createTaggedValue(doc, "BUSINESS_CATEGORY_ROOT", "Y", idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$
                if (category.getParentConcept() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "CONCEPT_PARENT_NAME", //$NON-NLS-1$
                            category.getParentConcept().getId(), idGen.getNextId()));
                }
                extent.appendChild(modelElement);
                Element cOwnedElement = doc.createElement("CWM:Namespace.ownedElement"); //$NON-NLS-1$
                extent.appendChild(cOwnedElement);
                for (LogicalColumn col : category.getLogicalColumns()) {
                    Element attribute = doc.createElement("CWM:Attribute"); //$NON-NLS-1$
                    attribute.setAttribute("name", col.getId()); //$NON-NLS-1$
                    attribute.setAttribute("xmi.id", idGen.getNextId()); //$NON-NLS-1$
                    modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                    modelElement.appendChild(
                            createTaggedValue(doc, "BUSINESS_CATEGORY_TYPE", "Column", idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$
                    attribute.appendChild(modelElement);
                    cOwnedElement.appendChild(attribute);
                }

                ownedElement.appendChild(extent);
            }

            for (LogicalRelationship rel : model.getLogicalRelationships()) {
                Element keyRel = doc.createElement("CWM:KeyRelationship"); //$NON-NLS-1$
                keyRel.setAttribute("xmi.id", idGen.getNextId()); //$NON-NLS-1$
                Element modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                keyRel.appendChild(modelElement);
                modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_TYPE", //$NON-NLS-1$
                        rel.getRelationshipType().getType(), idGen.getNextId()));

                if (rel.getToColumn() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_FIELDNAME_CHILD", //$NON-NLS-1$
                            rel.getToColumn().getId(), idGen.getNextId()));
                }
                if (rel.getFromColumn() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_FIELDNAME_PARENT", //$NON-NLS-1$
                            rel.getFromColumn().getId(), idGen.getNextId()));
                }
                if (rel.getToTable() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_TABLENAME_CHILD", //$NON-NLS-1$
                            rel.getToTable().getId(), idGen.getNextId()));
                }
                if (rel.getFromTable() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_TABLENAME_PARENT", //$NON-NLS-1$
                            rel.getFromTable().getId(), idGen.getNextId()));
                }
                if (rel.isComplex()) {
                    modelElement.appendChild(
                            createTaggedValue(doc, "RELATIONSHIP_IS_COMPLEX", "Y", idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$
                    modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_COMPLEX_JOIN", //$NON-NLS-1$
                            rel.getComplexJoin(), idGen.getNextId()));
                }
                if (rel.getDescription() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_DESCRIPTION", //$NON-NLS-1$
                            rel.getRelationshipDescription(), idGen.getNextId()));
                }
                if (rel.getJoinOrderKey() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "RELATIONSHIP_JOIN_ORDER_KEY", //$NON-NLS-1$
                            rel.getJoinOrderKey(), idGen.getNextId()));
                }
                ownedElement.appendChild(keyRel);
            }

            Element sdo = doc.createElement("CWMMDB:Schema.dimensionedObject"); //$NON-NLS-1$
            Element sd = doc.createElement("CWMMDB:Schema.dimension"); //$NON-NLS-1$
            mdbSchema.appendChild(sdo);
            mdbSchema.appendChild(sd);
            for (LogicalTable table : model.getLogicalTables()) {
                Element dim = doc.createElement("CWMMDB:Dimension"); //$NON-NLS-1$
                sd.appendChild(dim);
                dim.setAttribute("isAbstract", "false"); //$NON-NLS-1$ //$NON-NLS-2$
                dim.setAttribute("name", table.getId()); //$NON-NLS-1$
                String tblidstr = idGen.getNextId();
                dim.setAttribute("xmi.id", tblidstr); //$NON-NLS-1$
                createDescriptions(doc, table, "CWMMDB:Dimension", tblidstr, allDescriptions, idGen); //$NON-NLS-1$
                Element modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                if (table.getProperty("__LEGACY_TABLE_IS_DRAWN") != null) { //$NON-NLS-1$
                    modelElement.appendChild(createTaggedValue(doc, "TABLE_IS_DRAWN",
                            (String) table.getProperty("__LEGACY_TABLE_IS_DRAWN"), idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$
                }
                if (table.getProperty("__LEGACY_TAG_POSITION_Y") != null) { //$NON-NLS-1$
                    modelElement.appendChild(createTaggedValue(doc, "TAG_POSITION_Y",
                            (String) table.getProperty("__LEGACY_TAG_POSITION_Y"), idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$
                }
                if (table.getProperty("__LEGACY_TAG_POSITION_X") != null) { //$NON-NLS-1$
                    modelElement.appendChild(createTaggedValue(doc, "TAG_POSITION_X",
                            (String) table.getProperty("__LEGACY_TAG_POSITION_X"), idGen.getNextId())); //$NON-NLS-1$ //$NON-NLS-2$
                }
                if (table.getParentConcept() != null) {
                    modelElement.appendChild(createTaggedValue(doc, "CONCEPT_PARENT_NAME", //$NON-NLS-1$
                            table.getParentConcept().getId(), idGen.getNextId()));
                }
                modelElement.appendChild(createTaggedValue(doc, "BUSINESS_TABLE_PHYSICAL_TABLE_NAME",
                        table.getPhysicalTable().getId(), idGen.getNextId())); //$NON-NLS-1$
                dim.appendChild(modelElement);
                Element dimObjs = doc.createElement("CWMMDB:Dimension.dimensionedObject"); //$NON-NLS-1$
                dim.appendChild(dimObjs);

                for (LogicalColumn column : table.getLogicalColumns()) {
                    Element dimObj = doc.createElement("CWMMDB:DimensionedObject"); //$NON-NLS-1$
                    sdo.appendChild(dimObj);
                    dimObj.setAttribute("name", column.getId()); //$NON-NLS-1$
                    idstr = idGen.getNextId();
                    createDescriptions(doc, column, "CWMMDB:DimensionedObject", idstr, allDescriptions, idGen); //$NON-NLS-1$
                    dimObj.setAttribute("xmi.id", idstr); //$NON-NLS-1$
                    modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                    modelElement.appendChild(createTaggedValue(doc, "BUSINESS_COLUMN_BUSINESS_TABLE", //$NON-NLS-1$
                            column.getLogicalTable().getId(), idGen.getNextId()));
                    modelElement.appendChild(createTaggedValue(doc, "BUSINESS_COLUMN_PHYSICAL_COLUMN_NAME",
                            column.getPhysicalColumn().getId(), idGen.getNextId())); //$NON-NLS-1$
                    if (column.getParentConcept() != null) {
                        modelElement.appendChild(createTaggedValue(doc, "CONCEPT_PARENT_NAME", //$NON-NLS-1$
                                column.getParentConcept().getId(), idGen.getNextId()));
                    }
                    dimObj.appendChild(modelElement);
                    /*
                     * <CWMMDB:DimensionedObject.dimension> <CWMMDB:Dimension xmi.idref="a23"/>
                     * </CWMMDB:DimensionedObject.dimension>
                     */

                    Element parentRoot = doc.createElement("CWMMDB:DimensionedObject.dimension"); //$NON-NLS-1$
                    Element parent = doc.createElement("CWMMDB:Dimension"); //$NON-NLS-1$
                    parent.setAttribute("xmi.idref", tblidstr); //$NON-NLS-1$
                    dimObj.appendChild(parentRoot);
                    parentRoot.appendChild(parent);

                    // CWMMDB:DimensionedObject xmi.idref="a1365"/>
                    Element dimObjLink = doc.createElement("CWMMDB:DimensionedObject"); //$NON-NLS-1$
                    dimObjLink.setAttribute("xmi.idref", idstr); //$NON-NLS-1$
                    dimObjs.appendChild(dimObjLink);
                }
            }

            if (model.getParentConcept() != null) {
                Element modelElement = doc.createElement("CWM:ModelElement.taggedValue"); //$NON-NLS-1$
                modelElement.appendChild(createTaggedValue(doc, "CONCEPT_PARENT_NAME", //$NON-NLS-1$
                        model.getParentConcept().getId(), idGen.getNextId()));
                mdbSchema.appendChild(modelElement);
            }

            xmiContent.appendChild(mdbSchema);
        }

        for (Element element : allDescriptions) {
            xmiContent.insertBefore(element, beforeDesc);
        }

        return doc;
    } catch (Exception e) {
        logger.error(Messages.getErrorString("QueryXmlHelper.ERROR_0002_TO_DOCUMENT_FAILED"), e); //$NON-NLS-1$
    }
    return null;

}

From source file:org.sakaiproject.tool.assessment.qti.helper.AuthoringXml.java

/**
 * Based on method in XmlStringBuffer//from  w w w.  j av  a  2s.  c  o m
 * @author rpembry
 * @author casong changed XmlStringBuffer to be org.w3c.dom compliance,
 * @author Ed Smiley esmiley@stanford.edu changed method signatures used Document
 *
 * @param document Document
 * @param parentXpath
 * @param element
 * @return modified Document
 */
public Document addElement(Document document, String parentXpath, Element element) {
    if (log.isDebugEnabled()) {
        log.debug("addElement(String " + parentXpath + ", Element " + element + ")");
    }

    List nodes = this.selectNodes(document, parentXpath);
    Iterator iterator = nodes.iterator();
    while (iterator.hasNext()) {
        Element parent = (Element) iterator.next();
        if (!parent.getOwnerDocument().equals(element.getOwnerDocument())) {
            element = (Element) parent.getOwnerDocument().importNode(element, true);
        }

        parent.insertBefore(element, null);// inserts at end, as before-reference is null
    }
    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlStringBuffer.java

/**
 * insert element//  w  ww  .  jav  a  2  s  .  c o  m
 *
 * @param afterNode
 * @param parentXpath
 * @param childXpath
 */
public void insertElement(String afterNode, String parentXpath, String childXpath) {
    try {
        String nextXpath = parentXpath + "/" + afterNode;

        //*************************************************************
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();
        Element element = document.createElement(childXpath);

        //**************************************************************
        Element parent = null;
        List parentNodes = this.selectNodes(parentXpath);
        Iterator iteratorNext = parentNodes.iterator();
        while (iteratorNext.hasNext()) {
            parent = (Element) iteratorNext.next();
        }

        if (parent != null) {
            List nodes = this.selectNodes(nextXpath);
            Iterator iterator = nodes.iterator();
            Element nextSibling = null;
            while (iterator.hasNext()) {
                nextSibling = (Element) iterator.next();
            }

            if ((nextSibling != null) && !nextSibling.getOwnerDocument().equals(element.getOwnerDocument())) {
                element = (Element) parent.getOwnerDocument().importNode(element, true);
                parent.insertBefore(element, nextSibling);
            }
        }
    } catch (ParserConfigurationException pce) {
        log.error("Exception thrown from insertElement() : " + pce.getMessage());
        pce.printStackTrace();
    }
}

From source file:org.sipfoundry.sipxconfig.cmcprov.ContactSynchronizer.java

/**
 * @param phoneBookDocument/*  w ww  . j  a  va2 s  .c om*/
 * @param contactList
 */
private void mergeAddressEntries(Document phoneBookDocument, NodeList contactList) {
    Element root = phoneBookDocument.getDocumentElement();
    Element listElement = (Element) root.getElementsByTagName("list").item(0);
    NodeList theList = listElement.getElementsByTagName(ENTRY_NAME);

    for (int i = 0; i < contactList.getLength(); i++) {
        Node nNode = contactList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (!isDuplicated(eElement, theList) && canMerge(nNode)) {
                Node a = phoneBookDocument.importNode(nNode, true);
                listElement.insertBefore(a, theList.item(contactList.getLength() - 1));
            }
        }

    }

}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.utils.EditXML.java

/**
 * Adds a child node as the first child to a parent node.
 * If the first node has the same name as child node, then it 
 * is replaced./*from w w  w.  j av a  2  s .com*/
 * @param parent
 * @param child
 */
public void addFirstChild(Element parent, Element child) {
    Node firstChild = parent.getFirstChild();
    if (child.getNodeName() == firstChild.getNodeName()) {
        parent.replaceChild(child, firstChild);
    } else {
        parent.insertBefore(child, parent.getFirstChild());
    }

}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.utils.EditXML.java

/**
 * Adds a child node before the last child of the parent node.
 * @param parent//w  ww  .ja va2  s  . c o  m
 * @param child
 */
public void addBeforeLastChild(Element parent, Element child) {
    Node lastChild = parent.getLastChild();
    Node beforeLastChild = lastChild.getPreviousSibling();
    if (child.getNodeName() == beforeLastChild.getNodeName()) {
        parent.replaceChild(child, beforeLastChild);
    } else {
        parent.insertBefore(child, lastChild);
    }
}