View Javadoc

1   /**
2    * This file Copyright (c) 2009-2012 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.lang.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       * @param model
63       * @return
64       */
65      @SuppressWarnings("rawtypes")
66      public String createCssSelector(AbstractSTKTemplateModel<? extends TemplateDefinition>  model){
67          final StringBuffer cssSelector = new StringBuffer();
68          RenderingModel<?> rootModelObj = model.getRoot();
69          if((rootModelObj instanceof STKPageModel<?>)){
70              final STKPageModel<?> rootModel = (STKPageModel<?>) rootModelObj;
71              final STKPage rootDef = rootModel.getDefinition();
72  
73              // body id
74              final String bodyId = rootDef.getBodyID();
75              if(StringUtils.isNotEmpty(bodyId)){
76                  cssSelector.append("#").append(bodyId);
77              }
78  
79              // body class
80              final String bodyClass = rootModel.getBodyClass();
81              if(StringUtils.isNotEmpty(bodyClass)){
82                  cssSelector.append(".").append(bodyClass);
83              }
84          }
85  
86          // area div id --> under the assumption that the collection has the same name as the div
87  
88          // find page node --> model.root.content does not work as the current paragraph might be inherited
89          Node page = model.getNode();
90          try {
91              while(!NodeUtil.isNodeType(page, NodeTypes.Page.NAME)){
92                  page = page.getParent();
93              }
94          }
95          catch (RepositoryException e) {
96              throw new RuntimeException(e);
97          }
98          final String pathToPage = NodeUtil.getPathIfPossible(page);
99          final String pathInPage = NodeUtil.getPathIfPossible(model.getNode()).substring(pathToPage.length());
100         final String areaDivId = pathInPageToAreaId(pathInPage);
101         if(StringUtils.isNotEmpty(areaDivId)){
102             if(cssSelector.length()>0) {
103                 cssSelector.append(" ");
104             }
105             cssSelector.append("#").append(areaDivId);
106         }
107 
108         List<RenderingModel> models = new ArrayList<RenderingModel>();
109         for (RenderingModel current = model; current != rootModelObj; current = current.getParent()) {
110             models.add(current);
111         }
112         Collections.reverse(models);
113 
114         // now add all div classes to the top model
115         for (RenderingModel currentModel : models) {
116             final String cssClass;
117             if (currentModel instanceof CssSelectorSupportRenderingModel) {
118                 cssClass = ((CssSelectorSupportRenderingModel) currentModel).getCssClass();
119             }
120             else{
121                 final info.magnolia.rendering.template.RenderableDefinition definition = currentModel.getDefinition();
122                 cssClass = (String) definition.getParameters().get("divClass");
123             }
124             if(StringUtils.isNotEmpty(cssClass)){
125                 if(cssSelector.length()>0) {
126                     cssSelector.append(" ");
127                 }
128                 cssSelector.append(".").append(cssClass.replace(" ", "."));
129             }
130         }
131         return cssSelector.toString();
132     }
133 
134     protected String pathInPageToAreaId(final String pathInPage) {
135         final String areaDivId = StringUtils.substringBefore(StringUtils.removeStart(pathInPage, "/"), "/");
136         if(areaDivId.equals("opener")){
137             return "main";
138         }
139         // the area containing the main components is named content but the div is named main
140         // we should add a div id parameter to the area to make that configurable
141         else if(areaDivId.equals("content")){
142             return "main";
143         }
144         return areaDivId;
145     }
146 
147 }