Class DOCXEngine

All Implemented Interfaces:
IExtensionTemplateEngine, com.nomagic.magicreport.engine.IRuntimeInstanceVelocityEngine, ITemplateEngine, StyledDocument

@OpenApiAll public class DOCXEngine extends DefaultTemplateEngine implements StyledDocument
The Velocity engine for OpenXML Word Document (DOCX).
Since:
Oct 19, 2009
  • Constructor Details

    • DOCXEngine

      public DOCXEngine()
      A class constructor.
  • Method Details

    • isInMultipleLineComment

      public boolean isInMultipleLineComment()
    • setInMultipleLineComment

      public void setInMultipleLineComment(boolean inMultipleLineComment)
    • isHasNumbering

      public boolean isHasNumbering()
    • setHasNumbering

      public void setHasNumbering(boolean hasNumbering)
    • getAbStractId

      public int getAbStractId()
    • setAbStractId

      public void setAbStractId(int abStractId)
    • getNumId

      public int getNumId()
    • setNumId

      public void setNumId(int numId)
    • getBookmarkN

      public int getBookmarkN()
    • setBookmarkN

      public void setBookmarkN(int bookmarkN)
    • getDocument

      public Document getDocument()
    • setDocument

      public void setDocument(Document document)
    • getImageFormat

      public List<String> getImageFormat()
    • setImageFormat

      public void setImageFormat(List<String> imageFormat)
    • getHyperLinkMap

      public Map<String,String> getHyperLinkMap()
    • setHyperLinkMap

      public void setHyperLinkMap(Map<String,String> hyperLinkMap)
    • getImageMap

      public Map<String,String> getImageMap()
    • setImageMap

      public void setImageMap(Map<String,String> imageMap)
    • getEntryNameSet

      public Set<String> getEntryNameSet()
    • setEntryNameSet

      public void setEntryNameSet(Set<String> entryNameSet)
    • getEntryMap

      public Map<String,File> getEntryMap()
    • setEntryMap

      public void setEntryMap(Map<String,File> entryMap)
    • getBookmarkList

      public Set<String> getBookmarkList()
    • setBookmarkList

      public void setBookmarkList(Set<String> bookmarkList)
    • getBookmarkListMap

      public Map<String,com.nomagic.magicreport.engine.velocity.DOCXEngine.Bookmark> getBookmarkListMap()
    • setBookmarkListMap

      public void setBookmarkListMap(Map<String,com.nomagic.magicreport.engine.velocity.DOCXEngine.Bookmark> bookmarkListMap)
    • getNumberingDef

      public Map<String,String> getNumberingDef()
    • setNumberingDef

      public void setNumberingDef(Map<String,String> numberingDef)
    • getAbstractNumbering

      public Map<String,List<String[]>> getAbstractNumbering()
    • setAbstractNumbering

      public void setAbstractNumbering(Map<String,List<String[]>> abstractNumbering)
    • getDocProp

      public List<String> getDocProp()
    • setDocProp

      public void setDocProp(List<String> docProp)
    • getEntryName

      public String getEntryName()
    • setEntryName

      public void setEntryName(String entryName)
    • getCharsetName

      public String getCharsetName()
    • setCharsetName

      public void setCharsetName(String charsetName)
    • getOoxmlTemplate

      public OOXMLTemplate getOoxmlTemplate()
    • setOoxmlTemplate

      public void setOoxmlTemplate(OOXMLTemplate ooxmlTemplate)
    • getDocPropList

      public List<String> getDocPropList()
    • setDocPropList

      public void setDocPropList(List<String> docPropList)
    • getStyleNameMap

      public Map<String,String> getStyleNameMap()
    • setStyleNameMap

      public void setStyleNameMap(Map<String,String> styleNameMap)
    • updateStyleNameMap

      public void updateStyleNameMap(String name, String id)
    • getStyleIds

      public Set<String> getStyleIds()
    • setStyleIds

      public void setStyleIds(Set<String> styleIds)
    • updateStyleIds

      public void updateStyleIds(String styleId)
    • getPageSections

      public String getPageSections(int index)
    • addPageSections

      public int addPageSections(String str)
    • createTemplate

      public Template createTemplate()
      Creates a new Template object for generating report by this engine.
      Specified by:
      createTemplate in interface ITemplateEngine
      Overrides:
      createTemplate in class DefaultTemplateEngine
      Returns:
      an instance of Template
      See Also:
    • createTemplate

      public Template createTemplate(File input, File output) throws IOException
      Creates a new Template object.
      Specified by:
      createTemplate in interface ITemplateEngine
      Overrides:
      createTemplate in class DefaultTemplateEngine
      Parameters:
      input - template file.
      output - output file.
      Returns:
      an instance of Template
      Throws:
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened.
      IOException - If an I/O error occurs
    • createTemplate

      public Template createTemplate(File input, File output, boolean ignoreLocation) throws IOException
      Creates a new Template object.
      Overrides:
      createTemplate in class DefaultTemplateEngine
      Parameters:
      input - template file.
      output - output file.
      ignoreLocation - true to ignore location property for child document
      Returns:
      an instance of Template
      Throws:
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened.
      IOException - If an I/O error occurs
    • createTemplate

      public Template createTemplate(File input, File output, boolean ignoreDocInfo, boolean ignoreLocation) throws IOException
      Creates a new Template object.
      Parameters:
      input - template file.
      output - output file.
      ignoreDocInfo - true to ignore create document info. this parameter should be true for creating template of child document
      ignoreLocation - true to ignore location property for child document
      Returns:
      an instance of Template
      Throws:
      FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened.
      IOException - If an I/O error occurs
    • evaluate

      public void evaluate(Template template) throws TemplateException
      Merge a template with current context and rendered stream into the writer.
      Specified by:
      evaluate in interface ITemplateEngine
      Overrides:
      evaluate in class AbstractTemplateEngine
      Parameters:
      template - Template being evaluated
      Throws:
      ParseErrorException - if a syntax or other error which prevents it from being parsed.
      InitializationEngineException - error while initializing engine
      TemplateException - other error while processing the template
      See Also:
    • setWriterForEvaluate

      protected BufferedWriter setWriterForEvaluate(Template template)
      Overrides:
      setWriterForEvaluate in class DefaultTemplateEngine
    • handleSharedFiles

      protected void handleSharedFiles()
      Handle document.xml.rels, [Content_Types].xml, styles.xml, and numbering.xml
    • handleRelationship

      protected void handleRelationship(OOXMLStreamWriter ooxmlWriter, File sectionZipFile, Map<String,? extends com.nomagic.magicreport.engine.ooxml.child.docx.DocumentRelationship> relationshipMap, String relationshipPrefix, Map<String,List<String>> newFilesInZip, StringBuilder relationContentBuilder)
      Handle relationship file after evaluate.
      Parameters:
      ooxmlWriter - writer
      sectionZipFile - zip file of child template
      relationshipMap - map of relationship and id
      relationshipPrefix - prefix path for relationship
      newFilesInZip - map of new files that are created after generated
      relationContentBuilder - stringbuilder to keep relationship content
    • handleContentType

      protected void handleContentType(Set<com.nomagic.magicreport.engine.ooxml.child.docx.DocumentContentType> contentTypes, Set<com.nomagic.magicreport.engine.ooxml.child.docx.DocumentContentType> sectionContentTypes, StringBuilder contentTypeContentBuilder, Map<String,List<String>> newFilesInZip)
      Handle [Content_Type].xml after evaluate.
      Parameters:
      contentTypes - content type in main document
      sectionContentTypes - content type in child document
      contentTypeContentBuilder - Stringbuilder to create new content of [Content_Type].xml
      newFilesInZip - map of partname and new target path
    • handleStyle

      protected Document handleStyle(DocumentBuilder docBuilder, Document baseStyleDocument, File sectionZipFile, StringBuilder styleContent, List<String> styleIds, List<String> styleLatents, Map<String,String> namespaceMap)
      Handle styles.xml after evaluate.
      Parameters:
      docBuilder - DocumentBuilder
      baseStyleDocument - base document
      sectionZipFile - zip file to be added
      styleContent - all content of style
      styleIds - existing style
      styleLatents - existing latents
      namespaceMap - namespaces of style
      Returns:
      base document
    • handleNumbering

      protected Document handleNumbering(DocumentBuilder docBuilder, Document baseNumberingDocument, File sectionZipFile, StringBuilder numberingContent, List<String> abstractNumIds, List<String> numIds, Map<String,String> numberingNamespaceMap)
      Handle numbering.xml.
      Parameters:
      docBuilder - DocumentBuilder
      baseNumberingDocument - base document
      sectionZipFile - zip file
      numberingContent - numbering content
      abstractNumIds - list of existing abstractNumId
      numIds - list of existing numId
      numberingNamespaceMap - namespaces of numbering
      Returns:
      base document
    • getDocumentSubFileInfo

      public void getDocumentSubFileInfo(Template template, String name, DefaultHandler handler)
      Get content of sub file before evaluate
      Parameters:
      template - a template
      name - entry name
      handler - file handler
    • getEntryContent

      public void getEntryContent(Template template, String entryName)
      Get entry from input file and entryName. Then set reader to template.
      Parameters:
      template - OOXML template
      entryName - entry file name
    • intenalEvaluate

      public void intenalEvaluate(Template template) throws TemplateException
      Throws:
      TemplateException
    • translate

      public void translate(Template template, StringBuilder inProcessBuffer) throws ParseErrorException, IOException
      Process the template in inProcessBuffer into valid format.
      Overrides:
      translate in class DefaultTemplateEngine
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
      IOException - if IO Error occurs while reading template.
    • translate

      public void translate(Template template, StringBuilder inProcessBuffer, DocumentInfo child) throws ParseErrorException, IOException
      Process the template in inProcessBuffer into valid format.
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      child - document information to update some value in template
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
      IOException - if IO Error occurs while reading template.
    • translate

      public void translate(Template template, StringBuilder inProcessBuffer, String entryName, DocumentInfo child) throws ParseErrorException, IOException
      Process the template in inProcessBuffer into valid format.
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      entryName - entry name
      child - document information to update some value in template
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
      IOException - if IO Error occurs while reading template.
    • createParseErrorWrapper

      protected ParseErrorException createParseErrorWrapper(ParseErrorException parseError, Reader source)
      Create a ParseErrorException from the message.
      Parameters:
      parseError - root cause
      source - source template
      Returns:
      instance of ParseErrorException
    • processVelocityException

      protected TemplateException processVelocityException(Template template, org.apache.velocity.exception.ParseErrorException e)
      Call this method to process an Velocity exception and return in new format. Override DefaultTemplateEngine.processVelocityException(Template, org.apache.velocity.exception.ParseErrorException) to return exception with valid ODF line count.
      Overrides:
      processVelocityException in class DefaultTemplateEngine
      Parameters:
      template - Template
      e - Velocity exception
      Returns:
      TemplateException
    • lineCount

      public int lineCount(String content)
      Count number of line from given content string.
      Overrides:
      lineCount in class DefaultTemplateEngine
      Parameters:
      content - the input string
      Returns:
      number of line
    • getEntrySet

      public Set<String> getEntrySet()
      Return DOCX entry set.
      Returns:
      DOCX entry set.
    • getTemplate

      public Template getTemplate()
      Return template.
      Returns:
      a DOCX template
    • handleForPage

      protected void handleForPage(Template template, StringBuilder inProcessBuffer) throws ParseErrorException
      Handle the #forpage directive.
      Overrides:
      handleForPage in class DefaultTemplateEngine
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • handleForRow

      protected void handleForRow(Template template, StringBuilder inProcessBuffer) throws ParseErrorException
      Handle the #forrow directive.
      Overrides:
      handleForRow in class DefaultTemplateEngine
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • handleGridCol

      protected void handleGridCol(Template template, StringBuilder inProcessBuffer)
      Add id to w:gridCol tag.
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
    • handleForColumn

      protected void handleForColumn(Template template, StringBuilder inProcessBuffer) throws ParseErrorException
      Handle the #forcol directive.
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • handleMergeColumns

      protected void handleMergeColumns(Template template, StringBuilder inProcessBuffer) throws ParseErrorException
      Handle $tableprop.mergeColumns directive
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • handleMergeRows

      protected void handleMergeRows(Template template, StringBuilder inProcessBuffer) throws ParseErrorException
      Handle $tableprop.mergeRows directive
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • handleIncludeSection

      protected void handleIncludeSection(Template template, StringBuilder inProcessBuffer) throws ParseErrorException
      Handle the #includeSection directive.
      Overrides:
      handleIncludeSection in class DefaultTemplateEngine
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • handleIncludeSection

      protected void handleIncludeSection(Template template, StringBuilder inProcessBuffer, DocumentInfo child) throws ParseErrorException
      Handle the #includeSection directive.
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      child - DocumentInfo of child template
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • handleSectionBegin

      protected void handleSectionBegin(Template template, StringBuilder inProcessBuffer) throws ParseErrorException
      Handle the #sectionBegin directive.
      Overrides:
      handleSectionBegin in class DefaultTemplateEngine
      Parameters:
      template - Template source.
      inProcessBuffer - in process buffer.
      Throws:
      ParseErrorException - when error while translation, or syntax not recognized.
    • createComment

      public String createComment(String message)
      Return a formatted comment from given message. The default comment format return from this method is "" where {0} is message;
      Overrides:
      createComment in class DefaultTemplateEngine
      Parameters:
      message - message to put in document comment
      Returns:
      comment
    • postProcess

      protected void postProcess(Template template) throws TemplateException
      Perform a post-processing after the template has been evaluated.
      Overrides:
      postProcess in class DefaultTemplateEngine
      Parameters:
      template - template after evaluated.
      Throws:
      ParseErrorException - if a syntax or other error which prevents it from being parsed.
      InitializationEngineException - error while initializing engine
      TemplateException - other error while processing the template
      See Also:
    • destroy

      public void destroy()
      Description copied from class: DefaultTemplateEngine
      Called by the application or TemplateEngineFactory to inform this engine that it should destroy any resources that it has allocated.

      A subclass of engine should override this method if it has any operation that it wants to perform before it is destroyed.

      Specified by:
      destroy in interface ITemplateEngine
      Overrides:
      destroy in class DefaultTemplateEngine
    • addFont

      public int addFont(Font font)
      Add font for HTML to DOCX.
      Specified by:
      addFont in interface StyledDocument
      Parameters:
      font - font
      Returns:
      font id
    • addList

      public int addList(List list)
      Add list for HTML to DOCX.
      Specified by:
      addList in interface StyledDocument
      Parameters:
      list - list
      Returns:
      list id
    • addStyle

      public int addStyle(Style style)
      Add style for HTML to DOCX.
      Specified by:
      addStyle in interface StyledDocument
      Parameters:
      style - style
      Returns:
      style id
    • addColor

      public int addColor(Color color)
      Add color for HTML to DOCX.
      Specified by:
      addColor in interface StyledDocument
      Parameters:
      color - color
      Returns:
      color id
    • addHyperLink

      public void addHyperLink(String rId, String url)
      Add rId for hyper link.
      Parameters:
      rId - rId
      url - URL
    • addImage

      public void addImage(String rId, String url, String imageType)
      Add rId for hyper link.
      Parameters:
      rId - rId
      url - URL
      imageType - imageType
    • addRowSpanID

      public void addRowSpanID(String id, int value)
      Add rowspan id and its value to map
      Parameters:
      id - rowspan id
      value - number of rows to be merged
    • addObject

      public int addObject(Object object)
      Appends object content into the document.
      Specified by:
      addObject in interface StyledDocument
      Parameters:
      object - adding object
      Returns:
      index to object
    • getDocInfo

      public DocumentInfo getDocInfo()
    • setDocInfo

      public void setDocInfo(DocumentInfo docInfo)
    • getMainDocInfo

      public DocumentInfo getMainDocInfo()
    • getEvaluatedDocument

      public com.nomagic.magicreport.engine.ooxml.child.docx.IEvaluatedDocument getEvaluatedDocument()
    • setEvaluatedDocument

      public void setEvaluatedDocument(com.nomagic.magicreport.engine.ooxml.child.docx.IEvaluatedDocument evaluatedDocument)
    • getEvaluatedDocumentBuffer

      public StringBuilder getEvaluatedDocumentBuffer()
    • isMemoryMode

      public boolean isMemoryMode(Template template)
      Description copied from class: AbstractTemplateEngine
      Tests if this template should process in memory mode.
      This method is similar to code:
       
       template.getSize() > properties.getProperty(TemplateConstants.TEMPLATE_PROCESS_SIZE, "0")
       
       
      Overrides:
      isMemoryMode in class AbstractTemplateEngine
      Parameters:
      template - a test Template
      Returns:
      true if template size is less than TEMPLATE_PROCESS_SIZE
    • updateSize

      public void updateSize(long size)
    • setCheckSize

      public void setCheckSize(boolean isCheckFileSize)
    • addChart

      public void addChart()