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.
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 ^^