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