Example usage for org.w3c.dom Document replaceChild

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

Introduction

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

Prototype

public Node replaceChild(Node newChild, Node oldChild) throws DOMException;

Source Link

Document

Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.

Usage

From source file:com.enonic.vertical.adminweb.UserHandlerServlet.java

public void handlerReport(HttpServletRequest request, HttpServletResponse response, HttpSession session,
        AdminService admin, ExtendedMap formItems, String subop)
        throws VerticalAdminException, VerticalEngineException {

    // User user = securityService.getAdminUser();
    UserStoreKey userStoreKey = new UserStoreKey(formItems.getInt("userstorekey"));
    UserStoreEntity userStore = userStoreDao.findByKey(userStoreKey);
    try {//from  w ww .  j av  a 2s . c  om
        if (subop.equals("create")) {

            ResourceKey stylesheetKey = new ResourceKey(formItems.getString("stylesheetkey"));
            ResourceFile res = resourceService.getResourceFile(stylesheetKey);
            if (res == null) {
                throw new StylesheetNotFoundException(stylesheetKey);
            }

            Document reportDoc;

            String selection = formItems.getString("selection");
            if ("all".equals(selection)) {
                List<UserEntity> users = securityService.findUsersByQuery(userStoreKey, null, null, true);
                UserXmlCreator userXmlCreator = new UserXmlCreator();
                org.jdom.Document usersDoc = userXmlCreator.createUsersDocument(users, false, false);
                reportDoc = XMLDocumentFactory.create(usersDoc).getAsDOMDocument();
            } else {
                String[] groupStringArray = formItems.getStringArray("group");

                boolean recursive = false;
                if (formItems.containsKey("recursivegroups")) {
                    recursive = true;
                }

                Set<UserEntity> users = new LinkedHashSet<UserEntity>();
                for (String groupKey : groupStringArray) {
                    // Global groups do not belong to any user store
                    GroupSpecification groupSpecification = new GroupSpecification();
                    groupSpecification.setKey(new GroupKey(groupKey));
                    GroupEntity groupEntity = groupDao.findSingleBySpecification(groupSpecification);

                    // Show global groups and groups in current user store
                    if (groupEntity.getUserStore() == null
                            || groupEntity.getUserStore().getKey().equals(userStoreKey)) {
                        Set<GroupEntity> userGroups;
                        if (recursive) {
                            userGroups = groupEntity.getAllMembersRecursively();
                        } else {
                            userGroups = groupEntity.getMembers(false);
                        }

                        for (GroupEntity userGroup : userGroups) {
                            if (userGroup.isOfType(GroupType.USER, false)) {
                                users.add(userGroup.getUser());
                            }
                        }

                    }
                }

                UserXmlCreator userXmlCreator = new UserXmlCreator();
                org.jdom.Document usersDoc = userXmlCreator
                        .createUsersDocument(new ArrayList<UserEntity>(users), false, false);
                reportDoc = XMLDocumentFactory.create(usersDoc).getAsDOMDocument();

                //                    reportDoc = XMLTool.domparse(admin.getGroupUserMembers(groups.toNativeArray(), recursive, true));
            }
            Element usersElem = reportDoc.getDocumentElement();
            String datasourcesDefaultResultElementName = verticalProperties
                    .getDatasourceDefaultResultRootElement();
            Element verticaldataElem = XMLTool.createElement(reportDoc, datasourcesDefaultResultElementName);
            reportDoc.replaceChild(verticaldataElem, usersElem);
            verticaldataElem.appendChild(usersElem);
            DOMSource reportSource = new DOMSource(reportDoc);

            XsltResource xslResource = new XsltResource(res.getDataAsXml().getAsString());
            XsltProcessorManager procManager = XsltProcessorManagerAccessor.getProcessorManager();
            XsltProcessor proc = procManager.createProcessor(xslResource, getStylesheetURIResolver(admin));
            proc.setParameter("datetoday", DateUtil.formatISODateTime(new Date()));

            response.setContentType(proc.getOutputMediaType() + "; charset=UTF-8");
            response.getWriter().write(proc.process(reportSource));

        } else {
            Map<String, Object> xslParams = new HashMap<String, Object>();
            xslParams.put("userstorename", userStore.getName());
            xslParams.put("userstorekey", String.valueOf(userStoreKey));
            xslParams.put("page", formItems.getString("page"));

            DOMSource xmlSource = new DOMSource(XMLTool.createDocument("foo"));
            Source xslSource = AdminStore.getStylesheet(session, "user_report.xsl");
            transformXML(session, response.getWriter(), xmlSource, xslSource, xslParams);
        }

    } catch (XsltProcessorException e) {
        String message = "Failed to transmform XML document: %t";
        VerticalAdminLogger.errorAdmin(this.getClass(), 0, message, e);
    } catch (TransformerException e) {
        VerticalAdminLogger.errorAdmin(this.getClass(), 10, "XSLT error: %t", e);
    } catch (IOException e) {
        VerticalAdminLogger.errorAdmin(this.getClass(), 20, "I/O error: %t", e);
    }
}