Example usage for org.w3c.dom Element hasChildNodes

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

Introduction

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

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

From source file:org.infoscoop.service.SiteAggregationMenuService.java

private static void setElement2Buf(Element xml, StringBuffer buf) {
    buf.append("<").append(xml.getNodeName());
    NamedNodeMap attrs = xml.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        buf.append(" ").append(attr.getName()).append("=\"").append(XmlUtil.escapeXmlEntities(attr.getValue()))
                .append("\"");
    }/* w w w.ja  va 2 s .  c  om*/
    if (xml.hasChildNodes()) {
        buf.append(">").append(XmlUtil.escapeXmlEntities(xml.getFirstChild().getNodeValue())).append("</")
                .append(xml.getNodeName()).append(">");
    } else {
        buf.append("/>\n");
    }
}

From source file:org.jboss.pressgang.ccms.contentspec.builder.DocBookBuilder.java

/**
 * Creates the section component of a chapter.xml for a specific Level.
 *
 * @param buildData          Information and data structures for the build.
 * @param container          The level object to get content from.
 * @param doc                The document object that this container is to be added to.
 * @param parentNode         The parent XML node of this section.
 * @param parentFileLocation The parent file location, so any files can be saved in a subdirectory of the parents location.
 * @param flattenStructure   Whether or not the build should be flattened.
 * @throws BuildProcessingException Thrown if an unexpected error occurs during building.
 *///from  w  w w.  j  a v  a  2s .c o  m
protected void createContainerXML(final BuildData buildData, final Level container, final Document doc,
        final Element parentNode, final String parentFileLocation, final Boolean flattenStructure)
        throws BuildProcessingException {
    final List<org.jboss.pressgang.ccms.contentspec.Node> levelData = container.getChildNodes();

    // Get the name of the element based on the type
    final String elementName = container.getLevelType() == LevelType.PROCESS ? "chapter"
            : container.getLevelType().getTitle().toLowerCase(Locale.ENGLISH);
    final Element intro = doc.createElement(elementName + "intro");

    // Storage container to hold the levels so they can be added in proper order with the intro
    final LinkedList<Node> childNodes = new LinkedList<Node>();

    // Add the section and topics for this level to the chapter.xml
    for (final org.jboss.pressgang.ccms.contentspec.Node node : levelData) {
        // Check if the app should be shutdown
        if (isShuttingDown.get()) {
            return;
        }

        if (node instanceof Level && node.getParent() != null
                && (((Level) node).getParent().getLevelType() == LevelType.BASE
                        || ((Level) node).getParent().getLevelType() == LevelType.PART)
                && ((Level) node).getLevelType() != LevelType.INITIAL_CONTENT) {
            final Level childContainer = (Level) node;

            // Create a new file for the Chapter/Appendix
            final Element xiInclude = createSubRootContainerXML(buildData, doc, childContainer,
                    parentFileLocation, flattenStructure);
            if (xiInclude != null) {
                childNodes.add(xiInclude);
            }
        } else if (node instanceof Level && ((Level) node).getLevelType() == LevelType.INITIAL_CONTENT) {
            if (container.getLevelType() == LevelType.PART) {
                addLevelsInitialContent(buildData, (InitialContent) node, doc, intro, false);
            } else {
                addLevelsInitialContent(buildData, (InitialContent) node, doc, parentNode, false);
            }
        } else if (node instanceof Level) {
            final Level childLevel = (Level) node;

            // Create the section and its title
            final Element sectionNode = doc.createElement("section");
            setUpRootElement(buildData, childLevel, doc, sectionNode);

            // Ignore sections that have no spec topics
            if (!childLevel.hasSpecTopics() && !childLevel.hasCommonContents()) {
                if (buildData.getBuildOptions().isAllowEmptySections()) {
                    Element warning = doc.createElement("warning");
                    warning.setTextContent("No Content");
                    sectionNode.appendChild(warning);
                } else {
                    continue;
                }
            } else {
                // Add this sections child sections/topics
                createContainerXML(buildData, childLevel, doc, sectionNode, parentFileLocation,
                        flattenStructure);
            }

            childNodes.add(sectionNode);
        } else if (node instanceof CommonContent) {
            final CommonContent commonContent = (CommonContent) node;
            final Node xiInclude = XMLUtilities.createXIInclude(doc,
                    "Common_Content/" + commonContent.getFixedTitle());
            if (commonContent.getParent() != null
                    && commonContent.getParent().getLevelType() == LevelType.PART) {
                intro.appendChild(xiInclude);
            } else {
                childNodes.add(xiInclude);
            }
        } else if (node instanceof SpecTopic) {
            final SpecTopic specTopic = (SpecTopic) node;
            final Node topicNode = createTopicDOMNode(specTopic, doc, flattenStructure, parentFileLocation);

            // Add the node to the chapter
            if (topicNode != null) {
                if (specTopic.getParent() != null
                        && ((Level) specTopic.getParent()).getLevelType() == LevelType.PART) {
                    intro.appendChild(topicNode);
                } else {
                    childNodes.add(topicNode);
                }
            }
        }
    }

    // Add the child nodes and intro to the parent
    if (intro.hasChildNodes()) {
        parentNode.appendChild(intro);
    }

    for (final Node node : childNodes) {
        parentNode.appendChild(node);
    }
}

From source file:org.jboss.pressgang.ccms.contentspec.builder.DocBookBuilder.java

/**
 * Process a topic and add the section info information. This information consists of the keywordset information. The
 * keywords are populated using the tags assigned to the topic.
 *
 * @param buildData Information and data structures for the build.
 * @param topic     The Topic to create the sectioninfo for.
 * @param doc       The XML Document DOM object for the topics XML.
 *//*  w  w  w  . j a v  a 2s  .  com*/
protected void processTopicSectionInfo(final BuildData buildData, final BaseTopicWrapper<?> topic,
        final Document doc) {
    if (doc == null || topic == null)
        return;

    final String infoName;
    if (buildData.getDocBookVersion() == DocBookVersion.DOCBOOK_50) {
        infoName = "info";
    } else {
        infoName = DocBookUtilities.TOPIC_ROOT_SECTIONINFO_NODE_NAME;
    }

    final CollectionWrapper<TagWrapper> tags = topic.getTags();
    final List<Integer> seoCategoryIds = buildData.getServerSettings().getSEOCategoryIds();

    if (seoCategoryIds != null && !seoCategoryIds.isEmpty() && tags != null && tags.getItems() != null
            && tags.getItems().size() > 0) {
        // Find the sectioninfo node in the document, or create one if it doesn't exist
        final Element sectionInfo;
        final List<Node> sectionInfoNodes = XMLUtilities.getDirectChildNodes(doc.getDocumentElement(),
                infoName);
        if (sectionInfoNodes.size() == 1) {
            sectionInfo = (Element) sectionInfoNodes.get(0);
        } else {
            sectionInfo = doc.createElement(infoName);
        }

        // Build up the keywordset
        final Element keywordSet = doc.createElement("keywordset");

        final List<TagWrapper> tagItems = tags.getItems();
        for (final TagWrapper tag : tagItems) {
            if (tag.getName() == null || tag.getName().isEmpty())
                continue;

            if (tag.containedInCategories(seoCategoryIds)) {
                final Element keyword = doc.createElement("keyword");
                keyword.setTextContent(tag.getName());

                keywordSet.appendChild(keyword);
            }
        }

        // Only update the section info if we've added data
        if (keywordSet.hasChildNodes()) {
            sectionInfo.appendChild(keywordSet);

            DocBookUtilities.setInfo(buildData.getDocBookVersion(), sectionInfo, doc.getDocumentElement());
        }
    }
}

From source file:org.jbpcc.admin.beans.navigation.TreeBean.java

private void buildTree(DefaultMutableTreeNode rootTreeNode, Element docElement) {
    ((UrlNodeUserObject) rootTreeNode.getUserObject()).setText(getLabelResource(docElement));

    // get nodelist of elements & loop through items
    NodeList nodeList = docElement.getElementsByTagName(ELEM_MODULE);
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Element itemElement = (Element) nodeList.item(i);
            DefaultMutableTreeNode itemNode = createParentMenuItem(itemElement);

            if (itemElement.hasChildNodes()) {
                NodeList childNodeList = itemElement.getChildNodes();
                for (int j = 0; j < childNodeList.getLength(); j++) {
                    if (childNodeList.item(j).getNodeType() == Node.ELEMENT_NODE) {
                        Element childElement = (Element) childNodeList.item(j);
                        if (ELEM_PAGE.equals(childElement.getNodeName())) {
                            createChildMenuItem(itemNode, childElement);
                        }//from w w  w  .  j  a va  2s .  co  m
                    }
                }
            }
            rootTreeNode.add(itemNode);
        }
    }
}

From source file:org.jbpcc.admin.beans.navigation.TreeBean.java

private DefaultMutableTreeNode createParentMenuItem(Element itemElement) {
    DefaultMutableTreeNode menuItem = new DefaultMutableTreeNode();
    UrlNodeUserObject menuObject = new UrlNodeUserObject(menuItem, this);

    if (itemElement.hasChildNodes()) {
        menuObject.setExpanded(true);//from  w w w. j  a  v  a  2s .  c o  m
    } else {
        menuObject.setLeaf(true);
    }

    menuObject.setText(getLabelResource(itemElement));

    String value = itemElement.getAttribute(ATTR_URL);
    if (StringUtils.isNotBlank(value)) {
        value = APP_CONTEXT_PATH + value;
        menuObject.setUrl(value);
    }

    value = itemElement.getAttribute(ATTR_ICON);
    if (StringUtils.isNotBlank(value)) {
        menuObject.setIconUrl(value);
    }

    menuItem.setUserObject(menuObject);

    return menuItem;
}

From source file:org.jbpcc.admin.beans.navigation.TreeBean.java

private void createChildMenuItem(DefaultMutableTreeNode parentNode, Element childElement) {
    DefaultMutableTreeNode subNode = createParentMenuItem(childElement);
    NodeList childNodeList = childElement.getChildNodes();

    if (childElement.hasChildNodes()) {
        childNodeList = childElement.getChildNodes();
        for (int i = 0; i < childNodeList.getLength(); i++) {
            if (childNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
                createChildMenuItem(subNode, (Element) childNodeList.item(i));
            }//  w w  w. jav a  2s.  c  o  m
        }
    }

    parentNode.add(subNode);
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void copyChildNodes(SOAPElement target, Element source) throws SOAPException {
    // easy way out: no child nodes
    if (!source.hasChildNodes())
        return;/* w  w w.ja va  2  s  .  co  m*/
    // traverse child nodes
    for (Node child = source.getFirstChild(); child != null; child = child.getNextSibling()) {
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE: {
            copyChildElement(target, (Element) child);
            break;
        }
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE: {
            String text = child.getNodeValue();
            // drop whitespace-only text nodes
            if (!StringUtils.isWhitespace(text)) {
                target.addTextNode(text);
                if (traceEnabled)
                    log.trace("appended text: " + text);
            }
            break;
        }
        default:
            log.debug("discarding child: " + child);
        }
    }
}

From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java

private Node getBudgetElement(Element xmlElement) throws Exception {
    Node budgetNode = (Node) xmlElement;
    NodeList budgetAttachments = XPathAPI.selectNodeList(xmlElement,
            "//*[local-name(.) = 'BudgetAttachments']");
    if (budgetAttachments != null && budgetAttachments.getLength() > 0) {
        Element budgetAttachment = (Element) budgetAttachments.item(0);
        if (budgetAttachment.hasChildNodes()) {
            budgetNode = budgetAttachment.getFirstChild();
        }//  ww w  .  ja  v a  2s.  co  m
    }
    return budgetNode;
}

From source file:org.kuali.test.utils.JWebBrowserDocumentGenerator.java

private void populateIframes(JWebBrowser webBrowser, Document doc, Element element) {
    if (Constants.HTML_TAG_TYPE_IFRAME.equalsIgnoreCase(element.getTagName()) && !element.hasChildNodes()) {
        String id = element.getAttribute("id");
        String name = element.getAttribute("name");

        String iframeCall = getIframeContentCall(element, id, name);

        if (StringUtils.isNotBlank(iframeCall)) {
            Object o = webBrowser.executeJavascriptWithResult(iframeCall);
            if (o != null) {
                if (o.toString().contains("<html>")) {
                    element.appendChild(Utils.cleanHtml(o.toString()).getDocumentElement());
                } else {
                    element.appendChild(
                            Utils.cleanHtml("<html>" + o.toString() + "</html>").getDocumentElement());
                }//  www.  j  ava 2 s .  c o  m
            }
        }
    }

    for (Element e : Utils.getChildElements(element)) {
        populateIframes(webBrowser, doc, e);
    }
}

From source file:org.kuali.test.utils.Utils.java

/**
 *
 * @param node/*from  w w w  .j a  va2 s  .  co m*/
 * @param childTagMatch
 * @return
 */
public static boolean isChildTagMatchFailure(Element node, ChildTagMatch childTagMatch) {
    boolean retval = false;

    if ((childTagMatch != null) && (node != null)) {
        if (node.hasChildNodes()) {
            boolean foundone = false;
            Set<String> childTagNames = new HashSet<String>();

            StringTokenizer st = new StringTokenizer(childTagMatch.getChildTagName(), "|");

            while (st.hasMoreTokens()) {
                childTagNames.add(st.nextToken());
            }

            for (Element child : getChildElements(node)) {
                if (childTagNames.contains(child.getTagName())) {
                    foundone = true;
                    if (childTagMatch.getMatchAttributes() != null) {
                        if (childTagMatch.getMatchAttributes().sizeOfMatchAttributeArray() > 0) {
                            for (TagMatchAttribute att : childTagMatch.getMatchAttributes()
                                    .getMatchAttributeArray()) {
                                if ((att != null) && StringUtils.isNotBlank(att.getName())) {
                                    String childAttr = child.getAttribute(att.getName());
                                    if (StringUtils.isBlank(childAttr)) {
                                        retval = true;
                                    } else {
                                        int pos = att.getValue().indexOf('*');

                                        if (pos > -1) {
                                            if (pos == 0) {
                                                retval = !childAttr.endsWith(att.getValue().substring(1));
                                            } else {
                                                String s1 = att.getValue().substring(0, pos);
                                                String s2 = att.getValue().substring(pos + 1);

                                                retval = (!childAttr.startsWith(s1) || !childAttr.endsWith(s2));
                                            }
                                        } else {
                                            retval = !childAttr.equalsIgnoreCase(att.getValue());
                                        }
                                    }
                                    break;
                                } else {
                                    retval = true;
                                    break;
                                }
                            }
                            // if retval is false then we found a match so break
                            if (!retval) {
                                break;
                            }
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                }

                if ((retval || !foundone) && childTagMatch.getDeep()) {
                    retval = isChildTagMatchFailure(child, childTagMatch);
                    if (!retval) {
                        foundone = true;
                        break;
                    }
                }
            }

            if (!foundone) {
                retval = true;
            }
        } else {
            retval = true;
        }
    }

    return retval;
}