public void run() { note.setFileName(file.AbsolutePath); // the note guid is not stored in the xml but in the filename note.setGuid(file.Name.Replace(".note", "")); // Try reading the file first string contents = ""; try { contents = readFile(file,buffer); } catch (IOException e) { e.PrintStackTrace(); TLog.w(TAG, "Something went wrong trying to read the note"); SendMessage(PARSING_FAILED, ErrorList.createError(note, e)); onWorkDone(); return; } try { // Parsing // XML // Get a SAXParser from the SAXPArserFactory SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); // Get the XMLReader of the SAXParser we created XMLReader xr = sp.getXMLReader(); // Create a new ContentHandler, send it this note to fill and apply it to the XML-Reader NoteHandler xmlHandler = new NoteHandler(note); xr.setContentHandler(xmlHandler); // Create the proper input source StringReader sr = new StringReader(contents); InputSource inputSource = new InputSource(sr); TLog.d(TAG, "parsing note. filename: {0}", file.Name()); xr.parse(inputSource); // TODO wrap and throw a new exception here } catch (System.Exception e) { e.PrintStackTrace(); if(e as TimeFormatException) TLog.e(TAG, "Problem parsing the note's date and time"); SendMessage(PARSING_FAILED, ErrorList.createErrorWithContents(note, e, contents)); onWorkDone(); return; } // FIXME here we are re-reading the whole note just to grab note-content out, there is probably a better way to do this (I'm talking to you xmlpull.org!) Matcher m = note_content.Matcher(contents); if (m.Find()) { note.setXmlContent(NoteManager.stripTitleFromContent(m.Group(1),note.getTitle())); } else { TLog.w(TAG, "Something went wrong trying to grab the note-content out of a note"); SendMessage(PARSING_FAILED, ErrorList.createErrorWithContents(note, "Something went wrong trying to grab the note-content out of a note", contents)); onWorkDone(); return; } syncableNotes.add(note); onWorkDone(); }
// actually pushes a note to sdcard, with optional subdirectory (e.g. backup) private static int doPushNote(Note note) { Note rnote = new Note(); try { File path = new File(Tomdroid.NOTES_PATH); if (!path.Exists()) path.Mkdir(); TLog.i(TAG, "Path {0} Exists: {1}", path, path.Exists()); // Check a second time, if not the most likely cause is the volume doesn't exist if(!path.Exists()) { TLog.w(TAG, "Couldn't create {0}", path); return NO_SD_CARD; } path = new File(Tomdroid.NOTES_PATH + "/"+note.getGuid() + ".note"); note.createDate = note.getLastChangeDate().Format3339(false); note.cursorPos = 0; note.width = 0; note.height = 0; note.X = -1; note.Y = -1; if (path.Exists()) { // update existing note // Try reading the file first string contents = ""; try { char[] buffer = new char[0x1000]; contents = readFile(path,buffer); } catch (IOException e) { e.PrintStackTrace(); TLog.w(TAG, "Something went wrong trying to read the note"); return PARSING_FAILED; } try { // Parsing // XML // Get a SAXParser from the SAXPArserFactory SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); // Get the XMLReader of the SAXParser we created XMLReader xr = sp.getXMLReader(); // Create a new ContentHandler, send it this note to fill and apply it to the XML-Reader NoteHandler xmlHandler = new NoteHandler(rnote); xr.setContentHandler(xmlHandler); // Create the proper input source StringReader sr = new StringReader(contents); InputSource inputSource = new InputSource(sr); TLog.d(TAG, "parsing note. filename: {0}", path.Name()); xr.parse(inputSource); // TODO wrap and throw a new exception here } catch (Exception e) { e.PrintStackTrace(); if(e as TimeFormatException) TLog.e(TAG, "Problem parsing the note's date and time"); return PARSING_FAILED; } note.createDate = rnote.createDate; note.cursorPos = rnote.cursorPos; note.width = rnote.width; note.height = rnote.height; note.X = rnote.X; note.Y = rnote.Y; note.setTags(rnote.getTags()); } string xmlOutput = note.getXmlFileString(); path.CreateNewFile(); FileOutputStream fOut = new FileOutputStream(path); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.Append(xmlOutput); myOutWriter.Close(); fOut.Close(); } catch (Exception e) { TLog.e(TAG, "push to sd card didn't work"); return NOTE_PUSH_ERROR; } return NOTE_PUSHED; }