View Javadoc

1   /**
2    * This file Copyright (c) 2011 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 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 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 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     @Deprecated
165     public CMSComment getComment() {
166         return comment;
167     }
168 
169     public List<MgnlElement> getComponents() {
170         List<MgnlElement> components = new LinkedList<MgnlElement>();
171         for (MgnlElement element : getChildren()) {
172             if (element.isComponent()) {
173                 components.add(element);
174             }
175         }
176         return components;
177     }
178     public List<MgnlElement> getAreas() {
179         List<MgnlElement> areas = new LinkedList<MgnlElement>();
180         for (MgnlElement element : getChildren()) {
181             if (element.isArea()) {
182                 areas.add(element);
183             }
184         }
185         return areas;
186     }
187 
188     public MgnlElement getRoot() {
189         MgnlElement root = null;
190         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
191                 root = parent;
192         }
193         return root;
194     }
195 
196     public boolean isRelated(MgnlElement relative) {
197 
198         if (relative != null && this.getRoot() == relative.getRoot()) {
199                 return true;
200         }
201         return false;
202     }
203 
204     public void delete() {
205         for (MgnlElement child : getChildren()) {
206             if (getParent() != null) {
207                 getParent().getChildren().add(child);
208             }
209             child.setParent(getParent());
210         }
211     }
212 
213     public Element getFirstElement() {
214         return firstElement;
215     }
216 
217     public void setFirstElement(Element firstElement) {
218         this.firstElement = firstElement;
219     }
220 
221     public Element getLastElement() {
222         return lastElement;
223     }
224 
225     public void setLastElement(Element lastElement) {
226         this.lastElement = lastElement;
227     }
228 
229     public void setComponentElement(Element componentElement) {
230         this.componentElement = componentElement;
231     }
232 
233     public void setAreaElement(Element areaElement) {
234         this.areaElement = areaElement;
235     }
236 
237     public void setEditElement(Element editElement) {
238         this.editElement = editElement;
239     }
240 
241     public Element getComponentElement() {
242         return componentElement;
243     }
244 
245     public Element getAreaElement() {
246         return areaElement;
247     }
248 
249     public Element getEditElement() {
250         return editElement;
251     }
252 
253     public void setEndComment(CMSComment endComment) {
254         this.endComment = endComment;
255     }
256 
257     public CMSComment getEndComment() {
258         return this.endComment;
259     }
260 
261     public String getAttribute(String key) {
262         return this.attributes.get(key);
263     }
264 
265     public boolean containsAttribute(String key) {
266         return this.attributes.containsKey(key);
267     }
268     public Map<String, String> getAttributes() {
269         return this.attributes;
270     }
271 }