View Javadoc
1   /**
2    * This file Copyright (c) 2009-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.templatingkit.style;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.module.templatingkit.templates.AbstractSTKTemplateModel;
39  import info.magnolia.module.templatingkit.templates.pages.STKPage;
40  import info.magnolia.module.templatingkit.templates.pages.STKPageModel;
41  import info.magnolia.rendering.model.RenderingModel;
42  import info.magnolia.rendering.template.TemplateDefinition;
43  
44  import java.util.ArrayList;
45  import java.util.Collections;
46  import java.util.List;
47  
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  
51  import org.apache.commons.lang3.StringUtils;
52  
53  
54  /**
55   * Define the Css to use.
56   */
57  public class CssSelectorBuilder {
58  
59  
60      /**
61       * Define the correct CssSelector to use.
62       */
63      @SuppressWarnings("rawtypes")
64      public String createCssSelector(AbstractSTKTemplateModel<? extends TemplateDefinition> model) {
65          final StringBuffer cssSelector = new StringBuffer();
66          RenderingModel<?> rootModelObj = model.getRoot();
67          if ((rootModelObj instanceof STKPageModel<?>)) {
68              final STKPageModel<?> rootModel = (STKPageModel<?>) rootModelObj;
69              final STKPage rootDef = rootModel.getDefinition();
70  
71              // body id
72              final String bodyId = rootDef.getBodyID();
73              if (StringUtils.isNotEmpty(bodyId)) {
74                  cssSelector.append("#").append(bodyId);
75              }
76  
77              // body class
78              final String bodyClass = rootModel.getBodyClass();
79              if (StringUtils.isNotEmpty(bodyClass)) {
80                  cssSelector.append(".").append(bodyClass);
81              }
82          }
83  
84          // area div id --> under the assumption that the collection has the same name as the div
85  
86          // find page node --> model.root.content does not work as the current paragraph might be inherited
87          Node page = model.getNode();
88          try {
89              while (!NodeUtil.isNodeType(page, NodeTypes.Page.NAME)) {
90                  page = page.getParent();
91              }
92          } catch (RepositoryException e) {
93              throw new RuntimeException(e);
94          }
95          final String pathToPage = NodeUtil.getPathIfPossible(page);
96          final String pathInPage = NodeUtil.getPathIfPossible(model.getNode()).substring(pathToPage.length());
97          final String areaDivId = pathInPageToAreaId(pathInPage);
98          if (StringUtils.isNotEmpty(areaDivId)) {
99              if (cssSelector.length() > 0) {
100                 cssSelector.append(" ");
101             }
102             cssSelector.append("#").append(areaDivId);
103         }
104 
105         List<RenderingModel> models = new ArrayList<RenderingModel>();
106         for (RenderingModel current = model; current != rootModelObj; current = current.getParent()) {
107             models.add(current);
108         }
109         Collections.reverse(models);
110 
111         // now add all div classes to the top model
112         for (RenderingModel currentModel : models) {
113             final String cssClass;
114             if (currentModel instanceof CssSelectorSupportRenderingModel) {
115                 cssClass = ((CssSelectorSupportRenderingModel) currentModel).getCssClass();
116             } else {
117                 final info.magnolia.rendering.template.RenderableDefinition definition = currentModel.getDefinition();
118                 cssClass = (String) definition.getParameters().get("divClass");
119             }
120             if (StringUtils.isNotEmpty(cssClass)) {
121                 if (cssSelector.length() > 0) {
122                     cssSelector.append(" ");
123                 }
124                 cssSelector.append(".").append(cssClass.replace(" ", "."));
125             }
126         }
127         return cssSelector.toString();
128     }
129 
130     protected String pathInPageToAreaId(final String pathInPage) {
131         final String areaDivId = StringUtils.substringBefore(StringUtils.removeStart(pathInPage, "/"), "/");
132         if (areaDivId.equals("opener")) {
133             return "main";
134         }
135         // the area containing the main components is named content but the div is named main
136         // we should add a div id parameter to the area to make that configurable
137         else if (areaDivId.equals("content")) {
138             return "main";
139         }
140         return areaDivId;
141     }
142 
143 }