Esempio n. 1
0
    public void endElement(String uri, String localName, String qname) {
      super.endElement(uri, localName, qname);

      if (record != null && localName.equals(DataRecord.ENCODING_RECORD)) {
        // end of record
        if (dataImport.importRecord(record, fieldsInImport)) {
          count++;
        }
        record = null;
        fieldsInImport = null;
      }
      String fieldValue = getFieldValue();
      if (record != null && fieldValue != null) {
        // end of field
        try {
          if (textImport) {
            if (!record.setFromText(fieldName, fieldValue.trim())) {
              dataImport.logImportWarning(
                  record,
                  "Value '"
                      + fieldValue
                      + "' for Field '"
                      + fieldName
                      + "' corrected to '"
                      + record.getAsText(fieldName)
                      + "'");
            }
          } else {
            record.set(fieldName, fieldValue.trim());
          }
          String[] equivFields = record.getEquivalentFields(fieldName);
          for (String f : equivFields) {
            fieldsInImport.add(f);
          }
        } catch (Exception esetvalue) {
          dataImport.logImportWarning(
              record,
              "Cannot set value '"
                  + fieldValue
                  + "' for Field '"
                  + fieldName
                  + "': "
                  + esetvalue.toString());
        }
      }
    }
Esempio n. 2
0
 public int runCsvImport() {
   int count = 0;
   try {
     int linecnt = 0;
     String[] header = null;
     ArrayList<String> fieldsInImport = new ArrayList<String>();
     BufferedReader f =
         new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding));
     String s;
     DataRecord dummyRecord = storageObject.createNewRecord();
     while ((s = f.readLine()) != null) {
       s = s.trim();
       if (s.length() == 0) {
         continue;
       }
       Vector<String> fields = splitFields(s);
       if (fields.size() > 0) {
         if (linecnt == 0) {
           // header
           header = new String[fields.size()];
           for (int i = 0; i < fields.size(); i++) {
             header[i] = fields.get(i);
             if (header[i].startsWith("#") && header[i].endsWith("#") && header.length > 2) {
               header[i] = header[i].substring(1, header[i].length() - 1).trim();
               overrideKeyField = header[i];
             }
             String[] equivFields = dummyRecord.getEquivalentFields(header[i]);
             for (String ef : equivFields) {
               fieldsInImport.add(ef);
             }
           }
         } else {
           // fields
           DataRecord r = storageObject.createNewRecord();
           for (int i = 0; i < header.length; i++) {
             String value = (fields.size() > i ? fields.get(i) : null);
             if (value != null && value.length() > 0) {
               try {
                 if (!r.setFromText(header[i], value.trim())) {
                   logImportWarning(
                       r,
                       "Value '"
                           + value
                           + "' for Field '"
                           + header[i]
                           + "' corrected to '"
                           + r.getAsText(header[i])
                           + "'");
                 }
               } catch (Exception esetvalue) {
                 logImportWarning(
                     r,
                     "Cannot set value '"
                         + value
                         + "' for Field '"
                         + header[i]
                         + "': "
                         + esetvalue.toString());
               }
             }
           }
           if (importRecord(r, fieldsInImport)) {
             count++;
           }
         }
       }
       linecnt++;
     }
     f.close();
   } catch (Exception e) {
     logInfo(e.toString());
     errorCount++;
     Logger.log(e);
     if (Daten.isGuiAppl()) {
       Dialog.error(e.toString());
     }
   }
   return count;
 }