1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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  /**
46   * <a href="TagsUtil.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
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 }