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.HashMap;
37  import java.util.Map;
38  
39  import com.google.gwt.dom.client.Element;
40  import com.google.gwt.regexp.shared.MatchResult;
41  import com.google.gwt.regexp.shared.RegExp;
42  
43  
44  /**
45  * CMSComment Constructor.
46  *
47  * @throws IllegalArgumentException if the tagname does not start with cms:
48  */
49  public class CMSComment {
50  
51      private Element element;
52      private String comment;
53      private String tagName;
54      private boolean isClosing = false;
55  
56      private HashMap<String, String> attributes;
57  
58      public CMSComment(Comment comment) throws IllegalArgumentException {
59          this.setElement((Element)comment.cast());
60          this.comment = comment.getData().trim();
61  
62  
63          int delimiter = this.comment.indexOf(" ");
64          String attributeString = "";
65  
66          if (delimiter < 0){
67              this.tagName = this.comment;
68          }
69          else {
70              this.tagName = this.comment.substring(0, delimiter);
71              attributeString = this.comment.substring(delimiter + 1);
72          }
73  
74          if (this.tagName.startsWith("/")) {
75              setClosing(true);
76              this.tagName = this.tagName.substring(1);
77          }
78  
79  
80          if (this.tagName.startsWith("cms:")) {
81              String[] keyValue;
82              this.attributes = new HashMap<String, String>();
83              RegExp regExp = RegExp.compile("(\\S+=[\"'][^\"]*[\"'])", "g");
84              for (MatchResult matcher = regExp.exec(attributeString); matcher != null; matcher = regExp.exec(attributeString)) {
85                  keyValue = matcher.getGroup(0).split("=");
86                  this.attributes.put(keyValue[0], keyValue[1].replace("\"", ""));
87              }
88          }
89          else {
90              throw new IllegalArgumentException("Tagname must start with 'cms:'.");
91          }
92  
93      }
94  
95      public boolean isClosing() {
96          return isClosing;
97      }
98  
99      public void setClosing(boolean isClosing) {
100         this.isClosing = isClosing;
101     }
102 
103 
104     public String getAttribute(String name) {
105         return this.attributes.get(name);
106     }
107 
108     public Map<String, String> getAttributes() {
109         return this.attributes;
110     }
111 
112     public boolean hasAttribute(String name) {
113         return this.attributes.containsKey(name);
114     }
115 
116     public String getTagName() {
117         return this.tagName;
118     }
119 
120     @Override
121     public String toString() {
122         return this.comment;
123     }
124 
125     public void setElement(Element element) {
126         this.element = element;
127     }
128 
129     public Element getElement() {
130         return element;
131     }
132 
133 }