1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.tags.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.CharPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.blogs.model.BlogsEntry;
26  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
27  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
28  import com.liferay.portlet.imagegallery.model.IGImage;
29  import com.liferay.portlet.journal.model.JournalArticle;
30  import com.liferay.portlet.messageboards.model.MBMessage;
31  import com.liferay.portlet.tags.NoSuchEntryException;
32  import com.liferay.portlet.tags.model.TagsEntry;
33  import com.liferay.portlet.tags.model.TagsProperty;
34  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
35  import com.liferay.portlet.tags.service.TagsPropertyLocalServiceUtil;
36  import com.liferay.portlet.wiki.model.WikiPage;
37  
38  import java.util.List;
39  
40  /**
41   * <a href="TagsUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class TagsUtil {
46  
47      public static final String[] ASSET_TYPE_CLASS_NAMES = {
48          BlogsEntry.class.getName(), BookmarksEntry.class.getName(),
49          DLFileEntry.class.getName(), IGImage.class.getName(),
50          JournalArticle.class.getName(), MBMessage.class.getName(),
51          WikiPage.class.getName()
52      };
53  
54      public static final String[] ASSET_TYPE_PORTLET_IDS = {
55          PortletKeys.BLOGS, PortletKeys.BOOKMARKS, PortletKeys.DOCUMENT_LIBRARY,
56          PortletKeys.IMAGE_GALLERY, PortletKeys.JOURNAL,
57          PortletKeys.MESSAGE_BOARDS, PortletKeys.WIKI
58      };
59  
60      public static char[] INVALID_CHARACTERS = new char[] {
61          CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
62          CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
63          CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
64          CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
65          CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
66          CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
67          CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
68          CharPool.STAR, CharPool.TILDE
69      };
70  
71      public static boolean isValidWord(String word) {
72          if (Validator.isNull(word)) {
73              return false;
74          }
75          else {
76              char[] wordCharArray = word.toCharArray();
77  
78              for (char c : wordCharArray) {
79                  for (char invalidChar : INVALID_CHARACTERS) {
80                      if (c == invalidChar) {
81                          if (_log.isDebugEnabled()) {
82                              _log.debug(
83                                  "Word " + word + " is not valid because " + c +
84                                      " is not allowed");
85                          }
86  
87                          return false;
88                      }
89                  }
90              }
91          }
92  
93          return true;
94      }
95  
96      public static String substitutePropertyVariables(
97              long companyId, String entryName, String s)
98          throws PortalException, SystemException {
99  
100         String result = s;
101 
102         TagsEntry entry = null;
103 
104         if (entryName != null) {
105             try {
106                 entry = TagsEntryLocalServiceUtil.getEntry(
107                     companyId, entryName);
108             }
109             catch (NoSuchEntryException nsee) {
110             }
111         }
112 
113         if (entry != null) {
114             List<TagsProperty> properties =
115                 TagsPropertyLocalServiceUtil.getProperties(entry.getEntryId());
116 
117             for (TagsProperty property : properties) {
118                 result = StringUtil.replace(
119                     result, "[$" + property.getKey() + "$]",
120                     property.getValue());
121             }
122         }
123 
124         return StringUtil.stripBetween(result, "[$", "$]");
125     }
126 
127     public static String toWord(String text) {
128         if (Validator.isNull(text)) {
129             return text;
130         }
131         else {
132             char[] textCharArray = text.toCharArray();
133 
134             for (int i = 0; i < textCharArray.length; i++) {
135                 char c = textCharArray[i];
136 
137                 for (char invalidChar : INVALID_CHARACTERS) {
138                     if (c == invalidChar) {
139                         textCharArray[i] = CharPool.SPACE;
140 
141                         break;
142                     }
143                 }
144             }
145 
146             return new String(textCharArray);
147         }
148     }
149 
150     private static Log _log = LogFactoryUtil.getLog(TagsUtil.class);
151 
152 }