public static Comments . Comment getCommentAround ( R run , WordprocessingMLPackage document ) { try { if ( run instanceof Child ) { Child child = ( Child ) run ; ContentAccessor parent = ( ContentAccessor ) child . getParent ( ) ; if ( parent == null ) return null ; CommentRangeStart possibleComment = null ; boolean foundChild = false ; for ( Object contentElement : parent . getContent ( ) ) { // so first we look for the start of the comment if ( XmlUtils . unwrap ( contentElement ) instanceof CommentRangeStart ) { possibleComment = ( CommentRangeStart ) contentElement ; } // then we check if the child we are looking for is ours else if ( possibleComment != null && child . equals ( contentElement ) ) { foundChild = true ; } // and then if we have an end of a comment we are good! else if ( possibleComment != null && foundChild && XmlUtils . unwrap ( contentElement ) instanceof CommentRangeEnd ) { try { BigInteger id = possibleComment . getId ( ) ; CommentsPart commentsPart = ( CommentsPart ) document . getParts ( ) . get ( new PartName ( "/word/comments.xml" ) ) ; Comments comments = commentsPart . getContents ( ) ; for ( Comments . Comment comment : comments . getComment ( ) ) { if ( comment . getId ( ) . equals ( id ) ) { return comment ; } } } catch ( InvalidFormatException e ) { logger . warn ( String . format ( "Error while searching comment. Skipping run %s." , run ) , e ) ; } } // else restart else { possibleComment = null ; foundChild = false ; } } } return null ; } catch ( Docx4JException e ) { throw new DocxStamperException ( "error accessing the comments of the document!" , e ) ; } }
Returns the comment the given DOCX4J run is commented with .