KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > AbbrevsMIMEOptionFile


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 package org.netbeans.modules.editor.options;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.netbeans.editor.Settings;
28 import org.netbeans.editor.SettingsNames;
29 import org.openide.xml.XMLUtil;
30
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35 import org.w3c.dom.Text JavaDoc;
36
37 /** MIME Option XML file for Abbreviations settings.
38  * Abbreviations settings are loaded and saved in XML format
39  * according to EditorAbbreviations-1_0.dtd.
40  *
41  * @author Martin Roskanin
42  * @since 08/2001
43  */

44 public class AbbrevsMIMEOptionFile extends MIMEOptionFile{
45     
46     /** Elements */
47     public static final String JavaDoc TAG_ROOT = "abbrevs"; //NOI18N
48
public static final String JavaDoc TAG_ABBREV = "abbrev"; //NOI18N
49

50     /** Attributes */
51     public static final String JavaDoc ATTR_KEY = "key"; //NOI18N
52
public static final String JavaDoc ATTR_ACTION = "action"; //NOI18N
53
public static final String JavaDoc ATTR_REMOVE = "remove"; //NOI18N
54
public static final String JavaDoc ATTR_XML_SPACE = "xml:space"; //NOI18N
55
public static final String JavaDoc VALUE_XML_SPACE = "preserve"; //NOI18N
56

57     /** File name of this MIMEOptionFile */
58     static final String JavaDoc FILENAME = "abbreviations"; //NOI18N
59

60     public AbbrevsMIMEOptionFile(BaseOptions base, Object JavaDoc proc) {
61         super(base, proc);
62     }
63     
64     /** Loads settings from XML file.
65      * @param propagate if true - propagates the loaded settings to Editor UI */

66     protected void loadSettings(boolean propagate){
67         synchronized (Settings.class) {
68             Document JavaDoc doc = dom;
69             Element JavaDoc rootElement = doc.getDocumentElement();
70
71             if (!TAG_ROOT.equals(rootElement.getTagName())) {
72                 // Wrong root element
73
return;
74             }
75
76             // gets current abbreviations map
77
Map JavaDoc abbrevsMap = (Map JavaDoc)Settings.getValue(base.getKitClass(), SettingsNames.ABBREV_MAP);
78             Map JavaDoc mapa = (abbrevsMap==null) ? new HashMap JavaDoc() : new HashMap JavaDoc(abbrevsMap);
79             properties.clear();
80
81             NodeList JavaDoc abbr = rootElement.getElementsByTagName(TAG_ABBREV);
82             int len = abbr.getLength();
83             for (int i=0; i < len; i++){
84                 Node JavaDoc node = abbr.item(i);
85                 Element JavaDoc FCElement = (Element JavaDoc)node;
86
87                 if (FCElement == null){
88                     continue;
89                 }
90
91                 String JavaDoc key = FCElement.getAttribute(ATTR_KEY);
92                 String JavaDoc delete = FCElement.getAttribute(ATTR_REMOVE);
93                 String JavaDoc expanded = "";
94
95                 if (! Boolean.valueOf(delete).booleanValue()){
96                     NodeList JavaDoc textList = FCElement.getChildNodes();
97                     if (textList.getLength() > 0) {
98                         Node JavaDoc subNode = textList.item(0);
99                         if (subNode instanceof Text JavaDoc) {
100                             Text JavaDoc textNode = (Text JavaDoc) subNode;
101                             expanded = textNode.getData();
102                         }
103                     }
104                 }
105
106                 properties.put(key, expanded);
107             }
108
109             if (properties.size()>0){
110                 // create updated map
111
mapa.putAll(properties);
112
113                 // remove all deleted values
114
for( Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext(); ) {
115                     String JavaDoc key = (String JavaDoc)i.next();
116                     if(((String JavaDoc)properties.get(key)).length() == 0){
117                         mapa.remove(key);
118                     }
119                 }
120                 // setAbbrevMap without saving to XML
121
if (propagate){
122                     base.setAbbrevMap(mapa, false);
123                 }
124             }
125             if (propagate) setLoaded(true);
126         }
127     }
128     
129     /** Save settings to XML file
130      * @param changedProp the Map of settings to save */

131     protected void updateSettings(Map JavaDoc changedProp){
132         synchronized (Settings.class) {
133             // put changed properties to local map
134
properties.putAll(changedProp);
135
136             // now we can save local map to XML file
137
Document JavaDoc doc = XMLUtil.createDocument(TAG_ROOT, null, processor.getPublicID(), processor.getSystemID());
138             Element JavaDoc rootElem = doc.getDocumentElement();
139
140             ArrayList JavaDoc removed = new ArrayList JavaDoc();
141
142             Map JavaDoc defaultAbbrevs = base.getDefaultAbbrevMap();
143             // if default abbreviations don't exist for appropriate kit, set them empty
144
if (defaultAbbrevs == null) defaultAbbrevs = new HashMap JavaDoc();
145
146             // save XML
147
for( Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext(); ) {
148                 String JavaDoc key = (String JavaDoc)i.next();
149                 if (properties.get(key) instanceof String JavaDoc){
150
151                     String JavaDoc action = (String JavaDoc) properties.get(key);
152                     if (action.length()==0){
153                         // null value => DETETE: if property is in default set, mark it as deleted else delete it completely
154
if (!defaultAbbrevs.containsKey(key)) {
155                             removed.add(key);
156                             continue;
157                         }
158                     } else{
159                         // if key and value is already in settings default, no need to store
160
// this in diff XML file
161
if (defaultAbbrevs.containsKey(key)){
162                             String JavaDoc defValue = (String JavaDoc) defaultAbbrevs.get(key);
163                             if (defValue.equals(action)){
164                                 removed.add(key);
165                                 continue;
166                             }
167                         }
168                     }
169
170                     Element JavaDoc abbrevElem = doc.createElement(TAG_ABBREV);
171                     abbrevElem.setAttribute(ATTR_KEY, key);
172                     if (action.length()==0){
173                         abbrevElem.setAttribute(ATTR_REMOVE, Boolean.TRUE.toString());
174                     }else{
175                         abbrevElem.setAttribute(ATTR_XML_SPACE, VALUE_XML_SPACE);
176                         abbrevElem.appendChild(doc.createTextNode(action));
177                     }
178
179                     rootElem.appendChild(abbrevElem);
180                 }
181             }
182
183             for (int i=0; i<removed.size(); i++){
184                 properties.remove(removed.get(i));
185             }
186
187             doc.getDocumentElement().normalize();
188
189             saveSettings(doc);
190         }
191     }
192     
193 }
194
Popular Tags