Inserting HTML into word documents

Document generation is one of the most useful things that any system can do for users as basically users tend to think in terms of Microsoft word and Excel documents and in my opinion the best library for producing them via Java is http://www.docx4java.org/

This library (I am using version 2.8) allows you to build everything from scratch, but its far easier to start with a template document normally supplied by a client and just substitute the values you want

so lets do that:

First create your empty “WordprocessingMLPackage” package which is the holding object for your word document, and create a normal java File object using your template file

Then load that file into your WordprocessingMLPackage

WordprocessingMLPackage wordMLPackage;
File templatefile = new File("C:\mytemplatefile.docx");
wordMLPackage = WordprocessingMLPackage.load(templatefile);

Ok, so now we have a word document, but its just a generic document its not personalised to our clients needs, to personalise it we first need to put placeholders in the template for our data to go in, these are just text in the word document in the format ${xxxxxxx} as you can see below

docx4j1.png

Once these are in you can use them to substitute your text
first build and populate yourself a hashmap of the values you want substituting

HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("Replace_Tex1", "This is some custome data");

As you can see the first part needs to be the placeholder name and the second part the value you want to be inserted.
Once that is ready you can do the substitution, but first you need to convert your existing WordprocessingMLPackage to XML so you have something easier to work with

String xml = XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true);

with the resultant xml and the hashmap we can use the XmlUtils.unmarshallFromTemplate function to do the swap then stuff the result back into the WordprocessingMLPackage

wordMLPackage.getMainDocumentPart().setJaxbElement((org.docx4j.wml.Document) XmlUtils.unmarshallFromTemplate(xml, mappings));

And that is us done we can just “SaveToZipFile” (basically docx files are just renamed zip files with XML inside them) and save the file to the fie system

SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save("C:\finaldocument.docx");

But where you ask was the html insertion you promised, you have just shown us plain text, where is the formatted html inserts.
For that dear reader with need to add a bunch more code
There is no built in function to do that for HTML so we are going to have to:
1. Get a list of all the object (such as paragraphs, lines of text etc etc) in the WordprocessingMLPackage
2. Search though then to find the location of the text we want to replace (the placeholder)
3. Remove the Place holder text.
4. Add a wrapper object to our HTML, convert it to DOCX XML and insert it at the correct place.
for No.1 we will pinch from the fabulous Jos Dirksen’s [Create complex Word documents programatically with docx4j](http://www.smartjava.org/content/create-complex-word-docx-documents-programatically-docx4j) to get our list of objects.

private static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
    List<Object> result = new ArrayList<Object>();
    if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();
    if (obj.getClass().equals(toSearch))
        result.add(obj);
    else if (obj instanceof ContentAccessor) {
        List<?> children = ((ContentAccessor) obj).getContent();
        for (Object child : children) {
            result.addAll(getAllElementFromObject(child, toSearch));
        }
    }
    return result;
}

That done we will use that function to go though and search each object for the placeholder text and if its found return the integer that tells us its location in the document (and while its at it, it removes the place holder text)

private int findPlaceHolder(String placeholder, WordprocessingMLPackage template) {
    int index = 0;
    List<Object> paragraphs = getAllElementFromObject(template.getMainDocumentPart(), P.class);
    P toReplace = null;
    for (Object p : paragraphs) {
        List<Object> texts = getAllElementFromObject(p, Text.class);
        for (Object t : texts) {
            Text content = (Text) t;
            if (content.getValue().equals(placeholder)) {
                toReplace = (P) p;
                index = template.getMainDocumentPart().getContent().indexOf(toReplace);
                break;
            }
        }
    }
    if ( toReplace != null ) {
        //remove placeholder
        ((ContentAccessor)toReplace.getParent()).getContent().remove(toReplace);    
    }
    return index;
}

This finally gives us a way to insert the HTML at the correct place, so we do our mapping again, but this time with HTML rather than plain text

mappings.put("Replace_Tex1", "<b>This is some custom html data</b>");

Then loop though the HashMap, finding the correct location, importing the HTML and inserting the HTML into the WordprocessingMLPackage using the addall function.

Iterator iterator = mappings.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry mapEntry = (Map.Entry) iterator.next();
    String xhtml = "<div>" + mapEntry.getValue().toString() + "</div>"; 
    int locationOfItem =  findPlaceHolder(mapEntry.getKey().toString(), wordMLPackage);
    if (locationOfItem > 0) {
        wordMLPackage.getMainDocumentPart().getContent().addAll(locationOfItem , XHTMLImporter.convert(xhtml, null, wordMLPackage)  );   
    }
    xhtml = "";
}

Thats it you can now insert any HTML you want anywhere you want into an existing Document
You can see that I had to wrap the HTML in a “DIV” (any root element will do but DIV is easiest), you have to do this or the XHTMLImporter will fail its validation, also the smallest item the HTML will import in as is a Paragraph so you can not use this method to change text and formatting inside an existing line, you will always get a paragraph break before and after (not really a limitation given the use cases)
As always yell if anything seems off.

New Anime Series: Magical Warfare

First Episode(s) Review for: Magical Warfare
Summary:

A normal Japanese high school boy who happens to be good at Kendo (stop me if you have heard this one before) Saves a young female magician who is being being hunted by her brain washed brother and his gang of evil magicians, the young lads friends join the fight and start manifesting magic of their own (to much comic surprise)

Animation:

The quality varies at different times, you get the feeling that the the artists who are painting the backgrounds are more talented than the ones drawing the characters, but still excellent quality all rounds

Plot Potential:

After a very grim start, it moves happily into “you perv” misunderstanding territory and then back to grim, then on to standard high school battle territory (where the only loser seems to be concrete), it looks like we are going to have to save the world again with the help of epic battles series, but I do like the “attack a magician with magic in the living world and get stripped of your magic” touch

Music:

Good rousing opening music and solid rock end theme, the kind of music that makes people new to anime go “Cooo!! they have proper music on these cartoons”

Reminds me of:

Code Geass is obviously a huge influence but with shades of Tenchi Muyo and a pinch of Needless

Overall:

The visual aspect of this anime totally out ways the plot, but hopefully that should improve, in the mean time its a solid series you could recommend to someone new to the genre, minor note: whats with the bloody stupid swords!!, there are hundreds of viable sword shapes without making up ones that would be naff to use

 

 

 

Disclaimer: These are mini reviews of anime’s that are fresh out in Japan and are not licensed in the UK, buy them once they have been licensed or at the very least buy the merchandise, remember if the anime makers make a loss, THEY WILL STOP MAKING ANIME!!

Connections aide memoire 01: Changing XML config

Now that we are all “connections developers” according to IBM, sooner or later you will need to starting dealing with Web sphere in a serious fashion, this is no simple task, as Web Sphere admin as far more complex that developing for Web sphere and the documentation makes massive assumptions, so I will be doing a series of simple and essential tips for developers so make your life a bit easier, starting off will be changing XML config on connections.

Most of the connections config resides in a file called “LotusConnections-config.xml”, now there are copies of this file all over the place, to change them you check them all out, make the change in one place then check them back in, this not only keeps the changes consistent over your connections installation but it validates your changes and keeps a backup.

To do this first go to the bin directory of your deployment manager, in my test server’s case that is (yours will most likely be very similar except for it being on “E”)

E:IBMWebSphereAppServerprofilesDmgr01bin>

Then enter the wasadmin console with

wsadmin -lang jython

Next run the Connections Config jpython file

execfile("connectionsConfig.py")

Once that is done (after a load of output to the screen) you need to check out the config files, you need to provide a location to check the files out to (just create a directory somewhere safe) and the name of the web sphere cell you want to get the files from (see below on how to get that)

LCConfigService.checkOutConfig("E:working","WIN-L5H5FQ2LKP1Cell01")

If you don’t know the name of the cell you are working on (hell I never know) you can find out by typing the following.

AdminControl.getCell()

You can now alter the files in the directory to suit what ever changes you want to make, the best way though is to use the “updateConfig” command which makes sure that you don’t make any bad types and screw your config up.

LCConfigService.updateConfig("profiles.directory.service.extension.enabled","false")

Once you are happy with your changes, you can check them them back in with the following (you will most likely need to restart the connections apps for the changes to take effect

LCConfigService.checkInConfig()

_NOTE: I’m not a proper Web Sphere admin, If you want proper Web Sphere and Connections admin you want [the Turtle Partnership](http://www.turtleweb.com/)_

IBM Connect 2014 Slide Decks

This year was my busiest year yet at Connect with 6 hours total on stage, and as there is a good chance that technical content will be cut from next years conference these might be my last presentations there, so enjoy

SHOW104 – Practical Java (with Julian Robichaux)

Sunday | 13:30-15:30 PM | Swan Osprey

Take a whirlwind tour of the many ways in which Java can make your life better as a developer. We’ll use Java in IBM Notes, Eclipse, and the latest IDEs. And we’ll show you examples of best of breed libraries that can analyze data, create PDFs, and perform image processing on the fly. Get connected to IBM Connections, access relational data, open sockets, and parse feeds. And along the way we’ll throw in tips for testing, performance, and writing good code.

#####SHOW303 – Proper Connections Development for Proper Domino Developers (with [Matt White](http://mattwhite.me/))

_Tuesday | 10:30-12:15 | Swan Osprey_

This is a session with a mission: to take a much loved IBM Domino app and deploy it to a client’s new IBM Connections environment. We’ll show different solutions such as the use of iWidgets or creating a REST API to make the application available to IBM Connections users and walk you through the process, from beginning to end. If you’re an IBM Domino developer who knows nothing about IBM Connections, this is the session for you.

Source Code Snippets to be uploaded in a bit

IBM Connect 2014 Round up

So I’m back from Connections 2014 and Ignoring a terrible case of the IBM Connect flu (which has been upgraded to include vomiting and diarrhoea) the event has been a great success

The previous 2 years of LotusSphere and IBM connection have been more than a little depressed and to a certain extent ‘desperate’ feeling, this year it felt much different, not the happy go Lucky ‘lets have a party’ of older Lotuspheres, but a heads down lets do some work feeling.

I ended up making far more contacts and having far more business conversations than in previous years, which has ended with the best set of business leads to be got from the conference in many a year. If I was honest this is largely due to the assistance provided to me by The turtle partnership and other members of LDC, which made my skill set perfectly timed for IBMs current focus.

IBM have re-badged us “Connections Developers” when what they really mean is ‘Web sphere Developers’, it’s what they have wanted us to be for years with little success and at last they have found a way to sell it to us without a revolt, this is fine by me as I had already started the transition, but it is a little bit ‘marketing’

The scheduling sucked, sorry but it did, people found lots of sessions with similar subjects matched against each other, there are tracks for a reason people!!, if you are a developer you should be able to follow the developer track without constantly deciding which developer sessions you can attend due to there being 3 on at once.

The social side of the conference was excellently done with Veronica Belmont (who’s autograph I got) providing a professional touch, plenty of things for people to do and the placement of the social cafe was just right as it provided a excellent amount of foot fall though the vendor area (always a good idea)

There were some indication that IBM is tightening the purse strings, such as the lack of entertainment at the party and there only being 4 rides open at the fairground trip, but that was compensated by none of the messing about with speaker tickets that we had last year, mind you as I was working neither worried me, but I might have cared if had paid 2k for a ticket.

My habit of walking though a crowd just listening in order to get a feel for it revealed a surprising large amount of Germanic and eastern European languages and quite a lot of Japanese, far more than I remember in previous years, perhaps market areas to pay attention to in the next year. Hmmmmmm

Conclusion: A good conference all round, and other than the scheduling, well done, if IBM can repeat it next year then I will be there, but if only 30% of the rumours of changes are true, it might only be a conference for management and HR staff, who tend not to ask difficult questions about technical products.

The Other LDC Blogs on the same subject

Matt White

Julian Woodward

LDC Group Photo for Connect 2014: aren’t we beautiful

SAMSUNG CSC