1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class TagsUtil {
54  
55      public static final String[] ASSET_TYPE_CLASS_NAMES = {
56          BlogsEntry.class.getName(), BookmarksEntry.class.getName(),
57          DLFileEntry.class.getName(), IGImage.class.getName(),
58          JournalArticle.class.getName(), MBMessage.class.getName(),
59          WikiPage.class.getName()
60      };
61  
62      public static final String[] ASSET_TYPE_PORTLET_IDS = {
63          PortletKeys.BLOGS, PortletKeys.BOOKMARKS, PortletKeys.DOCUMENT_LIBRARY,
64          PortletKeys.IMAGE_GALLERY, PortletKeys.JOURNAL,
65          PortletKeys.MESSAGE_BOARDS, PortletKeys.WIKI
66      };
67  
68      public static char[] INVALID_CHARACTERS = new char[] {
69          CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
70          CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
71          CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
72          CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
73          CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
74          CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.QUESTION,
75          CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON, CharPool.SLASH,
76          CharPool.STAR, CharPool.TILDE
77      };
78  
79      public static boolean isValidWord(String word) {
80          if (Validator.isNull(word)) {
81              return false;
82          }
83          else {
84              char[] wordCharArray = word.toCharArray();
85  
86              for (char c : wordCharArray) {
87                  for (char invalidChar : INVALID_CHARACTERS) {
88                      if (c == invalidChar) {
89                          if (_log.isDebugEnabled()) {
90                              _log.debug(
91                                  "Word " + word + " is not valid because " + c +
92                                      " is not allowed");
93                          }
94  
95                          return false;
96                      }
97                  }
98              }
99          }
100 
101         return true;
102     }
103 
104     public static String substitutePropertyVariables(
105             long companyId, String entryName, String s)
106         throws PortalException, SystemException {
107 
108         String result = s;
109 
110         TagsEntry entry = null;
111 
112         if (entryName != null) {
113             try {
114                 entry = TagsEntryLocalServiceUtil.getEntry(
115                     companyId, entryName);
116             }
117             catch (NoSuchEntryException nsee) {
118             }
119         }
120 
121         if (entry != null) {
122             List<TagsProperty> properties =
123                 TagsPropertyLocalServiceUtil.getProperties(entry.getEntryId());
124 
125             for (TagsProperty property : properties) {
126                 result = StringUtil.replace(
127                     result, "[$" + property.getKey() + "$]",
128                     property.getValue());
129             }
130         }
131 
132         return StringUtil.stripBetween(result, "[$", "$]");
133     }
134 
135     public static String toWord(String text) {
136         if (Validator.isNull(text)) {
137             return text;
138         }
139         else {
140             char[] textCharArray = text.toCharArray();
141 
142             for (int i = 0; i < textCharArray.length; i++) {
143                 char c = textCharArray[i];
144 
145                 for (char invalidChar : INVALID_CHARACTERS) {
146                     if (c == invalidChar) {
147                         textCharArray[i] = CharPool.SPACE;
148 
149                         break;
150                     }
151                 }
152             }
153 
154             return new String(textCharArray);
155         }
156     }
157 
158     private static Log _log = LogFactoryUtil.getLog(TagsUtil.class);
159 
160 }