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  
39  import com.google.gwt.dom.client.Element;
40  
41  /**
42  * MgnlElement Constructor.
43  *
44  * @throws IllegalArgumentException if comments tagname is not a defined marker.
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  
58      private static final String MARKER_AREA = "cms:area";
59      private static final String MARKER_COMPONENT = "cms:component";
60  
61      public MgnlElement(CMSComment comment, MgnlElement parent) throws IllegalArgumentException {
62  
63          if (!isMgnlElement(comment.getTagName())) {
64              throw new IllegalArgumentException("The tagname must be one of the defined marker Strings.");
65          }
66          this.comment = comment;
67          this.parent = parent;
68      }
69      public boolean isMgnlElement(String tagName) {
70          if (tagName.equals(MARKER_AREA)) {
71              this.isArea = true;
72              return true;
73          }
74          else if (tagName.equals(MARKER_COMPONENT)) {
75              this.isComponent = true;
76              return true;
77          }
78  
79          return false;
80      }
81  
82      public boolean isArea() {
83          return isArea;
84      }
85  
86      public boolean isComponent() {
87          return isComponent;
88      }
89  
90      public MgnlElement getParent() {
91          return parent;
92      }
93  
94      public void setParent(MgnlElement parent) {
95          this.parent = parent;
96      }
97  
98      public LinkedList<MgnlElement> getChildren() {
99          return children;
100     }
101 
102     public List<MgnlElement> getDescendants() {
103 
104         List<MgnlElement> descendants = new LinkedList<MgnlElement>();
105 
106         for (MgnlElement element : getChildren()) {
107             descendants.add(element);
108             descendants.addAll(element.getDescendants());
109         }
110         return descendants;
111     }
112 
113     public List<MgnlElement> getAscendants() {
114         List<MgnlElement> ascendants = new LinkedList<MgnlElement>();
115         MgnlElement parent = this.parent;
116         while (parent != null) {
117             ascendants.add(parent);
118             parent = parent.getParent();
119         }
120         return ascendants;
121     }
122 
123     public MgnlElement getRootArea() {
124         MgnlElement rootArea = null;
125         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
126             if (parent.isArea()) {
127                 rootArea = parent;
128             }
129         }
130         return rootArea;
131     }
132 
133     public MgnlElement getParentArea() {
134         MgnlElement parentArea = null;
135         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
136             if (parent.isArea()) {
137                 parentArea = parent;
138                 break;
139             }
140         }
141         return parentArea;
142     }
143 
144     public CMSComment getComment() {
145         return comment;
146     }
147 
148     public List<MgnlElement> getComponents() {
149         List<MgnlElement> components = new LinkedList<MgnlElement>();
150         for (MgnlElement element : getChildren()) {
151             if (element.isComponent()) {
152                 components.add(element);
153             }
154         }
155         return components;
156     }
157     public List<MgnlElement> getAreas() {
158         List<MgnlElement> areas = new LinkedList<MgnlElement>();
159         for (MgnlElement element : getChildren()) {
160             if (element.isArea()) {
161                 areas.add(element);
162             }
163         }
164         return areas;
165     }
166 
167     public MgnlElement getRoot() {
168         MgnlElement root = null;
169         for (MgnlElement parent = this; parent != null; parent = parent.getParent()) {
170                 root = parent;
171         }
172         return root;
173     }
174 
175     public boolean isRelated(MgnlElement relative) {
176 
177         if (relative != null && this.getRoot() == relative.getRoot()) {
178                 return true;
179         }
180         return false;
181     }
182 
183     public Element getFirstElement() {
184         return firstElement;
185     }
186 
187     public void setFirstElement(Element firstElement) {
188         this.firstElement = firstElement;
189     }
190 
191     public Element getLastElement() {
192         return lastElement;
193     }
194 
195     public void setLastElement(Element lastElement) {
196         this.lastElement = lastElement;
197     }
198 }