KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > spi > JSPColoringData


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.web.core.syntax.spi;
22
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.servlet.jsp.tagext.TagLibraryInfo JavaDoc;
28
29 /** Holds data relevant to the JSP coloring for one JSP page. The main purposes
30  * of this class are
31  * to report which prefixes are tag library prefixes in the page, and allows
32  * listening on the change of the prefixes, at which point the page needs to be
33  * recolored.
34  *
35  * @author Petr Jiricka
36  */

37 public final class JSPColoringData extends PropertyChangeSupport JavaDoc {
38     
39     /** An property whose change is fired every time the tag library
40     * information changes in such a way that recoloring of the document is required.
41     */

42     public static final String JavaDoc PROP_COLORING_CHANGE = "coloringChange"; // NOI18N
43
public static final String JavaDoc PROP_PARSING_SUCCESSFUL = "parsingSuccessful"; //NOI18N
44
public static final String JavaDoc PROP_PARSING_IN_PROGRESS = "parsingInProgress"; //NOI18N
45

46     /** Taglib id -> TagLibraryInfo */
47     private Map JavaDoc taglibs;
48     
49     /** Prefix -> Taglib id */
50     private Map JavaDoc prefixMapper;
51     
52     private boolean elIgnored = false;
53     
54     private boolean xmlSyntax = false;
55     
56     /** Creates a new instance of JSPColoringData. */
57     public JSPColoringData(Object JavaDoc sourceBean) {
58         super(sourceBean);
59     }
60     
61     public Map JavaDoc getPrefixMapper(){
62         return prefixMapper;
63     }
64     
65     public String JavaDoc toString() {
66         return "JSPColoringData, taglibMap:\n" +
67           (prefixMapper == null ?
68             "null" :
69             mapToString(prefixMapper, " ")
70           );
71     }
72     
73     private static String JavaDoc mapToString(Map JavaDoc m, String JavaDoc indent) {
74         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75         Iterator JavaDoc it = m.keySet().iterator();
76         while (it.hasNext()) {
77             Object JavaDoc key = it.next();
78             sb.append(indent).append(key).append(" -> ").append(m.get(key)).append("\n");
79         }
80         return sb.toString();
81     }
82
83     /** Returns true if the given tag library prefix is known in this page.
84      */

85     public boolean isTagLibRegistered(String JavaDoc prefix) {
86         if ((taglibs == null) || (prefixMapper == null)) {
87             return false;
88         }
89         return prefixMapper.containsKey(prefix);
90     }
91     
92     /** Returns true if the EL is ignored in this page.
93      */

94     public boolean isELIgnored() {
95         return elIgnored;
96     }
97     
98     /** Returns true if the page is in xml syntax (JSP Documnet).
99      * If the page is in standard syntax, returns false.
100      */

101     public boolean isXMLSyntax(){
102         return xmlSyntax;
103     }
104     /*public boolean isBodyIntepretedByTag(String prefix, String tagName) {
105     }*/

106         
107     public void parsingStarted() {
108         firePropertyChange(PROP_PARSING_IN_PROGRESS, null, new Boolean JavaDoc(true));
109     }
110     
111     /** Incorporates new parse data from the parser, possibly firing a change about coloring.
112      * @param newTaglibs the new map of (uri -> TagLibraryInfo)
113      * @param newPrefixMapper the new map of (prefix, uri)
114      * @param parseSuccessful wherher parsing was successful. If false, then the new information is partial only
115      */

116     public void applyParsedData(Map JavaDoc newTaglibs, Map JavaDoc newPrefixMapper, boolean newELIgnored, boolean newXMLSyntax, boolean parseSuccessful) {
117         // check whether coloring has not changed
118
boolean coloringSame = equalsColoringInformation(taglibs, prefixMapper, newTaglibs, newPrefixMapper);
119         
120         firePropertyChange(PROP_PARSING_SUCCESSFUL, null, new Boolean JavaDoc(parseSuccessful));
121         
122         // check and apply EL data
123
if (parseSuccessful) {
124             coloringSame = coloringSame && (elIgnored == newELIgnored);
125             elIgnored = newELIgnored;
126         }
127         
128         //An additional check for the coloring change ->
129
//if the elIgnored and xmlSyntax have default values and the taglibs and prefixes are empty,
130
//there is no need to repaint the editor (fire the property change).
131
//Test if this is a first call of this method - after opening of the editor
132
if((taglibs == null) && (prefixMapper == null)) {
133             coloringSame = ((newELIgnored == elIgnored) &&
134                            (newXMLSyntax == xmlSyntax) &&
135                            newTaglibs.isEmpty() &&
136                            newPrefixMapper.isEmpty());
137         }
138
139         if (newXMLSyntax != xmlSyntax){
140             xmlSyntax = newXMLSyntax;
141             coloringSame = false;
142         }
143         
144         // appy taglib data
145
if (parseSuccessful || (taglibs == null) || (prefixMapper == null)) {
146             // overwrite
147
taglibs = newTaglibs;
148             prefixMapper = newPrefixMapper;
149         }
150         else {
151             // merge
152
Iterator JavaDoc it = newPrefixMapper.keySet().iterator();
153             while (it.hasNext()) {
154                 Object JavaDoc prefix = it.next();
155                 Object JavaDoc uri = newPrefixMapper.get(prefix);
156                 Object JavaDoc uriOld = prefixMapper.get(prefix);
157                 if ((uriOld == null) || !uri.equals(uriOld)) {
158                     Object JavaDoc newTaglib = newTaglibs.get(uri);
159                     if (newTaglib != null) {
160                         // change - merge it
161
prefixMapper.put(prefix, uri);
162                         taglibs.put(uri, newTaglib);
163                     }
164                 }
165             }
166         }
167         // possibly fire the change
168
if (!coloringSame) {
169             firePropertyChange(PROP_COLORING_CHANGE, null, null);
170         }
171     }
172
173     private static boolean equalsColoringInformation(Map JavaDoc taglibs1, Map JavaDoc prefixMapper1, Map JavaDoc taglibs2, Map JavaDoc prefixMapper2) {
174         if ((taglibs1 == null) != (taglibs2 == null)) {
175             return false;
176         }
177         if ((prefixMapper1 == null) != (prefixMapper2 == null)) {
178             return false;
179         }
180         if (prefixMapper1.size() != prefixMapper2.size()) {
181             return false;
182         }
183         else {
184             Iterator JavaDoc it = prefixMapper1.keySet().iterator();
185             while (it.hasNext()) {
186                 Object JavaDoc prefix = it.next();
187                 Object JavaDoc key1 = prefixMapper1.get(prefix);
188                 Object JavaDoc key2 = prefixMapper2.get(prefix);
189                 if ((key1 == null) || (key2 == null)) {
190                     return false;
191                 }
192                 TagLibraryInfo JavaDoc tli1 = (TagLibraryInfo JavaDoc)taglibs1.get(key1);
193                 TagLibraryInfo JavaDoc tli2 = (TagLibraryInfo JavaDoc)taglibs2.get(key2);
194                 if ((tli1 == null) || (tli2 == null)) {
195                     return false;
196                 }
197                 if (!equalsColoringInformation(tli1, tli2)) {
198                     return false;
199                 }
200             }
201             return true;
202         }
203     }
204
205     private static boolean equalsColoringInformation(TagLibraryInfo JavaDoc tli1, TagLibraryInfo JavaDoc tli2) {
206         /** PENDING
207          * should be going through all tags and checking whether the value
208          * returned by tagInfo.getBodyContent() has not changed.
209          */

210         return true;
211     }
212     
213 }
214
Popular Tags