Working with ODFDOM

While working a little bit with ODFDOM to automatically generate some documentation I found some tasks rather difficult to accomplish due to the fact that they were hardly documented. There is a lot of ODF documentation, but how to use it with ODFDOM is another story.

So first a list of documentation I found:

Adding a new paragraph with a pre-defined style

To create a new paragraph with a style I used the following static function:

public static OdfTextParagraph newStyledParagraph ( final OdfTextDocument odt, final String style, final String content ) throws Exception
{
  final OdfTextParagraph p;
  if ( content != null ) {
    p = odt.newParagraph ( content );
  } else {
    p = odt.newParagraph ();
  }
  p.setStyleName ( style );
  return p;
}

Adding fields (like title and subtitle)

The following snippet will create a new paragraph

OdfTextParagraph p;
p = OdfHelper.newStyledParagraph ( odt, "Title", null );
p.newTextTitleElement ();

p = OdfHelper.newStyledParagraph ( odt, "Subtitle", null );
p.newTextSubjectElement ();

Creating a new parapgraph style

The next snippet creates a new a new style called “Source Text” dereived from “Text Boby”. The setFontFamily method is a helper to set all font properties at once.

protected void setFontFamily ( final OdfStyleBase style, final String value )
{
    style.setProperty ( OdfTextProperties.FontFamily, value );
    style.setProperty ( OdfTextProperties.FontFamilyAsian, value );
    style.setProperty ( OdfTextProperties.FontFamilyComplex, value );
}

private void createStyles ( final OdfTextDocument odt )
{
    final OdfOfficeStyles styles = odt.getOrCreateDocumentStyles ();

    final OdfStyle style = styles.newStyle ( "Source Text", OdfStyleFamily.Paragraph );
    style.setStyleParentStyleNameAttribute ( "Text Body" );
    style.setProperty ( OdfParagraphProperties.Margin, "1cm" );
    style.setProperty ( OdfParagraphProperties.BackgroundColor, "#DDDDDD" );
    setFontFamily ( style, "Courier New" );
}

Copy content from one to another ODF File

While most of the document I made was created some static part a the beginning was needed. Instead of creating it manually I created a new ODT File in LibreOffice and imported the content to the generated document.

The following snipped does that:

private void createStaticContent ( final OdfTextDocument odt, final File file ) throws Exception
{
    // check if file is readable and is a file
    if ( !file.canRead () || !file.isFile () )
    {
        return;
    }

    // Load the document to import

    final OdfTextDocument staticOdt = OdfTextDocument.loadDocument ( file );

    // Create a new text section which will receive the content. This is optional.
    // You could also load the content directly on the root of the document

    final TextSectionElement section = new TextSectionElement ( odt.getContentDom () );
    odt.getContentRoot ().appendChild ( section );
    section.setTextProtectedAttribute ( true );
    section.setTextNameAttribute ( "Static Content" ); //$NON-NLS-1$

    // iterate over all nodes
    final NodeList childNodes = staticOdt.getContentRoot ().getChildNodes ();
    for ( int i = 0; i < childNodes.getLength (); i++ )
    {
        // clone node from source
        final Node newNode = childNodes.item ( i ).cloneNode ( true );
        // import to target DOM
        final Node adoptedNode = odt.getContentDom ().adoptNode ( newNode );
        // append to section
        section.appendChild ( adoptedNode );
    }
}
Since only content is imported the styles of the target document will be used for the content that was imported.


Mayas knew about IPv4

Scientists recently found a correlation between the Maya calendar and the remaining amount of old IP addresses. While it was already known that the calendar marked the end of a period at the end of the year 2012, only recently they found the correlation to the amount of remaining IP addresses. The only open question is,nif this really is the end of the world as we know it.

Lick to unlock

The recent advances of Android seem to have forced Apple to find new features to distinguish it from the quite popular getting system. Android took a complete new way of unlocking your phone by using your face as key to unlock your mobile gadget. While the droidish companion already allowed pin code and pattern unlock, the iPhone only provides pin and “slide to unlock”. As android does.

But now Apple is ready to strike back and has added a bunch of new features to it upcoming iPhone 5 S. Adding a sensor to the touch screen that can performed a chemical analysis of substances that touches it allows the iOS to provide a neat new feature: Lick to unlock!

Right, by licking over the screen your phone it can detect if you are the respectful owner of the device. Insider information told us that Apple wanted to release this feature earlier, but hat to fix some simple issues that did not occur in the lab. No one tested the sensor after having lunch.

But Apple would not be called innovative if this would would be the end of the story. The additional “lunch information” is extracted and rendered out of the unlock pattern. But instead is feed into Siri to provider her with additional information of what the owner ate, when and were. This allows Siri to provide more accurate suggestions of restaurants and even can provide you a list of nearest dentists when if thinks appropriate.

We are really looking forward to “unlicking” our phone.


Bye bye Lotus Notes

We used it for some time. It was nice. But now our ways seem to …

well, let me just say: PNG, 64bit Linux, Windows 7, …

I was migrating E-Mails from Notes to Courier IMAP using “imapsync” when I stumbled over two strange problems…

Continue reading

JPA, EJB and Spring

Today I stumbled over a rather interesting issue in combination with JBoss, Hibernate, Spring, JPA and EJB.

First of all, we have mixed environment of EJB3 and Spring 3 using JPA2. All in JBoss 6 AS, with default Hibernate 3.6 as JPA provider. Now for a long time everything works fine until we re-organize the dependencies of our modules. Starting with that we got some strange JPA behavior like “class is not an entity” and some rather weird getter issues.

Altogether it looked like some classloader issue. But it took some time to find that one.

In the beginning there was a JPA persistence unit called “someModel”. The problem with spring was, that it needs to be imported using the “jee:jndi-lookup” tag, which requires a JNDI name. That one was provided by JBoss itself using the “jboss.entity.manager.factory.jndi.name” property in the “persistence.xml” file. This triggers JBoss/Hibernate to bind the persistence unit with that specified name. Of course the name was “persistence/someModel”.

Now everything grew, EJB was added in order to gain modularity. And now that the dependencies and the start order was changed, the same persistence unit was used multiple times in multiple EJBs beside the spring application. While EJB seems not to have any problems finding its persistence unit, spring needs a bit more information and requires the already discusses “jee:jndi-lookup” tag in order to find its persistence unit.

As it turned out, the multiple persistence units all registered with the same name, leaving spring with the first (or last) registration, but not necessarily the correct registration. So spring accesses “some” persistence unit with that name and possible used the wrong classloader.

The solution was quite and and lot cleaner than using the “jboss.entity.manager.factory.jndi.name” property. But, less intuitive I have to say.

In the “web.xml” of the servlet that starts the application context a reference has to be added to the persistence unit:

	<persistence-unit-ref>
		<persistence-unit-ref-name>persistence/someModel</persistence-unit-ref-name>
		<persistence-unit-name>someModel</persistence-unit-name>
	</persistence-unit-ref>

This will register the local (in the EAR file hosting the WAR) peristence unit “someModel” in the local JNDI space as “persistence/someModel” and you are done. Spring will can still import it as “persistence/someModel”.

Struggling with Acceleo 3.0 after Eclipse 3.7 upgrade

While the upgrade from Eclipse 3.6.2 to 3.7 went without nearly any trouble the upgrade of the Acceleo plugins to the versions provided with Eclipse Indigo was “a little bit more” problematic.

Starting the generator after the upgrade simply brought up:

Exception in thread "Main Thread" org.eclipse.acceleo.engine.AcceleoEvaluationException: The type of the first parameter of the main template named 'myTemplate' is a proxy.
    at org.eclipse.acceleo.engine.service.AcceleoService.doGenerate(AcceleoService.java:507)
    at org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator.generate(AbstractAcceleoGenerator.java:175)
    at org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator.doGenerate(AbstractAcceleoGenerator.java:154)
...

The main template did not accept the model anymore. Although everything was registered and working with the previous version.

Googling a bit I found out that Acceleo provides a different serialization model which stores models in binary form instead of XMI. Since all my models I flipped the switch in the project settings:

Acceleo Project Settings

Which worked at first until I stumbled over the next problem a few hours later:

org.eclipse.acceleo.engine.AcceleoEvaluationException: Invalid loop iteration at line 18 in Module myModule for block for (myObject.null). Last recorded value of self was com.thfour.config.model.model.impl.MyObjectImpl@81b69bf (....).
    at subModule.subModule(null)(subModule.mtl:18)
    at subModule.subModule(null)(subModule.mtl:6)
    at subModule.subModule(null)(subModule.mtl:4)
    at module.module(MyObject)(module.mtl:0)
    at module.module(MyObject)(module.mtl:7)

After some debugging it seemed as if the main module could not pass a model object to a template that is located in another bundle. We have structured our model generator to have a common part and a project specific bundle. At first it looked like as it this stopped working by an unknown reason.

After some testing, flipping switches, debugging, restructuring I found out that the solution was rather simple. The same switch (Binary, XMI) has to be set to the common generator module as well. All modules must agree to a model format (either binary or XMI).

While I can understand this for the generator that is launched, I cannot understand this for submodules that provide common generating templates and should not even care about the serialization form. But it is as it is ;-)

Fancy tooltips in Eclipse

Tooltips are quick way to add information to a widget that received the users attention. While one can argue about the pros and cons of tooltips this post focuses on the style of tooltips once you decided to use them.

Continue reading

Some thoughts on software testing

If you are working as a software developer in a project based development environment you will, hopefully, encounter the day the customer wants to see the result that was promised to him. The worst thing that can happen is that after months of development you finally end up in a scenario of it is not working or it is not what we need. Sure there are numerous reasons of why this happened and what types development process you could have used. But often quality management and software development definitions are written once and never lived as described. developers consider it a burden that is unnecessary and is blocking them in their daily task of creating new functionality.

Continue reading

Access to WMI in Java using Eclipse SWT OLE integration

Today I ran into a problem which could easily solved using a short WMI query. The problem was that the query must be executed within a Java UI application. Googling for a solution I came only up with either quite some ugly workarounds (like generating a VBScript fragment, forking off the VBScript runtime and parsing the result) or some full blown COM/DCOM interfaces (like J-Integra or J-Interop). Although I really like J-Interop (we are using it for DCOM when accessing OPC server in OpenSCADA Utgard) it has some drawbacks. For J-Interop every access (even local access) is a network based access. Since J-Interop only supports DCOM it is free of any platform specific code but required the machine to be accessible using “remoting” functionality (DCOM). Since I wanted to query the WMI from a UI application and I am sure that the WMI query will stay on the Win32 version of the application I was not keen on adding “remoting” as a requirement to the UI application.

After some digging I remembered that SWT brings an OLE interface which provides direct COM access. So I started poking around and finally come up with a solution that works quite well.

For the impatient: The full source code is available from github https://github.com/ctron/wmisample and the screenshot is here.

Continue reading