ingomohr / docwriter

API to write docx documents
MIT License
1 stars 0 forks source link

Support Passing Writer-Constructor-Parameter-Values to Rule-Constructors in #initRules #28

Closed eum2o closed 4 years ago

eum2o commented 4 years ago

In a concrete writer you have to implement the abstract method org.ingomohr.docwriter.docx.AbstractRuleBasedDocxWriter.initRules(). Since this method is called by the no-args-constructor of the AbstractRuleBasedDocxWriter (which is called before the constructor of the concrete writer is called) it's not possible to access parameters passed to the constructor of your concrete writer instance.

Example: (see comment in #initRules)

class MyWriter extends org.ingomohr.docwriter.docx.AbstractRuleBasedDocxWriter {

   final Object param;

   MyWriter (Object pParam) {
      this.param = pParam;
   }

   @Override
   protected List<DocumentRule> initRules() {
       // I cannot do something like "new MyRule(param);" because param is not yet set at this time.
      return Collections.emptyList();     
   }
}

Workaround: Override de.biotronik.biodocwriter.docs.documents.AlmDocumentPlaceholderReplaceWriter.modifyDoc(WordprocessingMLPackage) with something like this:

   @Override
   protected void modifyDoc(WordprocessingMLPackage pDoc) {
       super.modifyDoc(pDoc);
       new MyRule(param).apply(pDoc);
   }

while the #initRules method returns an empty list as shown in the first snippet.

ingomohr commented 4 years ago

Subclasses of AbstractRuleBasedWritercan now call init() themselves and suppress automatic initialization at construction time.