Page 1 of 1

VariablePrepare.prepare does not work for multiple variables

PostPosted: Wed Nov 27, 2013 11:26 pm
by atul.tewari
We have 5-6 variables and each of these unfortunately, run into variable runs. The first variable in the map gets replaced correctly with joinRuns()/prepare. However, other variables ${ gets replaced and the new text is displayed with the variable name without the ${ }.

Is this a known issue?

Thanks!
AT

Re: VariablePrepare.prepare does not work for multiple varia

PostPosted: Thu Nov 28, 2013 5:53 am
by jason
atul.tewari wrote:Is this a known issue?


No. Please post a docx exhibiting the issue.

Re: VariablePrepare.prepare does not work for multiple varia

PostPosted: Thu Nov 28, 2013 1:21 pm
by atul.tewari
Thanks Jason. I resolved this another way. The doc has confidential information, but basically, I created this document on Mac Word 2011 with several sections and images and 5-6 variables used multiple times in various parts of the document.

But I ended up developing the following code:

Code: Select all
public static void replacePlaceholder(WordprocessingMLPackage template, String name, String placeholder )
   {
      List<Object> texts = getAllElementFromObject(template.getMainDocumentPart(), Text.class);

      
      for (int i = 0 ; i < texts.size(); i++)
      {
         Object text = texts.get(i);
         Text textElement = (Text) text;
         
         if(textElement.getValue().contains("${"))
         {
            // if does not contain look ahead
            if(!textElement.getValue().contains("}"))
            {
               String variableName = textElement.getValue();
               
               // Apply a counter to avoid infinite loop
               // While this will not happen, it is better to handle this
               int j = 0;
               while(true && j++ < 100)
               {
                  Object innerText = texts.get(++i);
                  Text innerTextElement = (Text) innerText;
                  variableName += innerTextElement.getValue();
                  if(innerTextElement.getValue().contains("}"))
                  {
                     innerTextElement.setValue("");
                     textElement.setValue(variableName);
                     break;
                  }
                  else
                  {
                     innerTextElement.setValue("");
                  }
               }
            }
         }
         if (textElement.getValue().contains(placeholder))
         {
            try
            {
               //First extract the string
               String newstring = textElement.getValue();
               newstring = newstring.replace(placeholder, name);
               textElement.setValue(newstring);
            }
            catch(Exception e)
            {
               log.error(e);
               e.printStackTrace();
            }
         }
      }
   }

Re: VariablePrepare.prepare does not work for multiple varia

PostPosted: Thu Feb 11, 2016 12:51 am
by gisly
Hi! Sorry for raising this topic but i've got a similar issue: only some of the variables are replaced.
Here is an example of the file:
RELEASE_NOTES_no_conf_info.docx
a file with the replacement problem
(30.34 KiB) Downloaded 377 times

All variables are replaced, except for the "barFileName" variable. However, the mapping contains it, the value is a normal string (not null or empty), and I've also applied VariablePrepare.prepare