[hg] main-silver: #231121(partial): don't record empty Preferenc...

  • From: Jan Lahoda < >
  • To:
  • Subject: [hg] main-silver: #231121(partial): don't record empty Preferenc...
  • Date: Wed, 10 Jul 2013 23:36:26 -0700

changeset 54b3bcab144b in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/54b3bcab144b
description:
        #231121(partial): don't record empty Preferences nodes.

diffstat:

 
editor.tools.storage/src/org/netbeans/modules/editor/tools/storage/api/XMLHintPreferences.java
            |  32 +++++--
 
editor.tools.storage/test/unit/src/org/netbeans/modules/editor/tools/storage/api/ToolPreferencesTest.java
 |  40 ++++++++++
 2 files changed, 62 insertions(+), 10 deletions(-)

diffs (148 lines):

diff --git 
a/editor.tools.storage/src/org/netbeans/modules/editor/tools/storage/api/XMLHintPreferences.java
 
b/editor.tools.storage/src/org/netbeans/modules/editor/tools/storage/api/XMLHintPreferences.java
--- 
a/editor.tools.storage/src/org/netbeans/modules/editor/tools/storage/api/XMLHintPreferences.java
+++ 
b/editor.tools.storage/src/org/netbeans/modules/editor/tools/storage/api/XMLHintPreferences.java
@@ -81,12 +81,15 @@
 class XMLHintPreferences extends AbstractPreferences {
 
     private final HintPreferencesProviderImpl driver;
+    private final Element parentNode;
     private final Element node;
 
-    private XMLHintPreferences(HintPreferencesProviderImpl driver, 
XMLHintPreferences parent, String nodeName, Element node) {
+    private XMLHintPreferences(HintPreferencesProviderImpl driver, 
XMLHintPreferences parent, String nodeName, Element node, Element parentNode, 
boolean recordedInParent) {
         super(parent, nodeName);
         this.driver = driver;
         this.node = node;
+        this.parentNode = parentNode;
+        this.recordedInParent = recordedInParent;
     }
 
     private Element findAttribute(String key) {
@@ -115,6 +118,7 @@
         
         found.setAttribute("value", value);
         
+        ensureRecordedInParent();
         driver.writeNotify();
     }
 
@@ -174,7 +178,7 @@
             Node n = nl.item(i);
 
             if (n instanceof Element && escapedName.equals(((Element) 
n).getAttribute("name"))) {
-                return new XMLHintPreferences(driver, this, name, (Element) 
n);
+                return new XMLHintPreferences(driver, this, name, (Element) 
n, node, true);
             }
         }
 
@@ -182,11 +186,18 @@
         
         nue.setAttribute("name", escapedName);
 
-        node.appendChild(nue);
+        return new XMLHintPreferences(driver, this, name, nue, node, false);
+    }
 
-        driver.writeNotify();
-        
-        return new XMLHintPreferences(driver, this, name, nue);
+    private boolean recordedInParent;
+    protected synchronized void ensureRecordedInParent() {
+        if (recordedInParent) return;
+        recordedInParent = true;
+        Preferences parent = parent();
+        if (parent instanceof XMLHintPreferences) {
+            ((XMLHintPreferences) parent).ensureRecordedInParent();
+        }
+        parentNode.appendChild(node);
     }
 
     @Override
@@ -260,7 +271,8 @@
         }
 
         public Preferences getPreferences(String toolKind, String mimeType) {
-            NodeList nl = 
doc.getDocumentElement().getElementsByTagName("tool");
+            Element docEl = doc.getDocumentElement();
+            NodeList nl = docEl.getElementsByTagName("tool");
             String escapedToolKind = escape(toolKind);
             String escapedMimeType = escape(mimeType);
 
@@ -268,7 +280,7 @@
                 Element el = (Element) nl.item(i);
                 
                 if (escapedToolKind.equals(el.getAttribute("kind")) && 
escapedMimeType.equals(el.getAttribute("type"))) {
-                    return new XMLHintPreferences(this, null, "", el);
+                    return new XMLHintPreferences(this, null, "", el, docEl, 
true);
                 }
             }
             
@@ -277,9 +289,9 @@
             el.setAttribute("kind", escapedToolKind);
             el.setAttribute("type", escapedMimeType);
             
-            doc.getDocumentElement().appendChild(el);
+            docEl.appendChild(el);
             
-            return new XMLHintPreferences(this, null, "", el);
+            return new XMLHintPreferences(this, null, "", el, docEl, false);
         }
         
         private long modificationCount = 0;
diff --git 
a/editor.tools.storage/test/unit/src/org/netbeans/modules/editor/tools/storage/api/ToolPreferencesTest.java
 
b/editor.tools.storage/test/unit/src/org/netbeans/modules/editor/tools/storage/api/ToolPreferencesTest.java
--- 
a/editor.tools.storage/test/unit/src/org/netbeans/modules/editor/tools/storage/api/ToolPreferencesTest.java
+++ 
b/editor.tools.storage/test/unit/src/org/netbeans/modules/editor/tools/storage/api/ToolPreferencesTest.java
@@ -42,8 +42,12 @@
 package org.netbeans.modules.editor.tools.storage.api;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.lang.ref.Reference;
 import java.lang.ref.WeakReference;
+import java.util.prefs.Preferences;
 import org.netbeans.junit.NbTestCase;
 
 /**
@@ -84,4 +88,40 @@
         assertEquals(value, 
ToolPreferences.from(settingsFile.toURI()).getPreferences("test", 
"text/x-test").get(key, null));
         prefs.save();
     }
+    
+    public void testDontSaveEmptyNodes() throws Exception {
+        clearWorkDir();
+        File wd = getWorkDir();
+        File settingsFile = new File(wd, "settings.xml");
+        ToolPreferences prefs = ToolPreferences.from(settingsFile.toURI());
+        Preferences p = prefs.getPreferences("test", "text/x-test");
+        p.node("a/b/e/f");
+        p.node("a/b/c/d").put("test", "test");
+        prefs.save();
+        StringBuilder content = new StringBuilder();
+        try (Reader r = new InputStreamReader(new 
FileInputStream(settingsFile), "UTF-8")) {
+            int read;
+            
+            while ((read = r.read()) != (-1)) {
+                content.append((char) read);
 }
+        }
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+                     "<!DOCTYPE configuration PUBLIC \"-//NetBeans//DTD Tool 
Configuration 1.0//EN\" 
\"http://www.netbeans.org/dtds/ToolConfiguration-1_0.dtd\";>\n" +
+                     "<configuration>\n" +
+                     "    <tool kind=\"test\" type=\"text/x-test\">\n" +
+                     "        <node name=\"a\">\n" +
+                     "            <node name=\"b\">\n" +
+                     "                <node name=\"c\">\n" +
+                     "                    <node name=\"d\">\n" +
+                     "                        <attribute name=\"test\" 
value=\"test\"/>\n" +
+                     "                    </node>\n" +
+                     "                </node>\n" +
+                     "            </node>\n" +
+                     "        </node>\n" +
+                     "    </tool>\n" +
+                     "</configuration>\n",
+                     content.toString().replace("\r", ""));
+    }
+    
+}

[hg] main-silver: #231121(partial): don't record empty Preferenc...

Jan Lahoda 07/11/2013

Project Features

About this Project

Editor was started in November 2009, is owned by Martin Ryzl, and has 348 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close