Ignore:
Timestamp:
07/03/11 04:50:48 (11 months ago)
Author:
jharrop
Message:

Tinne's patch: Given an w:tc/w:sdt[contains(@w:tag, 'od:repeat=')] and the repeat results in an empty result set, there is the empty cell again. Introduce "eventually empty lists", which are empty lists whenever this does not produce an w:tc without content.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java

    r1582 r1583  
    671671                                        return newContent; 
    672672                                         
    673                                 } else { 
    674                                         // Remove it 
    675                                          
    676                                   final boolean sdtIsCell = sdt instanceof CTSdtCell; 
    677                                    
    678                                   final Object parent = obtainParent(sdt); 
    679                                   final int contentChildCount = countContentChildren(parent); 
    680                                   final boolean sdtIsSingleCellChild = parent instanceof Tc && contentChildCount == 1; 
    681                                    
    682                                    
    683                                         if (sdtIsCell && ! removeSdtCellsOnFailedCondition) { 
    684                                                 // Return an empty tc 
    685                                                 CTSdtContentCell sdtCellContent = (CTSdtContentCell)((org.docx4j.wml.CTSdtCell)sdt).getSdtContent(); 
    686                                                 Tc tc = (Tc)XmlUtils.unwrap(sdtCellContent.getEGContentCellContent().get(0)); 
    687                                                 tc.getEGBlockLevelElts().clear(); 
    688                                                 // Must contain a paragraph though (at least for Word 2007 and Word 2010) 
    689                                                 P p = Context.getWmlObjectFactory().createP(); 
    690                                                 tc.getEGBlockLevelElts().add(p); 
    691                                                  
    692                                                 List<Object> newContent = new ArrayList<Object>(); 
    693                                                 newContent.add(tc); 
    694                                                 return newContent; 
    695                                                  
    696                                         } else if (sdtIsSingleCellChild) { 
    697                                            
    698                                           // Must contain a paragraph (see above) 
    699                                           final Object p = Context.getWmlObjectFactory().createP(); 
    700                                           final List<Object> newContent = Arrays.asList(p); 
    701              
    702             return newContent; 
    703                                                  
    704                                         } else { 
    705                                          
    706 //                                      log.info("Removed? " + removeSdt(sdtParent, sdt) ); 
    707                                                 return new ArrayList<Object>();  // effectively, delete 
    708                                         } 
     673        } else { 
     674                                  return eventuallyEmptyList(sdt); 
    709675                                } 
    710676                                 
     
    713679                                log.info("Processing Repeat: " + tag.getVal()); 
    714680                                 
    715                                 return processRepeat(sdt, 
     681                                final List<Object> repeatResult = processRepeat(sdt, 
    716682                                                customXmlDataStorageParts, 
    717683                                                wordMLPackage.getMainDocumentPart().getXPathsPart()); 
     684                                 
     685                                if (repeatResult.isEmpty()) { 
     686                                  return eventuallyEmptyList(sdt); 
     687                                   
     688                                } else { 
     689                                  return repeatResult; 
     690                                } 
    718691                                 
    719692                        } else if ( xp!=null) { 
     
    790763                        return null; 
    791764                } 
     765                 
     766  /** 
     767   * Under normal instances, return an empty list in order to remove content. 
     768   *  
     769   * If, however, this would produce a table cell without a content, add an empty content node to this table cell. 
     770   *  
     771   * For backward reasons, also replace a cell to be removed upon a condition or due to a zero item repeat with an empty 
     772   * cell according to this condition, unless the global flag {@link #removeSdtCellsOnFailedCondition} is set. 
     773   *  
     774   * @param sdt The SDT node currently being processed. 
     775   * @return The "eventually empty" node list to replace the content of <code>sdt</code>. 
     776   */ 
     777  private static List<Object> eventuallyEmptyList(final Object sdt) { 
     778 
     779    final boolean sdtIsCell = sdt instanceof CTSdtCell; 
     780 
     781    final Object parent = obtainParent(sdt); 
     782    final int contentChildCount = countContentChildren(parent); 
     783    final boolean sdtIsSingleCellChild = parent instanceof Tc && contentChildCount == 1; 
     784 
     785    final List<Object> newContent; 
     786 
     787    if (sdtIsCell && !removeSdtCellsOnFailedCondition) { 
     788 
     789      final CTSdtContentCell sdtCellContent = (CTSdtContentCell) ((org.docx4j.wml.CTSdtCell) sdt).getSdtContent(); 
     790      final Tc tc = (Tc) XmlUtils.unwrap(sdtCellContent.getContent().get(0)); 
     791      tc.getContent().clear(); 
     792      final P p = Context.getWmlObjectFactory().createP(); 
     793      tc.getContent().add(p); 
     794      newContent = Arrays.asList((Object) tc); 
     795 
     796    } else if (sdtIsSingleCellChild) { 
     797 
     798      final Object p = Context.getWmlObjectFactory().createP(); 
     799      newContent = Arrays.asList(p); 
     800 
     801    } else { 
     802      newContent = Arrays.asList(); 
     803    } 
     804 
     805    return newContent; 
     806  } 
    792807                 
    793808    private static Object obtainParent(Object sdt) { 
Note: See TracChangeset for help on using the changeset viewer.