Jump to content

How to decide how to store your data?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Hi,
I'm thinking of making a Java app with just a lot of simple code examples in it listed by category. I'm not sure if anyone knows 'Tour de Flex', it's just like that but for Java instead.
Tour de flex image:
Posted Image

I'm obviously not going to hardcode all the code in Strings. I'll have to store it..somewhere.
Now I just can't decide how to do this. Just put it in a database to me feels like overkill, as the amount of data will be small. Will, as far as I currently know, not have relations. Just 1 table...

Now the other options are:
  • XML
  • CSV
  • 1 text file (.java file) / code sample, title can be used for the list on the left. Using folders and subfolders to structure the treelist on the left.
  • Hell, I can even make an class, parse it into JSON and store that in a file.
  • Just stick with a database.

How do I have to decide?:confused:

Edit: Maybe I'm writing each sample in a separate class, implementing an interface. Then each sample can have a button "run me", and I can then propably use these class files to run the samples and display the result.

Edited by wim DC, 29 May 2011 - 02:00 AM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Encoding documents of related data would be best left to an extended markup language, a boring example:

<?xml version="1.0" encoding="UTF-8"?>


<tour-de-java>

  <snippet>

    <title>ZipCodeFormatter</title>

    <description>To be displayed as details under menu</description>

    <category>Formatters</category>

    <code>

       doSomething(); //must all be XML entity encoded

    </code>

    ...

  </snippet>

  <snippet>

    ...

  </snippet>

</tour-de-java>

You could easily iterate through each child, and on the left build a tree list based on each children category for an added ease without an external index.

It would cost much less and be simpler to add elements to the file with an XML DOM manipulator than working with a database, with the benefit of human readable snippets (one snippet per XML file could work if this is in mind, and the snippets get larger.)

Edited by Alexander, 29 May 2011 - 02:05 AM.
Rewritten for clearity

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Hi, thanks for the response.

Quote

you could have an XML file per snippet if they get big
Yea, they seem to grow pretty fast. 2 examples allready give me 89 lines :P

Glad my IDE came up with CDATA, wouldn't have thought of that. Guess I can then just ignore the xml encoding. I just hope the xml parser ignores that cdata and gives me just the "real" content as string back... will know soon :)

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
As to format the Java code I have to use a JEditorPane or JTextPane.
Not a problem but the only possible way to format text, it seems, is by using HTML. And put a big HTML String in such a pane as text.

Now, I can easily let my IDE generate a HTML page from a .java file. And then it's all nice and formatted. But now I'm doubting whether I should dump that HTML in the XML.
Or instead store all HTML files, and use those files instead of reading the html from XML.
Because that HTML in the XML looks hidious.

sample:

<?xml version="1.0" encoding="UTF-8"?>

<tour-de-java>

    <categories>

        <category name="files">

            <category name="txt files">

                <sample title="read a txt file" author="wim DC">

                    <description>How to open a txt file, and read its content</description>

                    <info>

                        This piece of code opens a file, reads trough it line by line and prints all lines to the console.

                    </info>

                    <code><![CDATA[

                        <html>

                        <style type="text/css">

                        .ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }

                        .s0 { color: rgb(0,0,128); font-weight: bold; }

                        .s1 { }

                        .s2 { color: rgb(0,128,0); font-weight: bold; }

                        </style>

                        <BODY BGCOLOR="#ffffff">

                        <TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#C0C0C0" >

                        <TR><TD><CENTER>

                        <FONT FACE="Arial, Helvetica" COLOR="#000000">

                        Sample.java</FONT>

                        </center></TD></TR></TABLE>

                        <pre>


                        <a name="l1"><span class="s0">import </span><span class="s1">java.io.File;

                        <a name="l2"></span><span class="s0">import </span><span class="s1">java.io.FileNotFoundException;

                        <a name="l3"></span><span class="s0">import </span><span class="s1">java.util.Scanner;


                        <a name="l5"></span><span class="s0">public class </span><span class="s1">Sample {

                        <a name="l6">    </span><span class="s0">public </span><span class="s1">Sample() {

                        <a name="l7">        File file = </span><span class="s0">new </span><span class="s1">File(</span><span class="s2">"myTxtFile.txt"</span><span class="s1">);

                        <a name="l8">        Scanner scanner = </span><span class="s0">null</span><span class="s1">;

                        <a name="l9">        </span><span class="s0">try </span><span class="s1">{

                        <a name="l10">            scanner = </span><span class="s0">new </span><span class="s1">Scanner(file);

                        <a name="l11">        } </span><span class="s0">catch </span><span class="s1">(FileNotFoundException e) {

                        <a name="l12">            System.err.println(</span><span class="s2">"The file could not be found."</span><span class="s1">);

                        <a name="l13">        }

                        <a name="l14">        </span><span class="s0">while </span><span class="s1">(scanner.hasNextLine()) {

                        <a name="l15">            String line = scanner.nextLine();

                        <a name="l16">            System.out.println(line);

                        <a name="l17">        }

                        <a name="l18">    }

                        <a name="l19">}</span></pre>

                        </body>

                        </html>

                    ]]></code>

                </sample>


                <sample title="Write text to file" author="wim DC">

                    <description>Example of how to write text to a txt file.</description>

                    <info>

                        A file object is created with the desired filename.


                    </info>

                    <code><![CDATA[

                        <html>

                        <style type="text/css">

                        .ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }

                        .s0 { color: rgb(0,0,128); font-weight: bold; }

                        .s1 { }

                        .s2 { color: rgb(0,128,0); font-weight: bold; }

                        </style>

                        <BODY BGCOLOR="#ffffff">

                        <TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#C0C0C0" >

                        <TR><TD><CENTER>

                        <FONT FACE="Arial, Helvetica" COLOR="#000000">

                        Sample.java</FONT>

                        </center></TD></TR></TABLE>

                        <pre>


                        <a name="l1"><span class="s0">import </span><span class="s1">java.io.File;

                        <a name="l2"></span><span class="s0">import </span><span class="s1">java.io.IOException;

                        <a name="l3"></span><span class="s0">import </span><span class="s1">java.io.PrintWriter;

                        <a name="l4">

                        <a name="l5"></span><span class="s0">public class </span><span class="s1">Sample {

                        <a name="l6">    </span><span class="s0">public </span><span class="s1">Sample() {

                        <a name="l7">        File file = </span><span class="s0">new </span><span class="s1">File(</span><span class="s2">"myFile.txt"</span><span class="s1">);

                        <a name="l8">        </span><span class="s0">try </span><span class="s1">{

                        <a name="l9">            file.createNewFile();

                        <a name="l10">            PrintWriter out = </span><span class="s0">new </span><span class="s1">PrintWriter(file);

                        <a name="l11">            out.write(</span><span class="s2">"Hello</span><span class="s0">\r\n</span><span class="s2">"</span><span class="s1">);

                        <a name="l12">            out.write(</span><span class="s2">"World."</span><span class="s1">);

                        <a name="l13">            out.close();

                        <a name="l14">        } </span><span class="s0">catch </span><span class="s1">(IOException e) {

                        <a name="l15">            System.err.println(</span><span class="s2">"File could not be written / created."</span><span class="s1">);

                        <a name="l16">        }

                        <a name="l17">    }

                        <a name="l18">}</span></pre>

                        </body>

                        </html>

					]]></code>

                </sample>

            </category>

        </category>

    </categories>

</tour-de-java>

Or I could format it myself by just taking the code as plain text.. But that'll be very time consuming I think.

Update
Gonna format it myself, can't live with ugly xml ^^

Edited by wim DC, 29 May 2011 - 07:58 AM.


#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I think I would personally supply a file name in the XML file (otherwise it would be hard to maintain) and have the program pull it out and do error checking if it exists. It is all about personal preference, I hope you can find a solution that looks good to you - Glad I could provide insight.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I think your project is a great idea.

If I were storing the my data, I'd create a program that generates the correct HTML formatting from the .java files.
Thought the hard space requirements would be:
1 .java file
1 .html file.

Another approach which might be feasible is to have some method format the .java file as it is buffered/fed into the editor. As long as the indentation/whitespace is already correct in the .java file, you'd only need to worry about color coding keywords. (I think?)
Hard space requirements:
1 .java file

Speed is another issue though. That's where I'm stuck in my decision. I'm unsure as to which method is faster. (I'm thinking the first way would be faster)

Also, I'd add line numbers to the editor. I couldn't see one from the adobe application.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I ended up doing this:
I start with an XML like this :

<?xml version="1.0" encoding="UTF-8"?>

<tour-de-java>

    <categories>

        <category name="files">

            <category name="binary files">

                <sample title="Read objects from a file" author="wim DC">

                    <description>How to use the ObjectInputStream to read objects from a file</description>

                    <info>

                        This piece of code reads an object to a file.

                        Category is just a random class. Whichever object that's being read. Its class must implement

                        the java.ui.Serializable interface. No methods need to be written. It just needs that interface.


                        The stream gets closed in the finally block, not in the try-block. In case readObject goes wrong

                        the stream should still get closed properly.


                        Note the file extension does not matter.

                    </info>

                    <code>

                        import java.io.*;


                        public class Sample {

                            public Sample() {

                                File file = new File("binary.dat");

                                ObjectInputStream ois = null;

                                try {

                                    ois = new ObjectInputStream(new FileInputStream(file));

                                    Category category = (Category) ois.readObject();

                                } catch (IOException e) {

                                    System.err.println("Oops, error, don't worry stream will be closed...");

                                } catch (ClassNotFoundException e) {

                                    System.err.println("I don't know the class that's in the file...");

                                } finally {

                                    try {

                                        if (ois != null) {

                                            ois.close();

                                        }

                                    } catch (IOException e) {

                                        System.err.println("Something horribly went wrong.");

                                    }

                                }

                            }

                        }



                        import java.io.Serializable;


                        public class Category implements Serializable {

                            ...

                        }

                    </code>

                </sample>

                <sample title="Write objects to a file" author="wim DC">

                    <description>How to use the ObjectOutputStream to write objects to a file</description>

                    <info>

                        This piece of code write an object to a file.

                        Category is just a random class. Whichever object that's being written. Its class must implement

                        the java.ui.Serializable interface. No methods need to be written. It just needs that interface.


                        The stream gets closed in the finally block, not in the try-block. In case writeObject goes wrong

                        the stream should still get closed properly.


                        Note the file extension does not matter.

                    </info>

                    <code>

                        import java.io.File;

                        import java.io.FileOutputStream;

                        import java.io.IOException;

                        import java.io.ObjectOutputStream;


                        public class Sample {

                            public Sample() {

                                File file = new File("binary.dat");

                                ObjectOutputStream oos = null;

                                try {

                                    oos = new ObjectOutputStream(new FileOutputStream(file));

                                    Category category = new Category("cat");

                                    oos.writeObject(category);

                                    oos.flush();

                                } catch (IOException e) {

                                    System.err.println("Oops, error, don't worry stream will be closed...");

                                } finally {

                                    try {

                                        if (oos != null) {

                                            oos.close();

                                        }

                                    } catch (IOException e) {

                                        System.err.println("Something horribly went wrong.");

                                    }

                                }

                            }

                        }




                        import java.io.Serializable;


                        public class Category implements Serializable {

                            ...

                        }

                    </code>

                </sample>

            </category>

            <category name="txt files">

                <sample title="Read a txt file" author="wim DC">

                    <description>How to open a txt file, and read its content</description>

                    <info>

                        This piece of code opens a file, reads trough it line by line and prints all lines to the console.

                    </info>

                    <code><![CDATA[

                        import java.io.File;

                        import java.io.FileNotFoundException;

                        import java.util.Scanner;


                        public class Sample {

                            public Sample() {

                                File file = new File("myTxtFile.txt");

                                Scanner scanner = null;

                                try {

                                    scanner = new Scanner(file);

                                } catch (FileNotFoundException e) {

                                    System.err.println("The file could not be found.");

                                }

                                while (scanner.hasNextLine()) {

                                    String line = scanner.nextLine();

                                    System.out.println(line);

                                }

                            }

                        }

                    ]]></code>

                </sample>


                <sample title="Write text to file" author="wim DC">

                    <description>Example of how to write text to a txt file.</description>

                    <info>

                        A file object is created with the desired filename.


                        Note how new PrintWriter(file) will also create the file if it does not exist yet.


                        You probably know \n, maybe not \r. \r is the "carriage return" without this Notepad won't show

                        a newline.

                    </info>

                    <code><![CDATA[

                        import java.io.File;

                        import java.io.IOException;

                        import java.io.PrintWriter;


                        public class Sample {

                            public Sample() {

                                File file = new File("myFile.txt");

                                try {

                                    PrintWriter out = new PrintWriter(file);

                                    out.write("Hello\r\n");

                                    out.write("World.");

                                    out.close();

                                } catch (IOException e) {

                                    e.printStackTrace();

                                    System.err.println("File could not be written / created.");

                                }

                            }

                        }

					]]></code>

                </sample>

            </category>

        </category>

    </categories>

</tour-de-java>

With a lot of code in it - ugly.
The next time my program starts and goes reading the XML files, it will notice that code doesn't have an <url> tag
if (node.getChild("code").getChild("url") == null) { ...}
Then I take out the code, remove spaces in the front, add html tags and styles. Save this to a '.code' file and then remove the code from the code tag,
and place an <url> tag instead pointing to the newly created file.

After program startup:

<?xml version="1.0" encoding="UTF-8"?>

<tour-de-java>

  <categories>

    <category name="files">

      <category name="binary files">

        <sample title="Read objects from a file" author="wim DC">

          <description>How to use the ObjectInputStream to read objects from a file</description>

          <info>This piece of code reads an object to a file.

Category is just a random class. Whichever object that's being read. Its class must implement

the java.ui.Serializable interface. No methods need to be written. It just needs that interface.


The stream gets closed in the finally block, not in the try-block. In case readObject goes wrong

the stream should still get closed properly.


Note the file extension does not matter.</info>

          <code>

            [B]<url>sourceFiles/files/binary files/Read objects from a file.code</url>[/B]

          </code>

        </sample>

        <sample title="Write objects to a file" author="wim DC">

          <description>How to use the ObjectOutputStream to write objects to a file</description>

          <info>This piece of code write an object to a file.

Category is just a random class. Whichever object that's being written. Its class must implement

the java.ui.Serializable interface. No methods need to be written. It just needs that interface.


The stream gets closed in the finally block, not in the try-block. In case writeObject goes wrong

the stream should still get closed properly.


Note the file extension does not matter.</info>

          <code>

            [B]<url>sourceFiles/files/binary files/Write objects to a file.code</url>[/B]

          </code>

        </sample>

      </category>

      <category name="txt files">

        <sample title="Read a txt file" author="wim DC">

          <description>How to open a txt file, and read its content</description>

          <info>This piece of code opens a file, reads trough it line by line and prints all lines to the console.</info>

          <code>

            [B]<url>sourceFiles/files/txt files/Read a txt file.code</url>[/B]

          </code>

        </sample>

        <sample title="Write text to file" author="wim DC">

          <description>Example of how to write text to a txt file.</description>

          <info>A file object is created with the desired filename.


Note how new PrintWriter(file) will also create the file if it does not exist yet.


You probably know \n, maybe not \r. \r is the "carriage return" without this Notepad won't show

a newline.</info>

          <code>

            [B]<url>sourceFiles/files/txt files/Write text to file.code</url>[/B]

          </code>

        </sample>

      </category>

    </category>

  </categories>

</tour-de-java>

So only when new code is added it will have to be proccessed (read: formatted with the help of html), the nex time(s) it will notice the <url> tag, take the url.
And get the formatted code directly from this file.

Attached File  Tour de java.png   54.39K   28 downloads
Gonna work on linenumbers now. I can just put them right into the editor,
but then it would get selected if you want to copy the code which is what I really hate when I find code online with numbers.

Btw, (just noticed from the screenshot) I forgot to html-encode the source before formatting them, so my
'Collection<String>' became just 'Collection' :mad:

Edit: and I just discovered Java has no built in feature to html-encode :crying:

Edit2: hmmm line numbers have to scroll down together with the source.. possibly harder than I thought ^^

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Here's a good line number tutorial I found a while back. It could prove to be useful.
How To: Add line numbers to your JTextArea

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

wim DC said:

Edit: and I just discovered Java has no built in feature to html-encode
I believe you could use the HTML Tidy library, such as JTidy (JTidy - JTidy)
HTMLEncode (JTidy Servlet r8-SNAPSHOT API)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users