1
19
20 package com.liferay.portlet.tags.util;
21
22 import com.liferay.portal.PortalException;
23 import com.liferay.portal.SystemException;
24 import com.liferay.portal.kernel.log.Log;
25 import com.liferay.portal.kernel.log.LogFactoryUtil;
26 import com.liferay.portal.kernel.util.CharPool;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.util.PortletKeys;
30 import com.liferay.portlet.blogs.model.BlogsEntry;
31 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
32 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
33 import com.liferay.portlet.imagegallery.model.IGImage;
34 import com.liferay.portlet.journal.model.JournalArticle;
35 import com.liferay.portlet.messageboards.model.MBMessage;
36 import com.liferay.portlet.tags.NoSuchEntryException;
37 import com.liferay.portlet.tags.model.TagsEntry;
38 import com.liferay.portlet.tags.model.TagsProperty;
39 import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
40 import com.liferay.portlet.tags.service.TagsPropertyLocalServiceUtil;
41 import com.liferay.portlet.wiki.model.WikiPage;
42
43 import java.util.List;
44
45
51 public class TagsUtil {
52
53 public static final String[] ASSET_TYPE_CLASS_NAMES = {
54 BlogsEntry.class.getName(), BookmarksEntry.class.getName(),
55 DLFileEntry.class.getName(), IGImage.class.getName(),
56 JournalArticle.class.getName(), MBMessage.class.getName(),
57 WikiPage.class.getName()
58 };
59
60 public static final String[] ASSET_TYPE_PORTLET_IDS = {
61 PortletKeys.BLOGS, PortletKeys.BOOKMARKS, PortletKeys.DOCUMENT_LIBRARY,
62 PortletKeys.IMAGE_GALLERY, PortletKeys.JOURNAL,
63 PortletKeys.MESSAGE_BOARDS, PortletKeys.WIKI
64 };
65
66 public static char[] INVALID_CHARACTERS = new char[] {
67 CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
68 CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
69 CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
70 CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
71 CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
72 CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
73 CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
74 CharPool.STAR, CharPool.TILDE
75 };
76
77 public static boolean isValidWord(String word) {
78 if (Validator.isNull(word)) {
79 return false;
80 }
81 else {
82 char[] wordCharArray = word.toCharArray();
83
84 for (char c : wordCharArray) {
85 for (char invalidChar : INVALID_CHARACTERS) {
86 if (c == invalidChar) {
87 if (_log.isDebugEnabled()) {
88 _log.debug(
89 "Word " + word + " is not valid because " + c +
90 " is not allowed");
91 }
92
93 return false;
94 }
95 }
96 }
97 }
98
99 return true;
100 }
101
102 public static String substitutePropertyVariables(
103 long companyId, String entryName, String s)
104 throws PortalException, SystemException {
105
106 String result = s;
107
108 TagsEntry entry = null;
109
110 if (entryName != null) {
111 try {
112 entry = TagsEntryLocalServiceUtil.getEntry(
113 companyId, entryName);
114 }
115 catch (NoSuchEntryException nsee) {
116 }
117 }
118
119 if (entry != null) {
120 List<TagsProperty> properties =
121 TagsPropertyLocalServiceUtil.getProperties(entry.getEntryId());
122
123 for (TagsProperty property : properties) {
124 result = StringUtil.replace(
125 result, "[$" + property.getKey() + "$]",
126 property.getValue());
127 }
128 }
129
130 return StringUtil.stripBetween(result, "[$", "$]");
131 }
132
133 public static String toWord(String text) {
134 if (Validator.isNull(text)) {
135 return text;
136 }
137 else {
138 char[] textCharArray = text.toCharArray();
139
140 for (int i = 0; i < textCharArray.length; i++) {
141 char c = textCharArray[i];
142
143 for (char invalidChar : INVALID_CHARACTERS) {
144 if (c == invalidChar) {
145 textCharArray[i] = CharPool.SPACE;
146
147 break;
148 }
149 }
150 }
151
152 return new String(textCharArray);
153 }
154 }
155
156 private static Log _log = LogFactoryUtil.getLog(TagsUtil.class);
157
158 }