Skip to content

Instantly share code, notes, and snippets.

@Sadwyn
Created April 2, 2018 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sadwyn/5958737f20cb4caf8436741d74caa490 to your computer and use it in GitHub Desktop.
Save Sadwyn/5958737f20cb4caf8436741d74caa490 to your computer and use it in GitHub Desktop.
Пример кода
public static void writeTransactionToXml(Context context, Transaction transaction, List<Document> documents) {
boolean fileCreatedNow = false;
String date = transaction.getDate();
String customerName = transaction.getCustomerName();
if (!FUtils.isFileExists(context, FileProvider.TRANSACTION_HISTORY)) {
fileCreatedNow = true;
}
File encryptedFolder = context.getDir(FileProvider.ENCRYPTED_DATA, Context.MODE_PRIVATE);
File[] files = encryptedFolder.listFiles((dir, name) -> name.equals(FileProvider.TRANSACTION_HISTORY));
File transactionFile;
if (fileCreatedNow) {
transactionFile = new File(encryptedFolder, FileProvider.TRANSACTION_HISTORY);
try {
boolean createdTransaction = transactionFile.createNewFile();
Log.i(TAG, String.valueOf(createdTransaction));
} catch (IOException e) {
Log.e(XmlUtils.TAG, "Creating transactionFile", e);
}
} else {
transactionFile = files[0];
}
AESLocalStorageUtils aesLocalStorageUtils = new AESLocalStorageUtils();
byte[] fileBytes = FUtils.readFile(transactionFile);
if (!fileCreatedNow)
fileBytes = aesLocalStorageUtils.decrypt(fileBytes);
try (FileOutputStream fileOutputStream = new FileOutputStream(transactionFile, false)) {
fileOutputStream.write(fileBytes);
} catch (IOException e) {
Log.e(XmlUtils.class.getSimpleName(), AESLocalStorageUtils.EXCEPTION_DURING_DECRYPT, e);
}
try (RandomAccessFile raf = new RandomAccessFile(transactionFile, "rw")) {
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
if (fileCreatedNow) {
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag(null, TRANSACTIONS);
}
xmlSerializer.startTag(null, TRANSACTION);
//process customer name tag
xmlSerializer.startTag(null, CUSTOMER_NAME);
xmlSerializer.text(customerName);
xmlSerializer.endTag(null, CUSTOMER_NAME);
//process date tag
xmlSerializer.startTag(null, DATE);
xmlSerializer.text(date);
xmlSerializer.endTag(null, DATE);
//process Documents Tag
xmlSerializer.startTag(null, DOCUMENTS);
for (Document document : documents) {
xmlSerializer.startTag(null, DOCUMENT);
for (Map.Entry<String, String> mrz : document.getXmlPersonData().getMrz().entrySet()) {
xmlSerializer.startTag(null, mrz.getKey());
xmlSerializer.text(mrz.getValue());
xmlSerializer.endTag(null, mrz.getKey());
}
xmlSerializer.startTag(null, FACE_FROM_DOC_IMAGE);
processImageBase64(xmlSerializer, document.getFacePhotoFromDocument());
xmlSerializer.endTag(null, FACE_FROM_DOC_IMAGE);
xmlSerializer.startTag(null, FIRST_DOC_IMAGE);
processImageBase64(xmlSerializer, document.getFirstDocumentPhoto());
xmlSerializer.endTag(null, FIRST_DOC_IMAGE);
xmlSerializer.startTag(null, SECOND_DOC_IMAGE);
processImageBase64(xmlSerializer, document.getSecondDocumentPhoto());
xmlSerializer.endTag(null, SECOND_DOC_IMAGE);
xmlSerializer.endTag(null, DOCUMENT);
}
xmlSerializer.endTag(null, DOCUMENTS);
xmlSerializer.endTag(null, TRANSACTION);
xmlSerializer.endDocument();
xmlSerializer.flush();
String dataWrite = writer.toString();
if (!fileCreatedNow) {
raf.seek(raf.length() - TRANSACTION_END_TAG_LENGTH);
}
raf.write(dataWrite.getBytes());
if (!fileCreatedNow) {
raf.writeBytes(TRANSACTIONS_END);
}
fileBytes = aesLocalStorageUtils.encrypt(FUtils.readFile(transactionFile));
try (FileOutputStream fileOutputStream = new FileOutputStream(transactionFile, false)) {
fileOutputStream.write(fileBytes);
} catch (IOException er) {
Log.e(XmlUtils.class.getSimpleName(), AESLocalStorageUtils.EXCEPTION_DURING_DECRYPT, er);
}
} catch (FileNotFoundException e) {
Log.e(TAG, "FileNotFound", e);
} catch (IllegalArgumentException e) {
Log.e(TAG, "IllegalArgument", e);
} catch (IllegalStateException e) {
Log.e(TAG, "IllegalState", e);
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment