Search in sources :

Example 1 with CSVParser

use of com.codename1.io.CSVParser in project CodenameOne by codenameone.

the class L10nEditor method importResourceActionPerformed.

// GEN-LAST:event_exportResourceActionPerformed
private void importResourceActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_importResourceActionPerformed
    final String locale = (String) locales.getSelectedItem();
    int val = JOptionPane.showConfirmDialog(this, "This will overwrite existing values for " + locale + "\nAre you sure?", "Import", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (val == JOptionPane.YES_OPTION) {
        File[] files = ResourceEditorView.showOpenFileChooser("Properties, XML, CSV", "prop", "properties", "l10n", "locale", "xml", "csv");
        if (files != null) {
            FileInputStream f = null;
            try {
                f = new FileInputStream(files[0]);
                if (files[0].getName().toLowerCase().endsWith("xml")) {
                    SAXParserFactory spf = SAXParserFactory.newInstance();
                    SAXParser saxParser = spf.newSAXParser();
                    XMLReader xmlReader = saxParser.getXMLReader();
                    xmlReader.setErrorHandler(new ErrorHandler() {

                        public void warning(SAXParseException exception) throws SAXException {
                            exception.printStackTrace();
                        }

                        public void error(SAXParseException exception) throws SAXException {
                            exception.printStackTrace();
                        }

                        public void fatalError(SAXParseException exception) throws SAXException {
                            exception.printStackTrace();
                        }
                    });
                    xmlReader.setContentHandler(new ContentHandler() {

                        private String currentName;

                        private StringBuilder chars = new StringBuilder();

                        @Override
                        public void setDocumentLocator(Locator locator) {
                        }

                        @Override
                        public void startDocument() throws SAXException {
                        }

                        @Override
                        public void endDocument() throws SAXException {
                        }

                        @Override
                        public void startPrefixMapping(String prefix, String uri) throws SAXException {
                        }

                        @Override
                        public void endPrefixMapping(String prefix) throws SAXException {
                        }

                        @Override
                        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
                            if ("string".equals(localName) || "string".equals(qName)) {
                                currentName = atts.getValue("name");
                                chars.setLength(0);
                            }
                        }

                        @Override
                        public void endElement(String uri, String localName, String qName) throws SAXException {
                            if ("string".equals(localName) || "string".equals(qName)) {
                                String str = chars.toString();
                                if (str.startsWith("\"") && str.endsWith("\"")) {
                                    str = str.substring(1);
                                    str = str.substring(0, str.length() - 1);
                                    res.setLocaleProperty(localeName, locale, currentName, str);
                                    return;
                                }
                                str = str.replace("\\'", "'");
                                res.setLocaleProperty(localeName, locale, currentName, str);
                            }
                        }

                        @Override
                        public void characters(char[] ch, int start, int length) throws SAXException {
                            chars.append(ch, start, length);
                        }

                        @Override
                        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
                        }

                        @Override
                        public void processingInstruction(String target, String data) throws SAXException {
                        }

                        @Override
                        public void skippedEntity(String name) throws SAXException {
                        }
                    });
                    xmlReader.parse(new InputSource(new InputStreamReader(f, "UTF-8")));
                } else {
                    if (files[0].getName().toLowerCase().endsWith("csv")) {
                        CSVParserOptions po = new CSVParserOptions(this);
                        if (po.isCanceled()) {
                            f.close();
                            return;
                        }
                        CSVParser p = new CSVParser(po.getDelimiter());
                        String[][] data = p.parse(new InputStreamReader(f, po.getEncoding()));
                        for (int iter = 1; iter < data.length; iter++) {
                            if (data[iter].length > 0) {
                                String key = data[iter][0];
                                for (int col = 1; col < data[iter].length; col++) {
                                    if (res.getL10N(localeName, data[0][col]) == null) {
                                        res.addLocale(localeName, data[0][col]);
                                    }
                                    res.setLocaleProperty(localeName, data[0][col], key, data[iter][col]);
                                }
                            }
                        }
                    } else {
                        Properties prop = new Properties();
                        prop.load(f);
                        for (Object key : prop.keySet()) {
                            res.setLocaleProperty(localeName, locale, (String) key, prop.getProperty((String) key));
                        }
                    }
                }
                f.close();
                initLocaleList();
                for (Object localeObj : localeList) {
                    Hashtable current = res.getL10N(localeName, (String) localeObj);
                    for (Object key : current.keySet()) {
                        if (!keys.contains(key)) {
                            keys.add(key);
                        }
                    }
                }
                Collections.sort(keys);
                initTable();
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "Error: " + ex, "Error Occured", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) Attributes(org.xml.sax.Attributes) Properties(java.util.Properties) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException) Locator(org.xml.sax.Locator) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) XMLReader(org.xml.sax.XMLReader) ErrorHandler(org.xml.sax.ErrorHandler) InputStreamReader(java.io.InputStreamReader) Hashtable(java.util.Hashtable) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) CSVParser(com.codename1.io.CSVParser) EventObject(java.util.EventObject) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

CSVParser (com.codename1.io.CSVParser)1 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 EventObject (java.util.EventObject)1 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 Attributes (org.xml.sax.Attributes)1 ContentHandler (org.xml.sax.ContentHandler)1 ErrorHandler (org.xml.sax.ErrorHandler)1 InputSource (org.xml.sax.InputSource)1 Locator (org.xml.sax.Locator)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1 XMLReader (org.xml.sax.XMLReader)1