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