Example usage for com.google.gwt.dom.client Node getOwnerDocument

List of usage examples for com.google.gwt.dom.client Node getOwnerDocument

Introduction

In this page you can find the example usage for com.google.gwt.dom.client Node getOwnerDocument.

Prototype

@Override
    public Document getOwnerDocument() 

Source Link

Usage

From source file:client.net.sf.saxon.ce.dom.HTMLDocumentWrapper.java

License:Mozilla Public License

/**
 * Create a wrapper for a node in this document
 *
 * @param node the DOM node to be wrapped. This must be a node within the document wrapped by this
 *             XMLDocumentWrapper/*from w ww .j a  va  2s.  c  o m*/
 * @throws IllegalArgumentException if the node is not a descendant of the Document node wrapped by
 *                                  this XMLDocumentWrapper
 */
public HTMLNodeWrapper wrap(Node node) {
    if (node == this.node) {
        return this;
    }
    Document doc = node.getOwnerDocument();
    if (doc == this.node) {
        return makeWrapper(node, this);
    } else {
        throw new IllegalArgumentException(
                "XMLDocumentWrapper#wrap: supplied node does not belong to the wrapped DOM document");
    }
}

From source file:client.net.sf.saxon.ce.Xslt20ProcessorImpl.java

License:Mozilla Public License

public Node renderXML(JavaScriptObject inSourceDoc, DocumentInfo styleDoc,
        com.google.gwt.dom.client.Node target) {
    try {//  w ww  . java  2 s. c o m
        if (styleDoc == null) {
            throw new Exception("Stylesheet for transform is null");
        }
        docFetchRequired = inSourceDoc != null;
        CompilerInfo info = config.getDefaultXsltCompilerInfo();
        info.setErrorListener(new StandardErrorListener());

        String asyncSourceURI = null;

        // for now - don't use aync when using the JavaScript API calls that return a result
        if (docFetchRequired
                && (localController.getApiCommand() == APIcommand.UPDATE_HTML || (successCallback != null))) {
            asyncSourceURI = SaxonceApi.getAsyncUri(inSourceDoc);
            if (asyncSourceURI != null && asyncSourceURI.toLowerCase().startsWith("file:")) {
                asyncSourceURI = null; // force synchronous fetch if using file-system protocol
            }
        }

        // ----------- Start async code -------------
        fetchedSourceDoc = null;
        transformInvoked = false;

        if (asyncSourceURI != null) {
            final String URI = asyncSourceURI;
            final Node transformTarget = target;

            logger.log(Level.FINE, "Aynchronous GET for: " + asyncSourceURI);
            final HTTPHandler hr = new HTTPHandler();

            hr.doGet(asyncSourceURI, new RequestCallback() {

                public void onError(Request request, Throwable exception) {
                    //hr.setErrorMessage(exception.getMessage());
                    String msg = "HTTP Error " + exception.getMessage() + " for URI " + URI;
                    handleException(new RuntimeException(msg), "onError");
                }

                public void onResponseReceived(Request request, Response response) {
                    int statusCode = response.getStatusCode();
                    if (statusCode == 200) {
                        Logger.getLogger("ResponseReceived").fine("GET Ok for: " + URI);
                        Node responseNode;
                        try {
                            responseNode = (Node) XMLDOM.parseXML(response.getText());
                        } catch (Exception e) {
                            handleException(new RuntimeException(e.getMessage()), "onResponseReceived");
                            return;
                        }
                        DocumentInfo responseDoc = config.wrapXMLDocument(responseNode, URI);
                        // now document is here, we can transform it
                        Node result = invokeTransform(responseDoc, transformTarget);
                        hr.setResultNode(result); // TODO: This isn't used yet
                        // handle OK response from the server 
                    } else if (statusCode < 400) {
                        // transient
                    } else {
                        String msg = "HTTP Error " + statusCode + " " + response.getStatusText() + " for URI "
                                + URI;
                        handleException(new RuntimeException(msg), "onResponseReceived");
                        //hr.setErrorMessage(statusCode + " " + response.getStatusText());
                    }
                } // ends inner method
            }// ends inner class
            ); // ends doGet method call
        }
        // -------------- End async code

        /// we can compile - even while sourcedoc is being fetched asynchronously

        if (stylesheet == null) {
            if (LogConfiguration.loggingIsEnabled()) {
                LogController.InitializeTraceListener();
            }
            logger.log(Level.FINE, "Compiling Stylesheet...");
            PreparedStylesheet sheet = new PreparedStylesheet(config, info);
            sheet.prepare(styleDoc);
            stylesheet = sheet;
            logger.log(Level.FINE, "Stylesheet compiled OK");
        }

        // for async operation - this is called within the callback - so don't call here            
        if (asyncSourceURI == null && inSourceDoc != null) {
            int nodeType = (Node.is(inSourceDoc)) ? ((Node) inSourceDoc).getNodeType() : 0;

            if (nodeType > 0 && nodeType != Node.DOCUMENT_NODE) {
                // add a document node wrapper
                Node sourceNode = (Node) inSourceDoc;
                Document sourceDoc = sourceNode.getOwnerDocument();
                HTMLDocumentWrapper htmlDoc = new HTMLDocumentWrapper(sourceDoc, sourceDoc.getURL(), config,
                        DocType.UNKNOWN);
                fetchedSourceDoc = htmlDoc.wrap(sourceNode);
            } else {
                fetchedSourceDoc = SaxonceApi.getDocSynchronously(inSourceDoc, config);
            }
        }
        // this method only runs if transformInvoked == false - need to get sourceDoc reference if not invoked

        return invokeTransform(fetchedSourceDoc, target);

        //method ends - allowing onResponceReceived handler to call invokeTransform for async operation
    } catch (Exception e) {
        handleException(e, "renderXML");
        return null;
    }
}

From source file:com.cgxlib.xq.client.impl.SelectorEngine.java

License:Apache License

public static NodeList<Element> veryQuickId(String id, Node ctx) {
    Document d = ctx.getNodeType() == Node.DOCUMENT_NODE ? ctx.<Document>cast() : ctx.getOwnerDocument();
    return JsNodeArray.create(d.getElementById(id));
}

From source file:com.cgxlib.xq.client.js.JsUtils.java

License:Apache License

/**
 * Returns the owner document element of an element.
 *///from ww  w .  j  a  v a2s  .  c o m
public static Document getOwnerDocument(Node n) {
    return n == null || !isElement(n) ? null
            : n.getNodeType() == Node.DOCUMENT_NODE ? n.<Document>cast() : n.getOwnerDocument();
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.util.Visitor.java

License:Apache License

/**
 * process all the node from startNode to endNode.
 *
 * @param startNode/* w w  w . ja  v  a 2  s . co  m*/
 * @param endNode
 */
public void process(Node startNode, Node endNode) {
    this.processing = false;
    partialVisit = true;
    this.startNode = startNode;
    this.endNode = endNode;
    visit(startNode.getOwnerDocument());
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.util.XPathUtil.java

License:Apache License

public String getXPath(Node node) {
    Log.debug("XPathUtil] node: " + node.getNodeName() + " parent node: " + node.getParentNode().getNodeName());
    Document document = node.getOwnerDocument();
    Node current = node;/*w w w.j a  v  a2s .com*/
    StringBuilder xpath = new StringBuilder();
    while (!current.equals(document)) {
        int counter = 1;
        String name = current.getNodeName();
        while (current.getPreviousSibling() != null) {
            if (current.getPreviousSibling().getNodeName().equalsIgnoreCase(name)
                    && !isIgnored(current.getPreviousSibling())) {
                counter++;
            }
            current = current.getPreviousSibling();
        }
        xpath.insert(0, "/" + name.toLowerCase() + "[" + counter + "]");
        current = current.getParentNode();
    }
    Log.debug("XPathUtil] xpath: " + xpath);
    return xpath.toString();
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.decorator.AnnoteaDecoratorVisitor.java

License:Apache License

private void processDecoratedNode(Node node, String data, Node parent) {
    String[] selectedText = getSelectedText(data, textToAnnotate);
    if (selectedText[1].trim().length() == 0 && selectedText[2].trim().length() == 0
            && node.getParentNode().getNodeName().equalsIgnoreCase("tr")) {
        // don't add nodes to tr
        textToAnnotate -= selectedText[0].length();
        return;/*from   ww w  . j av a  2s . c o m*/
    }
    Document document = node.getOwnerDocument();
    SpanElement spanElement = getSpanElement(document);
    spanElement.setInnerText(selectedText[1]);
    insertBefore(parent, node.getNextSibling(), spanElement);
    if (selectedText[2].length() > 0) {
        insertBefore(parent, spanElement.getNextSibling(), document.createTextNode(selectedText[2]));
    }
    textToAnnotate -= selectedText[0].length();
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Searches for the first element descendant with the name <code>tagName</code>. Searching is done in a DFS order
 * with node processing on first pass through them.
 * //from  www . jav  a 2s. c  o  m
 * @param node the node to start the search from
 * @param tagName the name of the searched element
 * @return the first descendant of type <code>tagName</code> of the passed node, in DFS order.
 */
public Node getFirstDescendant(Node node, String tagName) {
    Iterator<Node> it = ((Document) node.getOwnerDocument()).getIterator(node);
    while (it.hasNext()) {
        Node currentNode = it.next();
        if (currentNode.getNodeType() == Node.ELEMENT_NODE
                && currentNode.getNodeName().equalsIgnoreCase(tagName)) {
            return currentNode;
        }
    }
    return null;
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Clones the contents of the given node. If node type is text, CDATA or comment then only the data between
 * startOffset (including) and endOffset is kept. If node type is element then only the child nodes with indexes
 * between startOffset (including) and endOffset are included in the document fragment returned.
 * //  www . j  a  va2s  .  c o m
 * @param node The DOM node whose contents will be cloned.
 * @param startOffset the index of the first child to clone or the first character to include in the cloned
 *            contents.
 * @param endOffset specifies where the cloned contents end.
 * @return the cloned contents of the given node, between start offset and end offset.
 */
public DocumentFragment cloneNodeContents(Node node, int startOffset, int endOffset) {
    DocumentFragment contents = ((Document) node.getOwnerDocument()).createDocumentFragment();
    switch (node.getNodeType()) {
    case CDATA_NODE:
    case COMMENT_NODE:
    case Node.TEXT_NODE:
        if (startOffset < endOffset) {
            Node clone = node.cloneNode(false);
            clone.setNodeValue(node.getNodeValue().substring(startOffset, endOffset));
            contents.appendChild(clone);
        }
        break;
    case Node.ELEMENT_NODE:
        for (int i = startOffset; i < endOffset; i++) {
            contents.appendChild(node.getChildNodes().getItem(i).cloneNode(true));
        }
        break;
    default:
        // ignore
    }
    return contents;
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Extracts the contents of the given node. If node type is text, CDATA or comment then only the data between
 * startOffset (including) and endOffset is kept. If node type is element then only the child nodes with indexes
 * between startOffset (including) and endOffset are included in the document fragment returned.
 * //from   w  w  w .ja v a2 s  .c om
 * @param node the DOM node whose contents will be extracted
 * @param startOffset the index of the first child to extract or the first character to include in the extracted
 *            contents
 * @param endOffset specifies where the extracted contents end
 * @return the extracted contents of the given node, between start offset and end offset
 */
public DocumentFragment extractNodeContents(Node node, int startOffset, int endOffset) {
    DocumentFragment contents = ((Document) node.getOwnerDocument()).createDocumentFragment();
    switch (node.getNodeType()) {
    case CDATA_NODE:
    case COMMENT_NODE:
    case Node.TEXT_NODE:
        if (startOffset < endOffset) {
            Node clone = node.cloneNode(false);
            clone.setNodeValue(node.getNodeValue().substring(startOffset, endOffset));
            contents.appendChild(clone);
            node.setNodeValue(
                    node.getNodeValue().substring(0, startOffset) + node.getNodeValue().substring(endOffset));
        }
        break;
    case Node.ELEMENT_NODE:
        for (int i = startOffset; i < endOffset; i++) {
            contents.appendChild(node.getChildNodes().getItem(startOffset));
        }
        break;
    default:
        // ignore
    }
    return contents;
}