Saturday, June 19, 2010

Enhancing your blog with proper code formatting

Today Luciano told me my blog is a mess, at least I should properly format my code. He is right.
I have followed these instructions:

http://concise-software.blogspot.com/2010/03/enabling-syntaxhighlighter-in-blogger.html



I am testing now:
this is done with the xml brush:

Using <pre name="code" class="brush: xml"> :

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
 <name>Pierre</name><age>33</age>
</Employee>


this is done with the java brush:

Using <pre name="code" class="brush: java"> :

public class XMLLoaderTest {
 @Test
 public void testname() throws Exception {
  XMLLoader<Employee> loader = new XMLLoader<Employee>();
  Employee emp = loader.getObjectFromXMLFile(Employee.class, "testResources/Employees.xml");
  System.out.println(emp.getName() + " " + emp.getAge());
 }
}



import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

/**
 * Helper class to load Java Objects from an XML document
 * See test methods for usage  
 * @author pierre
 *
 * @param <T>
 */
public class XMLLoader <T> {
 /**
  * Loads an object from a XML file
  * @param theClass
  * @param fileName
  * @return
  * @throws JAXBException
  */
 public T getObjectFromXMLFile(Class theClass, String fileName) throws JAXBException {
  T result = null;
  JAXBContext jc = JAXBContext.newInstance(theClass);
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  result = (T)unmarshaller.unmarshal(new File(fileName));
  return result;
 }
}


It works!
Make sure that you include the BrushXyz.js file when you use the "brush: xyz"


Also, I had to load some additional js (you can copy them from my HTML source) to make it look really pretty.

I have the impression that this beauty comes with a cost: the page is slower to load.


I have now copied the .js and .css files to my google site,  it seems much faster!

2 comments:

Thomas Heuring said...

Hi Pierluigi,

I use the same js scripts to make my code more aesthetic. But like you said, my blog is getting slow. Can you please show or tell me howe you added the scripts to you Google blog?

Greetz,

Thomas Heuring

Pierluigi Vernetto said...

I simply downloaded the scripts from alexgorbatchev's website and put it under another site (I think my Google Code or Google Site), faster than his... they were a lot faster... alex's site must be under a heavy load...
strangely, after some time they stopped working so I reverted to Alex's site.... as soon as I have time I will definitely try the experiment again, in fact some times it becomes excruciatingly slow