Example usage for org.w3c.dom Element getOwnerDocument

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

Introduction

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

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:XMLUtils.java

/**
 * Sets the text value for a given element.
 * @param el//from  w w  w . j a v  a  2 s .c om
 * @param value
 */
public static void setText(Element el, String value) {
    // remove the children if already exist
    while (el.getFirstChild() != null) {
        el.removeChild(el.getFirstChild());
    }
    if (value == null) {
        value = "";
    }
    Text txt = el.getOwnerDocument().createTextNode(value);
    el.appendChild(txt);
}

From source file:XMLUtils.java

/**
 * Sets the text value for a given element as a CDATA section
 * @param el/*from  w  w w  . j a va 2  s . co m*/
 * @param value
 */
public static void setCDATA(Element el, String value) {
    // remove the children if already exist
    while (el.getFirstChild() != null) {
        el.removeChild(el.getFirstChild());
    }
    if (value == null) {
        value = "";
    }
    CDATASection txt = el.getOwnerDocument().createCDATASection(value);
    el.appendChild(txt);
}

From source file:com.netspective.sparx.form.DialogContext.java

static public void exportParamToXml(Element parent, String name, String[] values) {
    Document doc = parent.getOwnerDocument();
    Element fieldElem = doc.createElement("request-param");
    fieldElem.setAttribute("name", name);
    if (values != null && values.length > 1) {
        fieldElem.setAttribute("value-type", "strings");
        Element valuesElem = doc.createElement("values");
        for (int i = 0; i < values.length; i++) {
            Element valueElem = doc.createElement("value");
            valueElem.appendChild(doc.createTextNode(values[i]));
            valuesElem.appendChild(valueElem);
        }/*from   ww w. j a  va 2  s  . c  o  m*/
        fieldElem.appendChild(valuesElem);
        parent.appendChild(fieldElem);
    } else if (values != null) {
        fieldElem.setAttribute("value-type", "string");
        Element valueElem = doc.createElement("value");
        valueElem.appendChild(doc.createTextNode(values[0]));
        fieldElem.appendChild(valueElem);
        parent.appendChild(fieldElem);
    }
}

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

private static void addFieldTypeAttribute(FieldMetadata field, Element columnElement, Element propertyElement,
        RDBMSDataSource.DataSourceDialect dialect) {
    Document document = columnElement.getOwnerDocument();
    Document propertyDocument = propertyElement.getOwnerDocument();
    Attr elementType = propertyDocument.createAttribute("type"); //$NON-NLS-1$
    TypeMetadata fieldType = field.getType();
    String elementTypeName;//from  ww  w  . ja v  a 2s  . c  om

    boolean isMultiLingualOrBase64Binary = Types.MULTI_LINGUAL.equalsIgnoreCase(fieldType.getName())
            || Types.BASE64_BINARY.equalsIgnoreCase(fieldType.getName());
    Object maxLength = CommonUtil.getSuperTypeMaxLength(fieldType, fieldType);

    if (isMultiLingualOrBase64Binary) {
        elementTypeName = TypeMapping.SQL_TYPE_TEXT;
    } else {
        Object sqlType = fieldType.getData(TypeMapping.SQL_TYPE);
        if (sqlType != null) { // SQL Type may enforce use of "CLOB" iso. "LONG VARCHAR"
            elementTypeName = String.valueOf(sqlType);
            if (dialect == RDBMSDataSource.DataSourceDialect.DB2) {
                Attr length = document.createAttribute("length"); //$NON-NLS-1$
                length.setValue("1048576"); //$NON-NLS-1$ 1MB CLOB limit for DB2
                columnElement.getAttributes().setNamedItem(length);
            }
        } else if (maxLength != null) {
            String maxLengthValue = String.valueOf(maxLength);
            int maxLengthInt = Integer.parseInt(maxLengthValue);
            if (maxLengthInt > dialect.getTextLimit()) {
                elementTypeName = TypeMapping.SQL_TYPE_TEXT;
            } else {
                Attr length = document.createAttribute("length"); //$NON-NLS-1$
                length.setValue(maxLengthValue);
                columnElement.getAttributes().setNamedItem(length);
                elementTypeName = HibernateMetadataUtils.getJavaType(fieldType);
            }
        } else if (fieldType.getData(MetadataRepository.DATA_TOTAL_DIGITS) != null
                || fieldType.getData(MetadataRepository.DATA_FRACTION_DIGITS) != null) { // TMDM-8022

            Object totalDigits = fieldType.getData(MetadataRepository.DATA_TOTAL_DIGITS);
            Object fractionDigits = fieldType.getData(MetadataRepository.DATA_FRACTION_DIGITS);

            if (totalDigits != null) {
                int totalDigitsInt = Integer.parseInt(totalDigits.toString());
                totalDigitsInt = totalDigitsInt > dialect.getDecimalPrecision() ? dialect.getDecimalPrecision()
                        : totalDigitsInt;

                String totalDigitsValue = String.valueOf(totalDigitsInt);
                Attr length = document.createAttribute("precision"); //$NON-NLS-1$
                length.setValue(totalDigitsValue);
                columnElement.getAttributes().setNamedItem(length);
            }

            if (fractionDigits != null) {
                int fractionDigitsInt = Integer.parseInt(fractionDigits.toString());
                fractionDigitsInt = fractionDigitsInt >= dialect.getDecimalScale() ? dialect.getDecimalScale()
                        : fractionDigitsInt;

                String fractionDigitsValue = String.valueOf(fractionDigitsInt);
                Attr length = document.createAttribute("scale"); //$NON-NLS-1$
                length.setValue(fractionDigitsValue);
                columnElement.getAttributes().setNamedItem(length);
            }
            elementTypeName = HibernateMetadataUtils.getJavaType(fieldType);
        } else {
            elementTypeName = HibernateMetadataUtils.getJavaType(fieldType);
        }
    }
    // TMDM-4975: Oracle doesn't like when there are too many LONG columns.
    if (dialect == RDBMSDataSource.DataSourceDialect.ORACLE_10G
            && TypeMapping.SQL_TYPE_TEXT.equals(elementTypeName)) {
        if (field.getType().getData(LongString.PREFER_LONGVARCHAR) == null
                && isMultiLingualOrBase64Binary == false) {
            elementTypeName = TypeMapping.SQL_TYPE_CLOB;
        } else {// DATA_CLUSTER_POJO.X_VOCABULARY, X_TALEND_STAGING_ERROR, X_TALEND_STAGING_VALUES, and
                // Types.MULTI_LINGUAL, Types.BASE64_BINARY still use VARCHAR2(4000 CHAR)
            elementTypeName = "string"; //$NON-NLS-1$
            Attr length = document.createAttribute("length"); //$NON-NLS-1$
            length.setValue("4000"); //$NON-NLS-1$
            columnElement.getAttributes().setNamedItem(length);
        }
    }
    elementType.setValue(elementTypeName);
    propertyElement.getAttributes().setNamedItem(elementType);
}

From source file:DomUtils.java

/**
 * Add literal text to the supplied element.
 * @param element Target DOM Element.//from  w  w  w . ja  va2 s .  co  m
 * @param literalText Literal text to be added.
 */
public static void addLiteral(Element element, String literalText) {

    Document document = element.getOwnerDocument();
    Text literal = document.createTextNode(literalText);
    element.appendChild(literal);
}

From source file:com.msopentech.odatajclient.engine.data.json.GeospatialJSONHandler.java

public static void deserialize(final JsonNode node, final Element parent, final String type) {
    final Iterator<JsonNode> cooItor = node.has(ODataConstants.JSON_COORDINATES)
            ? node.get(ODataConstants.JSON_COORDINATES).elements()
            : Collections.<JsonNode>emptyList().iterator();

    Element root = null;//from   ww w  .ja  va2  s  .co m
    final EdmSimpleType edmSimpleType = EdmSimpleType.fromValue(type);
    switch (edmSimpleType) {
    case GeographyPoint:
    case GeometryPoint:
        root = deserializePoint(parent.getOwnerDocument(), cooItor);
        break;

    case GeographyMultiPoint:
    case GeometryMultiPoint:
        root = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML, ODataConstants.ELEM_MULTIPOINT);
        if (cooItor.hasNext()) {
            final Element pointMembers = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML,
                    ODataConstants.ELEM_POINTMEMBERS);
            root.appendChild(pointMembers);
            while (cooItor.hasNext()) {
                final Iterator<JsonNode> mpItor = cooItor.next().elements();
                pointMembers.appendChild(deserializePoint(parent.getOwnerDocument(), mpItor));
            }
        }
        break;

    case GeographyLineString:
    case GeometryLineString:
        root = deserializeLineString(parent.getOwnerDocument(), cooItor);
        break;

    case GeographyMultiLineString:
    case GeometryMultiLineString:
        root = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML,
                ODataConstants.ELEM_MULTILINESTRING);
        if (cooItor.hasNext()) {
            final Element lineStringMembers = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML,
                    ODataConstants.ELEM_LINESTRINGMEMBERS);
            root.appendChild(lineStringMembers);
            while (cooItor.hasNext()) {
                final Iterator<JsonNode> mlsItor = cooItor.next().elements();
                lineStringMembers.appendChild(deserializeLineString(parent.getOwnerDocument(), mlsItor));
            }
        }
        break;

    case GeographyPolygon:
    case GeometryPolygon:
        root = deserializePolygon(parent.getOwnerDocument(), cooItor);
        break;

    case GeographyMultiPolygon:
    case GeometryMultiPolygon:
        root = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML,
                ODataConstants.ELEM_MULTIPOLYGON);
        if (cooItor.hasNext()) {
            final Element surfaceMembers = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML,
                    ODataConstants.ELEM_SURFACEMEMBERS);
            root.appendChild(surfaceMembers);
            while (cooItor.hasNext()) {
                final Iterator<JsonNode> mpItor = cooItor.next().elements();
                surfaceMembers.appendChild(deserializePolygon(parent.getOwnerDocument(), mpItor));
            }
        }
        break;

    case GeographyCollection:
    case GeometryCollection:
        root = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML,
                ODataConstants.ELEM_GEOCOLLECTION);
        if (node.has(ODataConstants.JSON_GEOMETRIES)) {
            final Iterator<JsonNode> geoItor = node.get(ODataConstants.JSON_GEOMETRIES).elements();
            if (geoItor.hasNext()) {
                final Element geometryMembers = parent.getOwnerDocument().createElementNS(ODataConstants.NS_GML,
                        ODataConstants.ELEM_GEOMEMBERS);
                root.appendChild(geometryMembers);

                while (geoItor.hasNext()) {
                    final JsonNode geo = geoItor.next();
                    final String collItemType = geo.get(ODataConstants.ATTR_TYPE).asText();
                    final String callAsType;
                    if (EdmSimpleType.GeographyCollection.name().equals(collItemType)
                            || EdmSimpleType.GeometryCollection.name().equals(collItemType)) {

                        callAsType = collItemType;
                    } else {
                        callAsType = (edmSimpleType == EdmSimpleType.GeographyCollection ? "Geography"
                                : "Geometry") + collItemType;
                    }

                    deserialize(geo, geometryMembers, EdmSimpleType.namespace() + "." + callAsType);
                }
            }
        }
        break;

    default:
    }

    if (root != null) {
        parent.appendChild(root);
        if (node.has(ODataConstants.JSON_CRS)) {
            root.setAttribute(ODataConstants.ATTR_SRSNAME,
                    ODataConstants.JSON_GIS_URLPREFIX + node.get(ODataConstants.JSON_CRS)
                            .get(ODataConstants.PROPERTIES).get(ODataConstants.NAME).asText().split(":")[1]);
        }
    }
}

From source file:DomUtils.java

/**
 * Rename element./*from   w  w w  . ja v a  2 s . c o  m*/
 * @param element The element to be renamed.
 * @param replacementElement The tag name of the replacement element.
 * @param keepChildContent <code>true</code> if the target element's child content
 * is to be copied to the replacement element, false if not. Default <code>true</code>.
 * @param keepAttributes <code>true</code> if the target element's attributes
 * are to be copied to the replacement element, false if not. Default <code>true</code>.
 * @return The renamed element.
 */
public static Element renameElement(Element element, String replacementElement, boolean keepChildContent,
        boolean keepAttributes) {

    Element replacement = element.getOwnerDocument().createElement(replacementElement);
    if (keepChildContent) {
        DomUtils.copyChildNodes(element, replacement);
    }
    if (keepAttributes) {
        NamedNodeMap attributes = element.getAttributes();
        int attributeCount = attributes.getLength();

        for (int i = 0; i < attributeCount; i++) {
            Attr attribute = (Attr) attributes.item(i);
            replacement.setAttribute(attribute.getName(), attribute.getValue());
        }
    }
    DomUtils.replaceNode(replacement, element);

    return replacement;
}

From source file:com.msopentech.odatajclient.engine.data.ODataBinder.java

/**
 * Gets an <tt>EntryResource</tt> from the given OData entity.
 *
 * @param <T> entry resource type.
 * @param entity OData entity./*from ww w  . j av  a 2 s . c o m*/
 * @param reference reference class.
 * @param setType whether to explicitly output type information.
 * @return <tt>EntryResource</tt> object.
 */
@SuppressWarnings("unchecked")
public static <T extends EntryResource> T getEntry(final ODataEntity entity, final Class<T> reference,
        final boolean setType) {

    final T entry = ResourceFactory.newEntry(reference);
    entry.setType(entity.getName());

    // -------------------------------------------------------------
    // Add edit and self link
    // -------------------------------------------------------------
    final URI editLink = entity.getEditLink();
    if (editLink != null) {
        final LinkResource entryEditLink = ResourceFactory.newLinkForEntry(reference);
        entryEditLink.setTitle(entity.getName());
        entryEditLink.setHref(editLink.toASCIIString());
        entryEditLink.setRel(ODataConstants.EDIT_LINK_REL);
        entry.setEditLink(entryEditLink);
    }

    if (entity.isReadOnly()) {
        final LinkResource entrySelfLink = ResourceFactory.newLinkForEntry(reference);
        entrySelfLink.setTitle(entity.getName());
        entrySelfLink.setHref(entity.getLink().toASCIIString());
        entrySelfLink.setRel(ODataConstants.SELF_LINK_REL);
        entry.setSelfLink(entrySelfLink);
    }
    // -------------------------------------------------------------

    // -------------------------------------------------------------
    // Append navigation links (handling inline entry / feed as well)
    // -------------------------------------------------------------
    // handle navigation links
    for (ODataLink link : entity.getNavigationLinks()) {
        // append link 
        LOG.debug("Append navigation link\n{}", link);
        entry.addNavigationLink(getLinkResource(link, ResourceFactory.linkClassForEntry(reference)));
    }
    // -------------------------------------------------------------

    // -------------------------------------------------------------
    // Append edit-media links
    // -------------------------------------------------------------
    for (ODataLink link : entity.getEditMediaLinks()) {
        LOG.debug("Append edit-media link\n{}", link);
        entry.addMediaEditLink(getLinkResource(link, ResourceFactory.linkClassForEntry(reference)));
    }
    // -------------------------------------------------------------

    // -------------------------------------------------------------
    // Append association links
    // -------------------------------------------------------------
    for (ODataLink link : entity.getAssociationLinks()) {
        LOG.debug("Append association link\n{}", link);
        entry.addAssociationLink(getLinkResource(link, ResourceFactory.linkClassForEntry(reference)));
    }
    // -------------------------------------------------------------

    final Element content = newEntryContent();
    if (entity.isMediaEntity()) {
        entry.setMediaEntryProperties(content);
        entry.setMediaContentSource(entity.getMediaContentSource());
        entry.setMediaContentType(entity.getMediaContentType());
    } else {
        entry.setContent(content);
    }

    for (ODataProperty prop : entity.getProperties()) {
        content.appendChild(toDOMElement(prop, content.getOwnerDocument(), setType));
    }

    return entry;
}

From source file:com.enonic.vertical.adminweb.handlers.fieldtypes.Field.java

public void setData(Element elem, String data) {
    XMLTool.createTextNode(elem.getOwnerDocument(), elem, data);
}

From source file:com.consol.citrus.admin.service.spring.filter.AddSpringBeanFilter.java

/**
 * {@inheritDoc}/*  w w  w  .j av  a2s  .  c om*/
 */
public short accept(Element element) {
    if (DomUtils.nodeNameEquals(element, "beans")) {
        element.appendChild(element.getOwnerDocument().createTextNode("\n    ")); //TODO make indentation configurable
        element.appendChild(element.getOwnerDocument().importNode(beanDefinition, true));
    }

    return NodeFilter.FILTER_ACCEPT;
}