Example usage for org.dom4j Element elementTextTrim

List of usage examples for org.dom4j Element elementTextTrim

Introduction

In this page you can find the example usage for org.dom4j Element elementTextTrim.

Prototype

String elementTextTrim(QName qname);

Source Link

Usage

From source file:de.fmaul.android.cmis.repo.CmisTypeDefinition.java

License:Apache License

public static CmisTypeDefinition createFromFeed(Document doc) {
    CmisTypeDefinition td = new CmisTypeDefinition();

    final Namespace CMISRA = Namespace.get("http://docs.oasis-open.org/ns/cmis/restatom/200908/");

    final QName CMISRA_TYPE = QName.get("type", CMISRA);
    Element type = doc.getRootElement().element(CMISRA_TYPE);

    td.id = type.elementTextTrim("id");
    td.localName = type.elementTextTrim("localName");
    td.localNamespace = type.elementTextTrim("localNamespace");
    td.displayName = type.elementTextTrim("displayName");
    td.queryName = type.elementTextTrim("queryName");
    td.description = type.elementTextTrim("description");
    td.baseId = type.elementTextTrim("baseId");
    td.creatable = Boolean.valueOf(type.elementTextTrim("creatable"));
    td.fileable = Boolean.valueOf(type.elementTextTrim("fileable"));
    td.queryable = Boolean.valueOf(type.elementTextTrim("queryable"));
    td.fulltextIndexed = Boolean.valueOf(type.elementTextTrim("fulltextIndexed"));
    td.includedInSupertypeQuery = Boolean.valueOf(type.elementTextTrim("includedInSupertypeQuery"));
    td.controllablePolicy = Boolean.valueOf(type.elementTextTrim("controllablePolicy"));
    td.controllableACL = Boolean.valueOf(type.elementTextTrim("controllableACL"));
    td.versionable = Boolean.valueOf(type.elementTextTrim("versionable"));
    td.contentStreamAllowed = type.elementTextTrim("contentStreamAllowed");

    List<Element> allElements = doc.getRootElement().element(CMISRA_TYPE).elements();
    for (Element element : allElements) {
        if (element.getName().startsWith("property")) {
            CmisPropertyTypeDefinition propTypeDef = CmisPropertyTypeDefinition.createFromElement(element);
            td.propertyDefinition.put(propTypeDef.getId(), propTypeDef);
        }/*from   w w  w .j a  v a2  s.  c o  m*/
    }

    return td;
}

From source file:edu.ncsa.sstde.indexing.IndexManager.java

License:Apache License

public void configure(InputStream file) {

    try {//w ww.j  a  v  a2 s .  c  o  m
        Document document = (new SAXReader()).read(file);
        @SuppressWarnings("unchecked")
        List<Element> indexerElms = document.getRootElement().elements("indexer");

        for (Element indexerElm : indexerElms) {
            String settingClass = indexerElm.elementText("setting-class").trim();
            //            Class.forName(settingClass);
            IndexerSettings indexerSettings = (IndexerSettings) Class.forName(settingClass).newInstance();
            Properties properties = new Properties();
            @SuppressWarnings("unchecked")
            List<Element> propElements = indexerElm.element("init").elements("property");
            for (Element propElm : propElements) {
                properties.put(propElm.attributeValue("name"), propElm.attributeValue("value"));
            }
            Element graphElm = indexerElm.element("indexGraph-setting");
            String pattern = graphElm.elementTextTrim("pattern");
            Collection<LiteralDef> literals = new ArrayList<LiteralDef>();
            for (Object literalDefElm : graphElm.elements("literal")) {
                Element literalDefElm2 = (Element) literalDefElm;
                literals.add(new LiteralDef(literalDefElm2.attributeValue("var"),
                        literalDefElm2.attributeValue("type")));
            }
            IndexGraph graph = new IndexGraph(pattern, literals);
            properties.put("index-graph", graph);
            properties.put("index-table", indexerElm.attributeValue("name"));
            indexerSettings.initProperties(properties);
            Indexer indexer = indexerSettings.createIndexer();
            indexer.setName(indexerElm.attributeValue("name"));
            getIndexers().add(indexer);
        }
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (MalformedQueryException e) {
        e.printStackTrace();
    } catch (UnsupportedQueryLanguageException e) {
        e.printStackTrace();
    }
}

From source file:info.smartkit.hairy_batman.plain.WxSogou.java

License:Apache License

public ArrayList<WxSogouSimple> getTitlesUrls() {
    // XML travel to title element,and push it to titles;
    titlesUrls = new ArrayList<WxSogouSimple>();
    for (Object item : this.items) {
        try {//from   ww w  .ja  v a  2  s.  c  o m
            Document doc = DocumentHelper.parseText(item.toString());
            Element root = doc.getRootElement();

            @SuppressWarnings("rawtypes")
            List aa = root.selectNodes("//item//display");

            @SuppressWarnings("rawtypes")
            Iterator iters = aa.iterator();
            //
            while (iters.hasNext()) {

                Element itemEle = (Element) iters.next();

                String title = itemEle.elementTextTrim("title");
                // String title1 = itemEle.elementTextTrim("title1");
                LOG.info("item title:" + title);
                String date = itemEle.elementTextTrim("date");
                LOG.info("item date:" + date);
                String url = itemEle.elementTextTrim("url");
                // String title1 = itemEle.elementTextTrim("title1");
                LOG.info("item url:" + url);
                // Store values.
                this.titlesUrls.add(new WxSogouSimple(title, date, url));
                // System.out.println("title1:" + title1);
            }
        } catch (DocumentException e) {
            // e.printStackTrace();
            LOG.error(e.toString());
        }
    }
    return this.titlesUrls;
}

From source file:itensil.repository.web.ShellServlet.java

License:Open Source License

/**
 *  /setProps//from   w ww  .j  a  va  2  s .  c o m
 *
 * Set node properties
 *
 */
@ContentType("text/xml")
public void webSetProps(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Document reqDoc = XMLDocument.readStream(request.getInputStream());
    Element root = reqDoc.getRootElement();
    String uri = root.attributeValue("uri");
    if (!(Check.isEmpty(uri) || uri.length() < 2)) {

        RepositoryHelper.beginTransaction();
        uri = RepositoryHelper.resolveUri(uri);
        MutableRepositoryNode node = getNode(uri);
        String name = UriHelper.name(node.getUri());

        Namespace davNs = root.getNamespaceForURI(PropertyHelper.DEFAULT_QNAMESPACE);

        // check rename
        String reqName = root.elementTextTrim(new org.dom4j.QName("displayname", davNs));
        if (!Check.isEmpty(reqName)) {
            if (!name.equals(reqName)) {
                String parUri = UriHelper.getParent(node.getUri());
                node.move(UriHelper.absoluteUri(parUri, UriHelper.filterName(reqName)));
            }
        }

        Namespace irNs = root.getNamespaceForURI(PropertyHelper.ITENSIL_QNAMESPACE);
        Element desc = root.element(new org.dom4j.QName("description", irNs));
        NodeProperties props = node.getProperties(new DefaultNodeVersion());
        if (props == null) {
            props = new DefaultNodeProperties(new DefaultNodeVersion());
        }
        props.setValue(PropertyHelper.itensilQName("description"), desc != null ? desc.getTextTrim() : "");
        node.setProperties(props);

        RepositoryHelper.commitTransaction();
        response.getWriter().print("<ok/>");

    } else {
        throw new NotFoundException("[blank]");
    }
}

From source file:itensil.security.web.CommunityAdmin.java

License:Open Source License

/**
 *  /create/*w  w w .  j a v a2  s.c om*/
 *
 *
 */
@ContentType("text/xml")
public void webCreate(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Document doc = XMLDocument.readStream(request.getInputStream());
    Element root = doc.getRootElement();
    if ("community".equals(root.getName())) {
        HibernateUtil.beginTransaction();

        String spaceName = root.elementTextTrim("name");

        // create userspace
        UserSpaceEntity uspace = UserSpaceEntity.createUserSpace(spaceName);
        uspace.setBaseUrl(root.elementTextTrim("baseUrl"));
        uspace.setBrand(root.elementTextTrim("brand"));
        uspace.setFeaturesStr(root.elementTextTrim("features"));
        HibernateUtil.getSession().update(uspace);

        HashSet<String> roles = new HashSet<String>();
        roles.add("admin");

        String userText = root.elementTextTrim("user");
        String email = root.elementTextTrim("email");
        String pass = root.elementTextTrim("pass");

        // create owner user
        User cUser = uspace.createUser(email, userText, pass, roles, Locale.getDefault(),
                TimeZone.getDefault());

        // generate editor group
        Group editGroup = uspace.createGroup("Editors", "Editors");
        uspace.addGroupUser(editGroup, cUser);

        // generate create group
        Group createGroup = uspace.createGroup("Creators", "Creators");

        // generate read group
        Group readGroup = uspace.createGroup("Readers", "Readers");

        // generate read group
        Group guestGroup = uspace.createGroup("Guests", "Guests");

        // make file system
        String mount = root.elementTextTrim("mount");
        RepositoryManager repoMan = RepositoryManagerFactory.getManager(cUser);

        // Repository repo
        repoMan.createRepository(mount, cUser, editGroup, createGroup, readGroup, guestGroup);
        repoMan.addRepositoryMount(mount, true);
        repoMan.addRepositoryMount("/system", false);

        // config default files
        SecurityAssociation.setUser(cUser);
        try {
            MutableRepositoryNode inColNode = (MutableRepositoryNode) RepositoryHelper.getNode("/system/.init",
                    false);
            if (inColNode.isCollection()) {
                for (RepositoryNode kid : inColNode.getChildren()) {

                    MutableRepositoryNode nKid = (MutableRepositoryNode) ((MutableRepositoryNode) kid)
                            .copy(UriHelper.absoluteUri(mount, UriHelper.name(kid.getUri())), true);

                    // add write to all non-collections
                    if (!kid.isCollection()) {
                        nKid.grantPermission(DefaultNodePermission.writePermission(createGroup));
                    }
                }
            }
        } catch (NotFoundException nfe) {
            log("Create warning.", nfe);
        } catch (AccessDeniedException ade) {
            log("Create warning.", ade);
        }

        HibernateUtil.commitTransaction();
        response.getWriter().print("<ok/>");
    }

}

From source file:itensil.security.web.CommunityAdmin.java

License:Open Source License

/**
 *  /setConfigProp//  w w  w.  jav a 2  s .c o m
 *
 *
 */
@ContentType("text/xml")
public void webSetConfigProp(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Document doc = XMLDocument.readStream(request.getInputStream());
    Element root = doc.getRootElement();
    if ("config-property".equals(root.getName())) {

        HibernateUtil.beginTransaction();

        Property prop = ConfigManager.getProperty(root.elementTextTrim("component"));
        if (prop != null) {
            Element pElem = root.element("properties");
            prop.setProperties(pElem.getText());
            HibernateUtil.commitTransaction();

            if ("mailer-default".equals(prop.getComponent())) {
                MailHoster mhost = (MailHoster) getServletContext().getAttribute("mailer-default");
                if (mhost != null) {
                    mhost.reloadMailService();
                }
            }
        } else {
            HibernateUtil.commitTransaction();
        }
        response.getWriter().print("<ok/>");
    }

}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /setUserMan//from www.j  a  v  a  2s  . co  m
 *
 *
 */
@ContentType("text/xml")
public void webSetUserMan(HttpServletRequest request, HttpServletResponse response) throws Exception {
    AuthenticatedUser self = (AuthenticatedUser) request.getUserPrincipal();
    HibernateUtil.beginTransaction();

    if (!self.getRoles().contains("admin"))
        throw new UserSpaceException("Admin role required");
    Document doc = XMLDocument.readStream(request.getInputStream());

    Element root = doc.getRootElement();
    if ("user".equals(root.getName())) {
        UserSpace uspace = self.getUserSpace();
        HibernateUtil.getSession().refresh(uspace);
        UserEntity uEnt = (UserEntity) uspace.resolve(new DefaultUser(root.attributeValue("id")));

        if (uEnt.getUserSpaceUsers().size() == 1) {
            uEnt.setUserName(root.elementTextTrim("email"));
            uEnt.setSimpleName(root.elementTextTrim("name"));
        }

        USpaceUserEntity uus = uEnt.getUserSpaceUsers().get(uspace);
        uus.setRoleStr(root.elementTextTrim("roles"));

        HibernateUtil.getSession().update(uus);
        HibernateUtil.getSession().update(uEnt);

        HibernateUtil.commitTransaction();
        response.getWriter().print("<ok/>");
    }
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /setSelf/*from w ww  .ja  v  a2 s .c om*/
 *
 *
 */
@ContentType("text/xml")
public void webSetSelf(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = (User) request.getUserPrincipal();
    Document doc = XMLDocument.readStream(request.getInputStream());
    Element root = doc.getRootElement();
    if ("user".equals(root.getName())) {
        HibernateUtil.beginTransaction();

        UserEntity uEnt = (UserEntity) user;

        uEnt.setUserName(root.elementTextTrim("email"));
        uEnt.setSimpleName(root.elementTextTrim("name"));
        uEnt.setFlagStr(root.elementTextTrim("flags"));
        uEnt.setTimezoneStr(root.elementTextTrim("timezone"));

        HibernateUtil.getSession().update(uEnt);

        HibernateUtil.commitTransaction();
        response.getWriter().print("<ok/>");
    }
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /setPass//from   w ww  .  jav a  2 s  .c  o  m
 *
 *
 */
@ContentType("text/xml")
public void webSetPass(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = (User) request.getUserPrincipal();
    Document doc = XMLDocument.readStream(request.getInputStream());
    Element root = doc.getRootElement();
    if ("pass".equals(root.getName())) {
        HibernateUtil.beginTransaction();
        UserEntity uEnt = (UserEntity) user;
        if (!Arrays.equals(uEnt.getPasswordHash(), SignOnHB.hashPassword(root.elementTextTrim("oldPass")))) {
            throw new UserSpaceException("Old password doesn't match.");
        }
        uEnt.setPasswordHash(SignOnHB.hashPassword(root.elementTextTrim("newPass")));
        HibernateUtil.getSession().update(uEnt);

        HibernateUtil.commitTransaction();
        response.getWriter().print("<ok/>");
    }
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /invite// w w  w. ja v  a2  s  .c  o m
 *
 *
 */
@ContentType("text/xml")
public void webInvite(HttpServletRequest request, HttpServletResponse response) throws Exception {

    User user = (User) request.getUserPrincipal();
    Document doc = XMLDocument.readStream(request.getInputStream());
    Element root = doc.getRootElement();
    if ("invite".equals(root.getName())) {
        MailService mailer = getMailer();
        InternetAddress fromAddr = new InternetAddress(
                mailer.getProperties().getProperty("alert.from.email", "alert@itensil.com"),
                mailer.getProperties().getProperty("alert.from.name", "Alert"));

        HibernateUtil.beginTransaction();
        UserSpace uspace = user.getUserSpace();
        String name = root.elementTextTrim("name");
        String email = root.elementTextTrim("email");
        boolean asGuest = "1".equals(root.elementTextTrim("guest"));
        boolean guestLog = "1".equals(root.elementTextTrim("log"));

        String password = root.elementTextTrim("password");

        String comment = root.elementText("comment");

        StringBuffer buf = new StringBuffer("You are invited to join ");
        buf.append(user.getSimpleName());
        buf.append("'s community.\n");
        buf.append(uspace.getBaseUrl());
        buf.append("\n\n");
        HibernateUtil.getSession().refresh(uspace);
        String pass = Check.isEmpty(password) ? PasswordGen.generatePassword() : password;
        HashSet<String> roles = new HashSet<String>(1);
        if (asGuest) {
            roles.add("guest");
            if (guestLog)
                roles.add("actlog");
        } else {
            roles.add("inviter");
        }
        UserEntity newUser = (UserEntity) uspace.createUser(email, name, pass, roles, user.getLocale(),
                user.getTimeZone());

        Group createGroup = null;
        Group editGroup = null;
        Group guestGroup = null;
        for (Group grp : uspace.getGroups()) {
            if ("Creators".equals(grp.getSimpleName()))
                createGroup = grp;
            else if ("Editors".equals(grp.getSimpleName()))
                editGroup = grp;
            else if ("Guests".equals(grp.getSimpleName()))
                guestGroup = grp;
        }
        if (asGuest) {
            if (guestGroup != null)
                uspace.addGroupUser(guestGroup, newUser);
        } else if (editGroup != null && user.isUserInGroup(editGroup)) {
            uspace.addGroupUser(editGroup, newUser);
        } else {
            if (createGroup != null)
                uspace.addGroupUser(createGroup, newUser);
        }

        // created users have non-null provider
        if (newUser != null) {
            buf.append("Username:\n");
            buf.append(email.trim());

            if (newUser.getUserSpaceUsers().size() == 1) {
                buf.append('\n');
                buf.append("\nPassword:\n");
                buf.append(pass.trim());
            } else {
                buf.append("\nPlease use your existing password for this community.");
            }
        }

        if (!Check.isEmpty(comment)) {
            buf.append("\n\n");
            buf.append(comment);
            buf.append('\n');
        }

        // email only a generated password

        HibernateUtil.commitTransaction();

        Document resDoc = DocumentHelper.createDocument();
        Element resRoot = resDoc.addElement("invite");
        resRoot.addAttribute("userId", newUser.getUserId());
        resRoot.addAttribute("email", email);
        resRoot.addElement("body").setText(buf.toString());

        resDoc.write(response.getWriter());
    }
}