1   /**
2    * Copyright (c) 2000-2008 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.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  /**
50   * <a href="TagsUtil.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
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 }