Example usage for java.text NumberFormat setGroupingUsed

List of usage examples for java.text NumberFormat setGroupingUsed

Introduction

In this page you can find the example usage for java.text NumberFormat setGroupingUsed.

Prototype

public void setGroupingUsed(boolean newValue) 

Source Link

Document

Set whether or not grouping will be used in this format.

Usage

From source file:it.prato.comune.sit.LayerTerritorio.java

/**
 * @param store/*from ww w.  ja va2s . c  o  m*/
 * @param query
 * @param tempdirpath
 * @return File
 * @throws SITException
 */
public File csvExport(DataStore store, Query query, String tempdirpath) throws SITException {

    File csvFile = null;
    try {
        SimpleFeatureSource featureSource = store.getFeatureSource(this.getTypeName());
        SimpleFeatureType ft = featureSource.getSchema();

        String fileName = ft.getTypeName();

        csvFile = new File(tempdirpath, fileName + ".csv");

        // //////////////////////////////////////////////////
        // Creazione del writer da usare per il CSV finale
        // //////////////////////////////////////////////////
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile, false)));

        SimpleFeatureCollection collection = featureSource.getFeatures(query);

        if (collection.getSchema() instanceof SimpleFeatureType) {
            // Scrittura dell'intestazione
            SimpleFeatureType featureType = (SimpleFeatureType) collection.getSchema();
            w.write("FID,");
            for (int i = 0; i < ft.getAttributeCount(); i++) {
                AttributeDescriptor ad = featureType.getDescriptor(i);
                w.write(prepCSVField(ad.getLocalName()));

                if (i < ft.getAttributeCount() - 1) {
                    w.write(",");
                }
            }
        } else {
            // Features complesse
            w.write("gml:id,");

            int i = 0;
            for (PropertyDescriptor att : collection.getSchema().getDescriptors()) {
                // Escludere attributi temporanei
                if (!att.getName().getLocalPart().startsWith("FEATURE_LINK")) {
                    if (i > 0) {
                        w.write(",");
                    }
                    String elName = att.getName().toString();
                    Object xsd = att.getUserData().get(XSDElementDeclaration.class);
                    if (xsd != null && xsd instanceof XSDElementDeclarationImpl) {
                        // ////////////////////////////////////////////////////////////////////////
                        // Ottenere il nome del prefisso, se possibile, altrimenti il default 
                        //  il nome completo dello spazio dei nomi con URI
                        // ////////////////////////////////////////////////////////////////////////
                        XSDElementDeclarationImpl xsdEl = (XSDElementDeclarationImpl) xsd;
                        elName = xsdEl.getQName();
                    }

                    w.write(prepCSVField(elName));
                    i++;
                }
            }
        }

        w.write("\r\n");

        //
        // Preparare il formatter per i campi numerici
        //
        NumberFormat coordFormatter = NumberFormat.getInstance(Locale.US);
        coordFormatter.setMaximumFractionDigits(3);
        coordFormatter.setGroupingUsed(false);

        //
        // Scrivere le features per compilare il file CSV finale
        // 
        FeatureIterator<?> i = collection.features();
        try {
            while (i.hasNext()) {
                Feature f = i.next();
                // Recupero del fid
                w.write(prepCSVField(f.getIdentifier().getID()));
                w.write(",");
                if (f instanceof SimpleFeature) {
                    // Resupero degli attributi
                    for (int j = 0; j < ((SimpleFeature) f).getAttributeCount(); j++) {
                        Object att = ((SimpleFeature) f).getAttribute(j);
                        if (att != null) {
                            String value = formatToString(att, coordFormatter);
                            w.write(prepCSVField(value));
                        }
                        if (j < ((SimpleFeature) f).getAttributeCount() - 1) {
                            w.write(",");
                        }
                    }
                } else {
                    // Features complesse
                    Iterator<PropertyDescriptor> descriptors = collection.getSchema().getDescriptors()
                            .iterator();

                    // Resupero degli attributi
                    int j = 0;
                    while (descriptors.hasNext()) {
                        PropertyDescriptor desc = descriptors.next();

                        if (desc.getName().getLocalPart().startsWith("FEATURE_LINK")) {
                            // Saltare attributi temporanei
                            continue;
                        }
                        if (j > 0) {
                            w.write(",");
                        }
                        j++;

                        // Propriet multivalore non sono supportate, solamente per SF0 per adesso
                        Collection<Property> values = f.getProperties(desc.getName());
                        if (values.size() > 1) {
                            throw new UnsupportedOperationException(
                                    "Propriet multivalore non sono supportate per il formato CSV!");
                        }

                        Object att = null;
                        if (!values.isEmpty()) {
                            att = values.iterator().next().getValue();
                        }

                        if (att != null) {
                            String value = formatToString(att, coordFormatter);
                            w.write(prepCSVField(value));
                        }
                    }
                }

                w.write("\r\n");
            }

        } finally {
            i.close();
        }

        w.flush();

    } catch (Exception e) {
        String errMsg = "Errore durante la ricerca del filtro";
        logger.error(errMsg, e);
        throw new SITException(errMsg, e);
    }

    filtroTotale.ResetFiltro();
    return csvFile;
}