1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 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 ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 import org.w3c.dom.Text ; 36 37 44 public class AbbrevsMIMEOptionFile extends MIMEOptionFile{ 45 46 47 public static final String TAG_ROOT = "abbrevs"; public static final String TAG_ABBREV = "abbrev"; 50 51 public static final String ATTR_KEY = "key"; public static final String ATTR_ACTION = "action"; public static final String ATTR_REMOVE = "remove"; public static final String ATTR_XML_SPACE = "xml:space"; public static final String VALUE_XML_SPACE = "preserve"; 57 58 static final String FILENAME = "abbreviations"; 60 public AbbrevsMIMEOptionFile(BaseOptions base, Object proc) { 61 super(base, proc); 62 } 63 64 66 protected void loadSettings(boolean propagate){ 67 synchronized (Settings.class) { 68 Document doc = dom; 69 Element rootElement = doc.getDocumentElement(); 70 71 if (!TAG_ROOT.equals(rootElement.getTagName())) { 72 return; 74 } 75 76 Map abbrevsMap = (Map )Settings.getValue(base.getKitClass(), SettingsNames.ABBREV_MAP); 78 Map mapa = (abbrevsMap==null) ? new HashMap () : new HashMap (abbrevsMap); 79 properties.clear(); 80 81 NodeList abbr = rootElement.getElementsByTagName(TAG_ABBREV); 82 int len = abbr.getLength(); 83 for (int i=0; i < len; i++){ 84 Node node = abbr.item(i); 85 Element FCElement = (Element )node; 86 87 if (FCElement == null){ 88 continue; 89 } 90 91 String key = FCElement.getAttribute(ATTR_KEY); 92 String delete = FCElement.getAttribute(ATTR_REMOVE); 93 String expanded = ""; 94 95 if (! Boolean.valueOf(delete).booleanValue()){ 96 NodeList textList = FCElement.getChildNodes(); 97 if (textList.getLength() > 0) { 98 Node subNode = textList.item(0); 99 if (subNode instanceof Text ) { 100 Text textNode = (Text ) subNode; 101 expanded = textNode.getData(); 102 } 103 } 104 } 105 106 properties.put(key, expanded); 107 } 108 109 if (properties.size()>0){ 110 mapa.putAll(properties); 112 113 for( Iterator i = properties.keySet().iterator(); i.hasNext(); ) { 115 String key = (String )i.next(); 116 if(((String )properties.get(key)).length() == 0){ 117 mapa.remove(key); 118 } 119 } 120 if (propagate){ 122 base.setAbbrevMap(mapa, false); 123 } 124 } 125 if (propagate) setLoaded(true); 126 } 127 } 128 129 131 protected void updateSettings(Map changedProp){ 132 synchronized (Settings.class) { 133 properties.putAll(changedProp); 135 136 Document doc = XMLUtil.createDocument(TAG_ROOT, null, processor.getPublicID(), processor.getSystemID()); 138 Element rootElem = doc.getDocumentElement(); 139 140 ArrayList removed = new ArrayList (); 141 142 Map defaultAbbrevs = base.getDefaultAbbrevMap(); 143 if (defaultAbbrevs == null) defaultAbbrevs = new HashMap (); 145 146 for( Iterator i = properties.keySet().iterator(); i.hasNext(); ) { 148 String key = (String )i.next(); 149 if (properties.get(key) instanceof String ){ 150 151 String action = (String ) properties.get(key); 152 if (action.length()==0){ 153 if (!defaultAbbrevs.containsKey(key)) { 155 removed.add(key); 156 continue; 157 } 158 } else{ 159 if (defaultAbbrevs.containsKey(key)){ 162 String defValue = (String ) defaultAbbrevs.get(key); 163 if (defValue.equals(action)){ 164 removed.add(key); 165 continue; 166 } 167 } 168 } 169 170 Element 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 |