@@ -0,0 +1,2732 @@
+package net.fortytwo.myotherbrain.notes;%0A%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport java.io.OutputStream;%0Aimport java.io.PrintStream;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.util.List;%0A%0A/**%0A * @author Joshua Shinavier (http://fortytwo.net)%0A */%0Apublic class NoteWriter %7B%0A%0A    public JSONObject toJSON(final Note n) throws JSONException %7B%0A        JSONObject json = new JSONObject();%0A%0A        JSONObject target = new JSONObject();%0A        json.put(%22target%22, target);%0A        target.put(%22key%22, n.getId());%0A        target.put(%22weight%22, n.getWeight());%0A        target.put(%22sharability%22, n.getSharability());%0A        target.put(%22value%22, n.getValue());%0A        target.put(%22created%22, n.getCreated());%0A        if (null != n.getAlias()) %7B%0A            target.put(%22alias%22, n.getAlias());%0A        %7D%0A%0A        if (0 %3C n.getChildren().size()) %7B%0A            JSONArray c = new JSONArray();%0A            json.put(%22children%22, c);%0A            int i = 0;%0A            for (Note child : n.getChildren()) %7B%0A                c.put(i, toJSON(child));%0A                i++;%0A            %7D%0A        %7D%0A%0A        return json;%0A    %7D%0A%0A    public void writeNotes(final List%3CNote%3E notes,%0A                           final OutputStream out) %7B%0A        PrintStream p;%0A        try %7B%0A            p = new PrintStream(out, false, %22UTF-8%22);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            throw new IllegalStateException(e);%0A        %7D%0A%0A        for (Note n : notes) %7B%0A            printNote(n, 0, p);%0A        %7D%0A    %7D%0A%0A    private static void printNote(final Note n,%0A                                  final int indent,%0A                                  final PrintStream p) %7B%0A%0A        if (null != n.getId()) %7B%0A            if (null != n.getId()) %7B%0A                p.print(padKey(n.getId()));%0A            %7D%0A            p.print(%22: %22);%0A        %7D%0A%0A        for (int i = 0; i %3C indent; i++) %7B%0A            p.print(%22    %22);%0A        %7D%0A%0A        p.print(%22* %22);%0A%0A        p.print(sanitizeValue(n.getValue()));%0A%0A        p.print(%22%5Cn%22);%0A%0A        for (Note child : n.getChildren()) %7B%0A            printNote(child, indent + 1, p);%0A        %7D%0A    %7D%0A%0A    private static String sanitizeValue(final String value) %7B%0A        return null == value %7C%7C 0 == value.length() %7C%7C !isValidValue(value)%0A                ? %22???%22%0A                : value;%0A    %7D%0A%0A    private static String padKey(String id) %7B%0A        while (id.length() %3C 5) %7B%0A            id = %220%22 + id;%0A        %7D%0A%0A        return id;%0A    %7D%0A%0A    private static boolean isValidValue(final String value) %7B%0A        for (char c : value.toCharArray()) %7B%0A            if (Character.isISOControl(c)) %7B%0A                return false;%0A            %7D%0A        %7D%0A%0A        return true;%0A    %7D%0A%7D%0A

Separated output functionality from what was NoteSyntax into NoteWriter