/** * @param sourceFile File to read from * @return List of String objects with the shas */ public static FileRequestFileContent readRequestFile(final File sourceFile) { if (!sourceFile.isFile() || !(sourceFile.length() > 0)) { return null; } Document d = null; try { d = XMLTools.parseXmlFile(sourceFile.getPath()); } catch (final Throwable t) { logger.log(Level.SEVERE, "Exception in readRequestFile, during XML parsing", t); return null; } if (d == null) { logger.log(Level.SEVERE, "Could'nt parse the request file"); return null; } final Element rootNode = d.getDocumentElement(); if (rootNode.getTagName().equals(TAG_FrostFileRequestFile) == false) { logger.severe( "Error: xml request file does not contain the root tag '" + TAG_FrostFileRequestFile + "'"); return null; } final String timeStampStr = XMLTools.getChildElementsTextValue(rootNode, TAG_timestamp); if (timeStampStr == null) { logger.severe("Error: xml file does not contain the tag '" + TAG_timestamp + "'"); return null; } final long timestamp = Long.parseLong(timeStampStr); final List<Element> nodelist = XMLTools.getChildElementsByTagName(rootNode, TAG_shaList); if (nodelist.size() != 1) { logger.severe("Error: xml request files must contain only one element '" + TAG_shaList + "'"); return null; } final Element rootShaNode = nodelist.get(0); final List<String> shaList = new LinkedList<String>(); final List<Element> xmlKeys = XMLTools.getChildElementsByTagName(rootShaNode, TAG_sha); for (final Element el : xmlKeys) { final Text txtname = (Text) el.getFirstChild(); if (txtname == null) { continue; } final String sha = txtname.getData(); shaList.add(sha); } final FileRequestFileContent content = new FileRequestFileContent(timestamp, shaList); return content; }
/** * @param chkKeys List of String objects with the shas * @param targetFile target file * @return true if write was successful */ public static boolean writeRequestFile( final FileRequestFileContent content, final File targetFile) { final Document doc = XMLTools.createDomDocument(); if (doc == null) { logger.severe("Error - writeRequestFile: factory could'nt create XML Document."); return false; } final Element rootElement = doc.createElement(TAG_FrostFileRequestFile); doc.appendChild(rootElement); final Element timeStampElement = doc.createElement(TAG_timestamp); final Text timeStampText = doc.createTextNode(Long.toString(content.getTimestamp())); timeStampElement.appendChild(timeStampText); rootElement.appendChild(timeStampElement); final Element rootChkElement = doc.createElement(TAG_shaList); rootElement.appendChild(rootChkElement); for (final String chkKey : content.getShaStrings()) { final Element nameElement = doc.createElement(TAG_sha); final Text text = doc.createTextNode(chkKey); nameElement.appendChild(text); rootChkElement.appendChild(nameElement); } boolean writeOK = false; try { writeOK = XMLTools.writeXmlFile(doc, targetFile); } catch (final Throwable t) { logger.log(Level.SEVERE, "Exception in writeRequestFile/writeXmlFile", t); } return writeOK; }