Example usage for org.w3c.dom Element getNextSibling

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

Introduction

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

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:org.gvnix.web.screen.roo.addon.WebScreenOperationsImpl.java

/**
 * Updates load-scripts.tagx adding in the right position some elements:
 * <ul>//from   www. j a  v  a  2  s.  com
 * <li><code>spring:url</code> elements for JS and CSS</li>
 * <li><code>link</code> element for CSS</li>
 * <li><code>script</code> element for JS</li>
 * </ul>
 */
private void modifyLoadScriptsTagx() {
    PathResolver pathResolver = projectOperations.getPathResolver();
    String loadScriptsTagx = pathResolver.getIdentifier(LogicalPath.getInstance(Path.SRC_MAIN_WEBAPP, ""),
            "WEB-INF/tags/util/load-scripts.tagx");

    if (!fileManager.exists(loadScriptsTagx)) {
        // load-scripts.tagx doesn't exist, so nothing to do
        return;
    }

    InputStream loadScriptsIs = fileManager.getInputStream(loadScriptsTagx);

    Document loadScriptsXml;
    try {
        loadScriptsXml = XmlUtils.getDocumentBuilder().parse(loadScriptsIs);
    } catch (Exception ex) {
        throw new IllegalStateException("Could not open load-scripts.tagx file", ex);
    }

    Element lsRoot = loadScriptsXml.getDocumentElement();

    Node nextSibiling;

    // spring:url elements
    Element testElement = XmlUtils.findFirstElement("/root/url[@var='pattern_css_url']", lsRoot);
    if (testElement == null) {
        Element urlPatternCss = new XmlElementBuilder("spring:url", loadScriptsXml)
                .addAttribute(VALUE, "/resources/styles/pattern.css").addAttribute(VAR, "pattern_css_url")
                .build();
        Element urlQlJs = new XmlElementBuilder("spring:url", loadScriptsXml)
                .addAttribute(VALUE, "/resources/scripts/quicklinks.js").addAttribute(VAR, "qljs_url").build();
        // Add i18n messages for quicklinks.js
        List<Element> qlJsI18n = new ArrayList<Element>();
        qlJsI18n.add(new XmlElementBuilder("spring:message", loadScriptsXml)
                .addAttribute("code", "message_selectrowtodelete_alert")
                .addAttribute(VAR, "msg_selectrowtodelete_alert").addAttribute("htmlEscape", "false").build());
        qlJsI18n.add(new XmlElementBuilder("spring:message", loadScriptsXml)
                .addAttribute("code", "message_selectrowtoupdate_alert")
                .addAttribute(VAR, "msg_selectrowtoupdate_alert").addAttribute("htmlEscape", "false").build());
        qlJsI18n.add(new XmlElementBuilder("spring:message", loadScriptsXml)
                .addAttribute("code", "message_updateonlyonerow_alert")
                .addAttribute(VAR, "msg_updateonlyonerow_alert").addAttribute("htmlEscape", "false").build());
        StringBuilder qlJsI18nScriptText = new StringBuilder("<!--\n");
        qlJsI18nScriptText.append("var GVNIX_MSG_SELECT_ROW_TO_DELETE=\"${msg_selectrowtodelete_alert}\";\n");
        qlJsI18nScriptText.append("var GVNIX_MSG_SELECT_ROW_TO_UPDATE=\"${msg_selectrowtoupdate_alert}\";\n");
        qlJsI18nScriptText.append("var GVNIX_MSG_UPDATE_ONLY_ONE_ROW=\"${msg_updateonlyonerow_alert}\";\n");
        qlJsI18nScriptText.append("-->\n");
        Element qlJsI18nScript = new XmlElementBuilder("script", loadScriptsXml)
                .setText(qlJsI18nScriptText.toString()).build();
        List<Element> springUrlElements = XmlUtils.findElements("/root/url", lsRoot);
        // Element lastSpringUrl = null;
        if (!springUrlElements.isEmpty()) {
            Element lastSpringUrl = springUrlElements.get(springUrlElements.size() - 1);
            if (lastSpringUrl != null) {
                nextSibiling = lastSpringUrl.getNextSibling().getNextSibling();
                lsRoot.insertBefore(urlPatternCss, nextSibiling);
                lsRoot.insertBefore(urlQlJs, nextSibiling);
                lsRoot.insertBefore(qlJsI18nScript, nextSibiling);
                for (Element item : qlJsI18n) {
                    lsRoot.insertBefore(item, qlJsI18nScript);
                }
            }
        } else {
            // Add at the end of the document
            lsRoot.appendChild(urlPatternCss);
            lsRoot.appendChild(urlQlJs);
            for (Element item : qlJsI18n) {
                lsRoot.appendChild(item);
            }
            lsRoot.appendChild(qlJsI18nScript);
        }
    }

    // pattern.css stylesheet element
    testElement = XmlUtils.findFirstElement("/root/link[@href='${pattern_css_url}']", lsRoot);
    if (testElement == null) {
        Element linkPatternCss = new XmlElementBuilder("link", loadScriptsXml).addAttribute("rel", "stylesheet")
                .addAttribute("type", "text/css").addAttribute("media", "screen")
                .addAttribute("href", "${pattern_css_url}").build();
        linkPatternCss.appendChild(loadScriptsXml.createComment(" required for FF3 and Opera "));
        Node linkTrundraCssNode = XmlUtils.findFirstElement("/root/link[@href='${tundra_url}']", lsRoot);
        if (linkTrundraCssNode != null) {
            nextSibiling = linkTrundraCssNode.getNextSibling().getNextSibling();
            lsRoot.insertBefore(linkPatternCss, nextSibiling);
        } else {
            // Add ass last link element
            // Element lastLink = null;
            List<Element> linkElements = XmlUtils.findElements("/root/link", lsRoot);
            if (!linkElements.isEmpty()) {
                Element lastLink = linkElements.get(linkElements.size() - 1);
                if (lastLink != null) {
                    nextSibiling = lastLink.getNextSibling().getNextSibling();
                    lsRoot.insertBefore(linkPatternCss, nextSibiling);
                }
            } else {
                // Add at the end of document
                lsRoot.appendChild(linkPatternCss);
            }
        }
    }

    // quicklinks.js script element
    testElement = XmlUtils.findFirstElement("/root/script[@src='${qljs_url}']", lsRoot);
    if (testElement == null) {
        Element scriptQlJs = new XmlElementBuilder("script", loadScriptsXml).addAttribute("src", "${qljs_url}")
                .addAttribute("type", "text/javascript").build();
        scriptQlJs.appendChild(loadScriptsXml.createComment(" required for FF3 and Opera "));
        List<Element> scrtiptElements = XmlUtils.findElements("/root/script", lsRoot);
        // Element lastScript = null;
        if (!scrtiptElements.isEmpty()) {
            Element lastScript = scrtiptElements.get(scrtiptElements.size() - 1);
            if (lastScript != null) {
                nextSibiling = lastScript.getNextSibling().getNextSibling();
                lsRoot.insertBefore(scriptQlJs, nextSibiling);
            }
        } else {
            // Add at the end of document
            lsRoot.appendChild(scriptQlJs);
        }
    }

    writeToDiskIfNecessary(loadScriptsTagx, loadScriptsXml.getDocumentElement());

}

From source file:org.gvnix.web.theme.roo.addon.ThemeOperationsImpl.java

/**
 * Updates load-scripts.tagx adding in the right position some elements:
 * <ul>/*from  ww w  . j  a  v  a 2  s . c  om*/
 * <li><code>spring:url</code> elements for JS and CSS</li>
 * <li><code>link</code> element for CSS</li>
 * <li><code>script</code> element for JS</li>
 * </ul>
 */
private void modifyLoadScriptsTagx() {
    PathResolver pathResolver = projectOperations.getPathResolver();
    String loadScriptsTagx = pathResolver.getIdentifier(LogicalPath.getInstance(Path.SRC_MAIN_WEBAPP, ""),
            "WEB-INF/tags/util/load-scripts.tagx");

    if (!fileManager.exists(loadScriptsTagx)) {
        // load-scripts.tagx doesn't exist, so nothing to do
        return;
    }

    InputStream loadScriptsIs = fileManager.getInputStream(loadScriptsTagx);

    Document loadScriptsXml;
    try {
        loadScriptsXml = org.springframework.roo.support.util.XmlUtils.getDocumentBuilder()
                .parse(loadScriptsIs);
    } catch (Exception ex) {
        throw new IllegalStateException("Could not open load-scripts.tagx file", ex);
    }

    Element lsRoot = loadScriptsXml.getDocumentElement();
    // Add new tag namesapces
    Element jspRoot = org.springframework.roo.support.util.XmlUtils.findFirstElement("/root", lsRoot);
    jspRoot.setAttribute("xmlns:util", "urn:jsptagdir:/WEB-INF/tags/util");

    Node nextSibiling;

    // spring:url elements
    Element testElement = org.springframework.roo.support.util.XmlUtils
            .findFirstElement("/root/url[@var='roo_css-ie_url']", lsRoot);
    if (testElement == null) {
        Element urlCitIECss = new XmlElementBuilder(SPRING_URL, loadScriptsXml)
                .addAttribute(VALUE_ATTRIBUTE, "/resources/styles/cit-IE.css")
                .addAttribute(VAR_ATTRIBUTE, "roo_css-ie_url").build();
        Element urlApplicationCss = new XmlElementBuilder(SPRING_URL, loadScriptsXml)
                .addAttribute(VALUE_ATTRIBUTE, "/resources/styles/application.css")
                .addAttribute(VAR_ATTRIBUTE, "application_css_url").build();
        Element urlYuiEventJs = new XmlElementBuilder(SPRING_URL, loadScriptsXml)
                .addAttribute(VALUE_ATTRIBUTE, "/resources/scripts/yui/yahoo-dom-event.js")
                .addAttribute(VAR_ATTRIBUTE, "yui_event").build();
        Element urlYuiCoreJs = new XmlElementBuilder(SPRING_URL, loadScriptsXml)
                .addAttribute(VALUE_ATTRIBUTE, "/resources/scripts/yui/container_core-min.js")
                .addAttribute(VAR_ATTRIBUTE, "yui_core").build();
        Element urlYoiMenuJs = new XmlElementBuilder(SPRING_URL, loadScriptsXml)
                .addAttribute(VALUE_ATTRIBUTE, "/resources/scripts/yui/menu-min.js")
                .addAttribute(VAR_ATTRIBUTE, "yui_menu").build();
        Element urlCitJs = new XmlElementBuilder(SPRING_URL, loadScriptsXml)
                .addAttribute(VALUE_ATTRIBUTE, "/resources/scripts/utils.js")
                .addAttribute(VAR_ATTRIBUTE, "cit_js_url").build();
        List<Element> springUrlElements = org.springframework.roo.support.util.XmlUtils
                .findElements("/root/url", lsRoot);
        // Element lastSpringUrl = null;
        if (!springUrlElements.isEmpty()) {
            Element lastSpringUrl = springUrlElements.get(springUrlElements.size() - 1);
            if (lastSpringUrl != null) {
                nextSibiling = lastSpringUrl.getNextSibling().getNextSibling();
                lsRoot.insertBefore(urlCitIECss, nextSibiling);
                lsRoot.insertBefore(urlApplicationCss, nextSibiling);
                lsRoot.insertBefore(urlYuiEventJs, nextSibiling);
                lsRoot.insertBefore(urlYuiCoreJs, nextSibiling);
                lsRoot.insertBefore(urlYoiMenuJs, nextSibiling);
                lsRoot.insertBefore(urlCitJs, nextSibiling);
            }
        } else {
            // Add at the end of the document
            lsRoot.appendChild(urlCitIECss);
            lsRoot.appendChild(urlApplicationCss);
            lsRoot.appendChild(urlYuiEventJs);
            lsRoot.appendChild(urlYuiCoreJs);
            lsRoot.appendChild(urlYoiMenuJs);
            lsRoot.appendChild(urlCitJs);
        }
    }

    Element setUserLocale = org.springframework.roo.support.util.XmlUtils.findFirstElement("/root/set/out",
            lsRoot);
    setUserLocale.setAttribute("default", "es");

    // cit-IE.css stylesheet element
    testElement = org.springframework.roo.support.util.XmlUtils
            .findFirstElement("/root/iecondition/link[@href='${roo_css-ie_url}']", lsRoot);
    if (testElement == null) {
        Element ifIE = new XmlElementBuilder("util:iecondition", loadScriptsXml).build();

        Element linkCitIECss = new XmlElementBuilder("link", loadScriptsXml).addAttribute("rel", "stylesheet")
                .addAttribute(TYPE_ATTRIBUTE, "text/css").addAttribute(HREF_ATTRIBUTE, "${roo_css-ie_url}")
                .build();
        ifIE.appendChild(linkCitIECss);
        Node linkFaviconNode = org.springframework.roo.support.util.XmlUtils
                .findFirstElement("/root/link[@href='${favicon}']", lsRoot);
        if (linkFaviconNode != null) {
            nextSibiling = linkFaviconNode.getNextSibling().getNextSibling();
            lsRoot.insertBefore(ifIE, linkFaviconNode);
        } else {
            // Add ass last link element
            // Element lastLink = null;
            List<Element> linkElements = org.springframework.roo.support.util.XmlUtils
                    .findElements("/root/link", lsRoot);
            if (!linkElements.isEmpty()) {
                Element lastLink = linkElements.get(linkElements.size() - 1);
                if (lastLink != null) {
                    nextSibiling = lastLink.getNextSibling().getNextSibling();
                    lsRoot.insertBefore(ifIE, nextSibiling);
                }
            } else {
                // Add at the end of document
                lsRoot.appendChild(ifIE);
            }
        }
    }

    // pattern.css stylesheet element
    testElement = org.springframework.roo.support.util.XmlUtils
            .findFirstElement("/root/link[@href='${application_css_url}']", lsRoot);
    if (testElement == null) {
        Element linkApplicationCss = new XmlElementBuilder("link", loadScriptsXml)
                .addAttribute("rel", "stylesheet").addAttribute(TYPE_ATTRIBUTE, "text/css")
                .addAttribute("media", "screen").addAttribute(HREF_ATTRIBUTE, "${application_css_url}").build();
        linkApplicationCss.appendChild(loadScriptsXml.createComment(FF3_AND_OPERA));
        Node linkFaviconNode = org.springframework.roo.support.util.XmlUtils
                .findFirstElement("/root/link[@href='${favicon}']", lsRoot);
        if (linkFaviconNode != null) {
            nextSibiling = linkFaviconNode.getNextSibling().getNextSibling();
            lsRoot.insertBefore(linkApplicationCss, linkFaviconNode);
        } else {
            // Add ass last link element
            // Element lastLink = null;
            List<Element> linkElements = org.springframework.roo.support.util.XmlUtils
                    .findElements("/root/link", lsRoot);
            if (!linkElements.isEmpty()) {
                Element lastLink = linkElements.get(linkElements.size() - 1);
                if (lastLink != null) {
                    nextSibiling = lastLink.getNextSibling().getNextSibling();
                    lsRoot.insertBefore(linkApplicationCss, nextSibiling);
                }
            } else {
                // Add at the end of document
                lsRoot.appendChild(linkApplicationCss);
            }
        }
    }

    // utils.js script element
    testElement = org.springframework.roo.support.util.XmlUtils
            .findFirstElement("/root/script[@src='${cit_js_url}']", lsRoot);
    if (testElement == null) {
        Element scriptYuiEventJs = new XmlElementBuilder(SCRIPT, loadScriptsXml)
                .addAttribute(SRC, "${yui_event}").addAttribute(TYPE_ATTRIBUTE, TEXT_JSCRPT).build();
        scriptYuiEventJs.appendChild(loadScriptsXml.createComment(FF3_AND_OPERA));
        Element scriptYuiCoreJs = new XmlElementBuilder(SCRIPT, loadScriptsXml).addAttribute(SRC, "${yui_core}")
                .addAttribute(TYPE_ATTRIBUTE, TEXT_JSCRPT).build();
        scriptYuiCoreJs.appendChild(loadScriptsXml.createComment(FF3_AND_OPERA));
        Element scriptYoiMenuJs = new XmlElementBuilder(SCRIPT, loadScriptsXml).addAttribute(SRC, "${yui_menu}")
                .addAttribute(TYPE_ATTRIBUTE, TEXT_JSCRPT).build();
        scriptYoiMenuJs.appendChild(loadScriptsXml.createComment(FF3_AND_OPERA));
        Element scriptCitJs = new XmlElementBuilder(SCRIPT, loadScriptsXml).addAttribute(SRC, "${cit_js_url}")
                .addAttribute(TYPE_ATTRIBUTE, TEXT_JSCRPT).build();
        scriptCitJs.appendChild(loadScriptsXml.createComment(FF3_AND_OPERA));
        List<Element> scrtiptElements = org.springframework.roo.support.util.XmlUtils
                .findElements("/root/script", lsRoot);
        // Element lastScript = null;
        if (!scrtiptElements.isEmpty()) {
            Element lastScript = scrtiptElements.get(scrtiptElements.size() - 1);
            if (lastScript != null) {
                nextSibiling = lastScript.getNextSibling().getNextSibling();
                lsRoot.insertBefore(scriptYuiEventJs, nextSibiling);
                lsRoot.insertBefore(scriptYuiCoreJs, nextSibiling);
                lsRoot.insertBefore(scriptYoiMenuJs, nextSibiling);
                lsRoot.insertBefore(scriptCitJs, nextSibiling);
            }
        } else {
            // Add at the end of document
            lsRoot.appendChild(scriptYuiEventJs);
            lsRoot.appendChild(scriptYuiCoreJs);
            lsRoot.appendChild(scriptYoiMenuJs);
            lsRoot.appendChild(scriptCitJs);
        }
    }

    writeToDiskIfNecessary(loadScriptsTagx, loadScriptsXml.getDocumentElement());

}

From source file:org.infoglue.cms.applications.managementtool.actions.ViewContentTypeDefinitionAction.java

/**
 * This method moves an content type asset key down one step.
 *//*  ww  w. j  a v a 2 s  . co  m*/

public String doMoveAssetKeyDown() throws Exception {
    this.initialize(getContentTypeDefinitionId());

    try {
        Document document = createDocumentFromDefinition();

        String attributesXPath = "/xs:schema/xs:simpleType[@name = '"
                + ContentTypeDefinitionController.ASSET_KEYS + "']/xs:restriction/xs:enumeration[@value='"
                + this.assetKey + "']";
        NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
        if (anl != null && anl.getLength() > 0) {
            Element element = (Element) anl.item(0);
            Node parentElement = element.getParentNode();
            Node nextSibling = element.getNextSibling();
            if (nextSibling != null) {
                parentElement.removeChild(nextSibling);
                parentElement.insertBefore(nextSibling, element);
            }
        }

        saveUpdatedDefinition(document);
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.initialize(getContentTypeDefinitionId());
    return USE_EDITOR;
}

From source file:org.infoglue.cms.applications.structuretool.actions.ViewSiteNodePageComponentsAction.java

/**
 * This method moves the component up a step if possible within the same slot. 
 *///from   w  w  w  .j  a  va 2  s.  c  om

public String doMoveComponent() throws Exception {
    initialize();

    String componentXML = getPageComponentsString(siteNodeId, this.masterLanguageVO.getId());
    //logger.info("componentXML:" + componentXML);

    Document document = XMLHelper.readDocumentFromByteArray(componentXML.getBytes("UTF-8"));
    String componentXPath = "//component[@id=" + this.componentId + "]";

    NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), componentXPath);
    if (anl.getLength() > 0) {
        Element component = (Element) anl.item(0);
        String name = component.getAttribute("name");
        //logger.info(XMLHelper.serializeDom(component, new StringBuffer()));
        Node parentNode = component.getParentNode();

        boolean hasChanged = false;

        if (this.direction.intValue() == 0) //Up
        {
            Node previousNode = component.getPreviousSibling();

            while (previousNode != null && previousNode.getNodeType() != Node.ELEMENT_NODE) {
                previousNode = previousNode.getPreviousSibling();
                //break;
            }

            Element element = ((Element) previousNode);
            while (element != null && !element.getAttribute("name").equalsIgnoreCase(name)) {
                previousNode = previousNode.getPreviousSibling();
                while (previousNode != null && previousNode.getNodeType() != Node.ELEMENT_NODE) {
                    previousNode = previousNode.getPreviousSibling();
                    //break;
                }
                element = ((Element) previousNode);
            }

            if (previousNode != null) {
                parentNode.removeChild(component);
                parentNode.insertBefore(component, previousNode);
                hasChanged = true;
            }
        } else if (this.direction.intValue() == 1) //Down
        {
            Node nextNode = component.getNextSibling();

            while (nextNode != null && nextNode.getNodeType() != Node.ELEMENT_NODE) {
                nextNode = nextNode.getNextSibling();
                break;
            }

            Element element = ((Element) nextNode);
            while (element != null && !element.getAttribute("name").equalsIgnoreCase(name)) {
                nextNode = nextNode.getNextSibling();
                element = ((Element) nextNode);
            }

            if (nextNode != null)
                nextNode = nextNode.getNextSibling();

            if (nextNode != null) {
                parentNode.removeChild(component);
                parentNode.insertBefore(component, nextNode);
                hasChanged = true;
            } else {
                parentNode.removeChild(component);
                parentNode.appendChild(component);
                hasChanged = true;
            }
        }

        if (hasChanged) {
            String modifiedXML = XMLHelper.serializeDom(document, new StringBuffer()).toString();
            //logger.info("modifiedXML:" + modifiedXML);

            ContentVO contentVO = NodeDeliveryController
                    .getNodeDeliveryController(siteNodeId, languageId, contentId)
                    .getBoundContent(this.getInfoGluePrincipal(), siteNodeId, this.masterLanguageVO.getId(),
                            true, "Meta information", DeliveryContext.getDeliveryContext());
            ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController()
                    .getLatestActiveContentVersionVO(contentVO.getId(), this.masterLanguageVO.getId());
            ContentVersionController.getContentVersionController().updateAttributeValue(
                    contentVersionVO.getContentVersionId(), "ComponentStructure", modifiedXML,
                    this.getInfoGluePrincipal());
        }
    }

    this.url = getComponentRendererUrl() + getComponentRendererAction() + "?siteNodeId=" + this.siteNodeId
            + "&languageId=" + this.languageId + "&contentId=" + this.contentId + "&focusElementId="
            + this.componentId + "&showSimple=" + this.showSimple;
    //this.getResponse().sendRedirect(url);      

    this.url = this.getResponse().encodeURL(url);
    this.getResponse().sendRedirect(url);
    return NONE;
}

From source file:org.infoglue.common.contenttypeeditor.actions.ViewContentTypeDefinitionAction.java

/**
 * This method moves an content type asset key down one step.
 *//*  w  ww.j  av a2s  . c o m*/

public String doMoveAssetKeyDown() throws Exception {
    this.initialize(getContentTypeDefinitionId());

    try {
        Document document = createDocumentFromDefinition();

        String attributesXPath = "/xs:schema/xs:simpleType[@name = '"
                + ContentTypeDefinitionController.ASSET_KEYS + "']/xs:restriction/xs:enumeration[@value='"
                + this.assetKey + "']";
        NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
        if (anl != null && anl.getLength() > 0) {
            Element element = (Element) anl.item(0);
            Node parentElement = element.getParentNode();
            Node nextSibling = element.getNextSibling();
            if (nextSibling != null) {
                parentElement.removeChild(nextSibling);
                parentElement.insertBefore(nextSibling, element);
            }
        }

        saveUpdatedDefinition(document);
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.initialize(getContentTypeDefinitionId());

    return UPDATED;
}

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

/**
 * @param doc//from   w w w. j av  a 2 s.co  m
 * @param widgetConfNode
 * @param updatePrefList
 * @throws JSONException
 */
public static void updateWidgetPrefNode(Document doc, Element widgetConfNode, JSONObject updatePrefList)
        throws JSONException {

    NodeList prefList = widgetConfNode.getElementsByTagName("WidgetPref");

    Iterator keys = updatePrefList.keys();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        JSONObject prefJson = updatePrefList.getJSONObject(id);
        if (!prefJson.has("name"))
            continue;

        String name = prefJson.getString("name");

        String datatype = "";
        if (prefJson.has("datatype"))
            datatype = prefJson.getString("datatype");

        String value = prefJson.getString("value");

        int prefLength = prefList.getLength();
        boolean update = false;
        for (int i = 0; i < prefLength; i++) {
            Element pref = (Element) prefList.item(i);
            if (!name.equals(pref.getAttribute("name")))
                continue;

            if ("xml".equals(datatype) || "json".equals(datatype)) {
                while (pref.getFirstChild() != null)
                    pref.removeChild(pref.getFirstChild());

                pref.appendChild(doc.createTextNode(value));
            } else {
                pref.setAttribute("value", value);
            }

            update = true;
        }

        // is this code require ?
        if (!update) {
            Element newPref = doc.createElement("WidgetPref");
            newPref.setAttribute("name", name);

            if ("xml".equals(datatype) || "json".equals(datatype)) {
                newPref.appendChild(doc.createTextNode(value));
            } else {
                newPref.setAttribute("value", value);
            }
            int lastPrefIndex = prefList.getLength() - 1;
            Element lastPref = (Element) prefList.item(lastPrefIndex);
            Element nextPrefNode = (Element) lastPref.getNextSibling();
            if (nextPrefNode != null) {
                widgetConfNode.insertBefore(newPref, nextPrefNode);
            } else {
                widgetConfNode.appendChild(newPref);
            }
        }
    }
}

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

/**
 * @param doc//from   w  w  w.j  av  a  2 s.  c  o m
 * @param widgetConfNode
 * @param updatePrefList
 * @throws JSONException
 */
private void updateUserPrefNode(Document doc, Element widgetConfNode, JSONObject updatePrefList)
        throws JSONException {

    NodeList prefList = widgetConfNode.getElementsByTagName("UserPref");

    Iterator keys = updatePrefList.keys();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        JSONObject prefJson = updatePrefList.getJSONObject(id);

        if (!prefJson.has("name"))
            continue;

        String name = prefJson.getString("name");

        if (!prefJson.has("default_value"))
            continue;
        String value = prefJson.getString("default_value");

        String datatype = null;
        if (prefJson.has("datatype"))
            datatype = prefJson.getString("datatype");

        int prefLength = prefList.getLength();
        boolean update = false;
        for (int i = 0; i < prefLength; i++) {
            Element pref = (Element) prefList.item(i);
            if (!name.equals(pref.getAttribute("name")))
                continue;

            if ("xml".equals(datatype) || "json".equals(datatype)) {
                while (pref.getFirstChild() != null)
                    pref.removeChild(pref.getFirstChild());

                pref.appendChild(doc.createTextNode(value));
            } else {
                pref.setAttribute("default_value", value);
            }

            update = true;
        }

        // is this code require ?
        // and this code is obsolete.  what is "inputType" attribute ?
        if (!update) {
            Element newPref = doc.createElement("UserPref");
            newPref.setAttribute("name", name);

            if (("xml".equals(datatype) || "json".equals(datatype))) {
                newPref.appendChild(doc.createTextNode(value));
            } else {
                newPref.setAttribute("default_value", value);
            }

            String inputType = prefJson.getString("inputType");
            if (inputType != null) {
                newPref.setAttribute("inputType", inputType);
            }
            String displayName = prefJson.getString("display_name");
            if (prefJson.has("display_name")) {
                newPref.setAttribute("display_name", displayName);
            }
            if (prefJson.has("EnumValue")) {
                JSONArray options = prefJson.getJSONArray("EnumValue");
                for (int j = 0; j < options.length(); j++) {
                    JSONObject option = options.getJSONObject(j);
                    Element optionNode = doc.createElement("EnumValue");
                    optionNode.setAttribute("display_value", option.getString("display_value"));
                    optionNode.setAttribute("value", option.getString("value"));
                    newPref.appendChild(optionNode);
                }
            }

            int lastPrefIndex = prefList.getLength() - 1;
            Element lastPref = (Element) prefList.item(lastPrefIndex);
            Element nextPrefNode = (Element) lastPref.getNextSibling();
            if (nextPrefNode != null) {
                widgetConfNode.insertBefore(newPref, nextPrefNode);
            } else {
                widgetConfNode.appendChild(newPref);
            }
        }
    }
}

From source file:org.jasig.portal.layout.dlm.DistributedLayoutManager.java

protected boolean canMoveNode(IUserLayoutNodeDescription node, IUserLayoutNodeDescription parent,
        String nextSiblingId) throws PortalException {
    // are we moving to a new parent?
    if (!getParentId(node.getId()).equals(parent.getId()))
        return node.isMoveAllowed() && canAddNode(node, parent, nextSiblingId);

    // same parent. which direction are we moving?
    Document uld = this.getUserLayoutDOM();
    Element parentE = uld.getElementById(parent.getId());
    Element child = (Element) parentE.getFirstChild();
    int idx = 0;//from w w  w .j av a2 s .  c o  m
    int nodeIdx = -1;
    int sibIdx = -1;

    while (child != null) {
        String id = child.getAttribute(Constants.ATT_ID);
        if (id.equals(node.getId()))
            nodeIdx = idx;
        if (id.equals(nextSiblingId))
            sibIdx = idx;
        idx++;
        child = (Element) child.getNextSibling();
    }
    if (nodeIdx == -1 || // couldn't find node
            (nextSiblingId != null && sibIdx == -1)) // couldn't find sibling
        return false;

    if (nodeIdx < sibIdx || // moving right
            sibIdx == -1) // appending to end
        return canMoveRight(node.getId(), nextSiblingId);
    return canMoveLeft(node.getId(), nextSiblingId);
}

From source file:org.jasig.portal.layout.dlm.PLFIntegrator.java

private static void applyChildChanges(Element plfParent, Element ilfParent, IntegrationResult result)
        throws PortalException {
    Element positions = null;/*ww w. ja v  a 2  s  . c o m*/
    Element node = (Element) plfParent.getFirstChild();

    while (node != null) {
        Element nextNode = (Element) node.getNextSibling();

        if (node.getNodeName().equals("folder"))
            mergeFolder(node, plfParent, ilfParent, result);
        else if (node.getNodeName().equals(Constants.ELM_POSITION_SET))
            positions = node;
        else if (node.getNodeName().equals("channel"))
            mergeChannel(node, plfParent, ilfParent, result);
        node = nextNode;
    }

    if (positions != null) {
        IntegrationResult posResult = new IntegrationResult();
        if (LOG.isInfoEnabled())
            LOG.info("applying positions");
        PositionManager.applyPositions(ilfParent, positions, posResult);
        if (posResult.changedILF == false) {
            if (LOG.isInfoEnabled())
                LOG.info("removing positionSet");
            plfParent.removeChild(positions);
            result.changedPLF = true;
        } else {
            result.changedILF = true;
            if (posResult.changedPLF)
                result.changedPLF = true;
        }
    }
}

From source file:org.jasig.portal.layout.dlm.PositionManager.java

/**
   This method trims down the position set to the position directives on
   the node info elements still having a position directive. Any directives
   that violated restrictions were removed from the node info objects so
   the position set should be made to match the order of those still
   having one.//  w ww .  ja va 2s .  c o  m
 */
static void adjustPositionSet(List<NodeInfo> order, Element positionSet, IntegrationResult result) {
    Node nodeToMatch = positionSet.getFirstChild();
    Element nodeToInsertBefore = positionSet.getOwnerDocument().createElement("INSERT_POINT");
    positionSet.insertBefore(nodeToInsertBefore, nodeToMatch);

    for (Iterator iter = order.iterator(); iter.hasNext();) {
        NodeInfo ni = (NodeInfo) iter.next();

        if (ni.positionDirective != null) {
            // found one check it against the current one in the position
            // set to see if it is different. If so then indicate that
            // something (the position set) has changed in the plf
            if (ni.positionDirective != nodeToMatch)
                result.changedPLF = true;

            // now bump the insertion point forward prior to
            // moving on to the next position node to be evaluated
            if (nodeToMatch != null)
                nodeToMatch = nodeToMatch.getNextSibling();

            // now insert it prior to insertion point
            positionSet.insertBefore(ni.positionDirective, nodeToInsertBefore);
        }
    }

    // now for any left over after the insert point remove them.

    while (nodeToInsertBefore.getNextSibling() != null)
        positionSet.removeChild(nodeToInsertBefore.getNextSibling());

    // now remove the insertion point
    positionSet.removeChild(nodeToInsertBefore);
}