1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import org.netbeans.editor.Settings; 29 import org.netbeans.editor.SettingsNames; 30 import org.openide.xml.XMLUtil; 31 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 import org.w3c.dom.Text ; 37 38 39 46 public class MacrosMIMEOptionFile extends MIMEOptionFile{ 47 48 49 public static final String TAG_ROOT = "macros"; public static final String TAG_MACRO = "macro"; 52 53 public static final String ATTR_NAME = "name"; public static final String ATTR_REMOVE = "remove"; public static final String ATTR_XML_SPACE = "xml:space"; public static final String VALUE_XML_SPACE = "preserve"; 58 59 static final String FILENAME = "macros"; 61 public MacrosMIMEOptionFile(BaseOptions base, Object proc) { 62 super(base, proc); 63 } 64 65 67 protected void loadSettings(boolean propagate){ 68 synchronized (Settings.class) { 69 Document doc = dom; 70 Element rootElement = doc.getDocumentElement(); 71 72 if (!TAG_ROOT.equals(rootElement.getTagName())) { 73 return; 75 } 76 77 Map mapa = new HashMap ((Map ) Settings.getValue(base.getKitClass(), SettingsNames.MACRO_MAP)); 79 80 properties.clear(); 81 82 NodeList mcr = rootElement.getElementsByTagName(TAG_MACRO); 83 int len = mcr.getLength(); 84 for (int i=0;i<len;i++){ 85 org.w3c.dom.Node node = mcr.item(i); 86 Element FCElement = (Element )node; 87 88 if (FCElement == null){ 89 continue; 90 } 91 92 String key = FCElement.getAttribute(ATTR_NAME); 93 String delete = FCElement.getAttribute(ATTR_REMOVE); 94 String action = ""; 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 action = textNode.getData(); 102 } 103 } 104 } 105 106 properties.put(key, action); 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 121 if (propagate){ 123 base.setMacroMap(mapa, false); 124 } 125 } 126 if (propagate) setLoaded(true); 127 } 128 } 129 130 132 protected void updateSettings(Map changedProp){ 133 synchronized (Settings.class) { 134 properties.putAll(changedProp); 136 137 Document doc = XMLUtil.createDocument(TAG_ROOT, null, processor.getPublicID(), processor.getSystemID()); 139 org.w3c.dom.Element rootElem = doc.getDocumentElement(); 140 141 ArrayList removed = new ArrayList (); 142 143 Map defaultMacros = base.getDefaultMacrosMap(); 144 if (defaultMacros == null) defaultMacros = new HashMap (); 146 147 148 for( Iterator i = properties.keySet().iterator(); i.hasNext(); ) { 150 String key = (String )i.next(); 151 if (properties.get(key) instanceof String ){ 152 153 String action = (String ) properties.get(key); 154 if (action.length()==0){ 155 if (!defaultMacros.containsKey(key)) { 157 removed.add(key); 158 continue; 159 } 160 } else{ 161 if (defaultMacros.containsKey(key)){ 164 String defValue = (String ) defaultMacros.get(key); 165 if (defValue.equals(action)){ 166 removed.add(key); 167 continue; 168 } 169 } 170 } 171 172 org.w3c.dom.Element macroElem = doc.createElement(TAG_MACRO); 173 macroElem.setAttribute(ATTR_NAME, key); 174 if (action.length()==0){ 175 macroElem.setAttribute(ATTR_REMOVE, Boolean.TRUE.toString()); 176 }else{ 177 macroElem.setAttribute(ATTR_XML_SPACE, VALUE_XML_SPACE); 178 macroElem.appendChild(doc.createTextNode(action)); 179 } 180 rootElem.appendChild(macroElem); 181 } 182 } 183 184 for (int i=0; i<removed.size(); i++){ 185 properties.remove(removed.get(i)); 186 } 187 188 doc.getDocumentElement().normalize(); 189 190 saveSettings(doc); 191 } 192 } 193 194 } 195 | Popular Tags |