Changeset 1745 for trunk/docx4j/src/main


Ignore:
Timestamp:
01/10/12 11:46:36 (5 months ago)
Author:
jharrop
Message:

Support <w:startOverride w:val="10"/>.
A single counter is maintained for an abstract list level, and shared by all instances.

Location:
trunk/docx4j/src/main/java/org/docx4j/model/listnumbering
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/listnumbering/AbstractListNumberingDefinition.java

    r1298 r1745  
    9898import org.docx4j.wml.PPrBase.NumPr; 
    9999 
     100/** 
     101 * Represents: 
     102 *  
     103          <w:abstractNum w:abstractNumId="0"> 
     104            <w:nsid w:val="2DD860C0"/> 
     105            <w:multiLevelType w:val="multilevel"/> 
     106            <w:tmpl w:val="0409001D"/> 
     107            <w:lvl w:ilvl="0"> 
     108              <w:start w:val="1"/> 
     109              <w:numFmt w:val="decimal"/> 
     110              <w:lvlText w:val="%1)"/> 
     111              <w:lvlJc w:val="left"/> 
     112              <w:pPr> 
     113                <w:ind w:left="360" w:hanging="360"/> 
     114              </w:pPr> 
     115            </w:lvl> 
     116            <w:lvl w:ilvl="1"> 
     117              <w:start w:val="1"/> 
     118              <w:numFmt w:val="lowerLetter"/> 
     119              <w:lvlText w:val="%2)"/> 
     120              <w:lvlJc w:val="left"/> 
     121              <w:pPr> 
     122                <w:ind w:left="720" w:hanging="360"/> 
     123              </w:pPr> 
     124            </w:lvl> 
     125            etc 
     126             
     127            (layered on top of the JAXB object representing same) 
     128     
     129 */ 
    100130public class AbstractListNumberingDefinition { 
    101131         
  • trunk/docx4j/src/main/java/org/docx4j/model/listnumbering/ListLevel.java

    r1435 r1745  
    103103         * for each story!  
    104104         *  
     105         * 2012 01 10: numbering set up on a per-part basis 
     106         * seems the most sensible approach. 
     107         *  
    105108         */ 
    106109         
     
    117120        } 
    118121         
     122        /** 
     123         *  The counter is kept at the abstract level, since 
     124         *  each instance definition shares a single counter. 
     125         *   
     126         */ 
     127        private Counter counter;  
     128         
     129        private boolean encounteredAlready = false; 
     130         
     131         
     132    /** 
     133     * Constructor for a ListLevel in an abstract definition. 
     134     */ 
    119135    public ListLevel(Lvl levelNode) 
    120136    { 
     
    122138         
    123139        this.id = levelNode.getIlvl().toString();  
     140         
     141        counter = new Counter(); 
    124142 
    125143        Lvl.Start startValueNode = levelNode.getStart(); 
     
    129147                        // Start value is one less than the user set it to, 
    130148                        // since whenever we fetch the number, we first increment it. 
    131             this.counter = this.startValue; 
     149            counter.setCurrentValue(this.startValue);                         
    132150        } 
    133151 
     
    163181    } 
    164182 
    165     /** copy constructor 
    166      *  
    167      * @param masterCopy 
     183    /**  
     184     * Constructor for a ListLevel in an instance definition. 
    168185     */ 
    169186    public ListLevel(ListLevel masterCopy) 
     
    174191        this.levelText = masterCopy.levelText; 
    175192        this.startValue = masterCopy.startValue; 
    176         this.counter = this.startValue; 
     193        //this.counter = this.startValue; 
     194        this.counter = masterCopy.counter;  // reference the abstract one, since this is shared 
    177195        this.font = masterCopy.font; 
    178196        this.isBullet = masterCopy.isBullet; 
     
    191209        if (startValueNode != null) 
    192210        { 
    193                 this.startValue = startValueNode.getVal();  
    194         } 
    195         this.counter = this.startValue; 
     211                this.startValue = startValueNode.getVal().subtract(BigInteger.ONE); 
     212                // Start value is one less than the user set it to, 
     213                // since whenever we fetch the number, we first increment it. 
     214                counter.setCurrentValue(this.startValue);                         
     215        } 
    196216 
    197217        Lvl.LvlText levelTextNode = levelNode.getLvlText(); 
     
    238258    private BigInteger startValue = BigInteger.ZERO; 
    239259 
    240     /** 
     260    public void setStartValue(BigInteger startValue) { 
     261                this.startValue = startValue; 
     262        } 
     263 
     264        /** 
    241265     * start value of that level 
    242266     * @return 
     
    247271    } 
    248272 
    249     private BigInteger counter; 
    250  
    251     /** 
    252      * returns the current count of list items of that level 
    253      * @return 
    254      */ 
    255     public BigInteger getCurrentValueRaw() 
    256     {            
    257             return this.counter; 
    258     } 
    259273 
    260274    /** 
     
    286300         
    287301        if (numFmt.equals( NumberFormat.DECIMAL ) ) { 
    288                 return this.counter.toString(); 
     302                return this.counter.getCurrentValue().toString(); 
    289303        } 
    290304         
     
    301315        } 
    302316                         
    303         int current = this.counter.intValue(); 
     317        int current = this.counter.getCurrentValue().intValue(); 
    304318         
    305319        if (numFmt.equals( NumberFormat.UPPER_ROMAN ) ) {                        
     
    321335         
    322336        log.error("Unhandled numFmt: " + numFmt.name() ); 
    323         return this.counter.toString(); 
     337        return this.counter.getCurrentValue().toString(); 
    324338    } 
    325339     
     
    330344    public void IncrementCounter() 
    331345    { 
    332         this.counter = this.counter.add(BigInteger.ONE);  
     346        if (!encounteredAlready) { 
     347                // Defer setting the startValue until the list 
     348                // is actually encountered in the main document part, 
     349                // since otherwise earlier numbering (using the 
     350                // same abstract number) would use this startValue 
     351                counter.setCurrentValue(this.startValue);   
     352                encounteredAlready = true; 
     353        } 
     354         
     355         
     356        counter.IncrementCounter(); 
    333357         
    334         log.debug("counter now: " + this.counter.toString() ); 
    335          
    336358    } 
    337359 
     
    341363    public void ResetCounter() 
    342364    { 
    343         this.counter = this.startValue; 
     365        counter.setCurrentValue(this.startValue); 
    344366    } 
    345367 
     
    387409            return this.isBullet; 
    388410    } 
     411     
     412    protected class Counter { 
     413         
     414        private BigInteger currentValue; 
     415         
     416        Counter() { 
     417                currentValue = BigInteger.ZERO; 
     418        } 
     419 
     420        public void setCurrentValue(BigInteger currentValue) { 
     421                        this.currentValue = currentValue; 
     422                } 
     423 
     424                /** 
     425         * returns the current count of list items of that level 
     426         * @return 
     427         */ 
     428        public BigInteger getCurrentValue() 
     429        {                
     430                log.debug("counter: " + currentValue.intValue() ); 
     431            return this.currentValue; 
     432        } 
     433         
     434        /** 
     435         * increments the current count of list items of that level  
     436         */ 
     437        public void IncrementCounter() 
     438        { 
     439                setCurrentValue( currentValue.add(BigInteger.ONE));  
     440             
     441            log.debug("counter now: " + currentValue.intValue() ); 
     442             
     443        } 
     444         
     445    } 
    389446 
    390447} 
  • trunk/docx4j/src/main/java/org/docx4j/model/listnumbering/ListNumberingDefinition.java

    r1298 r1745  
    8484package org.docx4j.model.listnumbering; 
    8585 
     86import java.math.BigInteger; 
    8687import java.util.HashMap; 
    8788import java.util.Iterator; 
     
    9091 
    9192import org.apache.log4j.Logger; 
     93import org.docx4j.XmlUtils; 
    9294import org.docx4j.wml.Lvl; 
    9395import org.docx4j.wml.Numbering; 
    94  
     96import org.docx4j.wml.Numbering.Num.LvlOverride.StartOverride; 
     97 
     98/** 
     99 * Represents: 
     100 *  
     101          <w:num w:numId="1"> 
     102            <w:abstractNumId w:val="0"/> 
     103          </w:num> 
     104           
     105          or  
     106           
     107          <w:num w:numId="2"> 
     108            <w:abstractNumId w:val="0"/> 
     109            <w:lvlOverride w:ilvl="0"> 
     110              <w:startOverride w:val="10"/> 
     111            </w:lvlOverride> 
     112          </w:num> 
     113                     
     114            (layered on top of the JAXB object representing same) 
     115     
     116 */ 
    95117public class ListNumberingDefinition { 
    96          
    97         /* There should be only one Emulator object per  
    98          * WordprocessingML package.  It is set on the  
    99          * numbering part. 
    100          */ 
    101          
     118                 
    102119        // The underlying JAXB object  
    103120        private Numbering.Num numNode; 
     
    108125        protected static Logger log = Logger.getLogger(ListNumberingDefinition.class); 
    109126         
    110     /// <summary> 
    111     /// constructor 
    112     /// </summary> 
    113     /// <param name="numNode"></param> 
    114     /// <param name="nsm"></param> 
    115     /// <param name="abstractListDefinitions"></param> 
     127    /** 
     128     * @param numNode 
     129     * @param abstractListDefinitions 
     130     */ 
    116131    public ListNumberingDefinition(Numbering.Num numNode,  
    117132                HashMap<String, AbstractListNumberingDefinition> abstractListDefinitions) 
     
    120135         
    121136        this.listNumberId =  numNode.getNumId().toString(); //getAttributeValue(numNode, "w:numId"); 
     137        log.debug("Constructing model for numId=" + listNumberId); 
    122138 
    123139        //XmlNode abstractNumNode = numNode.SelectSingleNode("./w:abstractNumId", nsm); 
     
    146162            if (levelOverrideNodes != null) 
    147163            { 
     164                /* 
     165                 *     <w:lvlOverride w:ilvl="0"> 
     166                                              <w:startOverride w:val="10"/> 
     167                                            </w:lvlOverride> 
     168                 */ 
    148169                for (Numbering.Num.LvlOverride overrideNode : levelOverrideNodes) 
    149170                { 
    150                     //XmlNode node = overrideNode.SelectSingleNode("./w:lvl", nsm); 
    151                         Lvl node = overrideNode.getLvl(); 
    152                     if (node != null) 
     171                        log.debug("found LvlOverride " + XmlUtils.marshaltoString(overrideNode, true)); 
     172                    if (overrideNode.getIlvl() != null) 
    153173                    { 
    154                         String overrideLevelId = node.getIlvl().toString(); //getAttributeValue(node, "w:ilvl"); 
    155  
    156                         if (overrideLevelId!=null && !overrideLevelId.equals("") ) 
    157                         { 
    158                             this.levels.get(overrideLevelId).SetOverrides(node); 
     174                        String overrideLevelId = overrideNode.getIlvl().toString(); //getAttributeValue(node, "w:ilvl"); 
     175                        log.debug(".. " + overrideLevelId ); 
     176 
     177                        if (!overrideLevelId.equals("") ) 
     178                        {                             
     179                            // Is there a w:startOverride? 
     180                                // This is given effect the first time the instance is encountered in the document 
     181                            StartOverride startOverride = overrideNode.getStartOverride(); 
     182                            if (startOverride!=null 
     183                                        && startOverride.getVal()!=null) { 
     184                                this.levels.get(overrideLevelId).setStartValue(startOverride.getVal().subtract(BigInteger.ONE)); 
     185                                log.debug("level " + overrideLevelId + "starts at " + startOverride.getVal() ); 
     186                            } 
    159187                        } 
    160188                    } 
     189//                      Lvl ilvl = overrideNode.getLvl(); 
     190//                    if (ilvl != null) 
     191//                    { 
     192//                    this.levels.get(overrideLevelId).SetOverrides(overrideLevelId); 
     193//                    }                     
    161194                } 
    162195            } 
     
    181214    public void IncrementCounter(String level) 
    182215    { 
     216         
    183217        log.debug("Increment level " + level); 
    184218        this.levels.get(level).IncrementCounter(); 
Note: See TracChangeset for help on using the changeset viewer.