Example usage for org.w3c.dom Document importNode

List of usage examples for org.w3c.dom Document importNode

Introduction

In this page you can find the example usage for org.w3c.dom Document importNode.

Prototype

public Node importNode(Node importedNode, boolean deep) throws DOMException;

Source Link

Document

Imports a node from another document to this document, without altering or removing the source node from the original document; this method creates a new copy of the source node.

Usage

From source file:org.sakaiproject.portal.xsltcharon.impl.XsltRenderContext.java

protected Element createToolXml(Document doc, Map tool, Map page) throws ToolRenderException {
    Element toolElement = doc.createElement("tool");

    Element title = doc.createElement("title");
    safeAppendTextNode(doc, title, tool.get("toolTitle").toString(), true);

    Element escapedId = doc.createElement("escapedId");
    safeAppendTextNode(doc, escapedId, tool.get("toolPlacementIDJS").toString(), true);

    toolElement.appendChild(title);/*  ww  w .ja va  2  s  .  c  o m*/
    toolElement.appendChild(escapedId);

    appendTextElementNode(doc, "toolReset", tool.get("toolResetActionUrl").toString(), toolElement);
    appendTextElementNode(doc, "toolHelp", tool.get("toolHelpActionUrl").toString(), toolElement);
    if (tool.get("toolJSR168Edit") != null) {
        appendTextElementNode(doc, "toolJSR168Edit", tool.get("toolJSR168Edit").toString(), toolElement);
        toolElement.setAttribute("has168Edit", "true");
    }
    if (tool.get("toolJSR168Help") != null) {
        appendTextElementNode(doc, "toolJSR168Help", tool.get("toolJSR168Help").toString(), toolElement);
        toolElement.setAttribute("has168Help", "true");
    }

    toolElement.setAttribute("hasReset", tool.get("toolShowResetButton").toString());
    toolElement.setAttribute("hasHelp", tool.get("toolShowHelpButton").toString());

    if ((Boolean) tool.get("hasRenderResult")) {
        toolElement.setAttribute("renderResult", "true");
        RenderResult result = (RenderResult) tool.get("toolRenderResult");
        appendTextElementNode(doc, "resultTitle", result.getTitle(), toolElement);

        //SAK-18793 - readDocumentFromString returns null on error; this check prevents NPEs
        String contentStr = result.getContent();

        if (contentStr == null) {
            throw new ToolRenderException("tool xml failed to render and is null");
        }

        //remove the title b/c XML will freak out if there is some foreign language utf-8 symbols
        contentStr = contentStr.replaceAll("title=\".*" + result.getTitle() + ".*\"", "");

        Tidy tdpr = new Tidy();
        tdpr.setForceOutput(true);
        tdpr.setShowWarnings(false);
        tdpr.setXHTML(true);
        tdpr.setXmlOut(true);
        tdpr.setPrintBodyOnly(true);
        tdpr.setQuiet(true);
        StringWriter sw = new StringWriter();
        tdpr.parse(new StringReader(contentStr), sw);

        // Wrap tidy'd source in a div so we guarantee a root element
        Document content = Xml.readDocumentFromString("<div>" + sw.toString() + "</div>");
        Element contentRoot = (Element) doc.importNode(content.getDocumentElement(), true);
        Element contentElement = doc.createElement("content");
        contentElement.appendChild(contentRoot);
        toolElement.appendChild(contentElement);
    } else {
        //portal/tool/ad222467-e186-4cca-80e9-d12a9d6db392?panel=Main
        Element toolUrl = doc.createElement("url");
        safeAppendTextNode(doc, toolUrl, tool.get("toolUrl") + "?panel=Main", true);

        toolElement.appendChild(toolUrl);
    }

    return toolElement;
}

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

/**
 * Get an object bank of items in Document form.
 *
 * @param itemIds array of the the item ids
 * @return the Document with the item bank data
 *///from  w  w  w . j a v a 2 s.  c  o  m
public Document getItemBank(String[] itemIds) {
    Document objectBank = XmlUtil.createDocument();

    Element root = objectBank.createElement("questestinterop");
    Element ob = objectBank.createElement("objectbank");
    String objectBankIdent = "object" + Math.random();
    ob.setAttribute("ident", objectBankIdent);
    for (int i = 0; i < itemIds.length; i++) {
        Document itemDoc = getItem(itemIds[i]);
        Element itemElement = itemDoc.getDocumentElement();
        Node itemImport = objectBank.importNode(itemElement, true);
        ob.appendChild(itemImport);
    }
    root.appendChild(ob);
    objectBank.appendChild(root);

    return objectBank;
}

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

/**
 * Get an object bank of asessments (asi) in Document form.
 *
 * @param assessmentIds array of the the assessment ids
 * @return the Document with the item bank data
 *//*from  w w w  .  ja va2s  .com*/
public Document getAssessmentBank(String[] assessmentIds) {
    Document objectBank = XmlUtil.createDocument();

    Element root = objectBank.createElement("questestinterop");
    Element ob = objectBank.createElement("objectbank");
    String objectBankIdent = "object" + Math.random();
    ob.setAttribute("ident", objectBankIdent);
    for (int i = 0; i < assessmentIds.length; i++) {
        Document itemDoc = getAssessment(assessmentIds[i]);
        Element itemElement = itemDoc.getDocumentElement();
        Node itemImport = objectBank.importNode(itemElement, true);
        ob.appendChild(itemImport);
    }
    root.appendChild(ob);
    objectBank.appendChild(root);

    return objectBank;
}

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

/**
 * Pull apart object bank based on elementName
 * @param objectBank//from ww w .j  a v a2  s  . c  o m
 * @param elementName e.g. "assessment", "item", etc.
 * @return
 */
private Document[] getDocumentsFromObjectBankDoc(Document objectBank, String elementName) {
    Document[] documents = null;
    // Get the matching elements
    NodeList nodeList = objectBank.getElementsByTagName("//" + elementName);

    int numDocs = nodeList.getLength();
    if (numDocs == 0) {
        return null;
    }

    documents = new Document[numDocs];
    for (int i = 0; i < numDocs; i++) {
        Node node = (Node) nodeList.item(i);
        Document objectDoc = XmlUtil.createDocument();
        Node importNode = objectDoc.importNode(node, true);
        objectDoc.appendChild(importNode);
        documents[i] = objectDoc;
    }

    return documents;
}

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

/**
 * Look up a List of Section XML from Assessment Xml
 * @return a List of Section XML objects
 *///from   www  .  j a  v  a2  s. co m
public List getSectionXmlList(Assessment assessmentXml) {
    List nodeList = assessmentXml.selectNodes("//section");
    List sectionXmlList = new ArrayList();

    // now convert our list of Nodes to a list of section xml
    for (int i = 0; i < nodeList.size(); i++) {
        try {
            Node node = (Node) nodeList.get(i);
            // create a document for a section xml object
            Document sectionDoc = XmlUtil.createDocument();
            // Make a copy for inserting into the new document
            Node importNode = sectionDoc.importNode(node, true);
            // Insert the copy into sectionDoc
            sectionDoc.appendChild(importNode);
            Section sectionXml = new Section(sectionDoc, this.getQtiVersion());
            // add the new section xml object to the list
            sectionXmlList.add(sectionXml);
        } catch (DOMException ex) {
            log.error(ex);
            ex.printStackTrace(System.out);
        }
    }
    return sectionXmlList;
}

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

/**
 * Look up a List of Item XML from Section Xml
 * @param Section sectionXml/* w  w  w  .ja v  a 2  s  .  co m*/
 * @return a List of Item XML objects
 */
public List getItemXmlList(Section sectionXml) {
    String itemElementName = qtiVersion == QTIVersion.VERSION_1_2 ? "//item" : "//assessmentItem";

    // now convert our list of Nodes to a list of section xml
    List nodeList = sectionXml.selectNodes(itemElementName);
    List itemXmlList = new ArrayList();
    for (int i = 0; i < nodeList.size(); i++) {
        try {
            Node node = (Node) nodeList.get(i);
            // create a document for a item xml object
            Document itemDoc = XmlUtil.createDocument();
            // Make a copy for inserting into the new document
            Node importNode = itemDoc.importNode(node, true);
            // Insert the copy into itemDoc
            itemDoc.appendChild(importNode);
            Item itemXml = new Item(itemDoc, this.getQtiVersion());
            // add the new section xml object to the list
            itemXmlList.add(itemXml);
        } catch (DOMException ex) {
            log.error(ex);
            ex.printStackTrace(System.out);
        }
    }
    return itemXmlList;
}

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

/**
 * create child/*  w  ww . j  a va2s.  co m*/
 *
 * @param childXpath
 *
 * @return
 */
protected final Element createChildElement(String childXpath) {
    int index = childXpath.indexOf("/");
    String elementName = childXpath;
    String subChildXpath = null;
    Element element = null;
    Element child = null;
    if (index > 0) {
        elementName = childXpath.substring(0, index);
        subChildXpath = childXpath.substring(index + 1);
        child = createChildElement(subChildXpath);
    }
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();
        element = document.createElement(elementName);
        if (child != null) {
            Node importedNode = document.importNode(child, true);
            element.appendChild(importedNode);
        }
    } catch (ParserConfigurationException pce) {
        log.error("Exception thrown from createChildElement(): " + pce.getMessage());
        pce.printStackTrace();
    }

    return element;
}

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

/**
 * @param phoneBookDocument//  ww w .  j a v a2s . c o  m
 * @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.springframework.oxm.xmlbeans.XmlBeansMarshaller.java

@Override
protected final void marshalDomNode(Object graph, Node node) throws XmlMappingException {
    Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument();
    Node xmlBeansNode = ((XmlObject) graph).newDomNode(getXmlOptions());
    NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes();
    for (int i = 0; i < xmlBeansChildNodes.getLength(); i++) {
        Node xmlBeansChildNode = xmlBeansChildNodes.item(i);
        Node importedNode = document.importNode(xmlBeansChildNode, true);
        node.appendChild(importedNode);/*from w  w w  .j  a  va 2s.  c  o m*/
    }
}

From source file:org.theospi.portfolio.portal.web.XsltPortal.java

private Element createSitesTabArea(Session session, String siteId, Document doc, HttpServletRequest req)
        throws IOException, SAXException {
    Element siteTabs = doc.createElement("siteTabs");
    CharArrayWriter writer = new CharArrayWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    printWriter.write("<div id=\"blank\">");
    includeTabs(printWriter, req, session, siteId, "site", false);
    Document tabs = getDocumentBuilder().parse(new InputSource(new CharArrayReader(writer.toCharArray())));
    siteTabs.appendChild(doc.importNode(tabs.getDocumentElement(), true));
    return siteTabs;
}