Hackubau Word Generator by template.docx
Mircrosoft Word (.docx) & OpenOffice (.docx) compatibility
Please write to me hck@hackubau.it if you have any questions.
What is this?
This is a Service to perform susbstitution of placeholders in .docx files (templates) writing simply something like this in the word template:
${yourObject.yourField}
${yourPlaceholder}
${today}
${yourObject.yourField.yourEventuallyNestedField}
${list_yourObject.yourField1@separator#yourField2.nestedField}
You can easly pass your custom Object/List of objects and the engine will retrieve everything! The engine will read the document to find the placeholders and then, for each of them, choose the right object from the provided parameters (objs, listObj or fixedMappings) and invoke the GET methods specified by the placeholder itself
Let me see!
API - Step by step guide
It is STRONGLY RECOMMENDED to follow EVERY steps starting by step 1 (it will take just about 5 min, seriously!)
I recommend this just because the guide have been wrote as a lessons-pack with growing-complexity-concepts.
- Today keyword
- HashMap key-value mappings
- Object mappings
- Object mappings with personalized identifiers
- List<Object> mappings with recursively printing
- List<Object> mappings - concatenate fields and set a separator
- Just modality randomly mixed together, not really important to see, it'sjust if you need more brain complexity.
(0) Today keyword
This is just a real banality.
template.docx
Today is the ${today}.
java (pseudocode)
//just invoke the service - today is a coded value used for the italian pizza-mario date
docxService.generateDocument(template.docx, output.docx, null,null,null);
out.docx
Today is the 19/03/2019
(Modality 1) HashMap key-value mappings
template.docx
This is the document of ${name}.
${name} happiness level is: ${happiness}!
java (pseudocode)
HashMap<String, String> maps;
maps.put("name","giorgio");
maps.put("happiness","cioppy bau");
docxService.generateDocument(template.docx, maps, output.docx);
out.docx
This is the document of Giorgio.
Giorgio happiness level is: cioppi bau!
(Modality 2.0) Object mappings
template.docx
This is the document of ${anagrafics.name}.
${anagrafics.name} happiness level is: ${anagrafics.happiness.actualLevel}!
java (pseudocode)
//instantiating my object that i want to use in .docx template (it must be extending HckReflect)
Anagrafics giorgio = new Anagrafics();
giorgio.setName("Giorgio");
//creating the giorgio's happiness value - (it is another object so it must be extending HckReflect too if i want to use it in .docx template)
Happiness happy = new Happiness("Cioppy Bau!");
//setting giorgio's happiness property
giorgio.setHappiness(happy);
//preparing my list of objects
List<HckReflect> myObjects = Lists.newArrayList();
myObjects.add(giorgio);
docxService.generateDocument(template.docx, outputFile, myObjects ,null,null)
out.docx
This is the document of Giorgio.
Giorgio happiness level is: Cioppi Bau!!
(Modality 2.1) Object mappings with personalized identifiers
It become usefull when you have more thant 1 object of the same class to be mapped in the document
template.docx
This is the document of ${father.name}, the father of ${child.name}
java (pseudocode)
//instantiating my objects that i want to use in .docx template (it must be extending HckReflect)
Anagrafics fatherObj = new Anagrafics();
Anagrafics childrenObj = new Anagrafics();
fatherObj.setName("Mario");
childrenObj.setName("Robinhood");
//N.B. identifier propery is ereditated by HckReflect, now i am setting a personalized identifier wich will be used in the .docx mapping
fatherObj.setIdentifier("father");
childrenObj.setIdentifier("child");
///preparing my *list of objects*
List<HckReflect> myObjects = Lists.newArrayList();
myObjects.add(fatherObj);
myObjects.add(childrenObj);
docxService.generateDocument(template.docx, outputFile, myObjects ,null,null)
out.docx
This is the document of Mario, the father of Robinhood
(Modality 3.0) List<Object> mappings with recursively printing
For example if you have to print the list of someone's childs
Special Keywords glossary:
list_
Place this keyword before your object identifier.
It will say to the engine that we want to print a list of objects.
template.docx
Theese are your childrens:
${list_anagrafics.name}
java (pseudocode)
//instantiating my objects that i want to use in .docx template (it must be extending HckReflect)
Anagrafics child1 = new Anagrafics("Giorgio");
Anagrafics child2 = new Anagrafics("Mario");
Anagrafics child3 = new Anagrafics("Pippo");
//now just prepare the List of childs
List<HckReflect> yourChilds = Lists.newArrayList();
yourChilds.add(child1);
yourChilds.add(child2);
yourChilds.add(child3);
//and here preparing my list of *list of objects*
List<HckReflect> myListObjectsList = Lists.newArrayList();
myListObjectsList.add(yourChilds);
docxService.generateDocument(template.docx, outputFile,null, myListObjectsList ,null)
out.docx
Theese are your childrens:
Giorgio, Mario, Pippo
(Modality 3.1) List<Object> mappings - concatenate fields and set a separator
For example if you have to print the list of someone's childs but you want specificy more fields and choose a personalized separator
Special Keywords glossary:
list_
Place this keyword before your object identifier.
It will say to the engine that we want to print a list of objects.
#
When you are dealing with lists, you can ask the engine to print multiple field for every single record.
This is the placeholder that you have to place between every field you want to print.
@
This is the separator keyword.
It say to the engine what char you want as separator.
Place it after the field you want to retrieve and write your separator chars after it
If you don't specify a separator, it use the default one's.
template.docx
Theese are your childrens:
${list_anagrafics@\r\n.name#mother.name#mother.surname@-#age}
java (pseudocode)
//instantiating my objects that i want to use in .docx template (it must be extending HckReflect)
Anagrafics child1 = new Anagrafics("Giorgio");
Anagrafics child2 = new Anagrafics("Mario");
Anagrafics child3 = new Anagrafics("Pippo");
//Mother class must be extending HckReflect because i want to call it in .docx
child1.setMother(new Mother("Lisa","asiL"));
child2.setMother(new Mother("Unknow","Unknow"));
child3.setMother(new Mother("The crazy one","Many many crazy"));
child1.setAge(10);
child2.setAge(12);
child3.setAge(13);
//now just prepare the List of childs
List<HckReflect> yourChilds = Lists.newArrayList();
yourChilds.add(child1);
yourChilds.add(child2);
yourChilds.add(child3);
//and here preparing my list of *list of objects*
List<HckReflect> myListObjectsList = Lists.newArrayList();
myListObjectsList.add(yourChilds);
docxService.generateDocument(template.docx, outputFile,null, myListObjectsList ,null)
out.docx
Theese are your childrens:
Giorgio, Lisa-asiL, 10
Mario, Unknow-Unknow, 12
Pippo, The crazy one-Many many crazy, 13
Of course you can mix up as many lists , objects and key-values as you wany
(Modality 4) HardCore mode - all mixed together very very randomly!
Special Keywords glossary:
list_
Place this keyword before your object identifier.
It will say to the engine that we want to print a list of objects.
#
When you are dealing with lists, you can ask the engine to print multiple field for every single record.
This is the placeholder that you have to place between every field you want to print.
@
This is the separator keyword.
It say to the engine what char you want as separator.
Place it after the field you want to retrieve and write your separator chars after it
If you don't specify a separator, it use the default one's.
template.docx
Today is ${today}.
In my hashmap cioppy stay for ${cioppy}
Your city is ${you.address.city}
I think that ${Anagrafics.favouriteGame.fancyName} is one of the best game evvah!
Theese are the best words of the world:
${list_bestWords@\r\n.description@---#author}
Theese are the children of ${father.name}:
${list_anagrafics@\r\n.name#mother.name#mother.surname@-#age}
java (pseudocode)
//instantiating all the objects that i want to use in .docx template (them must be extending HckReflect)
Anagrafics me = new Anagrafics("me");
Anagrafics you = new Anagrafics("Jonny");
Anagrafics fath = new Anagrafics("Roberto");
fath.setIdentifier("father");
you.setIdentifier("you");
you.setAddress(new Address("Milan"));
//simple hashmap key value
HashMap<String, String> maps;
maps.put("cioppy","bau");
(wait - Just to show to you how Game.class is defined in my mind) class Game extends HckReflect{ String name; setName(String name){this.name=name}; getName(){return name}; getFancyName(){return "+*+*"+name+"*+*+";} } (ok, you can continue now)
Game favGame = new Game();
g.setName("undertale");
me.setFavouriteGame(favGame);
//building the "list_bestWords" mapping object
Words w1 = new Words("bau","me");
Words w2 = new Words("cioppy","barik");
Words w3 = new Words("cioppi","me");
//i am setting the desired personalized identifier to the first element of my list (the one that will be checked by the engine)
w1.setIdentifier("bestWords");
List<HckReflect> bestWordsList = Lists.newArrayList();
bestWordsList.add(w1);
bestWordsList.add(w2);
bestWordsList.add(w3);
//and here the same childrens of the previous example...
Anagrafics child1 = new Anagrafics("Giorgio");
Anagrafics child2 = new Anagrafics("Mario");
Anagrafics child3 = new Anagrafics("Pippo");
//Mother class must be extending HckReflect because i want to call it in .docx
child1.setMother(new Mother("Lisa","asiL"));
child2.setMother(new Mother("Unknow","Unknow"));
child3.setMother(new Mother("The crazy one","Many many crazy"));
child1.setAge(10);
child2.setAge(12);
child3.setAge(13);
//now just prepare the List of childs
List<HckReflect> yourChilds = Lists.newArrayList();
yourChilds.add(child1);
yourChilds.add(child2);
yourChilds.add(child3);
//now list of single objects
List<HckReflect> myListOfObj = Lists.newArrayList();
myListOfObj.add(me);
myListOfObj.add(you);
myListOfObj.add();
//and here preparing my list of *list of objects*
List<HckReflect> myListObjectsList = Lists.newArrayList();
myListObjectsList.add(yourChilds);
myListObjectsList.add(bestWordsList);
docxService.generateDocument(template.docx, outputFile,myListOfObj, myListObjectsList ,maps)
out.docx
Today is 20/03/2019
In my hashmap cioppy stay for bau
Your city is Milan
I think that +*+*Undertale*+*+ is one of the best game evvah!
Theese are the best words of the world:
bau---me
cioppy---barik
cioppi---me
Theese are the childrens of Roberto:
Giorgio, Lisa-asiL, 10
Mario, Unknow-Unknow, 12
Pippo, The crazy one-Many many crazy, 13
POM.XML or others package managers
<< Here you can find gradle, groovy and others package managers entries >>
MAVEN pom.xml
<dependency>
<groupId>it.hackubau</groupId>
<artifactId>hackubau-docs</artifactId>
<version>1.0-RELEASE</version>
</dependency>
Please write to me hck@hackubau.it if you have any questions.