001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.nestedportlets.action;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.util.Validator;
025: import com.liferay.portal.model.LayoutTemplate;
026: import com.liferay.portal.model.Portlet;
027: import com.liferay.portal.model.Theme;
028: import com.liferay.portal.service.impl.LayoutTemplateLocalUtil;
029: import com.liferay.portal.struts.PortletAction;
030: import com.liferay.portal.theme.ThemeDisplay;
031: import com.liferay.portal.util.PropsValues;
032: import com.liferay.portal.util.WebKeys;
033: import com.liferay.portlet.PortletPreferencesFactoryUtil;
034:
035: import java.util.HashSet;
036: import java.util.Iterator;
037: import java.util.Set;
038: import java.util.regex.Matcher;
039: import java.util.regex.Pattern;
040:
041: import javax.portlet.PortletConfig;
042: import javax.portlet.PortletPreferences;
043: import javax.portlet.RenderRequest;
044: import javax.portlet.RenderResponse;
045:
046: import org.apache.struts.action.ActionForm;
047: import org.apache.struts.action.ActionForward;
048: import org.apache.struts.action.ActionMapping;
049:
050: /**
051: * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Berentey Zsolt
054: * @author Jorge Ferrer
055: *
056: */
057: public class ViewAction extends PortletAction {
058:
059: public ActionForward render(ActionMapping mapping, ActionForm form,
060: PortletConfig config, RenderRequest req, RenderResponse res)
061: throws Exception {
062:
063: ThemeDisplay themeDisplay = (ThemeDisplay) req
064: .getAttribute(WebKeys.THEME_DISPLAY);
065:
066: Portlet portlet = (Portlet) req
067: .getAttribute(WebKeys.RENDER_PORTLET);
068:
069: PortletPreferences prefs = PortletPreferencesFactoryUtil
070: .getPortletSetup(req, portlet.getPortletId(), true,
071: true);
072:
073: String layoutTemplateId = prefs.getValue("layout-template-id",
074: PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
075:
076: String content = StringPool.BLANK;
077:
078: if (Validator.isNotNull(layoutTemplateId)) {
079: Theme theme = themeDisplay.getTheme();
080:
081: LayoutTemplate layoutTemplate = LayoutTemplateLocalUtil
082: .getLayoutTemplate(layoutTemplateId, false, theme
083: .getThemeId());
084:
085: content = renameTemplateColumnsAndIds(layoutTemplate
086: .getContent(), portlet);
087: }
088:
089: req.setAttribute(WebKeys.LAYOUT_TEMPLATE_CONTENT, content);
090:
091: return mapping.findForward("portlet.nested_portlets.view");
092: }
093:
094: protected String renameTemplateColumnsAndIds(String content,
095: Portlet portlet) {
096:
097: Matcher m = _searchColumnsAndIdsPattern.matcher(content);
098:
099: Set columnIds = new HashSet();
100:
101: while (m.find()) {
102: if (Validator.isNotNull(m.group(1))) {
103: columnIds.add(m.group(1));
104: }
105:
106: if (Validator.isNotNull(m.group(2))) {
107: columnIds.add(m.group(2));
108: }
109: }
110:
111: Iterator itr = columnIds.iterator();
112:
113: while (itr.hasNext()) {
114: String columnId = (String) itr.next();
115:
116: if (columnId.indexOf(portlet.getPortletId()) == -1) {
117: content = content.replaceAll(columnId, portlet
118: .getPortletId()
119: + "_" + columnId);
120: }
121: }
122:
123: return content;
124: }
125:
126: private static final Pattern _searchColumnsAndIdsPattern = Pattern
127: .compile(
128: "processColumn[(]\"(.*?)\"[)]|[<].*?id=[\"']([^ ]*?)[\"'].*?[>]",
129: Pattern.DOTALL);
130:
131: }
|