1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.CharPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.blogs.model.BlogsEntry;
34  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
35  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.journal.model.JournalArticle;
38  import com.liferay.portlet.messageboards.model.MBMessage;
39  import com.liferay.portlet.tags.NoSuchEntryException;
40  import com.liferay.portlet.tags.model.TagsEntry;
41  import com.liferay.portlet.tags.model.TagsProperty;
42  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
43  import com.liferay.portlet.tags.service.TagsPropertyLocalServiceUtil;
44  import com.liferay.portlet.wiki.model.WikiPage;
45  
46  import java.util.List;
47  
48  /**
49   * <a href="TagsUtil.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class TagsUtil {
55  
56      public static final String[] ASSET_TYPE_CLASS_NAMES = {
57          BlogsEntry.class.getName(), BookmarksEntry.class.getName(),
58          DLFileEntry.class.getName(), IGImage.class.getName(),
59          JournalArticle.class.getName(), MBMessage.class.getName(),
60          WikiPage.class.getName()
61      };
62  
63      public static final String[] ASSET_TYPE_PORTLET_IDS = {
64          PortletKeys.BLOGS, PortletKeys.BOOKMARKS, PortletKeys.DOCUMENT_LIBRARY,
65          PortletKeys.IMAGE_GALLERY, PortletKeys.JOURNAL,
66          PortletKeys.MESSAGE_BOARDS, PortletKeys.WIKI
67      };
68  
69      public static char[] INVALID_CHARACTERS = new char[] {
70          CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
71          CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
72          CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
73          CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
74          CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
75          CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
76          CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
77          CharPool.STAR, CharPool.TILDE
78      };
79  
80      public static boolean isValidWord(String word) {
81          if (Validator.isNull(word)) {
82              return false;
83          }
84          else {
85              char[] wordCharArray = word.toCharArray();
86  
87              for (char c : wordCharArray) {
88                  for (char invalidChar : INVALID_CHARACTERS) {
89                      if (c == invalidChar) {
90                          if (_log.isDebugEnabled()) {
91                              _log.debug(
92                                  "Word " + word + " is not valid because " + c +
93                                      " is not allowed");
94                          }
95  
96                          return false;
97                      }
98                  }
99              }
100         }
101 
102         return true;
103     }
104 
105     public static String substitutePropertyVariables(
106             long companyId, String entryName, String s)
107         throws PortalException, SystemException {
108 
109         String result = s;
110 
111         TagsEntry entry = null;
112 
113         if (entryName != null) {
114             try {
115                 entry = TagsEntryLocalServiceUtil.getEntry(
116                     companyId, entryName);
117             }
118             catch (NoSuchEntryException nsee) {
119             }
120         }
121 
122         if (entry != null) {
123             List<TagsProperty> properties =
124                 TagsPropertyLocalServiceUtil.getProperties(entry.getEntryId());
125 
126             for (TagsProperty property : properties) {
127                 result = StringUtil.replace(
128                     result, "[$" + property.getKey() + "$]",
129                     property.getValue());
130             }
131         }
132 
133         return StringUtil.stripBetween(result, "[$", "$]");
134     }
135 
136     public static String toWord(String text) {
137         if (Validator.isNull(text)) {
138             return text;
139         }
140         else {
141             char[] textCharArray = text.toCharArray();
142 
143             for (int i = 0; i < textCharArray.length; i++) {
144                 char c = textCharArray[i];
145 
146                 for (char invalidChar : INVALID_CHARACTERS) {
147                     if (c == invalidChar) {
148                         textCharArray[i] = CharPool.SPACE;
149 
150                         break;
151                     }
152                 }
153             }
154 
155             return new String(textCharArray);
156         }
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(TagsUtil.class);
160 
161 }