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

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

Introduction

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

Prototype

@Override
    public Node getChild(int index) 

Source Link

Usage

From source file:client.net.sf.saxon.ce.expr.instruct.ResultDocument.java

License:Mozilla Public License

public TailCall processLeavingTail(XPathContext context) throws XPathException {
    final Controller controller = context.getController();
    final APIcommand command = controller.getApiCommand();
    XPathContext c2 = context.newMinorContext();

    int action = APPEND_CONTENT;
    if (methodExpression != null) {
        String method = methodExpression.evaluateAsString(context).toString();
        StructuredQName methodQ;/* w  w  w .j  a v a  2 s  .  co  m*/
        if (method.indexOf(':') >= 0) {
            methodQ = StructuredQName.fromLexicalQName(method, false, nsResolver);
        } else {
            methodQ = new StructuredQName("", "", method);
        }
        if ("replace-content".equals(methodQ.getLocalName())) {
            // TODO: check the namespace URI is NamespaceConstant.IXSL
            action = REPLACE_CONTENT;
        }
    }

    String hrefValue = null;
    if (href != null) {
        hrefValue = href.evaluateAsString(context).toString();
    } else if (command == APIcommand.UPDATE_HTML) {
        throw new XPathException("html update - no href value for result-document instruction");
    } else {
        hrefValue = "result" + (controller.getResultDocumentCount() + 1);
    }
    NodeInfo target = null;
    Node targetNode = null;
    String contextNodeName = "";
    String absURI = "";
    if (command == APIcommand.TRANSFORM_TO_DOCUMENT) {
        absURI = getValidAbsoluteURI(controller, hrefValue);
        targetNode = XMLDOM.createDocument(absURI);
    } else if (command == APIcommand.TRANSFORM_TO_FRAGMENT
            || command == APIcommand.TRANSFORM_TO_HTML_FRAGMENT) {
        absURI = getValidAbsoluteURI(controller, hrefValue);
        targetNode = HTMLDocumentWrapper.createDocumentFragment((Document) controller.getTargetNode());
    } else if (hrefValue.startsWith("#")) {
        hrefValue = hrefValue.substring(1);
        targetNode = ((Document) controller.getTargetNode()).getElementById(hrefValue); // com.google.gwt.dom.client.Document.get().getElementById(hrefValue);
    } else if (hrefValue.startsWith("?select=")) {
        String select = hrefValue.substring(8);
        AbstractStaticContext env = new AbstractStaticContext() {
            public String getURIForPrefix(String prefix) throws XPathException {
                return null;
            }

            public Expression bindVariable(StructuredQName qName) throws XPathException {
                return null;
            }

            public NamespaceResolver getNamespaceResolver() {
                return null;
            }

            //override getFunctionLibrary to return that loaded for the prepared stylesheet
            public FunctionLibrary getFunctionLibrary() {
                return controller.getPreparedStylesheet().getFunctionLibrary();
            }

        };
        ExpressionVisitor visitor = new ExpressionVisitor();
        visitor.setConfiguration(context.getConfiguration());
        visitor.setExecutable(new Executable(context.getConfiguration()));
        visitor.setStaticContext(env);
        env.setConfiguration(context.getConfiguration());
        Container container = (StyleElement) getSourceLocator();
        Expression expr = null;
        try {
            expr = ExpressionTool.make(select, env, container, 0, Token.EOF, getSourceLocator());
        } catch (Exception e) {
            // occurs if expression contains references to variables etc. within the dynamic context
            throw new XPathException(
                    "Error on evaluating (in static context) result-document href: " + hrefValue);
        }
        expr = visitor.typeCheck(expr, NodeKindTest.DOCUMENT);
        XPathContext c3 = context.newCleanContext();
        //context for ?select expression is the html page if an external node is the context
        Document page = (Document) controller.getTargetNode(); //com.google.gwt.dom.client.Document.get();
        Item cItem = context.getContextItem();

        NodeInfo currentContextItem;
        if (cItem instanceof JSObjectValue) {
            currentContextItem = null;
        } else {
            currentContextItem = (NodeInfo) cItem;
        }

        boolean useCurrentContext;
        if (currentContextItem == null) {
            useCurrentContext = false;
        } else {
            useCurrentContext = (currentContextItem.getBaseURI().equals(page.getURL()));
        }

        NodeInfo contextItem;

        if (useCurrentContext) {
            contextItem = currentContextItem;
            if (LogConfiguration.loggingIsEnabled() && contextItem.getNodeKind() == Type.ELEMENT) {
                contextNodeName = controller.getNamePool().getLocalName(contextItem.getNameCode());
            }
        } else {
            contextItem = new HTMLDocumentWrapper(page, page.getURL(), context.getConfiguration(),
                    DocType.UNKNOWN);
        }
        if (LogConfiguration.loggingIsEnabled()) {
            contextNodeName = (contextNodeName.equals("") ? "" : " context node: " + contextNodeName);
        }

        AxisIterator iterator = SingleNodeIterator.makeIterator(contextItem);
        iterator.next(); // position on the single item
        c3.setCurrentIterator(iterator);
        SequenceIterator iter = expr.iterate(c3);
        Item resultItem = iter.next();

        if (resultItem == null) {
        } // do nothing
        else if (!(resultItem instanceof NodeInfo)) {
            throw new XPathException("non-node returned by result-document href: " + hrefValue);
        } else {
            target = (NodeInfo) resultItem;
            targetNode = (com.google.gwt.dom.client.Node) ((HTMLNodeWrapper) target).getUnderlyingNode();
        }
    } else if (command == APIcommand.UPDATE_HTML) {
        throw new XPathException(
                "expected '?select=' or '#' at start of result-document href, found: " + hrefValue);
    }
    if (targetNode == null) {
        logger.warning("result-document target not found for href: " + hrefValue + contextNodeName);
        return null;
    } else {
        logger.fine("processing result-document for href: " + hrefValue + contextNodeName);
    }

    //checkAcceptableUri(context, absoluteResultURI.toString());
    //IFrameElement container = Document.get().createIFrameElement();

    Node container = null;
    if (command == APIcommand.UPDATE_HTML) {
        container = HTMLDocumentWrapper.createDocumentFragment((Document) controller.getTargetNode());
    } else {
        addResultDocument(context, new DocumentURI(absURI), (Document) targetNode);
        container = targetNode;
    }

    PipelineConfiguration pipe = controller.makePipelineConfiguration();

    Receiver out = controller.openResult(pipe, c2, container, action);

    try {
        content.process(c2);
        out.endDocument();
    } catch (XPathException err) {
        err.setXPathContext(context);
        err.maybeSetLocation(getSourceLocator());
        throw err;
    }
    controller.closeResult(out, c2);

    if (command == APIcommand.UPDATE_HTML) {
        PendingUpdateList list = controller.getPendingUpdateList();
        if (action == REPLACE_CONTENT && command == APIcommand.UPDATE_HTML) {
            int existingChildren = targetNode.getChildCount();
            for (int i = 0; i < existingChildren; i++) {
                Node child = targetNode.getChild(i);
                list.add(new DeleteAction(child));
            }
        }

        list.add(new InsertAction(container, targetNode, InsertAction.LAST));
    }
    //controller.setResultTree(absoluteResultURI.toString(), root);
    return null;
}

From source file:com.bfr.client.selection.HtmlBBox.java

License:Apache License

/**
 * Determine the index of a node within its parent
 *
 * @param child A node to determine the index of
 * @return index of the node, or -1 on failure
 */// w w w  .  ja  va2 s .  c om
public static int getChildIndex(Node child) {
    int res = -1;
    Node parent = child.getParentNode();
    if (parent != null) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            if (child == parent.getChild(i)) {
                res = i;
                break;
            }
        }
    }
    return res;
}

From source file:com.bfr.client.selection.impl.RangeImpl.java

License:Apache License

/**
 * If the found range is not on a text node, this finds the cooresponding
 * text node to where the selection is.  If it is on a text node, just
 * directly creates the endpoint from it.
 *
 * @param node   node returned as an endpoint of a range
 * @param offset offset returned to the endpoint of a range
 * @return A range end point with a proper (or null) text node
 *///from w w  w  . j  a v  a2s  .  com
private RangeEndPoint findTextPoint(Node node, int offset) {
    RangeEndPoint res;
    if (node.getNodeType() == Node.TEXT_NODE) {
        res = new RangeEndPoint((Text) node, offset);
    } else {
        // search backwards unless this is after the last node
        boolean dir = offset >= node.getChildCount();
        Node child = (node.getChildCount() == 0) ? node : node.getChild(dir ? offset - 1 : offset);
        // Get the previous/next text node
        Text text = Range.getAdjacentTextElement(child, dir);
        if (text == null) {
            // If we didn't find a text node in the preferred direction,
            // try the other direction
            dir = !dir;
            text = Range.getAdjacentTextElement(child, dir);
        }
        res = new RangeEndPoint(text, dir);
    }
    return res;
}

From source file:com.dom_distiller.client.ContentExtractor.java

License:Open Source License

/**
 * Strips all "id" attributes from nodes in the tree rooted at |clonedSubtree|
 *///ww w  . j a  v  a  2s . co  m
private static void stripIds(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        Element e = Element.as(node);
        if (e.hasAttribute("id")) {
            e.setAttribute("id", "");
        }
        // Intentional fall-through.
    case Node.DOCUMENT_NODE:
        for (int i = 0; i < node.getChildCount(); i++) {
            stripIds(node.getChild(i));
        }
    }
}

From source file:com.dom_distiller.client.JavaScript.java

License:Open Source License

private static boolean javaContains(Node l, Node r) {
    for (int i = 0; i < l.getChildCount(); i++) {
        Node c = l.getChild(i);
        if (c.equals(r))
            return true;
        if (javaContains(c, r))
            return true;
    }//www .ja  v  a 2s .c  o  m
    return false;
}

From source file:com.ephesoft.gxt.core.client.ui.widget.util.HTMLDomUtil.java

License:Open Source License

/**
 * Gets the <code> grandChildren </code> of a parent which is the nth child of a grandParent, where 'n' is the childNumber. If
 * child number is less than 1 it returns the grand children of the last child of grandparent.
 * /*  w  ww .j  av a  2  s .c o m*/
 * @param grandParent {@link Node} Element whose grandChildren needs to be returned. When NULL , null is returned.
 * @param childNumber int childNumber of the child whose children neeeds to be returned.
 * @return {@link NodeList} < @Node > which are all the grandChildren. NULL if child doesnot exist or has no children.
 */
public static NodeList<Node> getGrandChildren(Node grandParent, int childNumber) {
    NodeList<Node> grandChildren = null;
    if (grandParent != null) {
        Node childNode = null;
        if (childNumber > 0) {
            childNode = grandParent.getChild(childNumber - 1);
        } else {
            int totalChildren = grandParent.getChildCount();
            childNode = totalChildren > 0 ? grandParent.getChild(totalChildren - 1) : null;
        }
        grandChildren = childNode == null ? null : childNode.getChildNodes();
    }
    return grandChildren;
}

From source file:de.catma.ui.client.ui.tagger.AffectedNodesFinder.java

License:Open Source License

/**
 * Recursively walks the tree depth first and creates a list of affected nodes.
 * @param curNode the (relative) root node to start with 
 *///from w ww .  j av  a  2  s.c om
private void walk(Node curNode) {

    // check if we're still within the affected subtree 
    // and the current node has any taggable content
    if (!this.outOfAffectedSubtree
            && ((curNode.getNodeValue() == null) || !curNode.getNodeValue().trim().isEmpty())) {

        // all text nodes gets added, in case we get into the affected subtree with this 
        // node or one of its children 
        if (curNode.getNodeType() == Node.TEXT_NODE) {
            affectedNodes.push(curNode);
        }

        // we check for children and go down the subtrees
        if (curNode.hasChildNodes()) {
            for (int i = 0; i < curNode.getChildCount(); i++) {
                walk(curNode.getChild(i));
            }
        }
        // if we reach the outer left node
        // we're in the affacted subtree -> all parent nodes can stay on the stack
        else if (curNode.equals(outerLeftNode)) {
            this.inAffectedSubtree = true;
        }
        // if we reach the outer right node
        // we reject all the rest of the upcoming nodes
        else if (curNode.equals(outerRightNode)) {
            this.outOfAffectedSubtree = true;
        }
        // if the current node is a text node it has already been pushed onto the stack
        // and if we're not within the affected subtree, we're removing the current node from the stack
        // (not being in the affected subtree means neither the current node nor one of its 
        //  children is the outer left node)
        if (!inAffectedSubtree && (curNode.getNodeType() == Node.TEXT_NODE)) {
            affectedNodes.pop();
        }
    }
}

From source file:de.catma.ui.client.ui.tagger.AffectedNodesFinder.java

License:Open Source License

/**
 * @param parent the parent node //from ww  w .jav  a  2s . c o m
 * @param child the child node
 * @return the position of the child node within the array of children 
 *          or -1 if there is no parent child relationship
 */
public static int indexOf(Node parent, Node child) {
    for (int i = 0; i < parent.getChildCount(); i++) {
        if (child.equals(parent.getChild(i))) {
            return i;
        }
    }
    return -1;
}

From source file:de.catma.ui.client.ui.tagger.editor.AffectedNodesFinder.java

License:Open Source License

/**
 * Recursively walks the tree depth first and creates a list of affected nodes.
 * @param curNode the (relative) root node to start with 
 *//*  w  w w  .j a va 2 s.c  o m*/
private void walk(Node curNode) {
    VConsole.log("AffectedNodesFinder walking node: " + DebugUtil.getNodeInfo(curNode));

    // check if we're still within the affected subtree 
    // and the current node has any taggable content
    if (!this.outOfAffectedSubtree
            && ((curNode.getNodeValue() == null) || !curNode.getNodeValue().trim().isEmpty())) {

        // all text nodes gets added, in case we get into the affected subtree with this 
        // node or one of its children 
        if (curNode.getNodeType() == Node.TEXT_NODE) {
            VConsole.log("AffectedNodesFinder pushing text node: " + DebugUtil.getNodeInfo(curNode));
            affectedNodes.push(curNode);
        } else {
            VConsole.log("no text node " + DebugUtil.getNodeInfo(curNode));
        }

        // we check for children and go down the subtrees
        if (curNode.hasChildNodes()) {
            VConsole.log("AffectedNodesFinder walking down: " + DebugUtil.getNodeInfo(curNode));
            for (int i = 0; i < curNode.getChildCount(); i++) {
                walk(curNode.getChild(i));
            }
        }
        // if we reach the outer left node
        // we're in the affacted subtree -> all parent nodes can stay on the stack
        else if (curNode.equals(outerLeftNode)) {
            VConsole.log("AffectedNodesFinder outer left node reached: " + DebugUtil.getNodeInfo(curNode));

            this.inAffectedSubtree = true;
        }
        // if we reach the outer right node
        // we reject all the rest of the upcoming nodes
        else if (curNode.equals(outerRightNode)) {
            VConsole.log("AffectedNodesFinder outer right node reached: " + DebugUtil.getNodeInfo(curNode));
            this.outOfAffectedSubtree = true;
        }
        // if the current node is a text node it has already been pushed onto the stack
        // and if we're not within the affected subtree, we're removing the current node from the stack
        // (not being in the affected subtree means neither the current node nor one of its 
        //  children is the outer left node)
        if (!inAffectedSubtree && (curNode.getNodeType() == Node.TEXT_NODE)) {
            Node poppedNode = affectedNodes.pop();
            VConsole.log("not in affected subtree, popped node " + DebugUtil.getNodeInfo(poppedNode));
        }
    }
}

From source file:de.catma.ui.client.ui.tagger.editor.LeafFinder.java

License:Open Source License

private Node descentLeft(Node node) {
    if (node.hasChildNodes()) {
        return descentLeft(node.getChild(node.getChildCount() - 1));
    }//ww w . j a va 2  s. c o m
    return node;
}