Java Code Examples for cn.hutool.core.util.StrUtil#cleanBlank()

The following examples show how to use cn.hutool.core.util.StrUtil#cleanBlank() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PostController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 更新
 *
 * @param post     post
 * @param cateList 分类目录
 * @param tagList  标签
 * @param session  session
 * @return JsonResult
 */
@PostMapping(value = "/update")
@ResponseBody
public JsonResult update(@ModelAttribute Post post,
                         @RequestParam("cateList") List<String> cateList,
                         @RequestParam("tagList") String tagList) {
    //old data
    final Post oldPost = postService.findByPostId(post.getPostId()).orElse(new Post());
    post.setPostUpdate(new Date());
    post.setPostViews(oldPost.getPostViews());
    post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
    post.setUser(oldPost.getUser());
    if (null == post.getPostDate()) {
        post.setPostDate(new Date());
    }
    //摘要字数
    int postSummary = 50;
    if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
        postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
    }
    //设置文章摘要
    final String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
    if (summaryText.length() > postSummary) {
        final String summary = summaryText.substring(0, postSummary);
        post.setPostSummary(summary);
    } else {
        post.setPostSummary(summaryText);
    }
    post = postService.buildCategoriesAndTags(post, cateList, tagList);
    //当没有选择文章缩略图的时候,自动分配一张内置的缩略图
    if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
        post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
    }
    post = postService.save(post);
    if (null != post) {
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.update-success"));
    } else {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.update-failed"));
    }
}
 
Example 2
Source File: PostServiceImpl.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 批量更新文章摘要
 *
 * @param postSummary postSummary
 */
@Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public void updateAllSummary(Integer postSummary) {
    final List<Post> posts = this.findAll(PostTypeEnum.POST_TYPE_POST.getDesc());
    for (Post post : posts) {
        String text = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
        if (text.length() > postSummary) {
            post.setPostSummary(text.substring(0, postSummary));
        } else {
            post.setPostSummary(text);
        }
        postRepository.save(post);
    }
}
 
Example 3
Source File: PostServiceImpl.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
/**
 * 批量更新文章摘要
 *
 * @param postSummary postSummary
 */
@Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public void updateAllSummary(Integer postSummary) {
    List<Post> posts = this.findAll(PostTypeEnum.POST_TYPE_POST.getDesc());
    for (Post post : posts) {
        String text = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
        if (text.length() > postSummary) {
            post.setPostSummary(text.substring(0, postSummary));
        } else {
            post.setPostSummary(text);
        }
        postRepository.save(post);
    }
}
 
Example 4
Source File: PostController.java    From stone with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 添加文章
 *
 * @param post     post
 * @param cateList 分类列表
 * @param tagList  标签
 * @param session  session
 */
@PostMapping(value = "/save")
@ResponseBody
public JsonResult save(@ModelAttribute Post post,
                       @RequestParam("cateList") List<String> cateList,
                       @RequestParam("tagList") String tagList,
                       HttpSession session) {
    final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
    try {
        post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
        //摘要字数
        int postSummary = 50;
        if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
            postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
        }
        //设置文章摘要
        final String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
        if (summaryText.length() > postSummary) {
            final String summary = summaryText.substring(0, postSummary);
            post.setPostSummary(summary);
        } else {
            post.setPostSummary(summaryText);
        }
        post.setPostDate(DateUtil.date());
        post.setPostUpdate(DateUtil.date());
        post.setUser(user);

        post = postService.buildCategoriesAndTags(post, cateList, tagList);
        post.setPostUrl(urlFilter(post.getPostUrl()));
        //当没有选择文章缩略图的时候,自动分配一张内置的缩略图
        if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
            post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
        }
        postService.save(post);
        logsService.save(LogsRecord.PUSH_POST, post.getPostTitle(), request);
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
    } catch (Exception e) {
        log.error("Save article failed: {}", e.getMessage());
        e.printStackTrace();
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
    }
}
 
Example 5
Source File: AdminController.java    From stone with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Markdown 导入
 *
 * @param file    file
 * @param request request
 *
 * @return JsonResult
 */
@PostMapping(value = "/tools/markdownImport")
@ResponseBody
public JsonResult markdownImport(@RequestParam("file") MultipartFile file,
                                 HttpServletRequest request,
                                 HttpSession session) throws IOException {
    final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
    final String markdown = IoUtil.read(file.getInputStream(), "UTF-8");
    final String content = MarkdownUtils.renderMarkdown(markdown);
    final Map<String, List<String>> frontMatters = MarkdownUtils.getFrontMatter(markdown);
    final Post post = new Post();
    List<String> elementValue = null;
    final List<Tag> tags = new ArrayList<>();
    final List<Category> categories = new ArrayList<>();
    Tag tag = null;
    Category category = null;
    if (frontMatters.size() > 0) {
        for (String key : frontMatters.keySet()) {
            elementValue = frontMatters.get(key);
            for (String ele : elementValue) {
                if ("title".equals(key)) {
                    post.setPostTitle(ele);
                } else if ("date".equals(key)) {
                    post.setPostDate(DateUtil.parse(ele));
                } else if ("updated".equals(key)) {
                    post.setPostUpdate(DateUtil.parse(ele));
                } else if ("tags".equals(key)) {
                    tag = tagService.findTagByTagName(ele);
                    if (null == tag) {
                        tag = new Tag();
                        tag.setTagName(ele);
                        tag.setTagUrl(ele);
                        tag = tagService.save(tag);
                    }
                    tags.add(tag);
                } else if ("categories".equals(key)) {
                    category = categoryService.findByCateName(ele);
                    if (null == category) {
                        category = new Category();
                        category.setCateName(ele);
                        category.setCateUrl(ele);
                        category.setCateDesc(ele);
                        category = categoryService.save(category);
                    }
                    categories.add(category);
                }
            }
        }
    } else {
        post.setPostDate(new Date());
        post.setPostUpdate(new Date());
        post.setPostTitle(file.getOriginalFilename());
    }
    post.setPostContentMd(markdown);
    post.setPostContent(content);
    post.setPostType(PostTypeEnum.POST_TYPE_POST.getDesc());
    post.setAllowComment(AllowCommentEnum.ALLOW.getCode());
    post.setUser(user);
    post.setTags(tags);
    post.setCategories(categories);
    post.setPostUrl(StrUtil.removeSuffix(file.getOriginalFilename(), ".md"));
    int postSummary = 50;
    if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
        postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
    }
    final String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
    if (summaryText.length() > postSummary) {
        final String summary = summaryText.substring(0, postSummary);
        post.setPostSummary(summary);
    } else {
        post.setPostSummary(summaryText);
    }
    if (null == post.getPostDate()) {
        post.setPostDate(new Date());
    }
    if (null == post.getPostUpdate()) {
        post.setPostUpdate(new Date());
    }
    postService.save(post);
    return new JsonResult(ResultCodeEnum.SUCCESS.getCode());
}
 
Example 6
Source File: PostController.java    From blog-sharon with Apache License 2.0 4 votes vote down vote up
/**
 * 添加文章
 *
 * @param post     post
 * @param cateList 分类列表
 * @param tagList  标签
 * @param session  session
 */
@PostMapping(value = "/save")
@ResponseBody
public JsonResult save(@ModelAttribute Post post,
                       @RequestParam("cateList") List<String> cateList,
                       @RequestParam("tagList") String tagList,
                       HttpSession session) {
    User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
    try {
        post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
        //摘要字数
        int postSummary = 80;
        if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
            postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
        }
        //设置文章摘要
        String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
        if (summaryText.length() > postSummary) {
            String summary = summaryText.substring(0, postSummary);
            post.setPostSummary(summary);
        } else {
            post.setPostSummary(summaryText);
        }
        post.setPostDate(DateUtil.date());
        post.setPostUpdate(DateUtil.date());
        post.setUser(user);

        post = postService.buildCategoriesAndTags(post, cateList, tagList);
        post.setPostUrl(urlFilter(post.getPostUrl()));
        //当没有选择文章缩略图的时候,自动分配一张内置的缩略图
        if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
            post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
        }
        postService.save(post);
        logsService.save(LogsRecord.PUSH_POST, post.getPostTitle(), request);
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
    } catch (Exception e) {
        log.error("Save article failed: {}", e.getMessage());
        e.printStackTrace();
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
    }
}
 
Example 7
Source File: PostController.java    From blog-sharon with Apache License 2.0 4 votes vote down vote up
/**
 * 更新
 *
 * @param post     post
 * @param cateList 分类目录
 * @param tagList  标签
 * @param session  session
 * @return JsonResult
 */
@PostMapping(value = "/update")
@ResponseBody
public JsonResult update(@ModelAttribute Post post,
                         @RequestParam("cateList") List<String> cateList,
                         @RequestParam("tagList") String tagList) {
    //old data
    Post oldPost = postService.findByPostId(post.getPostId()).orElse(new Post());
    post.setPostUpdate(new Date());
    post.setPostViews(oldPost.getPostViews());
    post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
    post.setUser(oldPost.getUser());
    if (null == post.getPostDate()) {
        post.setPostDate(new Date());
    }
    //摘要字数
    int postSummary = 50;
    if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
        postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
    }
    //设置文章摘要
    String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
    if (summaryText.length() > postSummary) {
        String summary = summaryText.substring(0, postSummary);
        post.setPostSummary(summary);
    } else {
        post.setPostSummary(summaryText);
    }
    post = postService.buildCategoriesAndTags(post, cateList, tagList);
    //当没有选择文章缩略图的时候,自动分配一张内置的缩略图
    if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
        post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
    }

    post = postService.save(post);
    if (null != post) {
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.update-success"));
    } else {
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.update-failed"));
    }
}
 
Example 8
Source File: AdminController.java    From blog-sharon with Apache License 2.0 4 votes vote down vote up
/**
 * Markdown 导入
 *
 * @param file    file
 * @param request request
 * @return JsonResult
 */
@PostMapping(value = "/tools/markdownImport")
@ResponseBody
public JsonResult markdownImport(@RequestParam("file") MultipartFile file,
                                 HttpServletRequest request,
                                 HttpSession session) throws IOException {
    User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
    String markdown = IoUtil.read(file.getInputStream(), "UTF-8");
    String content = MarkdownUtils.renderMarkdown(markdown);
    Map<String, List<String>> frontMatters = MarkdownUtils.getFrontMatter(markdown);
    Post post = new Post();
    List<String> elementValue = null;
    List<Tag> tags = new ArrayList<>();
    List<Category> categories = new ArrayList<>();
    Tag tag = null;
    Category category = null;
    if (frontMatters.size() > 0) {
        for (String key : frontMatters.keySet()) {
            elementValue = frontMatters.get(key);
            for (String ele : elementValue) {
                if ("title".equals(key)) {
                    post.setPostTitle(ele);
                } else if ("date".equals(key)) {
                    post.setPostDate(DateUtil.parse(ele));
                } else if ("updated".equals(key)) {
                    post.setPostUpdate(DateUtil.parse(ele));
                } else if ("tags".equals(key)) {
                    tag = tagService.findTagByTagName(ele);
                    if (null == tag) {
                        tag = new Tag();
                        tag.setTagName(ele);
                        tag.setTagUrl(ele);
                        tag = tagService.save(tag);
                    }
                    tags.add(tag);
                } else if ("categories".equals(key)) {
                    category = categoryService.findByCateName(ele);
                    if (null == category) {
                        category = new Category();
                        category.setCateName(ele);
                        category.setCateUrl(ele);
                        category.setCateDesc(ele);
                        category = categoryService.save(category);
                    }
                    categories.add(category);
                }
            }
        }
    } else {
        post.setPostDate(new Date());
        post.setPostUpdate(new Date());
        post.setPostTitle(file.getOriginalFilename());
    }
    post.setPostContentMd(markdown);
    post.setPostContent(content);
    post.setPostType(PostTypeEnum.POST_TYPE_POST.getDesc());
    post.setAllowComment(AllowCommentEnum.ALLOW.getCode());
    post.setUser(user);
    post.setTags(tags);
    post.setCategories(categories);
    post.setPostUrl(StrUtil.removeSuffix(file.getOriginalFilename(), ".md"));
    int postSummary = 50;
    if (StrUtil.isNotEmpty(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
        postSummary = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
    }
    String summaryText = StrUtil.cleanBlank(HtmlUtil.cleanHtmlTag(post.getPostContent()));
    if (summaryText.length() > postSummary) {
        String summary = summaryText.substring(0, postSummary);
        post.setPostSummary(summary);
    } else {
        post.setPostSummary(summaryText);
    }
    if (null == post.getPostDate()) {
        post.setPostDate(new Date());
    }
    if (null == post.getPostUpdate()) {
        post.setPostUpdate(new Date());
    }
    postService.save(post);
    return new JsonResult(ResultCodeEnum.SUCCESS.getCode());
}