Example usage for org.w3c.dom DOMException getMessage

List of usage examples for org.w3c.dom DOMException getMessage

Introduction

In this page you can find the example usage for org.w3c.dom DOMException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.moviejukebox.tools.DOMHelper.java

/**
 * Gets the string value from a list of tag element names passed
 *
 * @param element/*from   w w  w.  j  a  v a 2  s  . com*/
 * @param tagNames
 * @return
 */
public static String getValueFromElement(Element element, String... tagNames) {
    String returnValue = DEFAULT_RETURN;
    NodeList nlElement;
    Element tagElement;
    NodeList tagNodeList;

    for (String tagName : tagNames) {
        try {
            nlElement = element.getElementsByTagName(tagName);
            if (nlElement != null) {
                tagElement = (Element) nlElement.item(0);
                if (tagElement != null) {
                    tagNodeList = tagElement.getChildNodes();
                    if (tagNodeList != null && tagNodeList.getLength() > 0) {
                        returnValue = tagNodeList.item(0).getNodeValue();
                    }
                }
            }
        } catch (DOMException ex) {
            LOG.trace("DOM processing exception, error: {}", ex.getMessage());
        } catch (NullPointerException ex) {
            // Shouldn't really catch null pointer exceptions, but there you go.
            LOG.trace("Null pointer exception, error: {}", ex.getMessage());
        }
    }

    return returnValue;
}

From source file:it.unibas.spicy.persistence.xml.DAOXmlUtility.java

public org.w3c.dom.Document buildNewDOM() throws DAOException {
    org.w3c.dom.Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  www  .ja  v  a  2 s.  c  om
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.newDocument();
    } catch (ParserConfigurationException pce) {
        logger.error(pce);
        throw new DAOException(pce.getMessage());
    } catch (DOMException doe) {
        logger.error(doe);
        throw new DAOException(doe.getMessage());
    }
    return document;
}

From source file:com.evolveum.midpoint.prism.parser.DomSerializer.java

private void serializePrimitiveElementOrAttribute(PrimitiveXNode<?> xprim, Element parentElement,
        QName elementOrAttributeName, boolean asAttribute) throws SchemaException {
    QName typeQName = xprim.getTypeQName();

    // if typeQName is not explicitly specified, we try to determine it from parsed value
    // TODO we should probably set typeQName when parsing the value...
    if (typeQName == null && xprim.isParsed()) {
        Object v = xprim.getValue();
        if (v != null) {
            typeQName = XsdTypeMapper.toXsdType(v.getClass());
        }/*from w ww.j  av  a 2s  .  c  om*/
    }

    if (typeQName == null) { // this means that either xprim is unparsed or it is empty
        if (com.evolveum.midpoint.prism.PrismContext.isAllowSchemalessSerialization()) {
            // We cannot correctly serialize without a type. But this is needed
            // sometimes. So just default to string
            String stringValue = xprim.getStringValue();
            if (stringValue != null) {
                if (asAttribute) {
                    DOMUtil.setAttributeValue(parentElement, elementOrAttributeName.getLocalPart(),
                            stringValue);
                    DOMUtil.setNamespaceDeclarations(parentElement, xprim.getRelevantNamespaceDeclarations());
                } else {
                    Element element;
                    try {
                        element = createElement(elementOrAttributeName, parentElement);
                        appendCommentIfPresent(element, xprim);
                    } catch (DOMException e) {
                        throw new DOMException(e.code, e.getMessage() + "; creating element "
                                + elementOrAttributeName + " in element " + DOMUtil.getQName(parentElement));
                    }
                    parentElement.appendChild(element);
                    DOMUtil.setElementTextContent(element, stringValue);
                    DOMUtil.setNamespaceDeclarations(element, xprim.getRelevantNamespaceDeclarations());
                }
            }
            return;
        } else {
            throw new IllegalStateException("No type for primitive element " + elementOrAttributeName
                    + ", cannot serialize (schemaless serialization is disabled)");
        }
    }

    // typeName != null after this point

    if (StringUtils.isBlank(typeQName.getNamespaceURI())) {
        typeQName = XsdTypeMapper.determineQNameWithNs(typeQName);
    }

    Element element = null;

    if (typeQName.equals(ItemPath.XSD_TYPE)) {
        ItemPath itemPath = (ItemPath) xprim.getValue();
        if (itemPath != null) {
            if (asAttribute) {
                throw new UnsupportedOperationException(
                        "Serializing ItemPath as an attribute is not supported yet");
            }
            XPathHolder holder = new XPathHolder(itemPath);
            element = holder.toElement(elementOrAttributeName, parentElement.getOwnerDocument());
            parentElement.appendChild(element);
        }

    } else {
        // not an ItemPathType

        if (!asAttribute) {
            try {
                element = createElement(elementOrAttributeName, parentElement);
            } catch (DOMException e) {
                throw new DOMException(e.code, e.getMessage() + "; creating element " + elementOrAttributeName
                        + " in element " + DOMUtil.getQName(parentElement));
            }
            appendCommentIfPresent(element, xprim);
            parentElement.appendChild(element);
        }

        if (typeQName.equals(DOMUtil.XSD_QNAME)) {
            QName value = (QName) xprim.getParsedValueWithoutRecording(DOMUtil.XSD_QNAME);
            value = setQNamePrefixExplicitIfNeeded(value);
            if (asAttribute) {
                try {
                    DOMUtil.setQNameAttribute(parentElement, elementOrAttributeName.getLocalPart(), value);
                } catch (DOMException e) {
                    throw new DOMException(e.code,
                            e.getMessage() + "; setting attribute " + elementOrAttributeName.getLocalPart()
                                    + " in element " + DOMUtil.getQName(parentElement) + " to QName value "
                                    + value);
                }
            } else {
                DOMUtil.setQNameValue(element, value);
            }
        } else {
            // not ItemType nor QName
            String value = xprim.getGuessedFormattedValue();

            if (asAttribute) {
                DOMUtil.setAttributeValue(parentElement, elementOrAttributeName.getLocalPart(), value);
            } else {
                DOMUtil.setElementTextContent(element, value);
            }
        }

    }
    if (!asAttribute && xprim.isExplicitTypeDeclaration()) {
        DOMUtil.setXsiType(element, setQNamePrefixExplicitIfNeeded(typeQName));
    }
}

From source file:com.evolveum.midpoint.prism.lex.dom.DomLexicalWriter.java

private void serializePrimitiveElementOrAttribute(PrimitiveXNode<?> xprim, Element parentElement,
        QName elementOrAttributeName, boolean asAttribute) throws SchemaException {
    QName typeQName = xprim.getTypeQName();

    // if typeQName is not explicitly specified, we try to determine it from parsed value
    // TODO we should probably set typeQName when parsing the value...
    if (typeQName == null && xprim.isParsed()) {
        Object v = xprim.getValue();
        if (v != null) {
            typeQName = XsdTypeMapper.toXsdType(v.getClass());
        }/*  w  w  w .  j a  va  2s  .  co  m*/
    }

    if (typeQName == null) { // this means that either xprim is unparsed or it is empty
        if (com.evolveum.midpoint.prism.PrismContextImpl.isAllowSchemalessSerialization()) {
            // We cannot correctly serialize without a type. But this is needed
            // sometimes. So just default to string
            String stringValue = xprim.getStringValue();
            if (stringValue != null) {
                if (asAttribute) {
                    DOMUtil.setAttributeValue(parentElement, elementOrAttributeName.getLocalPart(),
                            stringValue);
                    DOMUtil.setNamespaceDeclarations(parentElement, xprim.getRelevantNamespaceDeclarations());
                } else {
                    Element element;
                    try {
                        element = createElement(elementOrAttributeName, parentElement);
                        appendCommentIfPresent(element, xprim);
                    } catch (DOMException e) {
                        throw new DOMException(e.code, e.getMessage() + "; creating element "
                                + elementOrAttributeName + " in element " + DOMUtil.getQName(parentElement));
                    }
                    parentElement.appendChild(element);
                    DOMUtil.setElementTextContent(element, stringValue);
                    DOMUtil.setNamespaceDeclarations(element, xprim.getRelevantNamespaceDeclarations());
                }
            }
            return;
        } else {
            throw new IllegalStateException("No type for primitive element " + elementOrAttributeName
                    + ", cannot serialize (schemaless serialization is disabled)");
        }
    }

    // typeName != null after this point

    if (StringUtils.isBlank(typeQName.getNamespaceURI())) {
        typeQName = XsdTypeMapper.determineQNameWithNs(typeQName);
    }

    Element element = null;

    if (ItemPathType.COMPLEX_TYPE.equals(typeQName)) {
        //ItemPathType itemPathType = //ItemPathType.asItemPathType(xprim.getValue());         // TODO fix this hack
        ItemPathType itemPathType = (ItemPathType) xprim.getValue();
        if (itemPathType != null) {
            if (asAttribute) {
                throw new UnsupportedOperationException(
                        "Serializing ItemPath as an attribute is not supported yet");
            }
            ItemPathHolder holder = new ItemPathHolder(itemPathType.getItemPath());
            element = holder.toElement(elementOrAttributeName, parentElement.getOwnerDocument());
            parentElement.appendChild(element);
        }

    } else {
        // not an ItemPathType

        if (!asAttribute) {
            try {
                element = createElement(elementOrAttributeName, parentElement);
            } catch (DOMException e) {
                throw new DOMException(e.code, e.getMessage() + "; creating element " + elementOrAttributeName
                        + " in element " + DOMUtil.getQName(parentElement));
            }
            appendCommentIfPresent(element, xprim);
            parentElement.appendChild(element);
        }

        if (DOMUtil.XSD_QNAME.equals(typeQName)) {
            QName value = (QName) xprim.getParsedValueWithoutRecording(DOMUtil.XSD_QNAME);
            value = setQNamePrefixExplicitIfNeeded(value);
            if (asAttribute) {
                try {
                    DOMUtil.setQNameAttribute(parentElement, elementOrAttributeName.getLocalPart(), value);
                } catch (DOMException e) {
                    throw new DOMException(e.code,
                            e.getMessage() + "; setting attribute " + elementOrAttributeName.getLocalPart()
                                    + " in element " + DOMUtil.getQName(parentElement) + " to QName value "
                                    + value);
                }
            } else {
                DOMUtil.setQNameValue(element, value);
            }
        } else {
            // not ItemType nor QName
            String value = xprim.getGuessedFormattedValue();

            if (asAttribute) {
                DOMUtil.setAttributeValue(parentElement, elementOrAttributeName.getLocalPart(), value);
            } else {
                DOMUtil.setElementTextContent(element, value);
            }
        }

    }
    if (!asAttribute && xprim.isExplicitTypeDeclaration()) {
        DOMUtil.setXsiType(element, setQNamePrefixExplicitIfNeeded(typeQName));
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.dom.Node.java

/**
 * Add a DOM node as a child to this node before the referenced node.
 * If the referenced node is null, append to the end.
 * @param args the arguments//  w  w w  . j a  va 2 s.  co  m
 * @return the newly added child node
 */
protected Object insertBeforeImpl(final Object[] args) {
    final Object newChildObject = args[0];
    final Object refChildObject;
    if (args.length > 1) {
        refChildObject = args[1];
    } else {
        refChildObject = Undefined.instance;
    }
    Object insertedChild = null;

    if (newChildObject instanceof Node) {
        final Node newChild = (Node) newChildObject;
        final DomNode newChildNode = newChild.getDomNodeOrDie();

        // is the node allowed here?
        if (!isNodeInsertable(newChild)) {
            throw asJavaScriptException(
                    new DOMException("Node cannot be inserted at the specified point in the hierarchy",
                            DOMException.HIERARCHY_REQUEST_ERR));
        }
        if (newChildNode instanceof DomDocumentFragment) {
            final DomDocumentFragment fragment = (DomDocumentFragment) newChildNode;
            for (final DomNode child : fragment.getChildren()) {
                if (!isNodeInsertable((Node) child.getScriptableObject())) {
                    throw asJavaScriptException(
                            new DOMException("Node cannot be inserted at the specified point in the hierarchy",
                                    DOMException.HIERARCHY_REQUEST_ERR));
                }
            }
        }

        // extract refChild
        final DomNode refChildNode;
        if (refChildObject == Undefined.instance) {
            if (args.length == 2 || getBrowserVersion().hasFeature(JS_NODE_INSERT_BEFORE_REF_OPTIONAL)) {
                refChildNode = null;
            } else {
                throw Context.reportRuntimeError("insertBefore: not enough arguments");
            }
        } else if (refChildObject != null) {
            refChildNode = ((Node) refChildObject).getDomNodeOrDie();
        } else {
            refChildNode = null;
        }

        final DomNode domNode = getDomNodeOrDie();

        try {
            domNode.insertBefore(newChildNode, refChildNode);
        } catch (final org.w3c.dom.DOMException e) {
            throw asJavaScriptException(new DOMException(e.getMessage(), DOMException.HIERARCHY_REQUEST_ERR));
        }
        insertedChild = newChild;
    }
    return insertedChild;
}

From source file:es.upm.fi.dia.oeg.sitemap4rdf.Generator.java

/**
 * Generates the SitemapsSection of the sitemap index file
 * @param doc the XML Document// w  w w .  j  a  v  a 2  s  .c  o  m
 * @param elem the Element for the sitemapindex element
 */
protected void generateSitemapsSection(Element root, Document doc) {
    try {
        DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATEFORMAT);

        Iterator<String> iterator = (Iterator<String>) files.keySet().iterator();// Iterate on keys
        while (iterator.hasNext()) {
            String fileName = (String) iterator.next();
            if (gzip != null && gzip.equals(TRUE))
                fileName = fileName + zipFileExtension;

            Element sitemap = doc.createElement(siteMapTag);
            Element loc = doc.createElement(locTag);
            Element lastMod = doc.createElement(lastmodTag);

            loc.setTextContent(siteroot + fileName);
            Date date = new Date();

            lastMod.setTextContent(dateFormat.format(date));

            sitemap.appendChild(loc);
            sitemap.appendChild(lastMod);

            root.appendChild(sitemap);

        }
    } catch (DOMException e) {
        logger.debug("DOMException ", e);
        System.err.println(e.getMessage());
        System.exit(3);

    }
}

From source file:de.betterform.xml.xforms.model.Instance.java

public void createNode(List nodeset, int position, String qname) throws XFormsException {
    if (nodeset.size() < position) {
        return;/*from   ww w  .j ava 2  s . co  m*/
    }

    Node parentNode = (Node) ((NodeWrapper) nodeset.get(position - 1)).getUnderlyingNode();
    try {
        parentNode.appendChild(createElement(qname));
    } catch (DOMException e) {
        throw new XFormsBindingException(e.getMessage(), this.target, null);
    }
}

From source file:com.evolveum.midpoint.prism.marshaller.ItemPathHolder.java

public Element toElement(String elementNamespace, String localElementName, Document document) {
    Element element = document.createElementNS(elementNamespace, localElementName);
    if (!StringUtils.isBlank(elementNamespace)) {
        String prefix = GlobalDynamicNamespacePrefixMapper.getPreferredPrefix(elementNamespace);
        if (!StringUtils.isBlank(prefix)) {
            try {
                element.setPrefix(prefix);
            } catch (DOMException e) {
                throw new SystemException("Error setting XML prefix '" + prefix + "' to element {"
                        + elementNamespace + "}" + localElementName + ": " + e.getMessage(), e);
            }//from   ww w .  j a v a2  s. co  m
        }
    }
    element.setTextContent(getXPathWithDeclarations());
    Map<String, String> namespaceMap = getNamespaceMap();
    if (namespaceMap != null) {
        for (Entry<String, String> entry : namespaceMap.entrySet()) {
            DOMUtil.setNamespaceDeclaration(element, entry.getKey(), entry.getValue());
        }
    }
    return element;
}

From source file:com.aurel.track.exchange.track.exporter.TrackExportBL.java

/**
 * Create a dom element with text content and attributes
 * @param elementName/*from ww  w .  ja v a2  s  .c  o m*/
 * @param textContent
 * @param attributeValues
 * @param dom
 * @return
 */
private static Element createElementWithAttributes(String elementName, String textContent, boolean needCDATA,
        Map<String, String> attributeValues, Document dom) {
    //create the main element
    Element element = null;
    try {
        element = dom.createElement(elementName);
    } catch (DOMException e) {
        LOGGER.warn("Creating an XML node with the element name " + elementName + " failed with " + e);
    }
    if (element == null) {
        return null;
    }

    //create the text element
    if (textContent != null) {
        if (needCDATA) {
            CDATASection cdataText = dom.createCDATASection(Html2Text.stripNonValidXMLCharacters(textContent));
            element.appendChild(cdataText);
        } else {
            Text textElement;
            try {
                textElement = dom.createTextNode(textContent);
            } catch (DOMException e) {
                LOGGER.info("Creating the text node for the element " + elementName + " and text " + textContent
                        + " failed with " + e);
                textElement = dom.createTextNode("");
            }
            //append the text to the element
            element.appendChild(textElement);
        }
    }

    //set the attributes
    if (attributeValues != null) {
        Iterator<String> iterator = attributeValues.keySet().iterator();
        while (iterator.hasNext()) {
            String attributeName = iterator.next();
            String attributeValue = attributeValues.get(attributeName);
            if (attributeValue != null) {
                try {
                    element.setAttribute(attributeName, attributeValue);
                } catch (DOMException e) {
                    LOGGER.warn("Setting the attribute name " + attributeName + " to attribute value "
                            + attributeValue + " faield with " + e.getMessage());
                    LOGGER.debug(ExceptionUtils.getStackTrace(e));
                }
            }
        }
    }
    return element;
}