private void save() { try { Document doc = XmlUtil.getDocument(); Element root = doc.createElement("profiles"); doc.appendChild(root); Enumeration<String> keys = table.keys(); while (keys.hasMoreElements()) { table.get(keys.nextElement()).appendTo(root); } FileUtil.setText(new File(filename), FileUtil.utf8, XmlUtil.toString(doc)); } catch (Exception ignore) { } }
/** * Export a file. * * @param fileToExport the file to export. * @return the status of the attempt to export the file. */ public Status export(File fileToExport) { HttpURLConnection conn; OutputStream svros; OutputStreamWriter writer; XmlObject xmlObject = null; // Parse the file and make sure we can handle it try { xmlObject = new XmlObject(fileToExport); } catch (Exception invalid) { return Status.FAIL; } Document doc = xmlObject.getDocument(); Element root = doc.getDocumentElement(); String rootName = root.getTagName(); if (!rootName.equals("ImageAnnotation")) { logger.warn(name + ": XmlObject with illegal root (" + rootName + ") not transmitted."); return Status.FAIL; } // Get the text. String text = XmlUtil.toString(doc.getDocumentElement()); // Make the connection and send the text. try { // Establish the connection conn = HttpUtil.getConnection(url); conn.connect(); // Get a writer for UTF-8 svros = conn.getOutputStream(); writer = new OutputStreamWriter(svros, FileUtil.utf8); // Send the text to the server writer.write(text, 0, text.length()); writer.flush(); writer.close(); // Get the response code int responseCode = conn.getResponseCode(); // Get the response. String response = FileUtil.getText(conn.getInputStream()); // Check the response, make any necessary log entries, and return the appropriate Status // instance. boolean ok = response.toLowerCase().contains("document submitted"); if (logAll || (!ok && logFailed)) logger.info(name + ": export response code: " + responseCode + "\n" + response); return (ok ? Status.OK : Status.FAIL); } catch (Exception e) { // This indicates a network failure; log it and set up for a retry. logger.warn(name + ": export failed: " + e.getMessage()); logger.debug(e); } return Status.RETRY; }
private void load() { File file = new File(filename); if (file.exists()) { try { Document doc = XmlUtil.getDocument(file); Element root = doc.getDocumentElement(); Node child = root.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { Profile p = new Profile((Element) child); table.put(p.name, p); } child = child.getNextSibling(); } } catch (Exception ignore) { } } }
/** Get an XML object containing all the terms in the database. */ public Document getIndexDocument() { try { Document doc = XmlUtil.getDocument(); Element root = doc.createElement("Terms"); doc.appendChild(root); TupleBrowser browser = index.browse(); Tuple tuple = new Tuple(); while (browser.getNext(tuple)) { Element term = doc.createElement("Term"); root.appendChild(term); String key = (String) tuple.getKey(); String value = (String) tuple.getValue(); term.setAttribute("key", key.substring(value.length())); term.setAttribute("keyType", value); } return doc; } catch (Exception unable) { return null; } }