View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 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 MgnlElement getParent() {
112         return parent;
113     }
114 
115     public void setParent(MgnlElement parent) {
116         this.parent = parent;
117     }
118 
119     public LinkedList<MgnlElement> getChildren() {
120         return children;
121     }
122 
123     public List<MgnlElement> getDescendants() {
124 
125         List<MgnlElement> descendants = new LinkedList<MgnlElement>();
126 
127         for (MgnlElement element : getChildren()) {
128             descendants.add(element);
129             descendants.addAll(element.getDescendants());
130         }
131         return descendants;
132     }
133 
134     public List<MgnlElement> getAscendants() {
135         List<MgnlElement> ascendants = new LinkedList<MgnlElement>();
136         while (parent != null) {
137             ascendants.add(parent);
138             parent = parent.getParent();
139         }
140         return ascendants;
141     }
142 
143     public MgnlElement getRootArea() {
144         MgnlElement rootArea = null;
145         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
146             if (parent.isArea()) {
147                 rootArea = parent;
148             }
149         }
150         return rootArea;
151     }
152 
153     public MgnlElement getParentArea() {
154         MgnlElement parentArea = null;
155         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
156             if (parent.isArea()) {
157                 parentArea = parent;
158                 break;
159             }
160         }
161         return parentArea;
162     }
163 
164     public MgnlElement getTopParentArea() {
165         MgnlElement parentArea = null;
166         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
167             if (parent.isArea()) {
168                 parentArea = parent;
169             }
170         }
171         return parentArea;
172     }
173 
174     @Deprecated
175     public CMSComment getComment() {
176         return comment;
177     }
178 
179     public List<MgnlElement> getComponents() {
180         List<MgnlElement> components = new LinkedList<MgnlElement>();
181         for (MgnlElement element : getChildren()) {
182             if (element.isComponent()) {
183                 components.add(element);
184             }
185         }
186         return components;
187     }
188     public List<MgnlElement> getAreas() {
189         List<MgnlElement> areas = new LinkedList<MgnlElement>();
190         for (MgnlElement element : getChildren()) {
191             if (element.isArea()) {
192                 areas.add(element);
193             }
194         }
195         return areas;
196     }
197 
198     public MgnlElement getRoot() {
199         MgnlElement root = null;
200         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
201                 root = parent;
202         }
203         return root;
204     }
205 
206     public boolean isRelated(MgnlElement relative) {
207 
208         if (relative != null && this.getRoot() == relative.getRoot()) {
209                 return true;
210         }
211         return false;
212     }
213 
214     public void delete() {
215         for (MgnlElement child : getChildren()) {
216             if (getParent() != null) {
217                 getParent().getChildren().add(child);
218             }
219             child.setParent(getParent());
220         }
221     }
222 
223     public Element getFirstElement() {
224         return firstElement;
225     }
226 
227     public void setFirstElement(Element firstElement) {
228         this.firstElement = firstElement;
229     }
230 
231     public Element getLastElement() {
232         return lastElement;
233     }
234 
235     public void setLastElement(Element lastElement) {
236         this.lastElement = lastElement;
237     }
238 
239     public void setComponentElement(Element componentElement) {
240         this.componentElement = componentElement;
241     }
242 
243     public void setAreaElement(Element areaElement) {
244         this.areaElement = areaElement;
245     }
246 
247     public void setEditElement(Element editElement) {
248         this.editElement = editElement;
249     }
250 
251     public Element getComponentElement() {
252         return componentElement;
253     }
254 
255     public Element getAreaElement() {
256         return areaElement;
257     }
258 
259     public Element getEditElement() {
260         return editElement;
261     }
262 
263     public void setEndComment(CMSComment endComment) {
264         this.endComment = endComment;
265     }
266 
267     public CMSComment getEndComment() {
268         return this.endComment;
269     }
270 
271     public String getAttribute(String key) {
272         return this.attributes.get(key);
273     }
274 
275     public boolean containsAttribute(String key) {
276         return this.attributes.containsKey(key);
277     }
278     public Map<String, String> getAttributes() {
279         return this.attributes;
280     }
281 }