001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.deploy.sandbox;
016    
017    import com.liferay.portal.kernel.deploy.Deployer;
018    import com.liferay.portal.kernel.plugin.PluginPackage;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.TextFormatter;
021    import com.liferay.portal.kernel.xml.Document;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.Namespace;
024    import com.liferay.portal.kernel.xml.QName;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
027    
028    import java.io.File;
029    
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * @author Igor Spasic
037     * @author Brian Wing Shun Chan
038     */
039    public class PortletSandboxHandler extends BaseSandboxHandler {
040    
041            public PortletSandboxHandler(Deployer deployer) {
042                    super(deployer);
043    
044                    _deployer = deployer;
045            }
046    
047            @Override
048            protected void clonePlugin(File dir, PluginPackage pluginPackage)
049                    throws Exception {
050    
051                    Map<String, String> filterMap = new HashMap<String, String>();
052    
053                    filterMap.put(
054                            "portlet_class", "com.liferay.util.bridges.alloy.AlloyPortlet");
055                    filterMap.put("portlet_name", "1");
056                    filterMap.put("portlet_title", pluginPackage.getName());
057                    filterMap.put("restore_current_view", "false");
058                    filterMap.put(
059                            "friendly_url_mapper_class",
060                            "com.liferay.util.bridges.alloy.AlloyFriendlyURLMapper");
061                    filterMap.put(
062                            "friendly_url_mapping",
063                            TextFormatter.format(pluginPackage.getName(), TextFormatter.B));
064                    filterMap.put(
065                            "friendly_url_routes",
066                            "com/liferay/util/bridges/alloy/alloy-friendly-url-routes.xml");
067                    filterMap.put(
068                            "namespace",
069                            TextFormatter.format(pluginPackage.getName(), TextFormatter.D));
070                    filterMap.put(
071                            "package_path",
072                            "com.liferay." +
073                                    TextFormatter.format(pluginPackage.getName(), TextFormatter.B));
074    
075                    _deployer.copyDependencyXml(
076                            "liferay-display.xml", dir + "/WEB-INF", filterMap);
077    
078                    updateLiferayDisplayXML(dir);
079    
080                    _deployer.copyDependencyXml(
081                            "liferay-portlet.xml", dir + "/WEB-INF", filterMap);
082    
083                    _deployer.copyDependencyXml("portlet.xml", dir + "/WEB-INF", filterMap);
084    
085                    updatePortletXML(dir);
086    
087                    _deployer.copyDependencyXml("service.xml", dir + "/WEB-INF", filterMap);
088            }
089    
090            @Override
091            protected String getPluginType() {
092                    return _PLUGIN_TYPE;
093            }
094    
095            protected Namespace getPortletNamespace(Document document) {
096                    Element rootElement = document.getRootElement();
097    
098                    QName qName = rootElement.getQName();
099    
100                    return qName.getNamespace();
101            }
102    
103            protected void updateLiferayDisplayXML(File dir) throws Exception {
104                    File file = new File(dir + "/WEB-INF/liferay-display.xml");
105    
106                    String content = FileUtil.read(file);
107    
108                    Document document = UnsecureSAXReaderUtil.read(content);
109    
110                    Element rootElement = document.getRootElement();
111    
112                    Element categoryElement = rootElement.addElement("category");
113    
114                    categoryElement.addAttribute("name", "category.sandbox");
115    
116                    Element portletElement = categoryElement.addElement("portlet");
117    
118                    portletElement.addAttribute("id", "1");
119    
120                    FileUtil.write(file, document.formattedString());
121            }
122    
123            protected void updatePortletXML(File dir) throws Exception {
124                    File file = new File(dir + "/WEB-INF/portlet.xml");
125    
126                    String content = FileUtil.read(file);
127    
128                    Document document = UnsecureSAXReaderUtil.read(content);
129    
130                    Element rootElement = document.getRootElement();
131    
132                    List<Element> portletElements = rootElement.elements("portlet");
133    
134                    for (Element portletElement : portletElements) {
135                            List<Element> elements = new ArrayList<Element>();
136    
137                            Element resourceBundleElement = SAXReaderUtil.createElement(
138                                    SAXReaderUtil.createQName(
139                                            "resource-bundle", getPortletNamespace(document)));
140    
141                            resourceBundleElement.setText("content.Language");
142    
143                            for (Element element : portletElement.elements()) {
144                                    String elementName = element.getName();
145    
146                                    if (elementName.equals("init-param") ||
147                                            elementName.equals("portlet-info")) {
148                                    }
149                                    else {
150                                            elements.add(element);
151    
152                                            if (elementName.equals("supports")) {
153                                                    elements.add(resourceBundleElement);
154                                            }
155                                    }
156    
157                                    element.detach();
158                            }
159    
160                            for (Element element : elements) {
161                                    portletElement.add(element);
162                            }
163                    }
164    
165                    FileUtil.write(file, document.formattedString());
166            }
167    
168            private static final String _PLUGIN_TYPE = "portlet";
169    
170            private Deployer _deployer;
171    
172    }