View | Details | Raw Unified | Return to bug 58740
Collapse All | Expand All

(-)poi-3.14/org/apache/poi/xssf/model/StylesTable.java (-41 / +55 lines)
Lines 19-24 Link Here
19
19
20
import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
20
import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
21
21
22
import com.google.common.collect.HashBiMap;
23
22
import java.io.IOException;
24
import java.io.IOException;
23
import java.io.InputStream;
25
import java.io.InputStream;
24
import java.io.OutputStream;
26
import java.io.OutputStream;
Lines 69-79 Link Here
69
 */
71
 */
70
public class StylesTable extends POIXMLDocumentPart {
72
public class StylesTable extends POIXMLDocumentPart {
71
    private final SortedMap<Short, String> numberFormats = new TreeMap<Short,String>();
73
    private final SortedMap<Short, String> numberFormats = new TreeMap<Short,String>();
72
    private final List<XSSFFont> fonts = new ArrayList<XSSFFont>();
74
    private final HashBiMap<Integer, XSSFFont> fonts = HashBiMap.<Integer, XSSFFont>create();
73
    private final List<XSSFCellFill> fills = new ArrayList<XSSFCellFill>();
75
    private final HashBiMap<Integer, XSSFCellFill> fills = HashBiMap.<Integer, XSSFCellFill>create();
74
    private final List<XSSFCellBorder> borders = new ArrayList<XSSFCellBorder>();
76
    private final HashBiMap<Integer, XSSFCellBorder> borders = HashBiMap.<Integer, XSSFCellBorder>create();
75
    private final List<CTXf> styleXfs = new ArrayList<CTXf>();
77
    private final List<CTXf> styleXfs = new ArrayList<CTXf>();
76
    private final List<CTXf> xfs = new ArrayList<CTXf>();
78
    private final HashBiMap<Integer, CTXf> xfs = HashBiMap.<Integer, CTXf>create();
77
79
78
    private final List<CTDxf> dxfs = new ArrayList<CTDxf>();
80
    private final List<CTDxf> dxfs = new ArrayList<CTDxf>();
79
81
Lines 173-182 Link Here
173
175
174
        // Pass the themes table along to things which need to 
176
        // Pass the themes table along to things which need to 
175
        //  know about it, but have already been created by now
177
        //  know about it, but have already been created by now
176
        for(XSSFFont font : fonts) {
178
        for(XSSFFont font : fonts.values()) {
177
            font.setThemesTable(theme);
179
            font.setThemesTable(theme);
178
        }
180
        }
179
        for(XSSFCellBorder border : borders) {
181
        for(XSSFCellBorder border : borders.values()) {
180
            border.setThemesTable(theme);
182
            border.setThemesTable(theme);
181
        }
183
        }
182
    }
184
    }
Lines 219-244 Link Here
219
                for (CTFont font : ctfonts.getFontArray()) {
221
                for (CTFont font : ctfonts.getFontArray()) {
220
                    // Create the font and save it. Themes Table supplied later
222
                    // Create the font and save it. Themes Table supplied later
221
                    XSSFFont f = new XSSFFont(font, idx);
223
                    XSSFFont f = new XSSFFont(font, idx);
222
                    fonts.add(f);
224
                    fonts.put(fonts.size(), f);
223
                    idx++;
225
                    idx++;
224
                }
226
                }
225
            }
227
            }
226
            CTFills ctfills = styleSheet.getFills();
228
            CTFills ctfills = styleSheet.getFills();
227
            if(ctfills != null){
229
            if(ctfills != null){
228
                for (CTFill fill : ctfills.getFillArray()) {
230
                for (CTFill fill : ctfills.getFillArray()) {
229
                    fills.add(new XSSFCellFill(fill));
231
                    fills.put(fills.size(), new XSSFCellFill(fill));
230
                }
232
                }
231
            }
233
            }
232
234
233
            CTBorders ctborders = styleSheet.getBorders();
235
            CTBorders ctborders = styleSheet.getBorders();
234
            if(ctborders != null) {
236
            if(ctborders != null) {
235
                for (CTBorder border : ctborders.getBorderArray()) {
237
                for (CTBorder border : ctborders.getBorderArray()) {
236
                    borders.add(new XSSFCellBorder(border));
238
                    borders.put(borders.size(), new XSSFCellBorder(border));
237
                }
239
                }
238
            }
240
            }
239
241
240
            CTCellXfs cellXfs = styleSheet.getCellXfs();
242
            CTCellXfs cellXfs = styleSheet.getCellXfs();
241
            if(cellXfs != null) xfs.addAll(Arrays.asList(cellXfs.getXfArray()));
243
            if(cellXfs != null) {
244
                for (CTXf ctxf : cellXfs.getXfArray())
245
                    xfs.put(xfs.size(), ctxf);
246
            }
242
247
243
            CTCellStyleXfs cellStyleXfs = styleSheet.getCellStyleXfs();
248
            CTCellStyleXfs cellStyleXfs = styleSheet.getCellStyleXfs();
244
            if(cellStyleXfs != null) styleXfs.addAll(Arrays.asList(cellStyleXfs.getXfArray()));
249
            if(cellStyleXfs != null) styleXfs.addAll(Arrays.asList(cellStyleXfs.getXfArray()));
Lines 262-267 Link Here
262
     * @return number format code
267
     * @return number format code
263
     * @deprecated POI 3.14-beta2. Use {@link #getNumberFormatAt(short)} instead.
268
     * @deprecated POI 3.14-beta2. Use {@link #getNumberFormatAt(short)} instead.
264
     */
269
     */
270
    @Deprecated
265
    public String getNumberFormatAt(int idx) {
271
    public String getNumberFormatAt(int idx) {
266
        return getNumberFormatAt((short) idx);
272
        return getNumberFormatAt((short) idx);
267
    }
273
    }
Lines 360-366 Link Here
360
        String fmt = numberFormats.remove(index);
366
        String fmt = numberFormats.remove(index);
361
        boolean removed = (fmt != null);
367
        boolean removed = (fmt != null);
362
        if (removed) {
368
        if (removed) {
363
            for (final CTXf style : xfs) {
369
            for (final CTXf style : xfs.values()) {
364
                if (style.isSetNumFmtId() && style.getNumFmtId() == index) {
370
                if (style.isSetNumFmtId() && style.getNumFmtId() == index) {
365
                    style.unsetApplyNumberFormat();
371
                    style.unsetApplyNumberFormat();
366
                    style.unsetNumFmtId();
372
                    style.unsetNumFmtId();
Lines 398-405 Link Here
398
     */
404
     */
399
    public int putFont(XSSFFont font, boolean forceRegistration) {
405
    public int putFont(XSSFFont font, boolean forceRegistration) {
400
        int idx = -1;
406
        int idx = -1;
401
        if(!forceRegistration) {
407
        if(!forceRegistration && fonts.containsValue(font)) {
402
            idx = fonts.indexOf(font);
408
            idx = fonts.inverse().get(font);
403
        }
409
        }
404
410
405
        if (idx != -1) {
411
        if (idx != -1) {
Lines 407-413 Link Here
407
        }
413
        }
408
414
409
        idx = fonts.size();
415
        idx = fonts.size();
410
        fonts.add(font);
416
        fonts.put(idx, font);
411
        return idx;
417
        return idx;
412
    }
418
    }
413
    public int putFont(XSSFFont font) {
419
    public int putFont(XSSFFont font) {
Lines 427-436 Link Here
427
    public int putStyle(XSSFCellStyle style) {
433
    public int putStyle(XSSFCellStyle style) {
428
        CTXf mainXF = style.getCoreXf();
434
        CTXf mainXF = style.getCoreXf();
429
435
430
        if(! xfs.contains(mainXF)) {
436
        if(! xfs.containsValue(mainXF)) {
431
            xfs.add(mainXF);
437
            xfs.put(xfs.size(), mainXF);
432
        }
438
        }
433
        return xfs.indexOf(mainXF);
439
        return xfs.inverse().get(mainXF);
434
    }
440
    }
435
441
436
    public XSSFCellBorder getBorderAt(int idx) {
442
    public XSSFCellBorder getBorderAt(int idx) {
Lines 445-455 Link Here
445
     * @return the index of the added border
451
     * @return the index of the added border
446
     */
452
     */
447
    public int putBorder(XSSFCellBorder border) {
453
    public int putBorder(XSSFCellBorder border) {
448
        int idx = borders.indexOf(border);
454
        Integer idx = borders.inverse().get(border);
449
        if (idx != -1) {
455
        if (idx != null) {
450
            return idx;
456
            return idx;
451
        }
457
        }
452
        borders.add(border);
458
        borders.put(borders.size(), border);
453
        border.setThemesTable(theme);
459
        border.setThemesTable(theme);
454
        return borders.size() - 1;
460
        return borders.size() - 1;
455
    }
461
    }
Lines 458-473 Link Here
458
        return fills.get(idx);
464
        return fills.get(idx);
459
    }
465
    }
460
466
461
    public List<XSSFCellBorder> getBorders(){
467
    public List<XSSFCellBorder> getBorders() {
462
        return Collections.unmodifiableList(borders);
468
        return this.toList(borders);
463
    }
469
    }
464
470
465
    public List<XSSFCellFill> getFills(){
471
    public List<XSSFCellFill> getFills() {
466
        return Collections.unmodifiableList(fills);
472
        return this.toList(fills);
467
    }
473
    }
468
474
469
    public List<XSSFFont> getFonts(){
475
    public List<XSSFFont> getFonts() {
470
        return Collections.unmodifiableList(fonts);
476
        return this.toList(fonts);
477
    }
478
479
    private <T> List<T> toList(HashBiMap<Integer, T> map) {
480
        final ArrayList<T> list = new ArrayList<T>(map.size());
481
        for (int i = 0; i < map.size(); i++)
482
            list.add(map.get(i));
483
        return Collections.unmodifiableList(list);
471
    }
484
    }
472
485
473
    public Map<Short, String> getNumberFormats(){
486
    public Map<Short, String> getNumberFormats(){
Lines 482-492 Link Here
482
     * @return the index of the added fill
495
     * @return the index of the added fill
483
     */
496
     */
484
    public int putFill(XSSFCellFill fill) {
497
    public int putFill(XSSFCellFill fill) {
485
        int idx = fills.indexOf(fill);
498
        Integer idx = fills.inverse().get(fill);
486
        if (idx != -1) {
499
        if (idx != null) {
487
            return idx;
500
            return idx;
488
        }
501
        }
489
        fills.add(fill);
502
        fills.put(fills.size(), fill);
490
        return fills.size() - 1;
503
        return fills.size() - 1;
491
    }
504
    }
492
505
Lines 504-516 Link Here
504
     */
517
     */
505
    @Internal
518
    @Internal
506
    public int putCellXf(CTXf cellXf) {
519
    public int putCellXf(CTXf cellXf) {
507
        xfs.add(cellXf);
520
        xfs.put(xfs.size(), cellXf);
508
        return xfs.size();
521
        return xfs.size();
509
    }
522
    }
510
    
523
    
511
    @Internal
524
    @Internal
512
    public void replaceCellXfAt(int idx, CTXf cellXf) {
525
    public void replaceCellXfAt(int idx, CTXf cellXf) {
513
        xfs.set(idx, cellXf);
526
        xfs.put(idx, cellXf);
514
    }
527
    }
515
528
516
    @Internal
529
    @Internal
Lines 562-567 Link Here
562
     * For unit testing only
575
     * For unit testing only
563
     * @deprecated POI 3.14 beta 2. Use {@link #getNumDataFormats()} instead.
576
     * @deprecated POI 3.14 beta 2. Use {@link #getNumDataFormats()} instead.
564
     */
577
     */
578
    @Deprecated
565
    @Internal
579
    @Internal
566
    public int _getNumberFormatSize() {
580
    public int _getNumberFormatSize() {
567
        return getNumDataFormats();
581
        return getNumDataFormats();
Lines 627-633 Link Here
627
        ctFonts.setCount(fonts.size());
641
        ctFonts.setCount(fonts.size());
628
        CTFont[] ctfnt = new CTFont[fonts.size()];
642
        CTFont[] ctfnt = new CTFont[fonts.size()];
629
        idx = 0;
643
        idx = 0;
630
        for(XSSFFont f : fonts) ctfnt[idx++] = f.getCTFont();
644
        for(XSSFFont f : this.toList(fonts)) ctfnt[idx++] = f.getCTFont();
631
        ctFonts.setFontArray(ctfnt);
645
        ctFonts.setFontArray(ctfnt);
632
        styleSheet.setFonts(ctFonts);
646
        styleSheet.setFonts(ctFonts);
633
647
Lines 639-645 Link Here
639
        ctFills.setCount(fills.size());
653
        ctFills.setCount(fills.size());
640
        CTFill[] ctf = new CTFill[fills.size()];
654
        CTFill[] ctf = new CTFill[fills.size()];
641
        idx = 0;
655
        idx = 0;
642
        for(XSSFCellFill f : fills) ctf[idx++] = f.getCTFill();
656
        for(XSSFCellFill f : this.toList(fills)) ctf[idx++] = f.getCTFill();
643
        ctFills.setFillArray(ctf);
657
        ctFills.setFillArray(ctf);
644
        styleSheet.setFills(ctFills);
658
        styleSheet.setFills(ctFills);
645
659
Lines 651-657 Link Here
651
        ctBorders.setCount(borders.size());
665
        ctBorders.setCount(borders.size());
652
        CTBorder[] ctb = new CTBorder[borders.size()];
666
        CTBorder[] ctb = new CTBorder[borders.size()];
653
        idx = 0;
667
        idx = 0;
654
        for(XSSFCellBorder b : borders) ctb[idx++] = b.getCTBorder();
668
        for(XSSFCellBorder b : this.toList(borders)) ctb[idx++] = b.getCTBorder();
655
        ctBorders.setBorderArray(ctb);
669
        ctBorders.setBorderArray(ctb);
656
        styleSheet.setBorders(ctBorders);
670
        styleSheet.setBorders(ctBorders);
657
671
Lines 663-669 Link Here
663
            }
677
            }
664
            ctXfs.setCount(xfs.size());
678
            ctXfs.setCount(xfs.size());
665
            ctXfs.setXfArray(
679
            ctXfs.setXfArray(
666
                    xfs.toArray(new CTXf[xfs.size()])
680
                    this.toList(xfs).toArray(new CTXf[xfs.size()])
667
            );
681
            );
668
            styleSheet.setCellXfs(ctXfs);
682
            styleSheet.setCellXfs(ctXfs);
669
        }
683
        }
Lines 707-726 Link Here
707
    private void initialize() {
721
    private void initialize() {
708
        //CTFont ctFont = createDefaultFont();
722
        //CTFont ctFont = createDefaultFont();
709
        XSSFFont xssfFont = createDefaultFont();
723
        XSSFFont xssfFont = createDefaultFont();
710
        fonts.add(xssfFont);
724
        fonts.put(fonts.size(), xssfFont);
711
725
712
        CTFill[] ctFill = createDefaultFills();
726
        CTFill[] ctFill = createDefaultFills();
713
        fills.add(new XSSFCellFill(ctFill[0]));
727
        fills.put(fills.size(), new XSSFCellFill(ctFill[0]));
714
        fills.add(new XSSFCellFill(ctFill[1]));
728
        fills.put(fills.size(), new XSSFCellFill(ctFill[1]));
715
729
716
        CTBorder ctBorder = createDefaultBorder();
730
        CTBorder ctBorder = createDefaultBorder();
717
        borders.add(new XSSFCellBorder(ctBorder));
731
        borders.put(borders.size(), new XSSFCellBorder(ctBorder));
718
732
719
        CTXf styleXf = createDefaultXf();
733
        CTXf styleXf = createDefaultXf();
720
        styleXfs.add(styleXf);
734
        styleXfs.add(styleXf);
721
        CTXf xf = createDefaultXf();
735
        CTXf xf = createDefaultXf();
722
        xf.setXfId(0);
736
        xf.setXfId(0);
723
        xfs.add(xf);
737
        xfs.put(xfs.size(), xf);
724
    }
738
    }
725
739
726
    private static CTXf createDefaultXf() {
740
    private static CTXf createDefaultXf() {
Lines 805-811 Link Here
805
     * Finds a font that matches the one with the supplied attributes
819
     * Finds a font that matches the one with the supplied attributes
806
     */
820
     */
807
    public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
821
    public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
808
        for (XSSFFont font : fonts) {
822
        for (XSSFFont font : fonts.values()) {
809
            if (	(font.getBoldweight() == boldWeight)
823
            if (	(font.getBoldweight() == boldWeight)
810
                    && font.getColor() == color
824
                    && font.getColor() == color
811
                    && font.getFontHeight() == fontHeight
825
                    && font.getFontHeight() == fontHeight

Return to bug 58740