Author: bpoussin Date: 2011-08-18 16:53:22 +0200 (Thu, 18 Aug 2011) New Revision: 310 Url: http://chorem.org/repositories/revision/bow/310 Log: Evolution #438: Refactore source code and simplify all - move import/export sspecifique method from BookmarkUtils to specifique action - add normalizeXXXX method in BowUtils to help to convert object to right object to storage - remove many not use any more method Modified: trunk/bow-ui/src/main/java/org/chorem/bow/BookmarkUtils.java trunk/bow-ui/src/main/java/org/chorem/bow/BowSearchResult.java trunk/bow-ui/src/main/java/org/chorem/bow/BowUtils.java trunk/bow-ui/src/main/java/org/chorem/bow/action/ExportBookmarksAction.java trunk/bow-ui/src/main/java/org/chorem/bow/action/ImportBookmarksAction.java trunk/bow-ui/src/main/java/org/chorem/bow/action/OpenSearchSuggestionAction.java Modified: trunk/bow-ui/src/main/java/org/chorem/bow/BookmarkUtils.java =================================================================== --- trunk/bow-ui/src/main/java/org/chorem/bow/BookmarkUtils.java 2011-08-18 10:15:48 UTC (rev 309) +++ trunk/bow-ui/src/main/java/org/chorem/bow/BookmarkUtils.java 2011-08-18 14:53:22 UTC (rev 310) @@ -29,8 +29,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; @@ -75,7 +73,7 @@ } if (StringUtils.isNotBlank(searchLine)) { - List<String> tags = BowUtils.getWords(searchLine); + Set<String> tags = BowUtils.getWords(searchLine); search.eq(BowBookmark.FQ_FIELD_WIKITTYLABEL_LABELS, tags); } @@ -142,101 +140,6 @@ } /** - * @param html String html - * @param name String name - * @param user User user - * @return Bookmark the bookmark filled - */ - public static BowBookmark createBookmarkFromHtml(String html, String name, BowUser user) { - String link = getUrlFromHtml(html); - String tags = getTagsFromHtml(html); - Date date = getDateFromHtml(html); - BowBookmark bookmark = createBookmark(link, name, tags, user, null, null, date); - return bookmark; - } - - /** - * @param html String html - * @return Date from the html - */ - public static Date getDateFromHtml(String html) { - Date date = null; - if (html != null) { - Pattern p = Pattern.compile("ADD_DATE=\"([^\"]*)\""); - Matcher m = p.matcher(html); - if (m.find()) { - String str = m.group(1); - if (str != null && !str.isEmpty()) { - long time = Long.valueOf(str) * 1000; // the date in ms - date = new Date(time); - } - } - } - return date; - } - - /** - * @param bookmarks List<Bookmarks> bookmarks - * @return String the html file - */ - public static String getExportHtmlBookmark(List<BowBookmark> bookmarks) { - String export; - export = "<!DOCTYPE NETSCAPE-Bookmark-file-1>\n" - + "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n" - + "<DL><p>\n"; - if (bookmarks != null) { - for (BowBookmark bookmark : bookmarks) { - if (bookmark != null) { - export += "<DT><A HREF=\""; - export += bookmark.getLink() + "\" "; - Date date = bookmark.getCreationDate(); - if (date != null) { - long milli = date.getTime(); - String time = String.valueOf(milli / 1000); - export += "ADD_DATE=\"" + time + "\" LAST_MODIFIED=\"" + time + "\" "; - } - export += "LAST_CHARSET=\"UTF-8\" "; - Set<String> tags = bookmark.getLabels(); - if (tags != null && !tags.isEmpty()) { - export += "SHORTCUTURL=\"" + getBookmarkTagsString(bookmark) + "\""; - } - export += ">" + bookmark.getDescription() + "</A>\n"; - } - } - } - export += "</DL><p>"; - return export; - } - - /** - * @param html String html - * @return url - */ - public static String getUrlFromHtml(String html) { - String url = ""; - if (html != null) { - Pattern p = Pattern.compile("A HREF=\"([^\"]*)\""); - Matcher m = p.matcher(html); - if (m.find()) { - url = m.group(1); - } - } - return url; - } - - public static String getTagsFromHtml(String html) { - String tags = ""; - if (html != null) { - Pattern p = Pattern.compile("SHORTCUTURL=\"([^\"]*)\""); - Matcher m = p.matcher(html); - if (m.find()) { - tags = m.group(1); - } - } - return tags; - } - - /** * @param url String which contains the bookmark url * @param nameAndTags String which contains the name and the tags of the * bookmark separated by '|' ==> name|tag1 tag2... @@ -245,53 +148,25 @@ */ public static BowBookmark createBookmark(String url, String nameAndTags, BowUser user) { - BowBookmark bookmark = new BowBookmarkImpl(); + // on recherche les tags qui doivent etre apres le dernier | + // ce qu'il y a devant est la description qui peut elle aussi contenir + // des | - int nameIndex = nameAndTags.indexOf('|'); // get the index name of the website + // on s'assure que la chaine est non null, et en enleve les blanc en trop + String name = BowUtils.normalizeString(nameAndTags); + String tags = ""; // par defaut on a pas de tag + + int nameIndex = name.lastIndexOf('|'); if (nameIndex > 0) { - String name = nameAndTags.substring(0, nameIndex); // get the website name - if (!name.isEmpty()) { - name = name.trim(); - bookmark.setDescription(name); // set the description (website name) - } + // il faut faire tags avant name, car on change name ensuite + tags = name.substring(nameIndex + 1); + name = name.substring(0, nameIndex); // get the website description } - String tags = nameAndTags; - if (nameIndex >= 0) { - tags = nameAndTags.substring(nameIndex + 1); // get tags, +1 because of '|' - } - addTagsToBookmark(BowUtils.getWords(tags), bookmark); - addUrlToBookmark(url, bookmark); - bookmark.setClick(0); - bookmark.setOwner(user.getWikittyId()); - bookmark.addReader(user.getWikittyId()); // only owner can read it - bookmark.setCreationDate(new Date()); // set the date + BowBookmark bookmark = createBookmark(url, name, tags, user, "", "", null); + return bookmark; } - public static void addUrlToBookmark(String url, BowBookmark bookmark) { - if (url != null && !url.isEmpty()) { - String link = url.trim(); - if (! link.matches("[a-z]+://.+") ) { - link = "http://" + link; - } - bookmark.setLink(link); - } else { - bookmark.setLink(""); - } - } - - public static void addTagsToBookmark(List<String> tagList, BowBookmark bookmark) { - if (tagList != null) { - for (String tag : tagList) { - if (StringUtils.isNotEmpty(tag)) { - // on peut ajouter plusieurs fois le meme tag, car on les - // stocke dans un Set - bookmark.addLabels(tag); - } - } - } - } - /** * Create new Bookmark * @@ -307,64 +182,33 @@ BowUser user, String privateAlias, String publicAlias, Date date) { BowBookmark bookmark = new BowBookmarkImpl(); - if (name != null) { - name = name.trim(); - bookmark.setDescription(name); - } else { - bookmark.setDescription(""); - } - addTagsToBookmark(BowUtils.getWords(tags), bookmark); + bookmark.setDescription(BowUtils.normalizeString(name)); + + bookmark.addAllLabels(BowUtils.getWords(tags)); + bookmark.setLink(BowUtils.normalizeUrl(url)); - addUrlToBookmark(url, bookmark); - bookmark.setClick(0); bookmark.setOwner(user.getWikittyId()); bookmark.addReader(user.getWikittyId()); // only owner can read it - if (date == null) { - date = new Date(); - } - bookmark.setCreationDate(date); // set the date - if (privateAlias != null) { - privateAlias = privateAlias.trim(); - bookmark.setPrivateAlias(privateAlias); - } else { - bookmark.setPrivateAlias(""); - } - if (publicAlias != null) { - publicAlias = publicAlias.trim(); - bookmark.setPublicAlias(publicAlias); - } else { - bookmark.setPublicAlias(""); - } + bookmark.setCreationDate(BowUtils.normalizeDate(date)); + + bookmark.setPrivateAlias(BowUtils.normalizeString(privateAlias)); + bookmark.setPublicAlias(BowUtils.normalizeString(publicAlias)); + return bookmark; } - public static void updateBookmarkName(BowBookmark bookmark, String name) { - if (name != null) { - name = name.trim(); - bookmark.setDescription(name); - } - } - public static void updateBookmark(BowBookmark bookmark, String name, String link, String tags, String privateAlias, String publicAlias) { - if (name != null) { - name = name.trim(); - bookmark.setDescription(name); - } - addUrlToBookmark(link, bookmark); - bookmark.clearLabels(); - addTagsToBookmark(BowUtils.getWords(tags), bookmark); - if (privateAlias != null) { - privateAlias = privateAlias.trim(); - } - bookmark.setPrivateAlias(privateAlias); - if (publicAlias != null) { - publicAlias = publicAlias.trim(); - } - bookmark.setPublicAlias(publicAlias); + bookmark.setDescription(BowUtils.normalizeString(name)); + + bookmark.setLink(BowUtils.normalizeUrl(link)); + bookmark.setLabels(BowUtils.getWords(tags)); + + bookmark.setPrivateAlias(BowUtils.normalizeString(privateAlias)); + bookmark.setPublicAlias(BowUtils.normalizeString(publicAlias)); } public static String getBookmarkTagsString(BowBookmark bookmark) { Modified: trunk/bow-ui/src/main/java/org/chorem/bow/BowSearchResult.java =================================================================== --- trunk/bow-ui/src/main/java/org/chorem/bow/BowSearchResult.java 2011-08-18 10:15:48 UTC (rev 309) +++ trunk/bow-ui/src/main/java/org/chorem/bow/BowSearchResult.java 2011-08-18 14:53:22 UTC (rev 310) @@ -114,9 +114,8 @@ // on ne met pas dans le cloud les tags qui ont servi pour la recherche List<FacetTopic> save = new ArrayList<FacetTopic>(tagsCloud); - // on met dans un Set pour optimiser la recherche - Set<String> tagsSearch = - new HashSet<String>(BowUtils.getWords(getSearchLine())); + // on recupere les tags dans un set pour optimiser la recherche + Set<String> tagsSearch = BowUtils.getWords(getSearchLine()); for(Iterator<FacetTopic> i=tagsCloud.iterator(); i.hasNext();) { FacetTopic topic = i.next(); @@ -163,7 +162,7 @@ return tagsCloud; } - public List<String> getTagsSearch() { + public Set<String> getTagsSearch() { return BowUtils.getWords(getSearchLine()); } Modified: trunk/bow-ui/src/main/java/org/chorem/bow/BowUtils.java =================================================================== --- trunk/bow-ui/src/main/java/org/chorem/bow/BowUtils.java 2011-08-18 10:15:48 UTC (rev 309) +++ trunk/bow-ui/src/main/java/org/chorem/bow/BowUtils.java 2011-08-18 14:53:22 UTC (rev 310) @@ -25,7 +25,9 @@ import java.util.Arrays; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.commons.lang.StringUtils; import org.nuiton.util.StringUtil; @@ -34,8 +36,10 @@ import org.nuiton.wikitty.search.Search; import java.util.UUID; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.time.DateFormatUtils; +import org.nuiton.util.DateUtil; public class BowUtils { @@ -45,17 +49,71 @@ } /** + * Retourne toujours une date valide. Si date est null, on prend la date + * du jour + * + * @param date null or valide date + * @return date if date not null, or today date + */ + static public Date normalizeDate(Date date) { + Date result = date; + if (result == null) { + result = new Date(); + } + return result; + } + + /** + * Supprime les blancs de debut et de fin, et retourne une chaine vide si s + * est null + * + * @param s null or string + * @return empty string or string without blank at begin and end + */ + static public String normalizeString(String s) { + String result = StringUtils.defaultIfBlank(s, ""); + result = result.trim(); + return result; + } + + /** + * Normalise l'url, si elle est null ou vide retourne une chaine vide. + * Si elle ne commence pas par un protocol (ex: ftp://) alors on ajoute + * automatiquement http:// + * + * @param url l'url a normaliser + * @return l'url normalisee + */ + public static String normalizeUrl(String url) { + String result = ""; + if (StringUtils.isNotBlank(url)) { + result = url.trim(); + if (! result.matches("[a-z]+://.+") ) { + result = "http://" + result; + } + } + return result; + } + + /** * Donne la liste des mots de la chaine passee en parametre. - * Si tags est null ou vide, le tableau retourne sera vide + * Si tags est null ou vide, le 'set' retourne sera vide. * + * La liste retournee ne doit jamais contenir de chaine null ou vide. + * * @param tags * @return */ - static public List<String> getWords(String tags) { + static public Set<String> getWords(String tags) { String [] words = StringUtils.split(tags); words = ArrayUtils.nullToEmpty(words); - List<String> result = Arrays.asList(words); + Set<String> result = new HashSet<String>(Arrays.asList(words)); + + // on s'assure que la liste ne retourne jamais de chaine null ou vide. + result.remove(""); + result.remove(null); + return result; } Modified: trunk/bow-ui/src/main/java/org/chorem/bow/action/ExportBookmarksAction.java =================================================================== --- trunk/bow-ui/src/main/java/org/chorem/bow/action/ExportBookmarksAction.java 2011-08-18 10:15:48 UTC (rev 309) +++ trunk/bow-ui/src/main/java/org/chorem/bow/action/ExportBookmarksAction.java 2011-08-18 14:53:22 UTC (rev 310) @@ -29,7 +29,10 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.io.Writer; +import java.util.Date; import java.util.List; +import java.util.Set; import javax.servlet.http.HttpServletResponse; @@ -62,6 +65,41 @@ } /** + * Export tous les bookmarks au format HTML tel que le fait les navigateurs + * + * @param bookmarks List<Bookmarks> bookmarks + * @param out le flux dans lequel il faut ecrire l'export + * @return String the html file + */ + protected void generateExportHtmlBookmark(List<BowBookmark> bookmarks, Writer out) throws IOException { + out.write("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n" + + "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n" + + "<DL><p>\n"); + if (bookmarks != null) { + for (BowBookmark bookmark : bookmarks) { + if (bookmark != null) { + out.write("<DT><A HREF=\""); + out.write(bookmark.getLink() + "\" "); + Date date = bookmark.getCreationDate(); + if (date != null) { + long milli = date.getTime(); + String time = String.valueOf(milli / 1000); + out.write("ADD_DATE=\"" + time + "\" LAST_MODIFIED=\"" + time + "\" "); + } + out.write("LAST_CHARSET=\"UTF-8\" "); + Set<String> tags = bookmark.getLabels(); + if (tags != null && !tags.isEmpty()) { + out.write("SHORTCUTURL=\"" + + BookmarkUtils.getBookmarkTagsString(bookmark) + "\""); + } + out.write(">" + bookmark.getDescription() + "</A>\n"); + } + } + } + out.write("</DL><p>"); + } + + /** * Exports the bookmarks in HTML format */ @Override @@ -77,7 +115,6 @@ List<BowBookmark> bookmarks = proxy.findAllByCriteria(BowBookmark.class, criteria).getAll(); // Export all - String export = BookmarkUtils.getExportHtmlBookmark(bookmarks); BufferedWriter out = null; try { @@ -87,11 +124,12 @@ tmp.deleteOnExit(); out = new BufferedWriter(new FileWriter(tmp)); - out.write(export); + generateExportHtmlBookmark(bookmarks, out); + out.close(); inputStream = new FileInputStream(tmp); - } catch (IOException e) { - log.error("Error when exporting bookmarks to HTML : " + e.getMessage(), e); + } catch (IOException eee) { + log.error("Error when exporting bookmarks to HTML : ", eee); } finally { IOUtils.closeQuietly(out); } Modified: trunk/bow-ui/src/main/java/org/chorem/bow/action/ImportBookmarksAction.java =================================================================== --- trunk/bow-ui/src/main/java/org/chorem/bow/action/ImportBookmarksAction.java 2011-08-18 10:15:48 UTC (rev 309) +++ trunk/bow-ui/src/main/java/org/chorem/bow/action/ImportBookmarksAction.java 2011-08-18 14:53:22 UTC (rev 310) @@ -28,7 +28,12 @@ import java.net.URL; import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang.StringUtils; import org.chorem.bow.BookmarkUtils; import org.chorem.bow.BowBookmark; @@ -100,11 +105,72 @@ this.upfileFileName = upfileFileName; } + /** + * @param html String html + * @param name String name + * @param user User user + * @return Bookmark the bookmark filled + */ + protected BowBookmark createBookmarkFromHtml(String html, String name, BowUser user) { + String link = getUrlFromHtml(html); + String tags = getTagsFromHtml(html); + Date date = getDateFromHtml(html); + BowBookmark bookmark = BookmarkUtils.createBookmark(link, name, tags, user, null, null, date); + return bookmark; + } + + /** + * @param html String html + * @return Date from the html + */ + protected Date getDateFromHtml(String html) { + Date date = null; + if (html != null) { + Pattern p = Pattern.compile("ADD_DATE=\"([^\"]*)\""); + Matcher m = p.matcher(html); + if (m.find()) { + String str = m.group(1); + if (str != null && !str.isEmpty()) { + long time = Long.valueOf(str) * 1000; // the date in ms + date = new Date(time); + } + } + } + return date; + } + + /** + * @param html String html + * @return url + */ + protected String getUrlFromHtml(String html) { + String url = ""; + if (html != null) { + Pattern p = Pattern.compile("A HREF=\"([^\"]*)\""); + Matcher m = p.matcher(html); + if (m.find()) { + url = m.group(1); + } + } + return url; + } + + protected String getTagsFromHtml(String html) { + String tags = ""; + if (html != null) { + Pattern p = Pattern.compile("SHORTCUTURL=\"([^\"]*)\""); + Matcher m = p.matcher(html); + if (m.find()) { + tags = m.group(1); + } + } + return tags; + } + protected void parseHtmlToBookmarks(NodeList list, BowUser user, Date date, - List<BowBookmark> bookmarks, List<String> tagList) + List<BowBookmark> bookmarks, Set<String> tagList) throws ParserException { - boolean isFolder = false; SimpleNodeIterator it = list.elements(); while (it.hasMoreNodes()) { @@ -115,21 +181,20 @@ String text = node.getText(); // The text in the head ==> // <text></toto> - if (text != null && text.startsWith("H3")) { // H3 = folder - if (plainText != null && !plainText.isEmpty()) { + if (StringUtils.startsWithIgnoreCase(text, "h3")) { // H3 = folder + if (StringUtils.isNotBlank(plainText)) { tagList.add(plainText); // Adds the folder name to the // tagList - isFolder = true; } - } else if (text != null && text.startsWith("A HREF")) { // HREF + } else if (StringUtils.startsWithIgnoreCase(text, "a href")) { // HREF // = new // bookmarks - BowBookmark bookmark = BookmarkUtils - .createBookmarkFromHtml(text, plainText, user); + BowBookmark bookmark = createBookmarkFromHtml(text, plainText, user); Wikitty w = getBowProxy().getWikitty(bookmark); BowImportHelper.addExtension(w); BowImportHelper.setImportDate(w, date); - BookmarkUtils.addTagsToBookmark(tagList, bookmark); + + bookmark.addAllLabels(tagList); bookmarks.add(bookmark); } @@ -137,16 +202,12 @@ if (children != null) { parseHtmlToBookmarks(children, user, date, bookmarks, - tagList); // If there is an under node = recursion + // On utilise une nouvelle liste pour les enfants + // pour eviter que les tags de tous les enfants se + // retrouve ensemble + new HashSet<String>(tagList)); // If there is an under node = recursion } } - if (isFolder) { // If we find a folder, we have to remove it - int index = tagList.size() - 1; - - if (index > -1) { - tagList.remove(index); - } - } } @Override @@ -167,7 +228,7 @@ List<BowBookmark> bookmarks = new ArrayList<BowBookmark>(); parseHtmlToBookmarks(list, user, new Date(), bookmarks, - new ArrayList<String>()); + new HashSet<String>()); proxy.store(bookmarks); addActionMessage(_("bow.bookmark.import.successful")); Modified: trunk/bow-ui/src/main/java/org/chorem/bow/action/OpenSearchSuggestionAction.java =================================================================== --- trunk/bow-ui/src/main/java/org/chorem/bow/action/OpenSearchSuggestionAction.java 2011-08-18 10:15:48 UTC (rev 309) +++ trunk/bow-ui/src/main/java/org/chorem/bow/action/OpenSearchSuggestionAction.java 2011-08-18 14:53:22 UTC (rev 310) @@ -26,6 +26,7 @@ import com.opensymphony.xwork2.ActionContext; import java.util.ArrayList; import java.util.List; +import java.util.Set; import org.chorem.bow.BowBookmark; import org.chorem.bow.OpenSearchActions; @@ -66,7 +67,7 @@ openSearchActions = new OpenSearchActions(); WikittyProxy proxy = getBowProxy(); - List<String> searchLineList = BowUtils.getWords(searchLine); + Set<String> searchLineList = BowUtils.getWords(searchLine); if (searchLine.charAt(searchLine.length() - 1) == ' ') { searchLineList.add(""); //If the user doesn't type anything, we have to propose suggestions