View Javadoc
1   /**
2    * This file Copyright (c) 2011-2014 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.templating.editor.client.dom;
35  
36  import java.util.LinkedList;
37  import java.util.List;
38  import java.util.Map;
39  
40  import com.google.gwt.dom.client.Element;
41  
42  /**
43  * MgnlElement Constructor.
44  *
45  */
46  public class MgnlElement {
47  
48      private final CMSComment comment;
49      private MgnlElement parent;
50      private boolean isArea = false;
51      private boolean isComponent = false;
52  
53      private Element firstElement;
54      private Element lastElement;
55  
56      private final LinkedList<MgnlElement> children = new LinkedList<MgnlElement>();
57      private Element componentElement;
58      private Element areaElement;
59      private Element editElement;
60      private CMSComment endComment;
61  
62      private final Map<String, String> attributes;
63  
64      private static final String MARKER_AREA = "cms:area";
65      private static final String MARKER_COMPONENT = "cms:component";
66  
67      private static final String[] INHERITED_ATTRIBUTES = {"editable"};
68  
69  /**
70   * @throws IllegalArgumentException if comments tagname is not a defined marker.
71   */
72      public MgnlElement(CMSComment comment, MgnlElement parent) throws IllegalArgumentException {
73  
74          if (!isMgnlElement(comment.getTagName())) {
75              throw new IllegalArgumentException("The tagname must be one of the defined marker Strings.");
76          }
77          this.comment = comment;
78          this.parent = parent;
79  
80          this.attributes = comment.getAttributes();
81  
82          if (this.parent != null) {
83              for (String inheritedAttribute : INHERITED_ATTRIBUTES) {
84                  if (this.parent.containsAttribute(inheritedAttribute)) {
85                      attributes.put(inheritedAttribute, this.parent.getAttribute(inheritedAttribute));
86                  }
87              }
88          }
89      }
90      public boolean isMgnlElement(String tagName) {
91          if (tagName.equals(MARKER_AREA)) {
92              this.isArea = true;
93              return true;
94          }
95          else if (tagName.equals(MARKER_COMPONENT)) {
96              this.isComponent = true;
97              return true;
98          }
99  
100         return false;
101     }
102 
103     public boolean isArea() {
104         return isArea;
105     }
106 
107     public boolean isComponent() {
108         return isComponent;
109     }
110 
111     public boolean isInherited() {
112         return Boolean.parseBoolean(getAttribute("inherited")) || (getParent() != null && getParent().isInherited());
113     }
114 
115     public MgnlElement getParent() {
116         return parent;
117     }
118 
119     public void setParent(MgnlElement parent) {
120         this.parent = parent;
121     }
122 
123     public LinkedList<MgnlElement> getChildren() {
124         return children;
125     }
126 
127     public List<MgnlElement> getDescendants() {
128 
129         List<MgnlElement> descendants = new LinkedList<MgnlElement>();
130 
131         for (MgnlElement element : getChildren()) {
132             descendants.add(element);
133             descendants.addAll(element.getDescendants());
134         }
135         return descendants;
136     }
137 
138     public List<MgnlElement> getAscendants() {
139         List<MgnlElement> ascendants = new LinkedList<MgnlElement>();
140         while (parent != null) {
141             ascendants.add(parent);
142             parent = parent.getParent();
143         }
144         return ascendants;
145     }
146 
147     public MgnlElement getRootArea() {
148         MgnlElement rootArea = null;
149         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
150             if (parent.isArea()) {
151                 rootArea = parent;
152             }
153         }
154         return rootArea;
155     }
156 
157     public MgnlElement getParentArea() {
158         MgnlElement parentArea = null;
159         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
160             if (parent.isArea()) {
161                 parentArea = parent;
162                 break;
163             }
164         }
165         return parentArea;
166     }
167 
168     public MgnlElement getTopParentArea() {
169         MgnlElement parentArea = null;
170         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
171             if (parent.isArea()) {
172                 parentArea = parent;
173             }
174         }
175         return parentArea;
176     }
177 
178     @Deprecated
179     public CMSComment getComment() {
180         return comment;
181     }
182 
183     public List<MgnlElement> getComponents() {
184         List<MgnlElement> components = new LinkedList<MgnlElement>();
185         for (MgnlElement element : getChildren()) {
186             if (element.isComponent()) {
187                 components.add(element);
188             }
189         }
190         return components;
191     }
192     public List<MgnlElement> getAreas() {
193         List<MgnlElement> areas = new LinkedList<MgnlElement>();
194         for (MgnlElement element : getChildren()) {
195             if (element.isArea()) {
196                 areas.add(element);
197             }
198         }
199         return areas;
200     }
201 
202     public MgnlElement getRoot() {
203         MgnlElement root = null;
204         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
205                 root = parent;
206         }
207         return root;
208     }
209 
210     public boolean isRelated(MgnlElement relative) {
211 
212         if (relative != null && this.getRoot() == relative.getRoot()) {
213                 return true;
214         }
215         return false;
216     }
217 
218     public void delete() {
219         for (MgnlElement child : getChildren()) {
220             if (getParent() != null) {
221                 getParent().getChildren().add(child);
222             }
223             child.setParent(getParent());
224         }
225     }
226 
227     public Element getFirstElement() {
228         return firstElement;
229     }
230 
231     public void setFirstElement(Element firstElement) {
232         this.firstElement = firstElement;
233     }
234 
235     public Element getLastElement() {
236         return lastElement;
237     }
238 
239     public void setLastElement(Element lastElement) {
240         this.lastElement = lastElement;
241     }
242 
243     public void setComponentElement(Element componentElement) {
244         this.componentElement = componentElement;
245     }
246 
247     public void setAreaElement(Element areaElement) {
248         this.areaElement = areaElement;
249     }
250 
251     public void setEditElement(Element editElement) {
252         this.editElement = editElement;
253     }
254 
255     public Element getComponentElement() {
256         return componentElement;
257     }
258 
259     public Element getAreaElement() {
260         return areaElement;
261     }
262 
263     public Element getEditElement() {
264         return editElement;
265     }
266 
267     public void setEndComment(CMSComment endComment) {
268         this.endComment = endComment;
269     }
270 
271     public CMSComment getEndComment() {
272         return this.endComment;
273     }
274 
275     public String getAttribute(String key) {
276         return this.attributes.get(key);
277     }
278 
279     public boolean containsAttribute(String key) {
280         return this.attributes.containsKey(key);
281     }
282     public Map<String, String> getAttributes() {
283         return this.attributes;
284     }
285 }