OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 180
Line 266... Line 266...
266
            ins.close();
266
            ins.close();
267
        }
267
        }
268
    }
268
    }
269
 
269
 
270
    public static ODPackage createFromFile(final File f) throws IOException {
270
    public static ODPackage createFromFile(final File f) throws IOException {
271
        final FileInputStream ins = new FileInputStream(f);
271
        try (final FileInputStream ins = new FileInputStream(f)) {
272
        try {
-
 
273
            return create(f, ins, f.getName());
272
            return create(f, ins, f.getName());
274
        } finally {
-
 
275
            ins.close();
-
 
276
        }
273
        }
277
    }
274
    }
278
 
275
 
279
    private static final int mimetypeZipEndOffset = 250;
276
    private static final int mimetypeZipEndOffset = 250;
280
 
277
 
Line 598... Line 595...
598
    // document.
595
    // document.
599
    public final Locale getLocale() {
596
    public final Locale getLocale() {
600
        return this.locale == null ? Locale.getDefault() : this.locale;
597
        return this.locale == null ? Locale.getDefault() : this.locale;
601
    }
598
    }
602
 
599
 
603
    public final String formatNumber(Number n, final CellStyle defaultStyle) {
600
    public static final String formatNumber(Number n, final Locale locale, final CellStyle defaultStyle) {
604
        return formatNumber(NumberFormat.getNumberInstance(getLocale()), n, defaultStyle);
601
        return formatNumber(NumberFormat.getNumberInstance(locale), n, defaultStyle);
605
    }
602
    }
606
 
603
 
607
    public final String formatPercent(Number n, final CellStyle defaultStyle) {
604
    public static final String formatPercent(Number n, final Locale locale, final CellStyle defaultStyle) {
608
        return formatNumber(NumberFormat.getPercentInstance(getLocale()), n, defaultStyle);
605
        return formatNumber(NumberFormat.getPercentInstance(locale), n, defaultStyle);
609
    }
606
    }
610
 
607
 
611
    public final String formatCurrency(Number n, final CellStyle defaultStyle) {
608
    public static final String formatCurrency(Number n, final Locale locale, final CellStyle defaultStyle) {
612
        return formatNumber(NumberFormat.getCurrencyInstance(getLocale()), n, defaultStyle);
609
        return formatNumber(NumberFormat.getCurrencyInstance(locale), n, defaultStyle);
613
    }
610
    }
614
 
611
 
615
    private final String formatNumber(NumberFormat format, Number n, final CellStyle defaultStyle) {
612
    private static final String formatNumber(NumberFormat format, Number n, final CellStyle defaultStyle) {
616
        synchronized (format) {
613
        synchronized (format) {
617
            final int decPlaces = DataStyle.getDecimalPlaces(defaultStyle);
614
            final int decPlaces = DataStyle.getDecimalPlaces(defaultStyle);
618
            format.setMinimumFractionDigits(0);
615
            format.setMinimumFractionDigits(0);
619
            format.setMaximumFractionDigits(decPlaces);
616
            format.setMaximumFractionDigits(decPlaces);
620
            return format.format(n);
617
            return format.format(n);
Line 1181... Line 1178...
1181
            final ODPackageEntry entry = this.files.get(name);
1178
            final ODPackageEntry entry = this.files.get(name);
1182
            if (z != null) {
1179
            if (z != null) {
1183
                final Object val = entry.getData();
1180
                final Object val = entry.getData();
1184
                if (val != null) {
1181
                if (val != null) {
1185
                    if (val instanceof ODXMLDocument) {
1182
                    if (val instanceof ODXMLDocument) {
1186
                        final OutputStream o = z.createEntry(name);
1183
                        try (final OutputStream o = z.createEntryStream(name)) {
1187
                        outputter.output(((ODXMLDocument) val).getDocument(), o);
1184
                            outputter.output(((ODXMLDocument) val).getDocument(), o);
1188
                        o.close();
1185
                        }
1189
                    } else if (val instanceof Document) {
1186
                    } else if (val instanceof Document) {
1190
                        final OutputStream o = z.createEntry(name);
1187
                        try (final OutputStream o = z.createEntryStream(name)) {
1191
                        outputter.output((Document) val, o);
1188
                            outputter.output((Document) val, o);
1192
                        o.close();
1189
                        }
1193
                    } else {
1190
                    } else {
1194
                        z.zip(name, (byte[]) val, entry.isCompressed());
1191
                        z.zip(name, (byte[]) val, entry.isCompressed());
1195
                    }
1192
                    }
1196
                }
1193
                }
1197
            }
1194
            }
Line 1243... Line 1240...
1243
        this.getMeta().removeMetaChild("document-statistic");
1240
        this.getMeta().removeMetaChild("document-statistic");
1244
        final String pageCount = getPageCount();
1241
        final String pageCount = getPageCount();
1245
        if (pageCount != null && getContentType() != null && ContentType.TEXT.equals(getContentType().getType()))
1242
        if (pageCount != null && getContentType() != null && ContentType.TEXT.equals(getContentType().getType()))
1246
            this.getMeta().getMetaChild("document-statistic").setAttribute("page-count", pageCount, getVersion().getMETA());
1243
            this.getMeta().getMetaChild("document-statistic").setAttribute("page-count", pageCount, getVersion().getMETA());
1247
 
1244
 
1248
        final Zip z = new Zip(out);
1245
        try (final Zip z = new Zip(out)) {
1249
 
-
 
1250
        // magic number, see section 17.4
1246
            // magic number, see section 17.4
1251
        z.zipNonCompressed(MIMETYPE_ENTRY, this.getMimeType().getBytes(MIMETYPE_ENC));
1247
            z.zipNonCompressed(MIMETYPE_ENTRY, this.getMimeType().getBytes(MIMETYPE_ENC));
1252
 
1248
 
1253
        final Manifest manifest = createManifest(z);
1249
            final Manifest manifest = createManifest(z);
1254
 
1250
 
1255
        z.zip(Manifest.ENTRY_NAME, new StringInputStream(manifest.asString()));
1251
            z.zip(Manifest.ENTRY_NAME, new StringInputStream(manifest.asString()));
1256
        z.close();
1252
        }
1257
    }
1253
    }
1258
 
1254
 
1259
    /**
1255
    /**
1260
     * Save the content of this package to our file, overwriting it if it exists.
1256
     * Save the content of this package to our file, overwriting it if it exists.
1261
     * 
1257
     * 
Line 1270... Line 1266...
1270
        final File f = this.addExt(fNoExt);
1266
        final File f = this.addExt(fNoExt);
1271
        if (f.getParentFile() != null)
1267
        if (f.getParentFile() != null)
1272
            f.getParentFile().mkdirs();
1268
            f.getParentFile().mkdirs();
1273
        // ATTN at this point, we must have read all the content of this file
1269
        // ATTN at this point, we must have read all the content of this file
1274
        // otherwise we could save to File.createTempFile("oofd", null).deleteOnExit();
1270
        // otherwise we could save to File.createTempFile("oofd", null).deleteOnExit();
1275
        final FileOutputStream out = new FileOutputStream(f);
-
 
1276
        final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out, 512 * 1024);
1271
        try (final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(f), 512 * 1024);) {
1277
        try {
-
 
1278
            this.save(bufferedOutputStream);
1272
            this.save(bufferedOutputStream);
1279
        } finally {
-
 
1280
            bufferedOutputStream.close();
-
 
1281
        }
1273
        }
1282
        return f;
1274
        return f;
1283
    }
1275
    }
1284
}
1276
}